Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update qty in SABB if qty changed in stock reco (backport #44542) #44546

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading