Skip to content

Commit

Permalink
fix: update qty in SABB if qty changed in stock reco
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitwaghchaure committed Dec 5, 2024
1 parent 707d8ed commit 1d57c1a
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ def set_current_serial_and_batch_bundle(self, voucher_detail_no=None, save=False
if not frappe.db.exists("Item", item.item_code):
frappe.throw(_("Item {0} does not exist").format(item.item_code))

item_details = frappe.get_cached_value(
"Item", item.item_code, ["has_serial_no", "has_batch_no"], as_dict=1
)

if not (item_details.has_serial_no or item_details.has_batch_no):
continue

if (
not item.use_serial_batch_fields
and not item.reconcile_all_serial_batch
and not item.serial_and_batch_bundle
):
frappe.throw(
_("Row # {0}: Please add Serial and Batch Bundle for Item {1}").format(
item.idx, frappe.bold(item.item_code)
)
)

if not item.reconcile_all_serial_batch and item.serial_and_batch_bundle:
bundle = self.get_bundle_for_specific_serial_batch(item)
item.current_serial_and_batch_bundle = bundle.name
Expand All @@ -181,13 +199,6 @@ def set_current_serial_and_batch_bundle(self, voucher_detail_no=None, save=False
if voucher_detail_no and voucher_detail_no != item.name:
continue

item_details = frappe.get_cached_value(
"Item", item.item_code, ["has_serial_no", "has_batch_no"], as_dict=1
)

if not (item_details.has_serial_no or item_details.has_batch_no):
continue

if not item.current_serial_and_batch_bundle:
serial_and_batch_bundle = frappe.get_doc(
{
Expand Down Expand Up @@ -400,6 +411,28 @@ def set_new_serial_and_batch_bundle(self):
item.qty = bundle_doc.total_qty
item.valuation_rate = bundle_doc.avg_rate

elif item.serial_and_batch_bundle and item.qty:
self.update_existing_serial_and_batch_bundle(item)

def update_existing_serial_and_batch_bundle(self, item):
batch_details = frappe.get_all(
"Serial and Batch Entry",
fields=["batch_no", "qty", "name"],
filters={"parent": item.serial_and_batch_bundle, "batch_no": ("is", "set")},
)

if batch_details and len(batch_details) == 1:
batch = batch_details[0]
if abs(batch.qty) == abs(item.qty):
return

update_values = {
"qty": item.qty,
"stock_value_difference": flt(item.valuation_rate) * flt(item.qty),
}

frappe.db.set_value("Serial and Batch Entry", batch.name, update_values)

def remove_items_with_no_change(self):
"""Remove items if qty or rate is not changed"""
self.difference_amount = 0.0
Expand Down

0 comments on commit 1d57c1a

Please sign in to comment.