Skip to content

Commit

Permalink
refactor: extract common validation method
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra committed Aug 17, 2024
1 parent 34df6e3 commit 08646b7
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions erpnext/stock/doctype/delivery_note/delivery_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,32 +361,23 @@ def validate_references(self):
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(
Expand All @@ -396,7 +387,7 @@ def validate_sales_invoice_references(self):
)

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.."""
Expand Down

0 comments on commit 08646b7

Please sign in to comment.