From b57521a33719258ee3d9b41bc99ec50e4982ccaa Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Mon, 2 Aug 2021 18:37:45 +0200 Subject: [PATCH] feat: add `total_billing_hours` to Sales Invoice --- .../doctype/sales_invoice/sales_invoice.js | 50 +++++++++---------- .../doctype/sales_invoice/sales_invoice.json | 10 +++- .../doctype/sales_invoice/sales_invoice.py | 9 ++-- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index f813425e6b5..ca516439647 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -829,7 +829,7 @@ frappe.ui.form.on('Sales Invoice', { 'timesheet_detail': row.name }); frm.refresh_field('timesheets'); - calculate_total_billing_amount(frm); + frm.trigger("calculate_timesheet_totals"); }, refresh: function(frm) { @@ -937,50 +937,46 @@ frappe.ui.form.on('Sales Invoice', { frm: frm }); }, + create_dunning: function(frm) { frappe.model.open_mapped_doc({ method: "erpnext.accounts.doctype.sales_invoice.sales_invoice.create_dunning", frm: frm }); - } -}) + }, -frappe.ui.form.on('Sales Invoice Timesheet', { + calculate_timesheet_totals: function(frm) { + frm.set_value("total_billing_amount", + frm.doc.timesheets.reduce((a, b) => a + (b["billing_amount"] || 0.0), 0.0)); + frm.set_value("total_billing_hours", + frm.doc.timesheets.reduce((a, b) => a + (b["billing_hours"] || 0.0), 0.0)); + } +}); + + +frappe.ui.form.on("Sales Invoice Timesheet", { time_sheet: function(frm, cdt, cdn){ var d = locals[cdt][cdn]; if(d.time_sheet) { frappe.call({ method: "erpnext.projects.doctype.timesheet.timesheet.get_timesheet_data", args: { - 'name': d.time_sheet, - 'project': frm.doc.project || null + "name": d.time_sheet, + "project": frm.doc.project || null }, - callback: function(r, rt) { - if(r.message){ - let data = r.message; - frappe.model.set_value(cdt, cdn, "billing_hours", data.billing_hours); - frappe.model.set_value(cdt, cdn, "billing_amount", data.billing_amount); - frappe.model.set_value(cdt, cdn, "timesheet_detail", data.timesheet_detail); - calculate_total_billing_amount(frm) + callback: function(r) { + if(r.message) { + frappe.model.set_value(cdt, cdn, "billing_hours", r.message.billing_hours); + frappe.model.set_value(cdt, cdn, "billing_amount", r.message.billing_amount); + frappe.model.set_value(cdt, cdn, "timesheet_detail", r.message.timesheet_detail); + frm.trigger("calculate_timesheet_totals"); } } - }) + }); } } -}) +}); -var calculate_total_billing_amount = function(frm) { - var doc = frm.doc; - - doc.total_billing_amount = 0.0 - if(doc.timesheets) { - $.each(doc.timesheets, function(index, data){ - doc.total_billing_amount += data.billing_amount - }) - } - - refresh_field('total_billing_amount') -} var select_loyalty_program = function(frm, loyalty_programs) { var dialog = new frappe.ui.Dialog({ diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json index 0a9a105b7ca..6f16cd29223 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json @@ -74,6 +74,7 @@ "time_sheet_list", "timesheets", "total_billing_amount", + "total_billing_hours", "section_break_30", "total_qty", "base_total", @@ -1983,6 +1984,13 @@ "fieldtype": "Small Text", "label": "Dispatch Address", "read_only": 1 + }, + { + "fieldname": "total_billing_hours", + "fieldtype": "Float", + "label": "Total Billing Hours", + "print_hide": 1, + "read_only": 1 } ], "icon": "fa fa-file-text", @@ -1995,7 +2003,7 @@ "link_fieldname": "consolidated_invoice" } ], - "modified": "2021-07-08 14:03:55.502522", + "modified": "2021-08-02 18:36:51.978581", "modified_by": "Administrator", "module": "Accounts", "name": "Sales Invoice", diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 6d1f6249c13..c6beaabe19b 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -764,12 +764,11 @@ class SalesInvoice(SellingController): self.calculate_billing_amount_for_timesheet() def calculate_billing_amount_for_timesheet(self): - total_billing_amount = 0.0 - for data in self.timesheets: - if data.billing_amount: - total_billing_amount += data.billing_amount + def timesheet_sum(field): + return sum((ts.get(field) or 0.0) for ts in self.timesheets) - self.total_billing_amount = total_billing_amount + self.total_billing_amount = timesheet_sum("billing_amount") + self.total_billing_hours = timesheet_sum("billing_hours") def get_warehouse(self): user_pos_profile = frappe.db.sql("""select name, warehouse from `tabPOS Profile`