Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 4 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 5 | import frappe |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 6 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 7 | |
| 8 | def get_context(context): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 9 | project_user = frappe.db.get_value( |
| 10 | "Project User", |
| 11 | {"parent": frappe.form_dict.project, "user": frappe.session.user}, |
| 12 | ["user", "view_attachments"], |
| 13 | as_dict=True, |
| 14 | ) |
| 15 | if frappe.session.user != "Administrator" and ( |
| 16 | not project_user or frappe.session.user == "Guest" |
| 17 | ): |
Kanchan Chauhan | a4ff5d3 | 2016-04-08 23:22:04 +0530 | [diff] [blame] | 18 | raise frappe.PermissionError |
Faris Ansari | 82770d9 | 2019-07-16 09:40:09 +0530 | [diff] [blame] | 19 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 20 | context.no_cache = 1 |
Kanchan Chauhan | 11638ba | 2016-04-20 16:20:49 +0530 | [diff] [blame] | 21 | context.show_sidebar = True |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 22 | project = frappe.get_doc("Project", frappe.form_dict.project) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 23 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 24 | project.has_permission("read") |
Faris Ansari | 82770d9 | 2019-07-16 09:40:09 +0530 | [diff] [blame] | 25 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 26 | project.tasks = get_tasks( |
| 27 | project.name, start=0, item_status="open", search=frappe.form_dict.get("search") |
| 28 | ) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 29 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 30 | project.timesheets = get_timesheets(project.name, start=0, search=frappe.form_dict.get("search")) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 31 | |
Faris Ansari | 82770d9 | 2019-07-16 09:40:09 +0530 | [diff] [blame] | 32 | if project_user and project_user.view_attachments: |
JodeQ | 10dfd4a | 2018-09-10 13:40:43 +0200 | [diff] [blame] | 33 | project.attachments = get_attachments(project.name) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 34 | |
| 35 | context.doc = project |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 36 | |
| 37 | |
Kanchan Chauhan | 2ad801c | 2016-03-22 16:00:41 +0530 | [diff] [blame] | 38 | def get_tasks(project, start=0, search=None, item_status=None): |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 39 | filters = {"project": project} |
| 40 | if search: |
| 41 | filters["subject"] = ("like", "%{0}%".format(search)) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 42 | tasks = frappe.get_all( |
| 43 | "Task", |
| 44 | filters=filters, |
| 45 | fields=[ |
| 46 | "name", |
| 47 | "subject", |
| 48 | "status", |
| 49 | "modified", |
| 50 | "_assign", |
| 51 | "exp_end_date", |
| 52 | "is_group", |
| 53 | "parent_task", |
| 54 | ], |
| 55 | limit_start=start, |
| 56 | limit_page_length=10, |
| 57 | ) |
Jannat Patel | 5a42511 | 2021-07-01 17:17:34 +0530 | [diff] [blame] | 58 | task_nest = [] |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 59 | for task in tasks: |
Jannat Patel | 5a42511 | 2021-07-01 17:17:34 +0530 | [diff] [blame] | 60 | if task.is_group: |
| 61 | child_tasks = list(filter(lambda x: x.parent_task == task.name, tasks)) |
| 62 | if len(child_tasks): |
| 63 | task.children = child_tasks |
| 64 | task_nest.append(task) |
| 65 | return list(filter(lambda x: not x.parent_task, tasks)) |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 66 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 67 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 68 | @frappe.whitelist() |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 69 | def get_task_html(project, start=0, item_status=None): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 70 | return frappe.render_template( |
| 71 | "erpnext/templates/includes/projects/project_tasks.html", |
| 72 | { |
| 73 | "doc": { |
| 74 | "name": project, |
| 75 | "project_name": project, |
| 76 | "tasks": get_tasks(project, start, item_status=item_status), |
| 77 | } |
| 78 | }, |
| 79 | is_path=True, |
| 80 | ) |
| 81 | |
Rushabh Mehta | c20c536 | 2016-03-25 17:19:28 +0530 | [diff] [blame] | 82 | |
Kanchan Chauhan | 6d76359c | 2016-07-05 12:12:39 +0530 | [diff] [blame] | 83 | def get_timesheets(project, start=0, search=None): |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 84 | filters = {"project": project} |
| 85 | if search: |
Kanchan Chauhan | 6d76359c | 2016-07-05 12:12:39 +0530 | [diff] [blame] | 86 | filters["activity_type"] = ("like", "%{0}%".format(search)) |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 87 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 88 | timesheets = frappe.get_all( |
| 89 | "Timesheet Detail", |
| 90 | filters=filters, |
| 91 | fields=["project", "activity_type", "from_time", "to_time", "parent"], |
| 92 | limit_start=start, |
| 93 | limit_page_length=10, |
| 94 | ) |
Kanchan Chauhan | 6d76359c | 2016-07-05 12:12:39 +0530 | [diff] [blame] | 95 | for timesheet in timesheets: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 96 | info = frappe.get_all( |
| 97 | "Timesheet", |
| 98 | filters={"name": timesheet.parent}, |
| 99 | fields=["name", "status", "modified", "modified_by"], |
| 100 | limit_start=start, |
| 101 | limit_page_length=10, |
| 102 | ) |
Jannat Patel | 5a42511 | 2021-07-01 17:17:34 +0530 | [diff] [blame] | 103 | if len(info): |
| 104 | timesheet.update(info[0]) |
Kanchan Chauhan | 6d76359c | 2016-07-05 12:12:39 +0530 | [diff] [blame] | 105 | return timesheets |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 106 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 107 | |
Kanchan Chauhan | b3fe6a4 | 2016-03-16 18:01:22 +0530 | [diff] [blame] | 108 | @frappe.whitelist() |
Kanchan Chauhan | 6d76359c | 2016-07-05 12:12:39 +0530 | [diff] [blame] | 109 | def get_timesheet_html(project, start=0): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 110 | return frappe.render_template( |
| 111 | "erpnext/templates/includes/projects/project_timesheets.html", |
| 112 | {"doc": {"timesheets": get_timesheets(project, start)}}, |
| 113 | is_path=True, |
| 114 | ) |
| 115 | |
Rushabh Mehta | b2269dd | 2016-03-23 18:28:50 +0530 | [diff] [blame] | 116 | |
JodeQ | 10dfd4a | 2018-09-10 13:40:43 +0200 | [diff] [blame] | 117 | def get_attachments(project): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 118 | return frappe.get_all( |
| 119 | "File", |
| 120 | filters={"attached_to_name": project, "attached_to_doctype": "Project", "is_private": 0}, |
| 121 | fields=["file_name", "file_url", "file_size"], |
| 122 | ) |