feat: get consumed bins from Fifo queue on pop

This commit is contained in:
Ankush Menat 2021-12-19 18:37:12 +05:30 committed by Ankush Menat
parent 1833f7ab81
commit db1c0889d3

View File

@ -48,8 +48,8 @@ class FifoValuation:
return _round_off_if_near_zero(total_qty), _round_off_if_near_zero(total_value) return _round_off_if_near_zero(total_qty), _round_off_if_near_zero(total_value)
def add_stock(self, qty: float, rate: float) -> List[FifoBin]: def add_stock(self, qty: float, rate: float) -> None:
"""Update fifo queue with new stock and return queue. """Update fifo queue with new stock.
args: args:
qty: new quantity to add qty: new quantity to add
@ -71,12 +71,11 @@ class FifoValuation:
self.queue[-1] = [qty, rate] self.queue[-1] = [qty, rate]
else: # new balance qty is still negative, maintain same rate else: # new balance qty is still negative, maintain same rate
self.queue[-1][QTY] = qty self.queue[-1][QTY] = qty
return self.get_state()
def remove_stock( def remove_stock(
self, qty: float, rate: float, rate_generator: Callable[[], float] self, qty: float, rate: float, rate_generator: Callable[[], float]
) -> List[FifoBin]: ) -> List[FifoBin]:
"""Remove stock from the queue and return queue. """Remove stock from the queue and return popped bins.
args: args:
qty: quantity to remove qty: quantity to remove
@ -84,6 +83,7 @@ class FifoValuation:
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.
""" """
consumed_bins = []
while qty: while qty:
if not len(self.queue): if not len(self.queue):
# rely on rate generator. # rely on rate generator.
@ -111,19 +111,22 @@ class FifoValuation:
if qty >= fifo_bin[QTY]: if qty >= fifo_bin[QTY]:
# consume current bin # consume current bin
qty = _round_off_if_near_zero(qty - fifo_bin[QTY]) qty = _round_off_if_near_zero(qty - fifo_bin[QTY])
self.queue.pop(index) to_consume = self.queue.pop(index)
consumed_bins.append(list(to_consume))
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, rate or fifo_bin[RATE]])
consumed_bins.append([qty, 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
fifo_bin[QTY] = _round_off_if_near_zero(fifo_bin[QTY] - qty) fifo_bin[QTY] = _round_off_if_near_zero(fifo_bin[QTY] - qty)
consumed_bins.append([qty, fifo_bin[RATE]])
qty = 0 qty = 0
return self.get_state() return consumed_bins
def _round_off_if_near_zero(number: float, precision: int = 7) -> float: def _round_off_if_near_zero(number: float, precision: int = 7) -> float: