From 651b61290903384e8bb7ac975869983a51f10167 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 27 Mar 2018 11:24:20 +0530 Subject: [PATCH] Fixed logic for reserved qty for subcontract and production and written a patch (#13396) * Fixed logic for reserved qty for subcontract and production and written a patch * repost reserved qty for filtered bins --- erpnext/patches.txt | 3 +- .../fix_reserved_qty_for_sub_contract.py | 29 +++++++++++++++++++ erpnext/stock/doctype/bin/bin.py | 13 +++++++-- 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 erpnext/patches/v10_0/fix_reserved_qty_for_sub_contract.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 36e68ca078..410636fbd4 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -497,4 +497,5 @@ erpnext.patches.v10_0.update_warehouse_address_details erpnext.patches.v10_0.update_reserved_qty_for_purchase_order erpnext.patches.v10_0.update_hub_connector_domain erpnext.patches.v10_0.set_student_party_type -erpnext.patches.v10_0.update_project_in_sle \ No newline at end of file +erpnext.patches.v10_0.update_project_in_sle +erpnext.patches.v10_0.fix_reserved_qty_for_sub_contract \ No newline at end of file diff --git a/erpnext/patches/v10_0/fix_reserved_qty_for_sub_contract.py b/erpnext/patches/v10_0/fix_reserved_qty_for_sub_contract.py new file mode 100644 index 0000000000..8a456382ab --- /dev/null +++ b/erpnext/patches/v10_0/fix_reserved_qty_for_sub_contract.py @@ -0,0 +1,29 @@ +# Copyright (c) 2017, Frappe and Contributors +# License: GNU General Public License v3. See license.txt + +from __future__ import unicode_literals +import frappe +from erpnext.stock.utils import get_bin + +def execute(): + for d in frappe.db.sql(""" + select distinct rm_item_code, reserve_warehouse + from `tabPurchase Order Item Supplied` + where docstatus=1 and reserve_warehouse is not null and reserve_warehouse != ''"""): + + try: + bin_doc = get_bin(d[0], d[1]) + bin_doc.update_reserved_qty_for_sub_contracting() + except: + pass + + for d in frappe.db.sql("""select distinct item_code, source_warehouse + from `tabProduction Order Item` + where docstatus=1 and transferred_qty > required_qty + and source_warehouse is not null and source_warehouse != ''""", as_list=1): + + try: + bin_doc = get_bin(d[0], d[1]) + bin_doc.update_reserved_qty_for_production() + except: + pass \ No newline at end of file diff --git a/erpnext/stock/doctype/bin/bin.py b/erpnext/stock/doctype/bin/bin.py index 430d9fb404..eef405d7d4 100644 --- a/erpnext/stock/doctype/bin/bin.py +++ b/erpnext/stock/doctype/bin/bin.py @@ -76,14 +76,16 @@ class Bin(Document): def update_reserved_qty_for_production(self): '''Update qty reserved for production from Production Item tables in open production orders''' - self.reserved_qty_for_production = frappe.db.sql('''select sum(required_qty - transferred_qty) + self.reserved_qty_for_production = frappe.db.sql(''' + select sum(item.required_qty - item.transferred_qty) from `tabProduction Order` pro, `tabProduction Order Item` item where item.item_code = %s and item.parent = pro.name and pro.docstatus = 1 and item.source_warehouse = %s - and pro.status not in ("Stopped", "Completed")''', (self.item_code, self.warehouse))[0][0] + and pro.status not in ("Stopped", "Completed") + and item.required_qty > item.transferred_qty''', (self.item_code, self.warehouse))[0][0] self.set_projected_qty() @@ -123,7 +125,12 @@ class Bin(Document): and po.per_received < 100 """, (self.item_code))[0][0] - self.db_set('reserved_qty_for_sub_contract', (reserved_qty_for_sub_contract - materials_transferred)) + if reserved_qty_for_sub_contract > materials_transferred: + reserved_qty_for_sub_contract = reserved_qty_for_sub_contract - materials_transferred + else: + reserved_qty_for_sub_contract = 0 + + self.db_set('reserved_qty_for_sub_contract', reserved_qty_for_sub_contract) self.set_projected_qty() self.db_set('projected_qty', self.projected_qty)