From 428f8944bf3b918496453c56f898a3ee8f0e8e93 Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Fri, 29 Dec 2023 12:06:04 +0530 Subject: [PATCH] fix: purchase return without item code not working (#39014) (cherry picked from commit f983e09f92c6fb179e8e8a972b1cb969b40c8fd5) --- .../purchase_invoice/test_purchase_invoice.py | 21 +++++++++++++++++++ .../controllers/sales_and_purchase_return.py | 17 ++++++++------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py index e43ea6ecbe0b..e41cec7eee3e 100644 --- a/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -1985,6 +1985,26 @@ def test_default_cost_center_for_purchase(self): self.assertEqual(pi.items[0].cost_center, "_Test Cost Center Buying - _TC") + def test_debit_note_without_item(self): + pi = make_purchase_invoice(item_name="_Test Item", qty=10, do_not_submit=True) + pi.items[0].item_code = "" + pi.save() + + self.assertFalse(pi.items[0].item_code) + pi.submit() + + return_pi = make_purchase_invoice( + item_name="_Test Item", + is_return=1, + return_against=pi.name, + qty=-10, + do_not_save=True, + ) + return_pi.items[0].item_code = "" + return_pi.save() + return_pi.submit() + self.assertEqual(return_pi.docstatus, 1) + def set_advance_flag(company, flag, default_account): frappe.db.set_value( @@ -2121,6 +2141,7 @@ def make_purchase_invoice(**args): "items", { "item_code": args.item or args.item_code or "_Test Item", + "item_name": args.item_name, "warehouse": args.warehouse or "_Test Warehouse - _TC", "qty": args.qty or 5, "received_qty": args.received_qty or 0, diff --git a/erpnext/controllers/sales_and_purchase_return.py b/erpnext/controllers/sales_and_purchase_return.py index 81080f026656..e7bd2a7265ce 100644 --- a/erpnext/controllers/sales_and_purchase_return.py +++ b/erpnext/controllers/sales_and_purchase_return.py @@ -562,16 +562,17 @@ def update_item(source_doc, target_doc, source_parent): if default_warehouse_for_sales_return: target_doc.warehouse = default_warehouse_for_sales_return - item_details = frappe.get_cached_value( - "Item", source_doc.item_code, ["has_batch_no", "has_serial_no"], as_dict=1 - ) + if source_doc.item_code: + item_details = frappe.get_cached_value( + "Item", source_doc.item_code, ["has_batch_no", "has_serial_no"], as_dict=1 + ) - if not item_details.has_batch_no and not item_details.has_serial_no: - return + if not item_details.has_batch_no and not item_details.has_serial_no: + return - for qty_field in ["stock_qty", "rejected_qty"]: - if target_doc.get(qty_field): - update_serial_batch_no(source_doc, target_doc, source_parent, item_details, qty_field) + for qty_field in ["stock_qty", "rejected_qty"]: + if target_doc.get(qty_field): + update_serial_batch_no(source_doc, target_doc, source_parent, item_details, qty_field) def update_terms(source_doc, target_doc, source_parent): target_doc.payment_amount = -source_doc.payment_amount