From a00d8d02b22e71e5955fb118ad97d163e97efa41 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Sun, 19 Dec 2021 18:45:04 +0530 Subject: [PATCH] refactor: outgoing rate and defaults for remove_stock --- erpnext/stock/stock_ledger.py | 2 +- erpnext/stock/valuation.py | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py index 056e4a768d..498b9f51f6 100644 --- a/erpnext/stock/stock_ledger.py +++ b/erpnext/stock/stock_ledger.py @@ -714,7 +714,7 @@ class update_entries_after(object): else: 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() diff --git a/erpnext/stock/valuation.py b/erpnext/stock/valuation.py index 6e885ab518..86a4eb705e 100644 --- a/erpnext/stock/valuation.py +++ b/erpnext/stock/valuation.py @@ -73,7 +73,7 @@ class FifoValuation: self.queue[-1][QTY] = qty 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]: """Remove stock from the queue and return popped bins. @@ -82,6 +82,8 @@ class FifoValuation: rate: outgoing rate 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 = [] while qty: @@ -90,18 +92,18 @@ class FifoValuation: self.queue.append([0, rate_generator()]) index = None - if rate > 0: + if outgoing_rate > 0: # Find the entry where rate matched with outgoing rate for idx, fifo_bin in enumerate(self.queue): - if fifo_bin[RATE] == rate: + if fifo_bin[RATE] == outgoing_rate: index = idx break # If no entry found with outgoing rate, collapse stack 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 - 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 else: index = 0 @@ -117,8 +119,8 @@ class FifoValuation: if not self.queue and qty: # stock finished, qty still remains to be withdrawn # negative stock, keep in as a negative bin - self.queue.append([-qty, rate or fifo_bin[RATE]]) - consumed_bins.append([qty, rate or fifo_bin[RATE]]) + self.queue.append([-qty, outgoing_rate or fifo_bin[RATE]]) + consumed_bins.append([qty, outgoing_rate or fifo_bin[RATE]]) break else: # qty found in current bin consume it and exit