From 4f4dbf1996aed352c5030da8f04ccaa1c5354990 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Thu, 23 Jan 2020 12:42:42 +0530 Subject: [PATCH 1/2] fix: unlink references from batch on cacnellation of stock transactions --- .../accounts/doctype/purchase_invoice/purchase_invoice.py | 1 + erpnext/controllers/stock_controller.py | 7 +++++++ erpnext/stock/doctype/purchase_receipt/purchase_receipt.py | 1 + erpnext/stock/doctype/stock_entry/stock_entry.py | 1 + 4 files changed, 10 insertions(+) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 1a14a2a732d..261b0f03f7a 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -866,6 +866,7 @@ class PurchaseInvoice(BuyingController): # because updating ordered qty in bin depends upon updated ordered qty in PO if self.update_stock == 1: self.update_stock_ledger() + self.unlink_reference_from_batch() self.make_gl_entries_on_cancel() self.update_project() diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py index f344cb3a5ff..ba49daaa73d 100644 --- a/erpnext/controllers/stock_controller.py +++ b/erpnext/controllers/stock_controller.py @@ -234,6 +234,13 @@ class StockController(AccountsController): frappe.throw(_("{0} {1}: Cost Center is mandatory for Item {2}").format( _(self.doctype), self.name, item.get("item_code"))) + def unlink_reference_from_batch(self): + frappe.db.sql(""" UPDATE `tabBatch` + SET reference_doctype = NULL, reference_name = NULL + WHERE + reference_doctype = %s and reference_name = %s + """, (self.doctype, self.name)) + def get_sl_entries(self, d, args): sl_dict = frappe._dict({ "item_code": d.get("item_code", None), diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py index 691f92ffa72..e068dd7610f 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py @@ -195,6 +195,7 @@ class PurchaseReceipt(BuyingController): # because updating ordered qty in bin depends upon updated ordered qty in PO self.update_stock_ledger() self.make_gl_entries_on_cancel() + self.unlink_reference_from_batch() def get_current_stock(self): for d in self.get('supplied_items'): diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index ce11a236e2e..738ac421d9a 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -110,6 +110,7 @@ class StockEntry(StockController): self.update_cost_in_project() self.update_transferred_qty() self.update_quality_inspection() + self.unlink_reference_from_batch() def set_job_card_data(self): if self.job_card and not self.work_order: From 8996b7d8a403381505974e840106ed625e968cc8 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Thu, 23 Jan 2020 17:36:52 +0530 Subject: [PATCH 2/2] delete the auto created batch --- .../doctype/purchase_invoice/purchase_invoice.py | 2 +- erpnext/controllers/stock_controller.py | 16 ++++++++++------ .../doctype/purchase_receipt/purchase_receipt.py | 2 +- erpnext/stock/doctype/stock_entry/stock_entry.py | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 261b0f03f7a..d4676ff0375 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -866,7 +866,7 @@ class PurchaseInvoice(BuyingController): # because updating ordered qty in bin depends upon updated ordered qty in PO if self.update_stock == 1: self.update_stock_ledger() - self.unlink_reference_from_batch() + self.delete_auto_created_batches() self.make_gl_entries_on_cancel() self.update_project() diff --git a/erpnext/controllers/stock_controller.py b/erpnext/controllers/stock_controller.py index ba49daaa73d..14ee23b112f 100644 --- a/erpnext/controllers/stock_controller.py +++ b/erpnext/controllers/stock_controller.py @@ -234,12 +234,16 @@ class StockController(AccountsController): frappe.throw(_("{0} {1}: Cost Center is mandatory for Item {2}").format( _(self.doctype), self.name, item.get("item_code"))) - def unlink_reference_from_batch(self): - frappe.db.sql(""" UPDATE `tabBatch` - SET reference_doctype = NULL, reference_name = NULL - WHERE - reference_doctype = %s and reference_name = %s - """, (self.doctype, self.name)) + def delete_auto_created_batches(self): + for d in self.items: + if not d.batch_no: continue + + d.batch_no = None + d.db_set("batch_no", None) + + for data in frappe.get_all("Batch", + {'reference_name': self.name, 'reference_doctype': self.doctype}): + frappe.delete_doc("Batch", data.name) def get_sl_entries(self, d, args): sl_dict = frappe._dict({ diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py index e068dd7610f..09adb567513 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py @@ -195,7 +195,7 @@ class PurchaseReceipt(BuyingController): # because updating ordered qty in bin depends upon updated ordered qty in PO self.update_stock_ledger() self.make_gl_entries_on_cancel() - self.unlink_reference_from_batch() + self.delete_auto_created_batches() def get_current_stock(self): for d in self.get('supplied_items'): diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index 738ac421d9a..62e38c3572e 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -110,7 +110,7 @@ class StockEntry(StockController): self.update_cost_in_project() self.update_transferred_qty() self.update_quality_inspection() - self.unlink_reference_from_batch() + self.delete_auto_created_batches() def set_job_card_data(self): if self.job_card and not self.work_order: