refactor: extract common validation method

(cherry picked from commit 08646b7ab7)
This commit is contained in:
barredterra
2024-08-17 02:36:16 +02:00
committed by Mergify
parent ea4f7365ea
commit 0c0f103b83

View File

@@ -362,32 +362,23 @@ class DeliveryNote(SellingController):
self.validate_sales_invoice_references()
def validate_sales_order_references(self):
errors = []
for item in self.items:
missing_label = None
if item.against_sales_order and not item.so_detail:
missing_label = item.meta.get_label("so_detail")
elif item.so_detail and not item.against_sales_order:
missing_label = item.meta.get_label("against_sales_order")
if missing_label and missing_label != "No Label":
errors.append(
_("The field {0} in row {1} is not set").format(
frappe.bold(_(missing_label)), frappe.bold(item.idx)
)
)
if errors:
frappe.throw("<br>".join(errors), title=_("References to Sales Orders are Incomplete"))
self._validate_dependent_item_fields(
"against_sales_order", "so_detail", _("References to Sales Orders are Incomplete")
)
def validate_sales_invoice_references(self):
self._validate_dependent_item_fields(
"against_sales_invoice", "si_detail", _("References to Sales Invoices are Incomplete")
)
def _validate_dependent_item_fields(self, field_a: str, field_b: str, error_title: str):
errors = []
for item in self.items:
missing_label = None
if item.against_sales_invoice and not item.si_detail:
missing_label = item.meta.get_label("si_detail")
elif item.si_detail and not item.against_sales_invoice:
missing_label = item.meta.get_label("against_sales_invoice")
if item.get(field_a) and not item.get(field_b):
missing_label = item.meta.get_label(field_b)
elif item.get(field_b) and not item.get(field_a):
missing_label = item.meta.get_label(field_a)
if missing_label and missing_label != "No Label":
errors.append(
@@ -397,7 +388,7 @@ class DeliveryNote(SellingController):
)
if errors:
frappe.throw("<br>".join(errors), title=_("References to Sales Invoices are Incomplete"))
frappe.throw("<br>".join(errors), title=error_title)
def validate_proj_cust(self):
"""check for does customer belong to same project as entered.."""