From 3355dc2a41137a0fd00a9a37c9c00b5691181faa Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Thu, 15 Jun 2023 14:45:19 +0530 Subject: [PATCH 1/3] fix: incorrect gl entries for standalone debit note with update stock (cherry picked from commit 6e198188ff90ad7291a48f2fbfaac069eaee8381) # Conflicts: # erpnext/controllers/buying_controller.py --- erpnext/controllers/buying_controller.py | 57 ++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py index fa566f62ef0..7645266ebb5 100644 --- a/erpnext/controllers/buying_controller.py +++ b/erpnext/controllers/buying_controller.py @@ -30,6 +30,8 @@ class BuyingController(SubcontractingController): return _("From {0} | {1} {2}").format(self.supplier_name, self.currency, self.grand_total) def validate(self): + self.set_rate_for_standalone_debit_note() + super(BuyingController, self).validate() if getattr(self, "supplier", None) and not self.supplier_name: self.supplier_name = frappe.db.get_value("Supplier", self.supplier, "supplier_name") @@ -72,6 +74,61 @@ class BuyingController(SubcontractingController): ), ) +<<<<<<< HEAD +======= + def create_package_for_transfer(self) -> None: + """Create serial and batch package for Sourece Warehouse in case of inter transfer.""" + + if self.is_internal_transfer() and ( + self.doctype == "Purchase Receipt" or (self.doctype == "Purchase Invoice" and self.update_stock) + ): + field = "delivery_note_item" if self.doctype == "Purchase Receipt" else "sales_invoice_item" + + doctype = "Delivery Note Item" if self.doctype == "Purchase Receipt" else "Sales Invoice Item" + + ids = [d.get(field) for d in self.get("items") if d.get(field)] + bundle_ids = {} + if ids: + for bundle in frappe.get_all( + doctype, filters={"name": ("in", ids)}, fields=["serial_and_batch_bundle", "name"] + ): + bundle_ids[bundle.name] = bundle.serial_and_batch_bundle + + if not bundle_ids: + return + + for item in self.get("items"): + if item.get(field) and not item.serial_and_batch_bundle and bundle_ids.get(item.get(field)): + item.serial_and_batch_bundle = self.make_package_for_transfer( + bundle_ids.get(item.get(field)), + item.from_warehouse, + type_of_transaction="Outward", + do_not_submit=True, + ) + + def set_rate_for_standalone_debit_note(self): + if self.get("is_return") and self.get("update_stock") and not self.return_against: + for row in self.items: + row.rate = get_incoming_rate( + { + "item_code": row.item_code, + "warehouse": row.warehouse, + "posting_date": self.get("posting_date"), + "posting_time": self.get("posting_time"), + "qty": row.qty, + "serial_and_batch_bundle": row.get("serial_and_batch_bundle"), + "company": self.company, + "voucher_type": self.doctype, + "voucher_no": self.name, + }, + raise_error_if_no_rate=False, + ) + + row.discount_percentage = 0.0 + row.discount_amount = 0.0 + row.margin_rate_or_amount = 0.0 + +>>>>>>> 6e198188ff (fix: incorrect gl entries for standalone debit note with update stock) def set_missing_values(self, for_validate=False): super(BuyingController, self).set_missing_values(for_validate) From e2c4e16d7202011d8d88bd9b212820a7e9ecc6d0 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Thu, 15 Jun 2023 15:01:15 +0530 Subject: [PATCH 2/3] test: added test case (cherry picked from commit f9f662679fc7ebcbfb96198b931c449cecab6ec2) --- .../purchase_invoice/test_purchase_invoice.py | 22 +++++++++++++------ erpnext/controllers/buying_controller.py | 2 ++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index a6d7df6971f..e8766275f0b 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -637,13 +637,6 @@ class TestPurchaseInvoice(unittest.TestCase, StockTestMixin): gle_filters={"account": "Stock In Hand - TCP1"}, ) - # assert loss booked in COGS - self.assertGLEs( - return_pi, - [{"credit": 0, "debit": 200}], - gle_filters={"account": "Cost of Goods Sold - TCP1"}, - ) - def test_return_with_lcv(self): from erpnext.controllers.sales_and_purchase_return import make_return_doc from erpnext.stock.doctype.landed_cost_voucher.test_landed_cost_voucher import ( @@ -1662,6 +1655,21 @@ class TestPurchaseInvoice(unittest.TestCase, StockTestMixin): self.assertTrue(return_pi.docstatus == 1) + def test_gl_entries_for_standalone_debit_note(self): + make_purchase_invoice(qty=5, rate=500, update_stock=True) + + returned_inv = make_purchase_invoice(qty=-5, rate=5, update_stock=True, is_return=True) + + # override the rate with valuation rate + sle = frappe.get_all( + "Stock Ledger Entry", + fields=["stock_value_difference", "actual_qty"], + filters={"voucher_no": returned_inv.name}, + )[0] + + rate = flt(sle.stock_value_difference) / flt(sle.actual_qty) + self.assertAlmostEqual(returned_inv.items[0].rate, rate) + def check_gl_entries(doc, voucher_no, expected_gle, posting_date): gl_entries = frappe.db.sql( diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py index 7645266ebb5..6ca7533b084 100644 --- a/erpnext/controllers/buying_controller.py +++ b/erpnext/controllers/buying_controller.py @@ -109,6 +109,8 @@ class BuyingController(SubcontractingController): def set_rate_for_standalone_debit_note(self): if self.get("is_return") and self.get("update_stock") and not self.return_against: for row in self.items: + + # override the rate with valuation rate row.rate = get_incoming_rate( { "item_code": row.item_code, From 697fcef98bbbf5b2980cbfc817bceac34c99ff18 Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Thu, 15 Jun 2023 19:14:41 +0530 Subject: [PATCH 3/3] fix: conflicts --- erpnext/controllers/buying_controller.py | 33 ------------------------ 1 file changed, 33 deletions(-) diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py index 6ca7533b084..677e510d169 100644 --- a/erpnext/controllers/buying_controller.py +++ b/erpnext/controllers/buying_controller.py @@ -74,38 +74,6 @@ class BuyingController(SubcontractingController): ), ) -<<<<<<< HEAD -======= - def create_package_for_transfer(self) -> None: - """Create serial and batch package for Sourece Warehouse in case of inter transfer.""" - - if self.is_internal_transfer() and ( - self.doctype == "Purchase Receipt" or (self.doctype == "Purchase Invoice" and self.update_stock) - ): - field = "delivery_note_item" if self.doctype == "Purchase Receipt" else "sales_invoice_item" - - doctype = "Delivery Note Item" if self.doctype == "Purchase Receipt" else "Sales Invoice Item" - - ids = [d.get(field) for d in self.get("items") if d.get(field)] - bundle_ids = {} - if ids: - for bundle in frappe.get_all( - doctype, filters={"name": ("in", ids)}, fields=["serial_and_batch_bundle", "name"] - ): - bundle_ids[bundle.name] = bundle.serial_and_batch_bundle - - if not bundle_ids: - return - - for item in self.get("items"): - if item.get(field) and not item.serial_and_batch_bundle and bundle_ids.get(item.get(field)): - item.serial_and_batch_bundle = self.make_package_for_transfer( - bundle_ids.get(item.get(field)), - item.from_warehouse, - type_of_transaction="Outward", - do_not_submit=True, - ) - def set_rate_for_standalone_debit_note(self): if self.get("is_return") and self.get("update_stock") and not self.return_against: for row in self.items: @@ -130,7 +98,6 @@ class BuyingController(SubcontractingController): row.discount_amount = 0.0 row.margin_rate_or_amount = 0.0 ->>>>>>> 6e198188ff (fix: incorrect gl entries for standalone debit note with update stock) def set_missing_values(self, for_validate=False): super(BuyingController, self).set_missing_values(for_validate)