2022-01-15 07:06:56 +00:00
|
|
|
from abc import ABC, abstractmethod, abstractproperty
|
2021-12-18 13:10:22 +00:00
|
|
|
from typing import Callable, List, NewType, Optional, Tuple
|
|
|
|
|
|
|
|
from frappe.utils import flt
|
|
|
|
|
2022-01-15 07:22:10 +00:00
|
|
|
StockBin = NewType("StockBin", List[float]) # [[qty, rate], ...]
|
2021-12-18 13:10:22 +00:00
|
|
|
|
|
|
|
# Indexes of values inside FIFO bin 2-tuple
|
|
|
|
QTY = 0
|
|
|
|
RATE = 1
|
|
|
|
|
|
|
|
|
2022-01-15 07:06:56 +00:00
|
|
|
class BinWiseValuation(ABC):
|
|
|
|
@abstractmethod
|
|
|
|
def add_stock(self, qty: float, rate: float) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def remove_stock(
|
|
|
|
self, qty: float, outgoing_rate: float = 0.0, rate_generator: Callable[[], float] = None
|
|
|
|
) -> List[StockBin]:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractproperty
|
|
|
|
def state(self) -> List[StockBin]:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_total_stock_and_value(self) -> Tuple[float, float]:
|
|
|
|
total_qty = 0.0
|
|
|
|
total_value = 0.0
|
|
|
|
|
|
|
|
for qty, rate in self.state:
|
|
|
|
total_qty += flt(qty)
|
|
|
|
total_value += flt(qty) * flt(rate)
|
|
|
|
|
2022-02-19 15:28:36 +00:00
|
|
|
return round_off_if_near_zero(total_qty), round_off_if_near_zero(total_value)
|
2022-01-15 07:06:56 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self.state)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.state)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, list):
|
|
|
|
return self.state == other
|
|
|
|
return type(self) == type(other) and self.state == other.state
|
|
|
|
|
|
|
|
|
|
|
|
class FIFOValuation(BinWiseValuation):
|
2021-12-18 13:10:22 +00:00
|
|
|
"""Valuation method where a queue of all the incoming stock is maintained.
|
|
|
|
|
|
|
|
New stock is added at end of the queue.
|
|
|
|
Qty consumption happens on First In First Out basis.
|
|
|
|
|
|
|
|
Queue is implemented using "bins" of [qty, rate].
|
|
|
|
|
|
|
|
ref: https://en.wikipedia.org/wiki/FIFO_and_LIFO_accounting
|
|
|
|
"""
|
|
|
|
|
2021-12-19 13:38:09 +00:00
|
|
|
# specifying the attributes to save resources
|
|
|
|
# ref: https://docs.python.org/3/reference/datamodel.html#slots
|
2022-04-04 09:52:15 +00:00
|
|
|
__slots__ = ["queue"]
|
2021-12-19 13:38:09 +00:00
|
|
|
|
2022-01-15 07:06:56 +00:00
|
|
|
def __init__(self, state: Optional[List[StockBin]]):
|
|
|
|
self.queue: List[StockBin] = state if state is not None else []
|
2021-12-18 14:03:58 +00:00
|
|
|
|
2022-01-15 07:06:56 +00:00
|
|
|
@property
|
|
|
|
def state(self) -> List[StockBin]:
|
2021-12-18 13:10:22 +00:00
|
|
|
"""Get current state of queue."""
|
|
|
|
return self.queue
|
|
|
|
|
2021-12-19 13:07:12 +00:00
|
|
|
def add_stock(self, qty: float, rate: float) -> None:
|
|
|
|
"""Update fifo queue with new stock.
|
2021-12-18 13:10:22 +00:00
|
|
|
|
|
|
|
args:
|
|
|
|
qty: new quantity to add
|
|
|
|
rate: incoming rate of new quantity"""
|
|
|
|
|
|
|
|
if not len(self.queue):
|
|
|
|
self.queue.append([0, 0])
|
|
|
|
|
|
|
|
# last row has the same rate, merge new bin.
|
|
|
|
if self.queue[-1][RATE] == rate:
|
|
|
|
self.queue[-1][QTY] += qty
|
|
|
|
else:
|
|
|
|
# Item has a positive balance qty, add new entry
|
|
|
|
if self.queue[-1][QTY] > 0:
|
|
|
|
self.queue.append([qty, rate])
|
|
|
|
else: # negative balance qty
|
|
|
|
qty = self.queue[-1][QTY] + qty
|
|
|
|
if qty > 0: # new balance qty is positive
|
|
|
|
self.queue[-1] = [qty, rate]
|
|
|
|
else: # new balance qty is still negative, maintain same rate
|
|
|
|
self.queue[-1][QTY] = qty
|
|
|
|
|
|
|
|
def remove_stock(
|
2021-12-19 13:15:04 +00:00
|
|
|
self, qty: float, outgoing_rate: float = 0.0, rate_generator: Callable[[], float] = None
|
2022-01-15 07:06:56 +00:00
|
|
|
) -> List[StockBin]:
|
2021-12-19 13:07:12 +00:00
|
|
|
"""Remove stock from the queue and return popped bins.
|
2021-12-18 13:10:22 +00:00
|
|
|
|
|
|
|
args:
|
|
|
|
qty: quantity to remove
|
|
|
|
rate: outgoing rate
|
|
|
|
rate_generator: function to be called if queue is not found and rate is required.
|
|
|
|
"""
|
2021-12-19 13:15:04 +00:00
|
|
|
if not rate_generator:
|
|
|
|
rate_generator = lambda: 0.0 # noqa
|
2021-12-18 13:10:22 +00:00
|
|
|
|
2021-12-19 13:07:12 +00:00
|
|
|
consumed_bins = []
|
2021-12-18 13:10:22 +00:00
|
|
|
while qty:
|
|
|
|
if not len(self.queue):
|
|
|
|
# rely on rate generator.
|
|
|
|
self.queue.append([0, rate_generator()])
|
|
|
|
|
|
|
|
index = None
|
2021-12-19 13:15:04 +00:00
|
|
|
if outgoing_rate > 0:
|
2021-12-18 13:10:22 +00:00
|
|
|
# Find the entry where rate matched with outgoing rate
|
|
|
|
for idx, fifo_bin in enumerate(self.queue):
|
2021-12-19 13:15:04 +00:00
|
|
|
if fifo_bin[RATE] == outgoing_rate:
|
2021-12-18 13:10:22 +00:00
|
|
|
index = idx
|
|
|
|
break
|
|
|
|
|
2022-04-04 09:52:15 +00:00
|
|
|
# If no entry found with outgoing rate, consume as per FIFO
|
2021-12-18 13:10:22 +00:00
|
|
|
if index is None: # nosemgrep
|
2022-04-04 09:52:15 +00:00
|
|
|
index = 0
|
2021-12-18 13:10:22 +00:00
|
|
|
else:
|
|
|
|
index = 0
|
|
|
|
|
|
|
|
# select first bin or the bin with same rate
|
|
|
|
fifo_bin = self.queue[index]
|
|
|
|
if qty >= fifo_bin[QTY]:
|
|
|
|
# consume current bin
|
2022-02-19 15:28:36 +00:00
|
|
|
qty = round_off_if_near_zero(qty - fifo_bin[QTY])
|
2021-12-19 13:07:12 +00:00
|
|
|
to_consume = self.queue.pop(index)
|
|
|
|
consumed_bins.append(list(to_consume))
|
|
|
|
|
2021-12-18 13:10:22 +00:00
|
|
|
if not self.queue and qty:
|
|
|
|
# stock finished, qty still remains to be withdrawn
|
|
|
|
# negative stock, keep in as a negative bin
|
2021-12-19 13:15:04 +00:00
|
|
|
self.queue.append([-qty, outgoing_rate or fifo_bin[RATE]])
|
|
|
|
consumed_bins.append([qty, outgoing_rate or fifo_bin[RATE]])
|
2021-12-18 13:10:22 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
# qty found in current bin consume it and exit
|
2022-02-19 15:28:36 +00:00
|
|
|
fifo_bin[QTY] = round_off_if_near_zero(fifo_bin[QTY] - qty)
|
2021-12-19 13:07:12 +00:00
|
|
|
consumed_bins.append([qty, fifo_bin[RATE]])
|
2021-12-18 13:10:22 +00:00
|
|
|
qty = 0
|
|
|
|
|
2021-12-19 13:07:12 +00:00
|
|
|
return consumed_bins
|
2021-12-18 13:10:22 +00:00
|
|
|
|
|
|
|
|
2022-01-15 07:06:56 +00:00
|
|
|
class LIFOValuation(BinWiseValuation):
|
|
|
|
"""Valuation method where a *stack* of all the incoming stock is maintained.
|
|
|
|
|
|
|
|
New stock is added at top of the stack.
|
|
|
|
Qty consumption happens on Last In First Out basis.
|
|
|
|
|
|
|
|
Stack is implemented using "bins" of [qty, rate].
|
|
|
|
|
|
|
|
ref: https://en.wikipedia.org/wiki/FIFO_and_LIFO_accounting
|
2022-01-15 07:22:10 +00:00
|
|
|
Implementation detail: appends and pops both at end of list.
|
2022-01-15 07:06:56 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# specifying the attributes to save resources
|
|
|
|
# ref: https://docs.python.org/3/reference/datamodel.html#slots
|
2022-04-04 09:52:15 +00:00
|
|
|
__slots__ = ["stack"]
|
2022-01-15 07:06:56 +00:00
|
|
|
|
|
|
|
def __init__(self, state: Optional[List[StockBin]]):
|
|
|
|
self.stack: List[StockBin] = state if state is not None else []
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self) -> List[StockBin]:
|
|
|
|
"""Get current state of stack."""
|
|
|
|
return self.stack
|
|
|
|
|
|
|
|
def add_stock(self, qty: float, rate: float) -> None:
|
|
|
|
"""Update lifo stack with new stock.
|
|
|
|
|
|
|
|
args:
|
|
|
|
qty: new quantity to add
|
2022-01-15 07:22:10 +00:00
|
|
|
rate: incoming rate of new quantity.
|
|
|
|
|
|
|
|
Behaviour of this is same as FIFO valuation.
|
|
|
|
"""
|
|
|
|
if not len(self.stack):
|
|
|
|
self.stack.append([0, 0])
|
|
|
|
|
|
|
|
# last row has the same rate, merge new bin.
|
|
|
|
if self.stack[-1][RATE] == rate:
|
|
|
|
self.stack[-1][QTY] += qty
|
|
|
|
else:
|
|
|
|
# Item has a positive balance qty, add new entry
|
|
|
|
if self.stack[-1][QTY] > 0:
|
|
|
|
self.stack.append([qty, rate])
|
|
|
|
else: # negative balance qty
|
|
|
|
qty = self.stack[-1][QTY] + qty
|
|
|
|
if qty > 0: # new balance qty is positive
|
|
|
|
self.stack[-1] = [qty, rate]
|
|
|
|
else: # new balance qty is still negative, maintain same rate
|
|
|
|
self.stack[-1][QTY] = qty
|
2022-01-15 07:06:56 +00:00
|
|
|
|
|
|
|
def remove_stock(
|
|
|
|
self, qty: float, outgoing_rate: float = 0.0, rate_generator: Callable[[], float] = None
|
|
|
|
) -> List[StockBin]:
|
|
|
|
"""Remove stock from the stack and return popped bins.
|
|
|
|
|
|
|
|
args:
|
|
|
|
qty: quantity to remove
|
2022-01-15 07:22:10 +00:00
|
|
|
rate: outgoing rate - ignored. Kept for backwards compatibility.
|
2022-01-15 07:06:56 +00:00
|
|
|
rate_generator: function to be called if stack is not found and rate is required.
|
|
|
|
"""
|
2022-01-15 07:22:10 +00:00
|
|
|
if not rate_generator:
|
|
|
|
rate_generator = lambda: 0.0 # noqa
|
|
|
|
|
|
|
|
consumed_bins = []
|
|
|
|
while qty:
|
|
|
|
if not len(self.stack):
|
|
|
|
# rely on rate generator.
|
|
|
|
self.stack.append([0, rate_generator()])
|
|
|
|
|
|
|
|
# start at the end.
|
|
|
|
index = -1
|
|
|
|
|
|
|
|
stock_bin = self.stack[index]
|
|
|
|
if qty >= stock_bin[QTY]:
|
|
|
|
# consume current bin
|
2022-02-19 15:28:36 +00:00
|
|
|
qty = round_off_if_near_zero(qty - stock_bin[QTY])
|
2022-01-15 07:22:10 +00:00
|
|
|
to_consume = self.stack.pop(index)
|
|
|
|
consumed_bins.append(list(to_consume))
|
|
|
|
|
|
|
|
if not self.stack and qty:
|
|
|
|
# stock finished, qty still remains to be withdrawn
|
|
|
|
# negative stock, keep in as a negative bin
|
|
|
|
self.stack.append([-qty, outgoing_rate or stock_bin[RATE]])
|
|
|
|
consumed_bins.append([qty, outgoing_rate or stock_bin[RATE]])
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# qty found in current bin consume it and exit
|
2022-02-19 15:28:36 +00:00
|
|
|
stock_bin[QTY] = round_off_if_near_zero(stock_bin[QTY] - qty)
|
2022-01-15 07:22:10 +00:00
|
|
|
consumed_bins.append([qty, stock_bin[RATE]])
|
|
|
|
qty = 0
|
|
|
|
|
|
|
|
return consumed_bins
|
2022-01-15 07:06:56 +00:00
|
|
|
|
|
|
|
|
2022-02-19 15:28:36 +00:00
|
|
|
def round_off_if_near_zero(number: float, precision: int = 7) -> float:
|
2021-12-18 13:10:22 +00:00
|
|
|
"""Rounds off the number to zero only if number is close to zero for decimal
|
2021-12-18 14:07:41 +00:00
|
|
|
specified in precision. Precision defaults to 7.
|
2021-12-18 13:10:22 +00:00
|
|
|
"""
|
2021-12-18 15:06:47 +00:00
|
|
|
if abs(0.0 - flt(number)) < (1.0 / (10**precision)):
|
|
|
|
return 0.0
|
2021-12-18 13:10:22 +00:00
|
|
|
|
|
|
|
return flt(number)
|