blob: d23fed9e7d1ae7d98fc509c55793854f82f86dbf [file] [log] [blame]
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5import frappe
Rushabh Mehtab2269dd2016-03-23 18:28:50 +05306import json
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +05307
8def get_context(context):
JodeQ10dfd4a2018-09-10 13:40:43 +02009 project_user = frappe.db.get_value("Project User", {"parent": frappe.form_dict.project, "user": frappe.session.user} , ["user", "view_attachments"], as_dict= True)
Faris Ansari82770d92019-07-16 09:40:09 +053010 if frappe.session.user != 'Administrator' and (not project_user or frappe.session.user == 'Guest'):
Kanchan Chauhana4ff5d32016-04-08 23:22:04 +053011 raise frappe.PermissionError
Faris Ansari82770d92019-07-16 09:40:09 +053012
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053013 context.no_cache = 1
Kanchan Chauhan11638ba2016-04-20 16:20:49 +053014 context.show_sidebar = True
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053015 project = frappe.get_doc('Project', frappe.form_dict.project)
16
17 project.has_permission('read')
Faris Ansari82770d92019-07-16 09:40:09 +053018
Rushabh Mehtac20c5362016-03-25 17:19:28 +053019 project.tasks = get_tasks(project.name, start=0, item_status='open',
Kanchan Chauhan239b3512016-05-02 11:43:44 +053020 search=frappe.form_dict.get("search"))
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053021
Kanchan Chauhan6d76359c2016-07-05 12:12:39 +053022 project.timesheets = get_timesheets(project.name, start=0,
23 search=frappe.form_dict.get("search"))
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053024
Faris Ansari82770d92019-07-16 09:40:09 +053025 if project_user and project_user.view_attachments:
JodeQ10dfd4a2018-09-10 13:40:43 +020026 project.attachments = get_attachments(project.name)
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053027
28 context.doc = project
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053029
30
Kanchan Chauhan2ad801c2016-03-22 16:00:41 +053031def get_tasks(project, start=0, search=None, item_status=None):
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053032 filters = {"project": project}
33 if search:
34 filters["subject"] = ("like", "%{0}%".format(search))
Kanchan Chauhan6d76359c2016-07-05 12:12:39 +053035 # if item_status:
36# filters["status"] = item_status
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053037 tasks = frappe.get_all("Task", filters=filters,
Kanchan Chauhanb566d422016-03-29 11:21:42 +053038 fields=["name", "subject", "status", "_seen", "_comments", "modified", "description"],
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053039 limit_start=start, limit_page_length=10)
40
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053041 for task in tasks:
42 task.todo = frappe.get_all('ToDo',filters={'reference_name':task.name, 'reference_type':'Task'},
43 fields=["assigned_by", "owner", "modified", "modified_by"])
Rushabh Mehtac20c5362016-03-25 17:19:28 +053044
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053045 if task.todo:
46 task.todo=task.todo[0]
47 task.todo.user_image = frappe.db.get_value('User', task.todo.owner, 'user_image')
Rushabh Mehtac20c5362016-03-25 17:19:28 +053048
Faris Ansari82770d92019-07-16 09:40:09 +053049
Kanchan Chauhanb566d422016-03-29 11:21:42 +053050 task.comment_count = len(json.loads(task._comments or "[]"))
Rushabh Mehtac20c5362016-03-25 17:19:28 +053051
52 task.css_seen = ''
53 if task._seen:
54 if frappe.session.user in json.loads(task._seen):
55 task.css_seen = 'seen'
56
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053057 return tasks
58
59@frappe.whitelist()
Rushabh Mehtac20c5362016-03-25 17:19:28 +053060def get_task_html(project, start=0, item_status=None):
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053061 return frappe.render_template("erpnext/templates/includes/projects/project_tasks.html",
Rushabh Mehtac20c5362016-03-25 17:19:28 +053062 {"doc": {
63 "name": project,
64 "project_name": project,
65 "tasks": get_tasks(project, start, item_status=item_status)}
66 }, is_path=True)
67
Kanchan Chauhan6d76359c2016-07-05 12:12:39 +053068def get_timesheets(project, start=0, search=None):
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053069 filters = {"project": project}
70 if search:
Kanchan Chauhan6d76359c2016-07-05 12:12:39 +053071 filters["activity_type"] = ("like", "%{0}%".format(search))
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053072
Kanchan Chauhan6d76359c2016-07-05 12:12:39 +053073 timesheets = frappe.get_all('Timesheet Detail', filters=filters,
74 fields=['project','activity_type','from_time','to_time','parent'],
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053075 limit_start=start, limit_page_length=10)
Kanchan Chauhan6d76359c2016-07-05 12:12:39 +053076 for timesheet in timesheets:
77 timesheet.infos = frappe.get_all('Timesheet', filters={"name": timesheet.parent},
78 fields=['name','_comments','_seen','status','modified','modified_by'],
79 limit_start=start, limit_page_length=10)
Kanchan Chauhanb566d422016-03-29 11:21:42 +053080
Kanchan Chauhan6d76359c2016-07-05 12:12:39 +053081 for timesheet.info in timesheet.infos:
82 timesheet.info.user_image = frappe.db.get_value('User', timesheet.info.modified_by, 'user_image')
83
84 timesheet.info.comment_count = len(json.loads(timesheet.info._comments or "[]"))
85
86 timesheet.info.css_seen = ''
87 if timesheet.info._seen:
88 if frappe.session.user in json.loads(timesheet.info._seen):
Faris Ansari82770d92019-07-16 09:40:09 +053089 timesheet.info.css_seen = 'seen'
Kanchan Chauhan6d76359c2016-07-05 12:12:39 +053090 return timesheets
Kanchan Chauhanb3fe6a42016-03-16 18:01:22 +053091
92@frappe.whitelist()
Kanchan Chauhan6d76359c2016-07-05 12:12:39 +053093def get_timesheet_html(project, start=0):
94 return frappe.render_template("erpnext/templates/includes/projects/project_timesheets.html",
95 {"doc": {"timesheets": get_timesheets(project, start)}}, is_path=True)
Rushabh Mehtab2269dd2016-03-23 18:28:50 +053096
JodeQ10dfd4a2018-09-10 13:40:43 +020097def get_attachments(project):
98 return frappe.get_all('File', filters= {"attached_to_name": project, "attached_to_doctype": 'Project', "is_private":0},
99 fields=['file_name','file_url', 'file_size'])