From 642441688687286ba0fce0df1b2319529c723bdd Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Fri, 15 Nov 2019 14:18:45 +0530 Subject: [PATCH] fix: sales order item shwoing incorrect produced qty (#19584) --- .../doctype/work_order/work_order.py | 4 +++- ...ed_qty_field_in_sales_order_for_work_order.py | 14 +++++++++----- .../selling/doctype/sales_order/sales_order.py | 16 ++++++++++------ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/erpnext/manufacturing/doctype/work_order/work_order.py b/erpnext/manufacturing/doctype/work_order/work_order.py index 6ea3dc83ed..089cb8014d 100644 --- a/erpnext/manufacturing/doctype/work_order/work_order.py +++ b/erpnext/manufacturing/doctype/work_order/work_order.py @@ -216,7 +216,9 @@ class WorkOrder(Document): self.db_set(fieldname, qty) from erpnext.selling.doctype.sales_order.sales_order import update_produced_qty_in_so_item - update_produced_qty_in_so_item(self.sales_order_item) + + if self.sales_order and self.sales_order_item: + update_produced_qty_in_so_item(self.sales_order, self.sales_order_item) if self.production_plan: self.update_production_plan_status() diff --git a/erpnext/patches/v12_0/set_produced_qty_field_in_sales_order_for_work_order.py b/erpnext/patches/v12_0/set_produced_qty_field_in_sales_order_for_work_order.py index 44d8fa767a..07026732fd 100644 --- a/erpnext/patches/v12_0/set_produced_qty_field_in_sales_order_for_work_order.py +++ b/erpnext/patches/v12_0/set_produced_qty_field_in_sales_order_for_work_order.py @@ -3,8 +3,12 @@ from frappe.utils import flt from erpnext.selling.doctype.sales_order.sales_order import update_produced_qty_in_so_item def execute(): - frappe.reload_doctype('Sales Order Item') - frappe.reload_doctype('Sales Order') - sales_order_items = frappe.db.get_all('Sales Order Item', ['name']) - for so_item in sales_order_items: - update_produced_qty_in_so_item(so_item.get('name')) \ No newline at end of file + frappe.reload_doctype('Sales Order Item') + frappe.reload_doctype('Sales Order') + + for d in frappe.get_all('Work Order', + fields = ['sales_order', 'sales_order_item'], + filters={'sales_order': ('!=', ''), 'sales_order_item': ('!=', '')}): + + # update produced qty in sales order + update_produced_qty_in_so_item(d.sales_order, d.sales_order_item) \ No newline at end of file diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py index c4c3c0f81e..e12b359bdf 100755 --- a/erpnext/selling/doctype/sales_order/sales_order.py +++ b/erpnext/selling/doctype/sales_order/sales_order.py @@ -1038,14 +1038,18 @@ def create_pick_list(source_name, target_doc=None): return doc -def update_produced_qty_in_so_item(sales_order_item): +def update_produced_qty_in_so_item(sales_order, sales_order_item): #for multiple work orders against same sales order item linked_wo_with_so_item = frappe.db.get_all('Work Order', ['produced_qty'], { 'sales_order_item': sales_order_item, + 'sales_order': sales_order, 'docstatus': 1 }) - if len(linked_wo_with_so_item) > 0: - total_produced_qty = 0 - for wo in linked_wo_with_so_item: - total_produced_qty += flt(wo.get('produced_qty')) - frappe.db.set_value('Sales Order Item', sales_order_item, 'produced_qty', total_produced_qty) \ No newline at end of file + + total_produced_qty = 0 + for wo in linked_wo_with_so_item: + total_produced_qty += flt(wo.get('produced_qty')) + + if not total_produced_qty and frappe.flags.in_patch: return + + frappe.db.set_value('Sales Order Item', sales_order_item, 'produced_qty', total_produced_qty) \ No newline at end of file