Aggregate when adding truck dump child row already present in parent

This commit is contained in:
Lance Edgar 2019-02-18 18:19:11 -06:00
parent 0bf04a7272
commit 3336da3db0

View file

@ -134,10 +134,48 @@ class PurchaseBatchHandler(BatchHandler):
def append(child_row, i):
if not child_row.out_of_stock:
# if row for this product already exists in parent, must aggregate
parent_row = self.locate_parent_row_for_child(parent_batch, child_row)
if parent_row:
raise NotImplementedError("TODO: add some aggregation logic")
else:
# confirm 'case_quantity' matches
if parent_row.case_quantity != child_row.case_quantity:
raise ValueError("differing 'case_quantity' for item {}: {}".format(
child_row.item_entry, child_row.description))
# confirm 'out_of_stock' matches
if parent_row.out_of_stock != child_row.out_of_stock:
raise ValueError("differing 'out_of_stock' for item {}: {}".format(
cihld_row.item_entry, child_row.description))
# confirm 'invoice_unit_cost' matches
if parent_row.invoice_unit_cost != child_row.invoice_unit_cost:
raise ValueError("differing 'invoice_unit_cost' for item {}: {}".format(
cihld_row.item_entry, child_row.description))
# confirm 'invoice_case_cost' matches
if parent_row.invoice_case_cost != child_row.invoice_case_cost:
raise ValueError("differing 'invoice_case_cost' for item {}: {}".format(
cihld_row.item_entry, child_row.description))
# add 'ordered' quantities
if child_row.cases_ordered:
parent_row.cases_ordered = (parent_row.cases_ordered or 0) + child_row.cases_ordered
if child_row.units_ordered:
parent_row.units_ordered = (parent_row.units_ordered or 0) + child_row.units_ordered
# add 'shipped' quantities
if child_row.cases_shipped:
parent_row.cases_shipped = (parent_row.cases_shipped or 0) + child_row.cases_shipped
if child_row.units_shipped:
parent_row.units_shipped = (parent_row.units_shipped or 0) + child_row.units_shipped
# add 'invoice_total' quantities
if child_row.invoice_total:
parent_row.invoice_total = (parent_row.invoice_total or 0) + child_row.invoice_total
else: # new product; simply add new row to parent
parent_row = self.make_parent_row_from_child(child_row)
self.add_row(parent_batch, parent_row)