From 876082ea2ffba15a631ed9ff484a38bbbe79dc14 Mon Sep 17 00:00:00 2001 From: venkat102 Date: Thu, 13 Feb 2025 23:05:34 +0530 Subject: [PATCH 1/4] fix(project settings): add checkbox to auto fetch timesheet in sales invoice --- .../doctype/projects_settings/projects_settings.json | 12 ++++++++++-- .../doctype/projects_settings/projects_settings.py | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/erpnext/projects/doctype/projects_settings/projects_settings.json b/erpnext/projects/doctype/projects_settings/projects_settings.json index 3284eaee61f..2c8abde4e90 100644 --- a/erpnext/projects/doctype/projects_settings/projects_settings.json +++ b/erpnext/projects/doctype/projects_settings/projects_settings.json @@ -8,7 +8,8 @@ "timesheet_sb", "ignore_workstation_time_overlap", "ignore_user_time_overlap", - "ignore_employee_time_overlap" + "ignore_employee_time_overlap", + "fetch_timesheet_in_sales_invoice" ], "fields": [ { @@ -33,11 +34,18 @@ "fieldname": "ignore_employee_time_overlap", "fieldtype": "Check", "label": "Ignore Employee Time Overlap" + }, + { + "default": "0", + "description": "Enabling the check box will fetch timesheet on select of a Project in Sales Invoice", + "fieldname": "fetch_timesheet_in_sales_invoice", + "fieldtype": "Check", + "label": "Fetch Timesheet in Sales Invoice" } ], "issingle": 1, "links": [], - "modified": "2024-03-27 13:10:21.984404", + "modified": "2025-02-13 23:01:27.321902", "modified_by": "Administrator", "module": "Projects", "name": "Projects Settings", diff --git a/erpnext/projects/doctype/projects_settings/projects_settings.py b/erpnext/projects/doctype/projects_settings/projects_settings.py index 9d940184d98..4b1530fee8e 100644 --- a/erpnext/projects/doctype/projects_settings/projects_settings.py +++ b/erpnext/projects/doctype/projects_settings/projects_settings.py @@ -14,6 +14,7 @@ class ProjectsSettings(Document): if TYPE_CHECKING: from frappe.types import DF + fetch_timesheet_in_sales_invoice: DF.Check ignore_employee_time_overlap: DF.Check ignore_user_time_overlap: DF.Check ignore_workstation_time_overlap: DF.Check From 914ad357fd4172e4f34c0b61ace65f04a1430ffb Mon Sep 17 00:00:00 2001 From: venkat102 Date: Thu, 13 Feb 2025 23:07:45 +0530 Subject: [PATCH 2/4] fix(sales invoice): check fetch_timesheet_in_sales_invoice enabled before fetching the timesheet --- .../doctype/sales_invoice/sales_invoice.js | 8 ++++++-- .../doctype/sales_invoice/sales_invoice.py | 15 ++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index ace2d3ef6a6..e5976dde7e8 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -895,8 +895,12 @@ frappe.ui.form.on("Sales Invoice", { project: function (frm) { if (frm.doc.project) { - frm.events.add_timesheet_data(frm, { - project: frm.doc.project, + frappe.db.get_value("Projects Settings", {}, "fetch_timesheet_in_sales_invoice", (r) => { + if (r.fetch_timesheet_in_sales_invoice) { + frm.events.add_timesheet_data(frm, { + project: frm.doc.project, + }); + } }); } }, diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 076c7221b8d..bda53967543 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -1096,16 +1096,17 @@ class SalesInvoice(SellingController): timesheet.billing_amount = ts_doc.total_billable_amount def update_timesheet_billing_for_project(self): - if self.timesheets: + if ( + not self.timesheets + and self.project + and frappe.db.get_single_value("Projects Settings", "fetch_timesheet_in_sales_invoice") + ): + self.add_timesheet_data() + else: self.calculate_billing_amount_for_timesheet() - @frappe.whitelist(methods=["PUT"]) + @frappe.whitelist() def add_timesheet_data(self): - if not self.timesheets and self.project: - self._add_timesheet_data() - self.save() - - def _add_timesheet_data(self): self.set("timesheets", []) if self.project: for data in get_projectwise_timesheet_data(self.project): From 43b13b91be8649addd9339de1b2257dd530e1f2f Mon Sep 17 00:00:00 2001 From: venkat102 Date: Thu, 13 Feb 2025 23:24:05 +0530 Subject: [PATCH 3/4] fix: check value as int --- erpnext/accounts/doctype/sales_invoice/sales_invoice.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index e5976dde7e8..310ad992f6e 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -896,7 +896,7 @@ frappe.ui.form.on("Sales Invoice", { project: function (frm) { if (frm.doc.project) { frappe.db.get_value("Projects Settings", {}, "fetch_timesheet_in_sales_invoice", (r) => { - if (r.fetch_timesheet_in_sales_invoice) { + if (cint(r.fetch_timesheet_in_sales_invoice)) { frm.events.add_timesheet_data(frm, { project: frm.doc.project, }); From 5880f1d5c6b542328c288fdb2ad27bf251c0a6fe Mon Sep 17 00:00:00 2001 From: venkat102 Date: Mon, 17 Feb 2025 14:49:32 +0530 Subject: [PATCH 4/4] fix: enable fetch_timesheet_in_sales_invoice in test --- erpnext/projects/doctype/timesheet/test_timesheet.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/erpnext/projects/doctype/timesheet/test_timesheet.py b/erpnext/projects/doctype/timesheet/test_timesheet.py index cf95bd63680..311fe3da140 100644 --- a/erpnext/projects/doctype/timesheet/test_timesheet.py +++ b/erpnext/projects/doctype/timesheet/test_timesheet.py @@ -53,6 +53,7 @@ class TestTimesheet(IntegrationTestCase): self.assertEqual(item.qty, 2.00) self.assertEqual(item.rate, 50.00) + @IntegrationTestCase.change_settings("Projects Settings", {"fetch_timesheet_in_sales_invoice": 1}) def test_timesheet_billing_based_on_project(self): emp = make_employee("test_employee_6@salary.com") project = frappe.get_value("Project", {"project_name": "_Test Project"}) @@ -62,7 +63,7 @@ class TestTimesheet(IntegrationTestCase): ) sales_invoice = create_sales_invoice(do_not_save=True) sales_invoice.project = project - sales_invoice._add_timesheet_data() + sales_invoice.add_timesheet_data() sales_invoice.submit() ts = frappe.get_doc("Timesheet", timesheet.name)