fix: incorrect process loss validation for multiple finished items (backport #37576) (#37656)

fix: incorrect process loss validation for multiple finished items (#37576)

(cherry picked from commit 92cbe580e6)

Co-authored-by: rohitwaghchaure <rohitw1991@gmail.com>
This commit is contained in:
mergify[bot]
2023-10-24 18:03:34 +05:30
committed by GitHub
parent 356b1bdb38
commit 638c271d70

View File

@@ -451,31 +451,37 @@ class StockEntry(StockController):
item_code.append(item.item_code) item_code.append(item.item_code)
def validate_fg_completed_qty(self): def validate_fg_completed_qty(self):
item_wise_qty = {} if self.purpose != "Manufacture":
if self.purpose == "Manufacture" and self.work_order: return
for d in self.items:
if d.is_finished_item:
if self.process_loss_qty:
d.qty = self.fg_completed_qty - self.process_loss_qty
item_wise_qty.setdefault(d.item_code, []).append(d.qty) fg_qty = defaultdict(float)
for d in self.items:
if d.is_finished_item:
fg_qty[d.item_code] += flt(d.qty)
if not fg_qty:
return
precision = frappe.get_precision("Stock Entry Detail", "qty") precision = frappe.get_precision("Stock Entry Detail", "qty")
for item_code, qty_list in item_wise_qty.items(): fg_item = list(fg_qty.keys())[0]
total = flt(sum(qty_list), precision) fg_item_qty = flt(fg_qty[fg_item], precision)
fg_completed_qty = flt(self.fg_completed_qty, precision)
if (self.fg_completed_qty - total) > 0 and not self.process_loss_qty: for d in self.items:
self.process_loss_qty = flt(self.fg_completed_qty - total, precision) if not fg_qty.get(d.item_code):
self.process_loss_percentage = flt(self.process_loss_qty * 100 / self.fg_completed_qty) continue
if self.process_loss_qty: if (fg_completed_qty - fg_item_qty) > 0:
total += flt(self.process_loss_qty, precision) self.process_loss_qty = fg_completed_qty - fg_item_qty
if self.fg_completed_qty != total: if not self.process_loss_qty:
continue
if fg_completed_qty != (flt(fg_item_qty) + flt(self.process_loss_qty, precision)):
frappe.throw( frappe.throw(
_("The finished product {0} quantity {1} and For Quantity {2} cannot be different").format( _(
frappe.bold(item_code), frappe.bold(total), frappe.bold(self.fg_completed_qty) "Since there is a process loss of {0} units for the finished good {1}, you should reduce the quantity by {0} units for the finished good {1} in the Items Table."
) ).format(frappe.bold(self.process_loss_qty), frappe.bold(d.item_code))
) )
def validate_difference_account(self): def validate_difference_account(self):