From 157815ff0c1028dad4cd907e609ab789f3c9b4f3 Mon Sep 17 00:00:00 2001 From: Shreya Shah Date: Mon, 11 Jun 2018 17:32:17 +0530 Subject: [PATCH] Patch for total quantity (#14458) * Add path to patches.txt * Modify patch --- erpnext/patches.txt | 3 ++- .../patches/v11_0/update_total_qty_field.py | 27 ++++++++++++------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 658b7e7795..eb29528ac7 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -546,4 +546,5 @@ erpnext.patches.v11_0.update_backflush_subcontract_rm_based_on_bom erpnext.patches.v10_0.update_status_in_purchase_receipt erpnext.patches.v11_0.inter_state_field_for_gst erpnext.patches.v11_0.rename_members_with_naming_series #04-06-2018 -erpnext.patches.v11_0.set_update_field_and_value_in_workflow_state \ No newline at end of file +erpnext.patches.v11_0.set_update_field_and_value_in_workflow_state +erpnext.patches.v11_0.update_total_qty_field diff --git a/erpnext/patches/v11_0/update_total_qty_field.py b/erpnext/patches/v11_0/update_total_qty_field.py index e618593a54..6f78d8a77f 100644 --- a/erpnext/patches/v11_0/update_total_qty_field.py +++ b/erpnext/patches/v11_0/update_total_qty_field.py @@ -9,19 +9,26 @@ def execute(): frappe.reload_doc('stock', 'doctype', 'purchase_receipt') frappe.reload_doc('accounts', 'doctype', 'sales_invoice') frappe.reload_doc('accounts', 'doctype', 'purchase_invoice') - + doctypes = ["Sales Order", "Sales Invoice", "Delivery Note",\ "Purchase Order", "Purchase Invoice", "Purchase Receipt", "Quotation", "Supplier Quotation"] for doctype in doctypes: + total_qty = frappe.db.sql(''' + SELECT + parent, SUM(qty) as qty + FROM + `tab%s Item` + GROUP BY parent + ''' % (doctype), as_dict = True) + + when_then = [] + for d in total_qty: + when_then.append(""" + when dt.name = '{0}' then {1} + """.format(frappe.db.escape(d.get("parent")), d.get("qty"))) + frappe.db.sql(''' UPDATE - `tab%s` dt SET dt.total_qty = - ( - SELECT SUM(dt_item.qty) - FROM - `tab%s Item` dt_item - WHERE - dt_item.parent=dt.name - ) - ''' % (doctype, doctype)) + `tab%s` dt SET dt.total_qty = CASE %s END + ''' % (doctype, " ".join(when_then))) \ No newline at end of file