Skip to content

Commit

Permalink
fix: Use serial/batch fields for delivery note return (#40374)
Browse files Browse the repository at this point in the history
fix: User serial/batch fields for delivery note return
  • Loading branch information
rohitwaghchaure authored Mar 11, 2024
1 parent a97a851 commit b9a8dd4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
46 changes: 45 additions & 1 deletion erpnext/controllers/sales_and_purchase_return.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,52 @@ def update_item(source_doc, target_doc, source_parent):
return

for qty_field in ["stock_qty", "rejected_qty"]:
if target_doc.get(qty_field):
if target_doc.get(qty_field) and not target_doc.get("use_serial_batch_fields"):
update_serial_batch_no(source_doc, target_doc, source_parent, item_details, qty_field)
elif target_doc.get(qty_field) and target_doc.get("use_serial_batch_fields"):
update_non_bundled_serial_nos(source_doc, target_doc, source_parent)

def update_non_bundled_serial_nos(source_doc, target_doc, source_parent):
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos

if source_doc.serial_no:
returned_serial_nos = get_returned_non_bundled_serial_nos(source_doc, source_parent)
serial_nos = list(set(get_serial_nos(source_doc.serial_no)) - set(returned_serial_nos))
if serial_nos:
target_doc.serial_no = "\n".join(serial_nos)

if source_doc.get("rejected_serial_no"):
returned_serial_nos = get_returned_non_bundled_serial_nos(
source_doc, source_parent, serial_no_field="rejected_serial_no"
)
rejected_serial_nos = list(
set(get_serial_nos(source_doc.rejected_serial_no)) - set(returned_serial_nos)
)
if rejected_serial_nos:
target_doc.rejected_serial_no = "\n".join(rejected_serial_nos)

def get_returned_non_bundled_serial_nos(child_doc, parent_doc, serial_no_field="serial_no"):
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos

return_ref_field = frappe.scrub(child_doc.doctype)
if child_doc.doctype == "Delivery Note Item":
return_ref_field = "dn_detail"

serial_nos = []

fields = [f"`{'tab' + child_doc.doctype}`.`{serial_no_field}`"]

filters = [
[parent_doc.doctype, "return_against", "=", parent_doc.name],
[parent_doc.doctype, "is_return", "=", 1],
[child_doc.doctype, return_ref_field, "=", child_doc.name],
[parent_doc.doctype, "docstatus", "=", 1],
]

for row in frappe.get_all(parent_doc.doctype, fields=fields, filters=filters):
serial_nos.extend(get_serial_nos(row.get(serial_no_field)))

return serial_nos

def update_terms(source_doc, target_doc, source_parent):
target_doc.payment_amount = -source_doc.payment_amount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ def make_bundle_for_current_qty(self):
"voucher_type": self.doctype,
"voucher_no": self.name,
"voucher_detail_no": row.name,
"qty": row.qty,
"qty": row.current_qty,
"type_of_transaction": "Outward",
"company": self.company,
"is_rejected": 0,
"serial_nos": get_serial_nos(row.current_serial_no) if row.current_serial_no else None,
"batches": frappe._dict({row.batch_no: row.qty}) if row.batch_no else None,
"batches": frappe._dict({row.batch_no: row.current_qty}) if row.batch_no else None,
"batch_no": row.batch_no,
"do_not_submit": True,
}
Expand Down

0 comments on commit b9a8dd4

Please sign in to comment.