From ecc6af9d2a9aa27cd73b118384628857c1736e8d Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Mon, 20 Aug 2018 17:39:54 +0530 Subject: [PATCH] [Fix] Total materials consumed cost not consider in the calculation of Gross Margin in project (#15171) --- erpnext/patches.txt | 3 ++- .../v10_0/recalculate_gross_margin_for_project.py | 14 ++++++++++++++ erpnext/projects/doctype/project/project.py | 6 +++++- erpnext/stock/doctype/stock_entry/stock_entry.py | 5 +++-- 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 erpnext/patches/v10_0/recalculate_gross_margin_for_project.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 5b5812d404..e99bf59348 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -503,4 +503,5 @@ erpnext.patches.v10_0.set_qty_in_transactions_based_on_serial_no_input erpnext.patches.v10_0.show_leaves_of_all_department_members_in_calendar erpnext.patches.v10_0.update_status_in_purchase_receipt erpnext.patches.v10_0.update_address_template_for_india -erpnext.patches.v10_0.set_discount_amount \ No newline at end of file +erpnext.patches.v10_0.set_discount_amount +erpnext.patches.v10_0.recalculate_gross_margin_for_project \ No newline at end of file diff --git a/erpnext/patches/v10_0/recalculate_gross_margin_for_project.py b/erpnext/patches/v10_0/recalculate_gross_margin_for_project.py new file mode 100644 index 0000000000..6d461f3bc9 --- /dev/null +++ b/erpnext/patches/v10_0/recalculate_gross_margin_for_project.py @@ -0,0 +1,14 @@ +# Copyright (c) 2017, Frappe and Contributors +# License: GNU General Public License v3. See license.txt + +from __future__ import unicode_literals +import frappe + +def execute(): + frappe.reload_doc('projects', 'doctype', 'project') + for d in frappe.db.sql(""" select name from `tabProject` where + ifnull(total_consumed_material_cost, 0 ) > 0 and ifnull(total_billed_amount, 0) > 0""", as_dict=1): + doc = frappe.get_doc("Project", d.name) + doc.calculate_gross_margin() + doc.db_set('gross_margin', doc.gross_margin) + doc.db_set('per_gross_margin', doc.per_gross_margin) \ No newline at end of file diff --git a/erpnext/projects/doctype/project/project.py b/erpnext/projects/doctype/project/project.py index 09726dac28..4ee447db7f 100644 --- a/erpnext/projects/doctype/project/project.py +++ b/erpnext/projects/doctype/project/project.py @@ -236,9 +236,13 @@ class Project(Document): self.update_purchase_costing() self.update_sales_amount() self.update_billed_amount() + self.calculate_gross_margin() - self.gross_margin = flt(self.total_billed_amount) - (flt(self.total_costing_amount) + flt(self.total_expense_claim) + flt(self.total_purchase_cost)) + def calculate_gross_margin(self): + expense_amount = (flt(self.total_costing_amount) + flt(self.total_expense_claim) + + flt(self.total_purchase_cost) + flt(self.get('total_consumed_material_cost', 0))) + self.gross_margin = flt(self.total_billed_amount) - expense_amount if self.total_billed_amount: self.per_gross_margin = (self.gross_margin / flt(self.total_billed_amount)) *100 diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index 5ed8ada499..4905446c91 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -82,6 +82,7 @@ class StockEntry(StockController): if self.purchase_order and self.purpose == "Subcontract": self.update_purchase_order_supplied_items() self.make_gl_entries_on_cancel() + self.update_cost_in_project() def validate_purpose(self): valid_purposes = ["Material Issue", "Material Receipt", "Material Transfer", "Material Transfer for Manufacture", @@ -107,8 +108,8 @@ class StockEntry(StockController): se.docstatus = 1 and se.project = %s and sed.parent = se.name and (sed.t_warehouse is null or sed.t_warehouse = '')""", self.project, as_list=1) - if amount: - frappe.db.set_value('Project', self.project, 'total_consumed_material_cost', amount[0][0]) + amount = amount[0][0] if amount else 0 + frappe.db.set_value('Project', self.project, 'total_consumed_material_cost', amount) def validate_item(self): stock_items = self.get_stock_items()