refactor: add guard clause for readability
This commit is contained in:
parent
0b4858d8e5
commit
83e6e2e68a
@ -953,20 +953,22 @@ class Item(WebsiteGenerator):
|
|||||||
d.variant_of = self.variant_of
|
d.variant_of = self.variant_of
|
||||||
|
|
||||||
def cant_change(self):
|
def cant_change(self):
|
||||||
if not self.get("__islocal"):
|
if self.get("__islocal"):
|
||||||
fields = ("has_serial_no", "is_stock_item", "valuation_method", "has_batch_no")
|
return
|
||||||
|
|
||||||
values = frappe.db.get_value("Item", self.name, fields, as_dict=True)
|
fields = ("has_serial_no", "is_stock_item", "valuation_method", "has_batch_no")
|
||||||
if not values.get('valuation_method') and self.get('valuation_method'):
|
|
||||||
values['valuation_method'] = frappe.db.get_single_value("Stock Settings", "valuation_method") or "FIFO"
|
|
||||||
|
|
||||||
if values:
|
values = frappe.db.get_value("Item", self.name, fields, as_dict=True)
|
||||||
for field in fields:
|
if not values.get('valuation_method') and self.get('valuation_method'):
|
||||||
if cstr(self.get(field)) != cstr(values.get(field)):
|
values['valuation_method'] = frappe.db.get_single_value("Stock Settings", "valuation_method") or "FIFO"
|
||||||
if not self.check_if_linked_document_exists(field):
|
|
||||||
break # no linked document, allowed
|
if values:
|
||||||
else:
|
for field in fields:
|
||||||
frappe.throw(_("As there are existing transactions against item {0}, you can not change the value of {1}").format(self.name, frappe.bold(self.meta.get_label(field))))
|
if cstr(self.get(field)) != cstr(values.get(field)):
|
||||||
|
if not self.check_if_linked_document_exists(field):
|
||||||
|
break # no linked document, allowed
|
||||||
|
else:
|
||||||
|
frappe.throw(_("As there are existing transactions against item {0}, you can not change the value of {1}").format(self.name, frappe.bold(self.meta.get_label(field))))
|
||||||
|
|
||||||
def check_if_linked_document_exists(self, field):
|
def check_if_linked_document_exists(self, field):
|
||||||
linked_doctypes = ["Delivery Note Item", "Sales Invoice Item", "POS Invoice Item", "Purchase Receipt Item",
|
linked_doctypes = ["Delivery Note Item", "Sales Invoice Item", "POS Invoice Item", "Purchase Receipt Item",
|
||||||
|
Loading…
Reference in New Issue
Block a user