diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index 097d4f1ad03..be3e08a17c9 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -567,6 +567,9 @@ erpnext.accounts.SalesInvoiceController = class SalesInvoiceController extends ( set_dynamic_labels() { super.set_dynamic_labels(); this.frm.events.hide_fields(this.frm); + if (this.frm.doc.is_debit_note) { + this.frm.set_df_property("update_stock", "hidden", 1); + } } items_on_form_rendered() { @@ -1155,6 +1158,16 @@ frappe.ui.form.on("Sales Invoice", { ); }, + is_debit_note: function (frm) { + if (frm.doc.is_debit_note) { + frm.set_value("update_stock", 0); + frm.set_df_property("update_stock", "hidden", 1); + } else { + frm.set_df_property("update_stock", "hidden", 0); + } + frm.refresh_field("update_stock"); + }, + refresh: function (frm) { if (frm.doc.is_debit_note) { frm.set_df_property("return_against", "label", __("Adjustment Against")); diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py index 86f84be0973..08a9a35c4e0 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py @@ -315,6 +315,7 @@ class SalesInvoice(SellingController): self.validate_uom_is_integer("uom", "qty") self.check_sales_order_on_hold_or_close("sales_order") self.validate_debit_to_acc() + self.validate_debit_note_with_update_stock() self.clear_unallocated_advances("Sales Invoice Advance", "advances") self.validate_fixed_asset() self.set_income_account_for_fixed_assets() @@ -1284,6 +1285,16 @@ class SalesInvoice(SellingController): if flt(self.change_amount) and not self.account_for_change_amount: msgprint(_("Please enter Account for Change Amount"), raise_exception=1) + def validate_debit_note_with_update_stock(self): + if self.is_debit_note and cint(self.update_stock): + frappe.throw( + _( + "You cannot update stock for a Debit Note. A Debit Note is a financial " + "document that should not affect inventory. Please disable 'Update Stock'." + ), + title=_("Invalid Configuration"), + ) + def validate_dropship_item(self): """If items are drop shipped, stock cannot be updated.""" if not cint(self.update_stock): diff --git a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py index b2e4ea875d0..f56562ab330 100644 --- a/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -4862,6 +4862,12 @@ class TestSalesInvoice(ERPNextTestSuite): frappe.db.set_value("Company", "_Test Company 1", "cost_center", cost_center) + def test_debit_note_with_update_stock_validation(self): + si = create_sales_invoice(do_not_save=True) + si.is_debit_note = 1 + si.update_stock = 1 + self.assertRaises(frappe.ValidationError, si.save) + def make_item_for_si(item_code, properties=None): from erpnext.stock.doctype.item.test_item import make_item