refactor: outgoing rate and defaults for remove_stock
This commit is contained in:
parent
db1c0889d3
commit
a00d8d02b2
@ -714,7 +714,7 @@ class update_entries_after(object):
|
|||||||
else:
|
else:
|
||||||
return 0.0
|
return 0.0
|
||||||
|
|
||||||
fifo_queue.remove_stock(qty=abs(actual_qty), rate=outgoing_rate, rate_generator=rate_generator)
|
fifo_queue.remove_stock(qty=abs(actual_qty), outgoing_rate=outgoing_rate, rate_generator=rate_generator)
|
||||||
|
|
||||||
stock_qty, stock_value = fifo_queue.get_total_stock_and_value()
|
stock_qty, stock_value = fifo_queue.get_total_stock_and_value()
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ class FifoValuation:
|
|||||||
self.queue[-1][QTY] = qty
|
self.queue[-1][QTY] = qty
|
||||||
|
|
||||||
def remove_stock(
|
def remove_stock(
|
||||||
self, qty: float, rate: float, rate_generator: Callable[[], float]
|
self, qty: float, outgoing_rate: float = 0.0, rate_generator: Callable[[], float] = None
|
||||||
) -> List[FifoBin]:
|
) -> List[FifoBin]:
|
||||||
"""Remove stock from the queue and return popped bins.
|
"""Remove stock from the queue and return popped bins.
|
||||||
|
|
||||||
@ -82,6 +82,8 @@ class FifoValuation:
|
|||||||
rate: outgoing rate
|
rate: outgoing rate
|
||||||
rate_generator: function to be called if queue is not found and rate is required.
|
rate_generator: function to be called if queue is not found and rate is required.
|
||||||
"""
|
"""
|
||||||
|
if not rate_generator:
|
||||||
|
rate_generator = lambda : 0.0 # noqa
|
||||||
|
|
||||||
consumed_bins = []
|
consumed_bins = []
|
||||||
while qty:
|
while qty:
|
||||||
@ -90,18 +92,18 @@ class FifoValuation:
|
|||||||
self.queue.append([0, rate_generator()])
|
self.queue.append([0, rate_generator()])
|
||||||
|
|
||||||
index = None
|
index = None
|
||||||
if rate > 0:
|
if outgoing_rate > 0:
|
||||||
# Find the entry where rate matched with outgoing rate
|
# Find the entry where rate matched with outgoing rate
|
||||||
for idx, fifo_bin in enumerate(self.queue):
|
for idx, fifo_bin in enumerate(self.queue):
|
||||||
if fifo_bin[RATE] == rate:
|
if fifo_bin[RATE] == outgoing_rate:
|
||||||
index = idx
|
index = idx
|
||||||
break
|
break
|
||||||
|
|
||||||
# If no entry found with outgoing rate, collapse stack
|
# If no entry found with outgoing rate, collapse stack
|
||||||
if index is None: # nosemgrep
|
if index is None: # nosemgrep
|
||||||
new_stock_value = sum(d[QTY] * d[RATE] for d in self.queue) - qty * rate
|
new_stock_value = sum(d[QTY] * d[RATE] for d in self.queue) - qty * outgoing_rate
|
||||||
new_stock_qty = sum(d[QTY] for d in self.queue) - qty
|
new_stock_qty = sum(d[QTY] for d in self.queue) - qty
|
||||||
self.queue = [[new_stock_qty, new_stock_value / new_stock_qty if new_stock_qty > 0 else rate]]
|
self.queue = [[new_stock_qty, new_stock_value / new_stock_qty if new_stock_qty > 0 else outgoing_rate]]
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
index = 0
|
index = 0
|
||||||
@ -117,8 +119,8 @@ class FifoValuation:
|
|||||||
if not self.queue and qty:
|
if not self.queue and qty:
|
||||||
# stock finished, qty still remains to be withdrawn
|
# stock finished, qty still remains to be withdrawn
|
||||||
# negative stock, keep in as a negative bin
|
# negative stock, keep in as a negative bin
|
||||||
self.queue.append([-qty, rate or fifo_bin[RATE]])
|
self.queue.append([-qty, outgoing_rate or fifo_bin[RATE]])
|
||||||
consumed_bins.append([qty, rate or fifo_bin[RATE]])
|
consumed_bins.append([qty, outgoing_rate or fifo_bin[RATE]])
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
# qty found in current bin consume it and exit
|
# qty found in current bin consume it and exit
|
||||||
|
Loading…
x
Reference in New Issue
Block a user