Use "shipped" instead of "ordered" for truck dump child row "claims"

i.e. if 1 CS is received, that should count against the *shipped* quantity and
not the "ordered" quantity.  if shipped is not explicitly stated on the
invoice, its value should be copied from the ordered quantity
This commit is contained in:
Lance Edgar 2019-04-01 13:18:40 -05:00
parent 850c197834
commit 1d05f73bfe

View file

@ -361,24 +361,24 @@ class PurchaseBatchHandler(BatchHandler):
# for each child row we also calculate all "present", and "claimed" # for each child row we also calculate all "present", and "claimed"
# vs. "pending" product quantities # vs. "pending" product quantities
# cases_ordered # cases_shipped
cases_ordered = child_row.cases_ordered or 0 cases_shipped = child_row.cases_shipped or 0
cases_ordered_claimed = sum([(claim.cases_received or 0) cases_shipped_claimed = sum([(claim.cases_received or 0)
+ (claim.cases_damaged or 0) + (claim.cases_damaged or 0)
+ (claim.cases_expired or 0) + (claim.cases_expired or 0)
for claim in child_row.truck_dump_claims]) for claim in child_row.truck_dump_claims])
cases_ordered_pending = cases_ordered - cases_ordered_claimed cases_shipped_pending = cases_shipped - cases_shipped_claimed
# units_ordered # units_shipped
units_ordered = child_row.units_ordered or 0 units_shipped = child_row.units_shipped or 0
units_ordered_claimed = sum([(claim.units_received or 0) units_shipped_claimed = sum([(claim.units_received or 0)
+ (claim.units_damaged or 0) + (claim.units_damaged or 0)
+ (claim.units_expired or 0) + (claim.units_expired or 0)
for claim in child_row.truck_dump_claims]) for claim in child_row.truck_dump_claims])
units_ordered_pending = units_ordered - units_ordered_claimed units_shipped_pending = units_shipped - units_shipped_claimed
# skip this child row if everything in it is accounted for # skip this child row if everything in it is accounted for
if not (cases_ordered_pending or units_ordered_pending): if not (cases_shipped_pending or units_shipped_pending):
continue continue
# there should only be one claim for this parent/child combo # there should only be one claim for this parent/child combo
@ -388,84 +388,84 @@ class PurchaseBatchHandler(BatchHandler):
case_quantity = child_row.case_quantity case_quantity = child_row.case_quantity
# make case claims # make case claims
if cases_ordered_pending and cases_received_pending: if cases_shipped_pending and cases_received_pending:
claim = claim or make_claim(child_row) claim = claim or make_claim(child_row)
if cases_received_pending >= cases_ordered_pending: if cases_received_pending >= cases_shipped_pending:
claim.cases_received = (claim.cases_received or 0) + cases_ordered_pending claim.cases_received = (claim.cases_received or 0) + cases_shipped_pending
child_row.cases_received = (child_row.cases_received or 0) + cases_ordered_pending child_row.cases_received = (child_row.cases_received or 0) + cases_shipped_pending
cases_received_pending -= cases_ordered_pending cases_received_pending -= cases_shipped_pending
cases_ordered_pending = 0 cases_shipped_pending = 0
else: # ordered > received else: # shipped > received
claim.cases_received = (claim.cases_received or 0) + cases_received_pending claim.cases_received = (claim.cases_received or 0) + cases_received_pending
child_row.cases_received = (child_row.cases_received or 0) + cases_received_pending child_row.cases_received = (child_row.cases_received or 0) + cases_received_pending
cases_ordered_pending -= cases_received_pending cases_shipped_pending -= cases_received_pending
cases_received_pending = 0 cases_received_pending = 0
self.refresh_row(child_row) self.refresh_row(child_row)
if cases_ordered_pending and cases_damaged_pending: if cases_shipped_pending and cases_damaged_pending:
claim = claim or make_claim(child_row) claim = claim or make_claim(child_row)
if cases_damaged_pending >= cases_ordered_pending: if cases_damaged_pending >= cases_shipped_pending:
claim.cases_damaged = (claim.cases_damaged or 0) + cases_ordered_pending claim.cases_damaged = (claim.cases_damaged or 0) + cases_shipped_pending
child_row.cases_damaged = (child_row.cases_damaged or 0) + cases_ordered_pending child_row.cases_damaged = (child_row.cases_damaged or 0) + cases_shipped_pending
cases_damaged_pending -= cases_ordered_pending cases_damaged_pending -= cases_shipped_pending
cases_ordered_pending = 0 cases_shipped_pending = 0
else: # ordered > damaged else: # shipped > damaged
claim.cases_damaged = (claim.cases_damaged or 0) + cases_damaged_pending claim.cases_damaged = (claim.cases_damaged or 0) + cases_damaged_pending
child_row.cases_damaged = (child_row.cases_damaged or 0) + cases_damaged_pending child_row.cases_damaged = (child_row.cases_damaged or 0) + cases_damaged_pending
cases_ordered_pending -= cases_damaged_pending cases_shipped_pending -= cases_damaged_pending
cases_damaged_pending = 0 cases_damaged_pending = 0
self.refresh_row(child_row) self.refresh_row(child_row)
if cases_ordered_pending and cases_expired_pending: if cases_shipped_pending and cases_expired_pending:
claim = claim or make_claim(child_row) claim = claim or make_claim(child_row)
if cases_expired_pending >= cases_ordered_pending: if cases_expired_pending >= cases_shipped_pending:
claim.cases_expired = (claim.cases_expired or 0) + cases_ordered_pending claim.cases_expired = (claim.cases_expired or 0) + cases_shipped_pending
child_row.cases_expired = (child_row.cases_expired or 0) + cases_ordered_pending child_row.cases_expired = (child_row.cases_expired or 0) + cases_shipped_pending
cases_expired_pending -= cases_ordered_pending cases_expired_pending -= cases_shipped_pending
cases_ordered_pending = 0 cases_shipped_pending = 0
else: # ordered > expired else: # shipped > expired
claim.cases_expired = (claim.cases_expired or 0) + cases_expired_pending claim.cases_expired = (claim.cases_expired or 0) + cases_expired_pending
child_row.cases_expired = (child_row.cases_expired or 0) + cases_expired_pending child_row.cases_expired = (child_row.cases_expired or 0) + cases_expired_pending
cases_ordered_pending -= cases_expired_pending cases_shipped_pending -= cases_expired_pending
cases_expired_pending = 0 cases_expired_pending = 0
self.refresh_row(child_row) self.refresh_row(child_row)
# make unit claims # make unit claims
if units_ordered_pending and units_received_pending: if units_shipped_pending and units_received_pending:
claim = claim or make_claim(child_row) claim = claim or make_claim(child_row)
if units_received_pending >= units_ordered_pending: if units_received_pending >= units_shipped_pending:
claim.units_received = (claim.units_received or 0) + units_ordered_pending claim.units_received = (claim.units_received or 0) + units_shipped_pending
child_row.units_received = (child_row.units_received or 0) + units_ordered_pending child_row.units_received = (child_row.units_received or 0) + units_shipped_pending
units_received_pending -= units_ordered_pending units_received_pending -= units_shipped_pending
units_ordered_pending = 0 units_shipped_pending = 0
else: # ordered > received else: # shipped > received
claim.units_received = (claim.units_received or 0) + units_received_pending claim.units_received = (claim.units_received or 0) + units_received_pending
child_row.units_received = (child_row.units_received or 0) + units_received_pending child_row.units_received = (child_row.units_received or 0) + units_received_pending
units_ordered_pending -= units_received_pending units_shipped_pending -= units_received_pending
units_received_pending = 0 units_received_pending = 0
self.refresh_row(child_row) self.refresh_row(child_row)
if units_ordered_pending and units_damaged_pending: if units_shipped_pending and units_damaged_pending:
claim = claim or make_claim(child_row) claim = claim or make_claim(child_row)
if units_damaged_pending >= units_ordered_pending: if units_damaged_pending >= units_shipped_pending:
claim.units_damaged = (claim.units_damaged or 0) + units_ordered_pending claim.units_damaged = (claim.units_damaged or 0) + units_shipped_pending
child_row.units_damaged = (child_row.units_damaged or 0) + units_ordered_pending child_row.units_damaged = (child_row.units_damaged or 0) + units_shipped_pending
units_damaged_pending -= units_ordered_pending units_damaged_pending -= units_shipped_pending
units_ordered_pending = 0 units_shipped_pending = 0
else: # ordered > damaged else: # shipped > damaged
claim.units_damaged = (claim.units_damaged or 0) + units_damaged_pending claim.units_damaged = (claim.units_damaged or 0) + units_damaged_pending
child_row.units_damaged = (child_row.units_damaged or 0) + units_damaged_pending child_row.units_damaged = (child_row.units_damaged or 0) + units_damaged_pending
units_ordered_pending -= units_damaged_pending units_shipped_pending -= units_damaged_pending
units_damaged_pending = 0 units_damaged_pending = 0
self.refresh_row(child_row) self.refresh_row(child_row)
if units_ordered_pending and units_expired_pending: if units_shipped_pending and units_expired_pending:
claim = claim or make_claim(child_row) claim = claim or make_claim(child_row)
if units_expired_pending >= units_ordered_pending: if units_expired_pending >= units_shipped_pending:
claim.units_expired = (claim.units_expired or 0) + units_ordered_pending claim.units_expired = (claim.units_expired or 0) + units_shipped_pending
child_row.units_expired = (child_row.units_expired or 0) + units_ordered_pending child_row.units_expired = (child_row.units_expired or 0) + units_shipped_pending
units_expired_pending -= units_ordered_pending units_expired_pending -= units_shipped_pending
units_ordered_pending = 0 units_shipped_pending = 0
else: # ordered > expired else: # shipped > expired
claim.units_expired = (claim.units_expired or 0) + units_expired_pending claim.units_expired = (claim.units_expired or 0) + units_expired_pending
child_row.units_expired = (child_row.units_expired or 0) + units_expired_pending child_row.units_expired = (child_row.units_expired or 0) + units_expired_pending
units_ordered_pending -= units_expired_pending units_shipped_pending -= units_expired_pending
units_expired_pending = 0 units_expired_pending = 0
self.refresh_row(child_row) self.refresh_row(child_row)
@ -473,49 +473,49 @@ class PurchaseBatchHandler(BatchHandler):
# crosses the case/unit boundary, but is considered "safe" because # crosses the case/unit boundary, but is considered "safe" because
# we assume the child row has correct case quantity even if parent # we assume the child row has correct case quantity even if parent
# row has a different one. # row has a different one.
if cases_ordered_pending and units_received_pending: if cases_shipped_pending and units_received_pending:
received = units_received_pending // case_quantity received = units_received_pending // case_quantity
if received: if received:
claim = claim or make_claim(child_row) claim = claim or make_claim(child_row)
if received >= cases_ordered_pending: if received >= cases_shipped_pending:
claim.cases_received = (claim.cases_received or 0) + cases_ordered_pending claim.cases_received = (claim.cases_received or 0) + cases_shipped_pending
child_row.cases_received = (child_row.units_received or 0) + cases_ordered_pending child_row.cases_received = (child_row.units_received or 0) + cases_shipped_pending
units_received_pending -= (cases_ordered_pending * case_quantity) units_received_pending -= (cases_shipped_pending * case_quantity)
cases_ordered_pending = 0 cases_shipped_pending = 0
else: # ordered > received else: # shipped > received
claim.cases_received = (claim.cases_received or 0) + received claim.cases_received = (claim.cases_received or 0) + received
child_row.cases_received = (child_row.units_received or 0) + received child_row.cases_received = (child_row.units_received or 0) + received
cases_ordered_pending -= received cases_shipped_pending -= received
units_received_pending -= (received * case_quantity) units_received_pending -= (received * case_quantity)
self.refresh_row(child_row) self.refresh_row(child_row)
if cases_ordered_pending and units_damaged_pending: if cases_shipped_pending and units_damaged_pending:
damaged = units_damaged_pending // case_quantity damaged = units_damaged_pending // case_quantity
if damaged: if damaged:
claim = claim or make_claim(child_row) claim = claim or make_claim(child_row)
if damaged >= cases_ordered_pending: if damaged >= cases_shipped_pending:
claim.cases_damaged = (claim.cases_damaged or 0) + cases_ordered_pending claim.cases_damaged = (claim.cases_damaged or 0) + cases_shipped_pending
child_row.cases_damaged = (child_row.units_damaged or 0) + cases_ordered_pending child_row.cases_damaged = (child_row.units_damaged or 0) + cases_shipped_pending
units_damaged_pending -= (cases_ordered_pending * case_quantity) units_damaged_pending -= (cases_shipped_pending * case_quantity)
cases_ordered_pending = 0 cases_shipped_pending = 0
else: # ordered > damaged else: # shipped > damaged
claim.cases_damaged = (claim.cases_damaged or 0) + damaged claim.cases_damaged = (claim.cases_damaged or 0) + damaged
child_row.cases_damaged = (child_row.units_damaged or 0) + damaged child_row.cases_damaged = (child_row.units_damaged or 0) + damaged
cases_ordered_pending -= damaged cases_shipped_pending -= damaged
units_damaged_pending -= (damaged * case_quantity) units_damaged_pending -= (damaged * case_quantity)
self.refresh_row(child_row) self.refresh_row(child_row)
if cases_ordered_pending and units_expired_pending: if cases_shipped_pending and units_expired_pending:
expired = units_expired_pending // case_quantity expired = units_expired_pending // case_quantity
if expired: if expired:
claim = claim or make_claim(child_row) claim = claim or make_claim(child_row)
if expired >= cases_ordered_pending: if expired >= cases_shipped_pending:
claim.cases_expired = (claim.cases_expired or 0) + cases_ordered_pending claim.cases_expired = (claim.cases_expired or 0) + cases_shipped_pending
child_row.cases_expired = (child_row.units_expired or 0) + cases_ordered_pending child_row.cases_expired = (child_row.units_expired or 0) + cases_shipped_pending
units_expired_pending -= (cases_ordered_pending * case_quantity) units_expired_pending -= (cases_shipped_pending * case_quantity)
cases_ordered_pending = 0 cases_shipped_pending = 0
else: # ordered > expired else: # shipped > expired
claim.cases_expired = (claim.cases_expired or 0) + expired claim.cases_expired = (claim.cases_expired or 0) + expired
child_row.cases_expired = (child_row.units_expired or 0) + expired child_row.cases_expired = (child_row.units_expired or 0) + expired
cases_ordered_pending -= expired cases_shipped_pending -= expired
units_expired_pending -= (expired * case_quantity) units_expired_pending -= (expired * case_quantity)
self.refresh_row(child_row) self.refresh_row(child_row)
@ -524,61 +524,61 @@ class PurchaseBatchHandler(BatchHandler):
# only if the case quantity matches between child and parent rows. # only if the case quantity matches between child and parent rows.
# (otherwise who knows what could go wrong.) # (otherwise who knows what could go wrong.)
if case_quantity == truck_dump_row.case_quantity: if case_quantity == truck_dump_row.case_quantity:
if units_ordered_pending and cases_received_pending: if units_shipped_pending and cases_received_pending:
received = cases_received_pending * case_quantity received = cases_received_pending * case_quantity
claim = claim or make_claim(child_row) claim = claim or make_claim(child_row)
if received >= units_ordered_pending: if received >= units_shipped_pending:
claim.units_received = (claim.units_received or 0) + units_ordered_pending claim.units_received = (claim.units_received or 0) + units_shipped_pending
child_row.units_received = (child_row.units_received or 0) + units_ordered_pending child_row.units_received = (child_row.units_received or 0) + units_shipped_pending
leftover = received % units_ordered_pending leftover = received % units_shipped_pending
if leftover == 0: if leftover == 0:
cases_received_pending -= (received // units_ordered_pending) cases_received_pending -= (received // units_shipped_pending)
else: else:
cases_received_pending -= (received // units_ordered_pending) - 1 cases_received_pending -= (received // units_shipped_pending) - 1
units_received_pending += leftover units_received_pending += leftover
units_ordered_pending = 0 units_shipped_pending = 0
else: # ordered > received else: # shipped > received
claim.units_received = (claim.units_received or 0) + received claim.units_received = (claim.units_received or 0) + received
child_row.units_received = (child_row.units_received or 0) + received child_row.units_received = (child_row.units_received or 0) + received
units_ordered_pending -= received units_shipped_pending -= received
cases_received_pending = 0 cases_received_pending = 0
self.refresh_row(child_row) self.refresh_row(child_row)
if units_ordered_pending and cases_damaged_pending: if units_shipped_pending and cases_damaged_pending:
damaged = cases_damaged_pending * case_quantity damaged = cases_damaged_pending * case_quantity
claim = claim or make_claim(child_row) claim = claim or make_claim(child_row)
if damaged >= units_ordered_pending: if damaged >= units_shipped_pending:
claim.units_damaged = (claim.units_damaged or 0) + units_ordered_pending claim.units_damaged = (claim.units_damaged or 0) + units_shipped_pending
child_row.units_damaged = (child_row.units_damaged or 0) + units_ordered_pending child_row.units_damaged = (child_row.units_damaged or 0) + units_shipped_pending
leftover = damaged % units_ordered_pending leftover = damaged % units_shipped_pending
if leftover == 0: if leftover == 0:
cases_damaged_pending -= (damaged // units_ordered_pending) cases_damaged_pending -= (damaged // units_shipped_pending)
else: else:
cases_damaged_pending -= (damaged // units_ordered_pending) - 1 cases_damaged_pending -= (damaged // units_shipped_pending) - 1
units_damaged_pending += leftover units_damaged_pending += leftover
units_ordered_pending = 0 units_shipped_pending = 0
else: # ordered > damaged else: # shipped > damaged
claim.units_damaged = (claim.units_damaged or 0) + damaged claim.units_damaged = (claim.units_damaged or 0) + damaged
child_row.units_damaged = (child_row.units_damaged or 0) + damaged child_row.units_damaged = (child_row.units_damaged or 0) + damaged
units_ordered_pending -= damaged units_shipped_pending -= damaged
cases_damaged_pending = 0 cases_damaged_pending = 0
self.refresh_row(child_row) self.refresh_row(child_row)
if units_ordered_pending and cases_expired_pending: if units_shipped_pending and cases_expired_pending:
expired = cases_expired_pending * case_quantity expired = cases_expired_pending * case_quantity
claim = claim or make_claim(child_row) claim = claim or make_claim(child_row)
if expired >= units_ordered_pending: if expired >= units_shipped_pending:
claim.units_expired = (claim.units_expired or 0) + units_ordered_pending claim.units_expired = (claim.units_expired or 0) + units_shipped_pending
child_row.units_expired = (child_row.units_expired or 0) + units_ordered_pending child_row.units_expired = (child_row.units_expired or 0) + units_shipped_pending
leftover = expired % units_ordered_pending leftover = expired % units_shipped_pending
if leftover == 0: if leftover == 0:
cases_expired_pending -= (expired // units_ordered_pending) cases_expired_pending -= (expired // units_shipped_pending)
else: else:
cases_expired_pending -= (expired // units_ordered_pending) - 1 cases_expired_pending -= (expired // units_shipped_pending) - 1
units_expired_pending += leftover units_expired_pending += leftover
units_ordered_pending = 0 units_shipped_pending = 0
else: # ordered > expired else: # shipped > expired
claim.units_expired = (claim.units_expired or 0) + expired claim.units_expired = (claim.units_expired or 0) + expired
child_row.units_expired = (child_row.units_expired or 0) + expired child_row.units_expired = (child_row.units_expired or 0) + expired
units_ordered_pending -= expired units_shipped_pending -= expired
cases_expired_pending = 0 cases_expired_pending = 0
self.refresh_row(child_row) self.refresh_row(child_row)
@ -1392,11 +1392,11 @@ class PurchaseBatchHandler(BatchHandler):
# update child row # update child row
self.receiving_update_row_attrs(child_row, mode, cases, units) self.receiving_update_row_attrs(child_row, mode, cases, units)
if cases: if cases:
child_row.cases_ordered_claimed += cases child_row.cases_shipped_claimed += cases
child_row.cases_ordered_pending -= cases child_row.cases_shipped_pending -= cases
if units: if units:
child_row.units_ordered_claimed += units child_row.units_shipped_claimed += units
child_row.units_ordered_pending -= units child_row.units_shipped_pending -= units
# update child credit, if applicable # update child credit, if applicable
self.receiving_update_row_credits(child_row, mode, cases, units, **kwargs) self.receiving_update_row_credits(child_row, mode, cases, units, **kwargs)
@ -1418,28 +1418,28 @@ class PurchaseBatchHandler(BatchHandler):
# first we try to accommodate the full "as-is" amounts, if possible # first we try to accommodate the full "as-is" amounts, if possible
if positive: if positive:
if cases and units: if cases and units:
if child_row.cases_ordered_pending >= cases and child_row.units_ordered_pending >= units: if child_row.cases_shipped_pending >= cases and child_row.units_shipped_pending >= units:
update(cases, units) update(cases, units)
return 0, 0 return 0, 0
elif cases: elif cases:
if child_row.cases_ordered_pending >= cases: if child_row.cases_shipped_pending >= cases:
update(cases, 0) update(cases, 0)
return 0, 0 return 0, 0
else: # units else: # units
if child_row.units_ordered_pending >= units: if child_row.units_shipped_pending >= units:
update(0, units) update(0, units)
return 0, 0 return 0, 0
else: # negative else: # negative
if cases and units: if cases and units:
if child_row.cases_ordered_claimed >= -cases and child_row.units_ordered_claimed >= -units: if child_row.cases_shipped_claimed >= -cases and child_row.units_shipped_claimed >= -units:
update(cases, units) update(cases, units)
return 0, 0 return 0, 0
elif cases: elif cases:
if child_row.cases_ordered_claimed >= -cases: if child_row.cases_shipped_claimed >= -cases:
update(cases, 0) update(cases, 0)
return 0, 0 return 0, 0
else: # units else: # units
if child_row.units_ordered_claimed >= -units: if child_row.units_shipped_claimed >= -units:
update(0, units) update(0, units)
return 0, 0 return 0, 0
@ -1447,49 +1447,49 @@ class PurchaseBatchHandler(BatchHandler):
# much as possible, as simply as possible" # much as possible, as simply as possible"
if cases and units: if cases and units:
if positive: if positive:
if child_row.cases_ordered_pending >= cases: if child_row.cases_shipped_pending >= cases:
update(cases, 0) update(cases, 0)
return 0, units return 0, units
if child_row.units_ordered_pending >= units: if child_row.units_shipped_pending >= units:
update(0, units) update(0, units)
return cases, 0 return cases, 0
else: # negative else: # negative
if child_row.cases_ordered_claimed >= -cases: if child_row.cases_shipped_claimed >= -cases:
update(cases, 0) update(cases, 0)
return 0, units return 0, units
if child_row.units_ordered_claimed >= -units: if child_row.units_shipped_claimed >= -units:
update(0, units) update(0, units)
return cases, 0 return cases, 0
# okay then, try to (simply) use up any "child" quantities # okay then, try to (simply) use up any "child" quantities
if positive: if positive:
if cases and units and (child_row.cases_ordered_pending if cases and units and (child_row.cases_shipped_pending
and child_row.units_ordered_pending): and child_row.units_shipped_pending):
pending = (child_row.cases_ordered_pending, pending = (child_row.cases_shipped_pending,
child_row.units_ordered_pending) child_row.units_shipped_pending)
update(pending[0], pending[1]) update(pending[0], pending[1])
return cases - pending[0], units - pending[1] return cases - pending[0], units - pending[1]
if cases and child_row.cases_ordered_pending: if cases and child_row.cases_shipped_pending:
pending = child_row.cases_ordered_pending pending = child_row.cases_shipped_pending
update(pending, 0) update(pending, 0)
return cases - pending, 0 return cases - pending, 0
if units and child_row.units_ordered_pending: if units and child_row.units_shipped_pending:
pending = child_row.units_ordered_pending pending = child_row.units_shipped_pending
update(0, pending) update(0, pending)
return 0, units - pending return 0, units - pending
else: # negative else: # negative
if cases and units and (child_row.cases_ordered_claimed if cases and units and (child_row.cases_shipped_claimed
and child_row.units_ordered_claimed): and child_row.units_shipped_claimed):
claimed = (child_row.cases_ordered_claimed, claimed = (child_row.cases_shipped_claimed,
child_row.units_ordered_claimed) child_row.units_shipped_claimed)
update(-claimed[0], -claimed[1]) update(-claimed[0], -claimed[1])
return cases + claimed[0], units + claimed[1] return cases + claimed[0], units + claimed[1]
if cases and child_row.cases_ordered_claimed: if cases and child_row.cases_shipped_claimed:
claimed = child_row.cases_ordered_claimed claimed = child_row.cases_shipped_claimed
update(-claimed, 0) update(-claimed, 0)
return cases + claimed, 0 return cases + claimed, 0
if units and child_row.units_ordered_claimed: if units and child_row.units_shipped_claimed:
claimed = child_row.units_ordered_claimed claimed = child_row.units_shipped_claimed
update(0, -claimed) update(0, -claimed)
return 0, units + claimed return 0, units + claimed
@ -1497,9 +1497,9 @@ class PurchaseBatchHandler(BatchHandler):
if parent_row.case_quantity != child_row.case_quantity: if parent_row.case_quantity != child_row.case_quantity:
raise NotImplementedError("cannot split case when parent/child disagree about size") raise NotImplementedError("cannot split case when parent/child disagree about size")
if positive: if positive:
if cases and child_row.units_ordered_pending: if cases and child_row.units_shipped_pending:
if child_row.units_ordered_pending >= parent_row.case_quantity: if child_row.units_shipped_pending >= parent_row.case_quantity:
unit_cases = child_row.units_ordered_pending // parent_row.case_quantity unit_cases = child_row.units_shipped_pending // parent_row.case_quantity
if unit_cases >= cases: if unit_cases >= cases:
update(0, cases * parent_row.case_quantity) update(0, cases * parent_row.case_quantity)
return 0, units return 0, units
@ -1507,27 +1507,27 @@ class PurchaseBatchHandler(BatchHandler):
update(0, unit_cases * parent_row.case_quantity) update(0, unit_cases * parent_row.case_quantity)
return cases - unit_cases, units return cases - unit_cases, units
else: # units_pending < case_size else: # units_pending < case_size
pending = child_row.units_ordered_pending pending = child_row.units_shipped_pending
update(0, pending) update(0, pending)
return (cases - 1, return (cases - 1,
(units or 0) + parent_row.case_quantity - pending) (units or 0) + parent_row.case_quantity - pending)
if units and child_row.cases_ordered_pending: if units and child_row.cases_shipped_pending:
if units >= parent_row.case_quantity: if units >= parent_row.case_quantity:
unit_cases = units // parent_row.case_quantity unit_cases = units // parent_row.case_quantity
if unit_cases <= child_row.cases_ordered_pending: if unit_cases <= child_row.cases_shipped_pending:
update(unit_cases, 0) update(unit_cases, 0)
return 0, units - (unit_cases * parent_row.case_quantity) return 0, units - (unit_cases * parent_row.case_quantity)
else: # unit_cases > cases_pending else: # unit_cases > cases_pending
pending = child_row.cases_ordered_pending pending = child_row.cases_shipped_pending
update(pending, 0) update(pending, 0)
return 0, units - (pending * parent_row.case_quantity) return 0, units - (pending * parent_row.case_quantity)
else: # units < case_size else: # units < case_size
update(0, units) update(0, units)
return 0, 0 return 0, 0
else: # negative else: # negative
if cases and child_row.units_ordered_claimed: if cases and child_row.units_shipped_claimed:
if child_row.units_ordered_claimed >= parent_row.case_quantity: if child_row.units_shipped_claimed >= parent_row.case_quantity:
unit_cases = child_row.units_ordered_claimed // parent_row.case_quantity unit_cases = child_row.units_shipped_claimed // parent_row.case_quantity
if unit_cases >= -cases: if unit_cases >= -cases:
update(0, cases * parent_row.case_quantity) update(0, cases * parent_row.case_quantity)
return 0, units return 0, units
@ -1535,18 +1535,18 @@ class PurchaseBatchHandler(BatchHandler):
update(0, -unit_cases * parent_row.case_quantity) update(0, -unit_cases * parent_row.case_quantity)
return cases + unit_cases, units return cases + unit_cases, units
else: # units_claimed < case_size else: # units_claimed < case_size
claimed = child_row.units_ordered_claimed claimed = child_row.units_shipped_claimed
update(0, -claimed) update(0, -claimed)
return (cases + 1, return (cases + 1,
(units or 0) - parent_row.case_quantity + claimed) (units or 0) - parent_row.case_quantity + claimed)
if units and child_row.cases_ordered_claimed: if units and child_row.cases_shipped_claimed:
if -units >= parent_row.case_quantity: if -units >= parent_row.case_quantity:
unit_cases = -units // parent_row.case_quantity unit_cases = -units // parent_row.case_quantity
if unit_cases <= child_row.cases_ordered_claimed: if unit_cases <= child_row.cases_shipped_claimed:
update(-unit_cases, 0) update(-unit_cases, 0)
return 0, units + (unit_cases * parent_row.case_quantity) return 0, units + (unit_cases * parent_row.case_quantity)
else: # unit_cases > cases_claimed else: # unit_cases > cases_claimed
claimed = child_row.cases_ordered_claimed claimed = child_row.cases_shipped_claimed
update(-claimed, 0) update(-claimed, 0)
return 0, units + (claimed * parent_row.case_quantity) return 0, units + (claimed * parent_row.case_quantity)
else: # -units < case_size else: # -units < case_size
@ -1604,39 +1604,39 @@ class PurchaseBatchHandler(BatchHandler):
# for each child row we also calculate "claimed" vs. "pending" amounts # for each child row we also calculate "claimed" vs. "pending" amounts
# cases_ordered # cases_ordered
child_row.cases_ordered_claimed = sum([(claim.cases_received or 0) child_row.cases_shipped_claimed = sum([(claim.cases_received or 0)
+ (claim.cases_damaged or 0) + (claim.cases_damaged or 0)
+ (claim.cases_expired or 0) + (claim.cases_expired or 0)
for claim in child_row.truck_dump_claims]) for claim in child_row.truck_dump_claims])
child_row.cases_ordered_pending = (child_row.cases_ordered or 0) - child_row.cases_ordered_claimed child_row.cases_shipped_pending = (child_row.cases_ordered or 0) - child_row.cases_shipped_claimed
# units_ordered # units_ordered
child_row.units_ordered_claimed = sum([(claim.units_received or 0) child_row.units_shipped_claimed = sum([(claim.units_received or 0)
+ (claim.units_damaged or 0) + (claim.units_damaged or 0)
+ (claim.units_expired or 0) + (claim.units_expired or 0)
for claim in child_row.truck_dump_claims]) for claim in child_row.truck_dump_claims])
child_row.units_ordered_pending = (child_row.units_ordered or 0) - child_row.units_ordered_claimed child_row.units_shipped_pending = (child_row.units_ordered or 0) - child_row.units_shipped_claimed
# maybe account for split cases # maybe account for split cases
if child_row.units_ordered_pending < 0: if child_row.units_shipped_pending < 0:
split_cases = -child_row.units_ordered_pending // child_row.case_quantity split_cases = -child_row.units_shipped_pending // child_row.case_quantity
if -child_row.units_ordered_pending % child_row.case_quantity: if -child_row.units_shipped_pending % child_row.case_quantity:
split_cases += 1 split_cases += 1
if split_cases > child_row.cases_ordered_pending: if split_cases > child_row.cases_shipped_pending:
raise ValueError("too many cases have been split?") raise ValueError("too many cases have been split?")
child_row.cases_ordered_pending -= split_cases child_row.cases_shipped_pending -= split_cases
child_row.units_ordered_pending += split_cases * child_row.case_quantity child_row.units_shipped_pending += split_cases * child_row.case_quantity
all_child_rows.append(child_row) all_child_rows.append(child_row)
def sortkey(row): def sortkey(row):
if positive: if positive:
return self.get_units(row.cases_ordered_pending, return self.get_units(row.cases_shipped_pending,
row.units_ordered_pending, row.units_shipped_pending,
row.case_quantity) row.case_quantity)
else: # negative else: # negative
return self.get_units(row.cases_ordered_claimed, return self.get_units(row.cases_shipped_claimed,
row.units_ordered_claimed, row.units_shipped_claimed,
row.case_quantity) row.case_quantity)
# sort child rows such that smallest (relevant) quantities come first; # sort child rows such that smallest (relevant) quantities come first;
@ -1647,24 +1647,24 @@ class PurchaseBatchHandler(BatchHandler):
for child_row in all_child_rows: for child_row in all_child_rows:
if cases and units: if cases and units:
if positive: if positive:
if child_row.cases_ordered_pending == cases and child_row.units_ordered_pending == units: if child_row.cases_shipped_pending == cases and child_row.units_shipped_pending == units:
return child_row return child_row
else: # negative else: # negative
if child_row.cases_ordered_claimed == cases and child_row.units_ordered_claimed == units: if child_row.cases_shipped_claimed == cases and child_row.units_shipped_claimed == units:
return child_row return child_row
elif cases: elif cases:
if positive: if positive:
if child_row.cases_ordered_pending == cases: if child_row.cases_shipped_pending == cases:
return child_row return child_row
else: # negative else: # negative
if child_row.cases_ordered_claimed == cases: if child_row.cases_shipped_claimed == cases:
return child_row return child_row
else: # units else: # units
if positive: if positive:
if child_row.units_ordered_pending == units: if child_row.units_shipped_pending == units:
return child_row return child_row
else: # negative else: # negative
if child_row.units_ordered_claimed == units: if child_row.units_shipped_claimed == units:
return child_row return child_row
# next we try to find the "first" (smallest) match which satisfies, but # next we try to find the "first" (smallest) match which satisfies, but
@ -1672,24 +1672,24 @@ class PurchaseBatchHandler(BatchHandler):
for child_row in all_child_rows: for child_row in all_child_rows:
if cases and units: if cases and units:
if positive: if positive:
if child_row.cases_ordered_pending >= cases and child_row.units_ordered_pending >= units: if child_row.cases_shipped_pending >= cases and child_row.units_shipped_pending >= units:
return child_row return child_row
else: # negative else: # negative
if child_row.cases_ordered_claimed >= -cases and child_row.units_ordered_claimed >= -units: if child_row.cases_shipped_claimed >= -cases and child_row.units_shipped_claimed >= -units:
return child_row return child_row
elif cases: elif cases:
if positive: if positive:
if child_row.cases_ordered_pending >= cases: if child_row.cases_shipped_pending >= cases:
return child_row return child_row
else: # negative else: # negative
if child_row.cases_ordered_claimed >= -cases: if child_row.cases_shipped_claimed >= -cases:
return child_row return child_row
else: # units else: # units
if positive: if positive:
if child_row.units_ordered_pending >= units: if child_row.units_shipped_pending >= units:
return child_row return child_row
else: # negative else: # negative
if child_row.units_ordered_claimed >= -units: if child_row.units_shipped_claimed >= -units:
return child_row return child_row
# okay, we're getting desperate now; let's start splitting cases and # okay, we're getting desperate now; let's start splitting cases and
@ -1697,14 +1697,14 @@ class PurchaseBatchHandler(BatchHandler):
incoming_units = self.get_units(cases, units, parent_row.case_quantity) incoming_units = self.get_units(cases, units, parent_row.case_quantity)
for child_row in all_child_rows: for child_row in all_child_rows:
if positive: if positive:
pending_units = self.get_units(child_row.cases_ordered_pending, pending_units = self.get_units(child_row.cases_shipped_pending,
child_row.units_ordered_pending, child_row.units_shipped_pending,
child_row.case_quantity) child_row.case_quantity)
if pending_units >= incoming_units: if pending_units >= incoming_units:
return child_row return child_row
else: # negative else: # negative
claimed_units = self.get_units(child_row.cases_ordered_claimed, claimed_units = self.get_units(child_row.cases_shipped_claimed,
child_row.units_ordered_claimed, child_row.units_shipped_claimed,
child_row.case_quantity) child_row.case_quantity)
if claimed_units >= -incoming_units: if claimed_units >= -incoming_units:
return child_row return child_row
@ -1716,10 +1716,10 @@ class PurchaseBatchHandler(BatchHandler):
# list *backwards* here, hoping for the "biggest" match # list *backwards* here, hoping for the "biggest" match
for child_row in reversed(all_child_rows): for child_row in reversed(all_child_rows):
if positive: if positive:
if child_row.cases_ordered_pending or child_row.units_ordered_pending: if child_row.cases_shipped_pending or child_row.units_shipped_pending:
return child_row return child_row
else: # negative else: # negative
if child_row.cases_ordered_claimed or child_row.units_ordered_claimed: if child_row.cases_shipped_claimed or child_row.units_shipped_claimed:
return child_row return child_row
def remove_row(self, row): def remove_row(self, row):