From f0b0464cceba39c62e1bdc2017db8e9c777983c8 Mon Sep 17 00:00:00 2001 From: Neil Trini Lasrado Date: Thu, 22 Jan 2015 15:02:21 +0530 Subject: [PATCH 1/2] patch for is_batch_item --- erpnext/patches.txt | 1 + erpnext/patches/v4_2/set_item_batch.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 erpnext/patches/v4_2/set_item_batch.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 2773d69d78c..8978a3b19b3 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -92,3 +92,4 @@ execute:frappe.delete_doc("DocType", "Contact Control") erpnext.patches.v4_2.recalculate_bom_costs erpnext.patches.v4_2.discount_amount erpnext.patches.v4_2.update_landed_cost_voucher +erpnext.patches.v4_2.set_item_batch diff --git a/erpnext/patches/v4_2/set_item_batch.py b/erpnext/patches/v4_2/set_item_batch.py new file mode 100644 index 00000000000..990be6905f2 --- /dev/null +++ b/erpnext/patches/v4_2/set_item_batch.py @@ -0,0 +1,20 @@ +# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors +# License: GNU General Public License v3. See license.txt + +from __future__ import unicode_literals +import frappe + +def execute(): + item_list = frappe.db.sql("""select name from tabItem""", as_dict=1) + for d in item_list: + count_stock_entries = frappe.db.sql("""select count(name) from `tabStock Entry Detail` where item_code= %s""",d.get("name"))[0][0] + + count_batch_entries = frappe.db.sql("""select count(name) from `tabStock Entry Detail` where \ + item_code= %s and batch_no = '' """,d.get("name"))[0][0] + + if count_stock_entries > 0: + if count_stock_entries == count_batch_entries: + frappe.db.sql("""update `tabItem` set has_batch_no = 'Yes' where name = %s""",d.get("name")) + + if count_batch_entries == 0: + frappe.db.sql("""update `tabItem` set has_batch_no = 'No' where name = %s""",d.get("name")) From 40a9f6f8e9ab0678dfe9a03c4aff378f623ec48b Mon Sep 17 00:00:00 2001 From: Neil Trini Lasrado Date: Fri, 23 Jan 2015 15:30:46 +0530 Subject: [PATCH 2/2] patch fix --- erpnext/patches/v4_2/set_item_batch.py | 65 ++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/erpnext/patches/v4_2/set_item_batch.py b/erpnext/patches/v4_2/set_item_batch.py index 990be6905f2..3e84a59e00e 100644 --- a/erpnext/patches/v4_2/set_item_batch.py +++ b/erpnext/patches/v4_2/set_item_batch.py @@ -5,16 +5,61 @@ from __future__ import unicode_literals import frappe def execute(): - item_list = frappe.db.sql("""select name from tabItem""", as_dict=1) - for d in item_list: - count_stock_entries = frappe.db.sql("""select count(name) from `tabStock Entry Detail` where item_code= %s""",d.get("name"))[0][0] + frappe.db.sql("update tabItem set has_batch_no = 'No' where ifnull(has_batch_no, '') = ''") + frappe.db.sql("update tabItem set has_serial_no = 'No' where ifnull(has_serial_no, '') = ''") + + item_list = frappe.db.sql("""select name, has_batch_no, has_serial_no from tabItem + where ifnull(is_stock_item, 'No') = 'Yes'""", as_dict=1) - count_batch_entries = frappe.db.sql("""select count(name) from `tabStock Entry Detail` where \ - item_code= %s and batch_no = '' """,d.get("name"))[0][0] + sle_count = get_sle_count() + sle_with_batch = get_sle_with_batch() + sle_with_serial = get_sle_with_serial() + + batch_items = get_items_with_batch() + serialized_items = get_items_with_serial() + + for d in item_list: + if d.has_batch_no == 'Yes': + if d.name not in batch_items and sle_count.get(d.name) and sle_count.get(d.name) != sle_with_batch.get(d.name): + frappe.db.set_value("Item", d.name, "has_batch_no", "No") + else: + if d.name in batch_items or (sle_count.get(d.name) and sle_count.get(d.name) == sle_with_batch.get(d.name)): + frappe.db.set_value("Item", d.name, "has_batch_no", "Yes") + + if d.has_serial_no == 'Yes': + if d.name not in serialized_items and sle_count.get(d.name) and sle_count.get(d.name) != sle_with_serial.get(d.name): + frappe.db.set_value("Item", d.name, "has_serial_no", "No") + else: + if d.name in serialized_items or (sle_count.get(d.name) and sle_count.get(d.name) == sle_with_serial.get(d.name)): + frappe.db.set_value("Item", d.name, "has_serial_no", "Yes") - if count_stock_entries > 0: - if count_stock_entries == count_batch_entries: - frappe.db.sql("""update `tabItem` set has_batch_no = 'Yes' where name = %s""",d.get("name")) - if count_batch_entries == 0: - frappe.db.sql("""update `tabItem` set has_batch_no = 'No' where name = %s""",d.get("name")) +def get_sle_count(): + sle_count = {} + for d in frappe.db.sql("""select item_code, count(name) as cnt from `tabStock Ledger Entry` group by item_code""", as_dict=1): + sle_count.setdefault(d.item_code, d.cnt) + + return sle_count + +def get_sle_with_batch(): + sle_with_batch = {} + for d in frappe.db.sql("""select item_code, count(name) as cnt from `tabStock Ledger Entry` + where batch_no != '' group by item_code""", as_dict=1): + sle_with_batch.setdefault(d.item_code, d.cnt) + + return sle_with_batch + + +def get_sle_with_serial(): + sle_with_serial = {} + for d in frappe.db.sql("""select item_code, count(name) as cnt from `tabStock Ledger Entry` + where serial_no != '' group by item_code""", as_dict=1): + sle_with_serial.setdefault(d.item_code, d.cnt) + + return sle_with_serial + +def get_items_with_batch(): + return frappe.db.sql_list("select item from tabBatch") + +def get_items_with_serial(): + return frappe.db.sql_list("select item_code from `tabSerial No`") \ No newline at end of file