From b011a68a2ac7b718b591ac699a37172ced3791e0 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Thu, 14 Mar 2024 16:48:55 +0530 Subject: [PATCH 1/3] refactor: disallow '0' qty return invoices with stock effect (cherry picked from commit 898affbee902b5f1d76f3cc7c4876b8d2eeb1a46) # Conflicts: # erpnext/controllers/accounts_controller.py --- erpnext/controllers/accounts_controller.py | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 8055b8180de..771c03c2179 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -157,6 +157,13 @@ class AccountsController(TransactionBase): if not self.get("is_return") and not self.get("is_debit_note"): self.validate_qty_is_not_zero() + if ( + self.doctype in ["Sales Invoice", "Purchase Invoice"] + and self.get("is_return") + and self.get("update_stock") + ): + self.validate_zero_qty_for_return_invoices_with_stock() + if self.get("_action") and self._action != "update_after_submit": self.set_missing_values(for_validate=True) @@ -950,6 +957,46 @@ class AccountsController(TransactionBase): return gl_dict +<<<<<<< HEAD +======= + def get_voucher_subtype(self): + voucher_subtypes = { + "Journal Entry": "voucher_type", + "Payment Entry": "payment_type", + "Stock Entry": "stock_entry_type", + "Asset Capitalization": "entry_type", + } + if self.doctype in voucher_subtypes: + return self.get(voucher_subtypes[self.doctype]) + elif self.doctype == "Purchase Receipt" and self.is_return: + return "Purchase Return" + elif self.doctype == "Delivery Note" and self.is_return: + return "Sales Return" + elif (self.doctype == "Sales Invoice" and self.is_return) or self.doctype == "Purchase Invoice": + return "Credit Note" + elif (self.doctype == "Purchase Invoice" and self.is_return) or self.doctype == "Sales Invoice": + return "Debit Note" + return self.doctype + + def get_value_in_transaction_currency(self, account_currency, args, field): + if account_currency == self.get("currency"): + return args.get(field + "_in_account_currency") + else: + return flt(args.get(field, 0) / self.get("conversion_rate", 1)) + + def validate_zero_qty_for_return_invoices_with_stock(self): + rows = [] + for item in self.items: + if not flt(item.qty): + rows.append(item) + if rows: + frappe.throw( + _( + "For Return Invoices with Stock effect, '0' qty Items are not allowed. Following rows are affected: {0}" + ).format(frappe.bold(comma_and(["#" + str(x.idx) for x in rows]))) + ) + +>>>>>>> 898affbee9 (refactor: disallow '0' qty return invoices with stock effect) def validate_qty_is_not_zero(self): if self.doctype == "Purchase Receipt": return From a914eb5d4010962d6c0d567155cf7f820b65c7d7 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 15 Mar 2024 10:47:32 +0530 Subject: [PATCH 2/3] test: validation to prevent '0' qty return with stock effect (cherry picked from commit 647bba0f00d24a4b88c9e258cc86116783db81fe) --- .../accounts/doctype/sales_invoice/test_sales_invoice.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py index ce5456da5d3..4d0f6446da4 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -1569,6 +1569,12 @@ class TestSalesInvoice(FrappeTestCase): self.assertEqual(frappe.db.get_value("Sales Invoice", si1.name, "outstanding_amount"), -1000) self.assertEqual(frappe.db.get_value("Sales Invoice", si.name, "outstanding_amount"), 2500) + def test_zero_qty_return_invoice_with_stock_effect(self): + cr_note = create_sales_invoice(qty=-1, rate=300, is_return=1, do_not_submit=True) + cr_note.update_stock = True + cr_note.items[0].qty = 0 + self.assertRaises(frappe.ValidationError, cr_note.save) + def test_return_invoice_with_account_mismatch(self): debtors2 = create_account( parent_account="Accounts Receivable - _TC", From 0437274c58e4c6f3024b052254174fb7b608d4b5 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 15 Mar 2024 11:39:54 +0530 Subject: [PATCH 3/3] chore: resolve conflict --- erpnext/controllers/accounts_controller.py | 28 ---------------------- 1 file changed, 28 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 771c03c2179..61bdc4f52a9 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -957,33 +957,6 @@ class AccountsController(TransactionBase): return gl_dict -<<<<<<< HEAD -======= - def get_voucher_subtype(self): - voucher_subtypes = { - "Journal Entry": "voucher_type", - "Payment Entry": "payment_type", - "Stock Entry": "stock_entry_type", - "Asset Capitalization": "entry_type", - } - if self.doctype in voucher_subtypes: - return self.get(voucher_subtypes[self.doctype]) - elif self.doctype == "Purchase Receipt" and self.is_return: - return "Purchase Return" - elif self.doctype == "Delivery Note" and self.is_return: - return "Sales Return" - elif (self.doctype == "Sales Invoice" and self.is_return) or self.doctype == "Purchase Invoice": - return "Credit Note" - elif (self.doctype == "Purchase Invoice" and self.is_return) or self.doctype == "Sales Invoice": - return "Debit Note" - return self.doctype - - def get_value_in_transaction_currency(self, account_currency, args, field): - if account_currency == self.get("currency"): - return args.get(field + "_in_account_currency") - else: - return flt(args.get(field, 0) / self.get("conversion_rate", 1)) - def validate_zero_qty_for_return_invoices_with_stock(self): rows = [] for item in self.items: @@ -996,7 +969,6 @@ class AccountsController(TransactionBase): ).format(frappe.bold(comma_and(["#" + str(x.idx) for x in rows]))) ) ->>>>>>> 898affbee9 (refactor: disallow '0' qty return invoices with stock effect) def validate_qty_is_not_zero(self): if self.doctype == "Purchase Receipt": return