fix: system was allowing credit notes with serial numbers for any customer

This commit is contained in:
Rohit Waghchaure
2025-07-15 14:00:04 +05:30
parent 6e98adecdd
commit e073075834
7 changed files with 62 additions and 3 deletions

View File

@@ -38,6 +38,17 @@ def validate_return_against(doc):
party_type = "customer" if doc.doctype in ("Sales Invoice", "Delivery Note") else "supplier"
if ref_doc.get(party_type) != doc.get(party_type):
frappe.throw(
_("The {0} {1} does not match with the {0} {2} in the {3} {4}").format(
doc.meta.get_label(party_type),
doc.get(party_type),
ref_doc.get(party_type),
ref_doc.doctype,
ref_doc.name,
)
)
if (
ref_doc.company == doc.company
and ref_doc.get(party_type) == doc.get(party_type)

View File

@@ -61,6 +61,35 @@ class SellingController(StockController):
if self.get(table_field):
self.set_serial_and_batch_bundle(table_field)
def validate_standalone_serial_nos_customer(self):
if not self.is_return or self.return_against:
return
if self.doctype in ["Sales Invoice", "Delivery Note"]:
bundle_ids = [d.serial_and_batch_bundle for d in self.get("items") if d.serial_and_batch_bundle]
if not bundle_ids:
return
serial_nos = frappe.get_all(
"Serial and Batch Entry",
filters={"parent": ("in", bundle_ids)},
pluck="serial_no",
)
if serial_nos := frappe.get_all(
"Serial No",
filters={"name": ("in", serial_nos), "customer": ("is", "set")},
fields=["name", "customer"],
):
for sn in serial_nos:
if sn.customer and sn.customer != self.customer:
frappe.throw(
_(
"Serial No {0} is already assigned to customer {1}. Can only be returned against the customer {1}"
).format(frappe.bold(sn.name), frappe.bold(sn.customer)),
title=_("Serial No Already Assigned"),
)
def set_missing_values(self, for_validate=False):
super().set_missing_values(for_validate)