Ankush Menat | b855030 | 2022-01-15 12:36:56 +0530 | [diff] [blame] | 1 | from abc import ABC, abstractmethod, abstractproperty |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 2 | from typing import Callable, List, NewType, Optional, Tuple |
| 3 | |
| 4 | from frappe.utils import flt |
| 5 | |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 6 | StockBin = NewType("StockBin", List[float]) # [[qty, rate], ...] |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 7 | |
| 8 | # Indexes of values inside FIFO bin 2-tuple |
| 9 | QTY = 0 |
| 10 | RATE = 1 |
| 11 | |
| 12 | |
Ankush Menat | b855030 | 2022-01-15 12:36:56 +0530 | [diff] [blame] | 13 | class BinWiseValuation(ABC): |
Ankush Menat | b855030 | 2022-01-15 12:36:56 +0530 | [diff] [blame] | 14 | @abstractmethod |
| 15 | def add_stock(self, qty: float, rate: float) -> None: |
| 16 | pass |
| 17 | |
| 18 | @abstractmethod |
| 19 | def remove_stock( |
| 20 | self, qty: float, outgoing_rate: float = 0.0, rate_generator: Callable[[], float] = None |
| 21 | ) -> List[StockBin]: |
| 22 | pass |
| 23 | |
| 24 | @abstractproperty |
| 25 | def state(self) -> List[StockBin]: |
| 26 | pass |
| 27 | |
| 28 | def get_total_stock_and_value(self) -> Tuple[float, float]: |
| 29 | total_qty = 0.0 |
| 30 | total_value = 0.0 |
| 31 | |
| 32 | for qty, rate in self.state: |
| 33 | total_qty += flt(qty) |
| 34 | total_value += flt(qty) * flt(rate) |
| 35 | |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 36 | return round_off_if_near_zero(total_qty), round_off_if_near_zero(total_value) |
Ankush Menat | b855030 | 2022-01-15 12:36:56 +0530 | [diff] [blame] | 37 | |
| 38 | def __repr__(self): |
| 39 | return str(self.state) |
| 40 | |
| 41 | def __iter__(self): |
| 42 | return iter(self.state) |
| 43 | |
| 44 | def __eq__(self, other): |
| 45 | if isinstance(other, list): |
| 46 | return self.state == other |
| 47 | return type(self) == type(other) and self.state == other.state |
| 48 | |
| 49 | |
| 50 | class FIFOValuation(BinWiseValuation): |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 51 | """Valuation method where a queue of all the incoming stock is maintained. |
| 52 | |
| 53 | New stock is added at end of the queue. |
| 54 | Qty consumption happens on First In First Out basis. |
| 55 | |
| 56 | Queue is implemented using "bins" of [qty, rate]. |
| 57 | |
| 58 | ref: https://en.wikipedia.org/wiki/FIFO_and_LIFO_accounting |
| 59 | """ |
| 60 | |
Ankush Menat | 745caf9 | 2021-12-19 19:08:09 +0530 | [diff] [blame] | 61 | # specifying the attributes to save resources |
| 62 | # ref: https://docs.python.org/3/reference/datamodel.html#slots |
Ankush Menat | 12c01e2 | 2022-04-04 15:22:15 +0530 | [diff] [blame] | 63 | __slots__ = ["queue"] |
Ankush Menat | 745caf9 | 2021-12-19 19:08:09 +0530 | [diff] [blame] | 64 | |
Ankush Menat | b855030 | 2022-01-15 12:36:56 +0530 | [diff] [blame] | 65 | def __init__(self, state: Optional[List[StockBin]]): |
| 66 | self.queue: List[StockBin] = state if state is not None else [] |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 67 | |
Ankush Menat | b855030 | 2022-01-15 12:36:56 +0530 | [diff] [blame] | 68 | @property |
| 69 | def state(self) -> List[StockBin]: |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 70 | """Get current state of queue.""" |
| 71 | return self.queue |
| 72 | |
Ankush Menat | db1c088 | 2021-12-19 18:37:12 +0530 | [diff] [blame] | 73 | def add_stock(self, qty: float, rate: float) -> None: |
| 74 | """Update fifo queue with new stock. |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 75 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 76 | args: |
| 77 | qty: new quantity to add |
| 78 | rate: incoming rate of new quantity""" |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 79 | |
| 80 | if not len(self.queue): |
| 81 | self.queue.append([0, 0]) |
| 82 | |
| 83 | # last row has the same rate, merge new bin. |
| 84 | if self.queue[-1][RATE] == rate: |
| 85 | self.queue[-1][QTY] += qty |
| 86 | else: |
| 87 | # Item has a positive balance qty, add new entry |
| 88 | if self.queue[-1][QTY] > 0: |
| 89 | self.queue.append([qty, rate]) |
| 90 | else: # negative balance qty |
| 91 | qty = self.queue[-1][QTY] + qty |
| 92 | if qty > 0: # new balance qty is positive |
| 93 | self.queue[-1] = [qty, rate] |
| 94 | else: # new balance qty is still negative, maintain same rate |
| 95 | self.queue[-1][QTY] = qty |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 96 | |
| 97 | def remove_stock( |
Ankush Menat | a00d8d0 | 2021-12-19 18:45:04 +0530 | [diff] [blame] | 98 | self, qty: float, outgoing_rate: float = 0.0, rate_generator: Callable[[], float] = None |
Ankush Menat | b855030 | 2022-01-15 12:36:56 +0530 | [diff] [blame] | 99 | ) -> List[StockBin]: |
Ankush Menat | db1c088 | 2021-12-19 18:37:12 +0530 | [diff] [blame] | 100 | """Remove stock from the queue and return popped bins. |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 101 | |
| 102 | args: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 103 | qty: quantity to remove |
| 104 | rate: outgoing rate |
| 105 | rate_generator: function to be called if queue is not found and rate is required. |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 106 | """ |
Ankush Menat | a00d8d0 | 2021-12-19 18:45:04 +0530 | [diff] [blame] | 107 | if not rate_generator: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 108 | rate_generator = lambda: 0.0 # noqa |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 109 | |
Ankush Menat | db1c088 | 2021-12-19 18:37:12 +0530 | [diff] [blame] | 110 | consumed_bins = [] |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 111 | while qty: |
| 112 | if not len(self.queue): |
| 113 | # rely on rate generator. |
| 114 | self.queue.append([0, rate_generator()]) |
| 115 | |
| 116 | index = None |
Ankush Menat | a00d8d0 | 2021-12-19 18:45:04 +0530 | [diff] [blame] | 117 | if outgoing_rate > 0: |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 118 | # Find the entry where rate matched with outgoing rate |
| 119 | for idx, fifo_bin in enumerate(self.queue): |
Ankush Menat | a00d8d0 | 2021-12-19 18:45:04 +0530 | [diff] [blame] | 120 | if fifo_bin[RATE] == outgoing_rate: |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 121 | index = idx |
| 122 | break |
| 123 | |
Ankush Menat | 12c01e2 | 2022-04-04 15:22:15 +0530 | [diff] [blame] | 124 | # If no entry found with outgoing rate, consume as per FIFO |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 125 | if index is None: # nosemgrep |
Ankush Menat | 12c01e2 | 2022-04-04 15:22:15 +0530 | [diff] [blame] | 126 | index = 0 |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 127 | else: |
| 128 | index = 0 |
| 129 | |
| 130 | # select first bin or the bin with same rate |
| 131 | fifo_bin = self.queue[index] |
| 132 | if qty >= fifo_bin[QTY]: |
| 133 | # consume current bin |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 134 | qty = round_off_if_near_zero(qty - fifo_bin[QTY]) |
Ankush Menat | db1c088 | 2021-12-19 18:37:12 +0530 | [diff] [blame] | 135 | to_consume = self.queue.pop(index) |
| 136 | consumed_bins.append(list(to_consume)) |
| 137 | |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 138 | if not self.queue and qty: |
| 139 | # stock finished, qty still remains to be withdrawn |
| 140 | # negative stock, keep in as a negative bin |
Ankush Menat | a00d8d0 | 2021-12-19 18:45:04 +0530 | [diff] [blame] | 141 | self.queue.append([-qty, outgoing_rate or fifo_bin[RATE]]) |
| 142 | consumed_bins.append([qty, outgoing_rate or fifo_bin[RATE]]) |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 143 | break |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 144 | else: |
| 145 | # qty found in current bin consume it and exit |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 146 | fifo_bin[QTY] = round_off_if_near_zero(fifo_bin[QTY] - qty) |
Ankush Menat | db1c088 | 2021-12-19 18:37:12 +0530 | [diff] [blame] | 147 | consumed_bins.append([qty, fifo_bin[RATE]]) |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 148 | qty = 0 |
| 149 | |
Ankush Menat | db1c088 | 2021-12-19 18:37:12 +0530 | [diff] [blame] | 150 | return consumed_bins |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 151 | |
| 152 | |
Ankush Menat | b855030 | 2022-01-15 12:36:56 +0530 | [diff] [blame] | 153 | class LIFOValuation(BinWiseValuation): |
| 154 | """Valuation method where a *stack* of all the incoming stock is maintained. |
| 155 | |
| 156 | New stock is added at top of the stack. |
| 157 | Qty consumption happens on Last In First Out basis. |
| 158 | |
| 159 | Stack is implemented using "bins" of [qty, rate]. |
| 160 | |
| 161 | ref: https://en.wikipedia.org/wiki/FIFO_and_LIFO_accounting |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 162 | Implementation detail: appends and pops both at end of list. |
Ankush Menat | b855030 | 2022-01-15 12:36:56 +0530 | [diff] [blame] | 163 | """ |
| 164 | |
| 165 | # specifying the attributes to save resources |
| 166 | # ref: https://docs.python.org/3/reference/datamodel.html#slots |
Ankush Menat | 12c01e2 | 2022-04-04 15:22:15 +0530 | [diff] [blame] | 167 | __slots__ = ["stack"] |
Ankush Menat | b855030 | 2022-01-15 12:36:56 +0530 | [diff] [blame] | 168 | |
| 169 | def __init__(self, state: Optional[List[StockBin]]): |
| 170 | self.stack: List[StockBin] = state if state is not None else [] |
| 171 | |
| 172 | @property |
| 173 | def state(self) -> List[StockBin]: |
| 174 | """Get current state of stack.""" |
| 175 | return self.stack |
| 176 | |
| 177 | def add_stock(self, qty: float, rate: float) -> None: |
| 178 | """Update lifo stack with new stock. |
| 179 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 180 | args: |
| 181 | qty: new quantity to add |
| 182 | rate: incoming rate of new quantity. |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 183 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 184 | Behaviour of this is same as FIFO valuation. |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 185 | """ |
| 186 | if not len(self.stack): |
| 187 | self.stack.append([0, 0]) |
| 188 | |
| 189 | # last row has the same rate, merge new bin. |
| 190 | if self.stack[-1][RATE] == rate: |
| 191 | self.stack[-1][QTY] += qty |
| 192 | else: |
| 193 | # Item has a positive balance qty, add new entry |
| 194 | if self.stack[-1][QTY] > 0: |
| 195 | self.stack.append([qty, rate]) |
| 196 | else: # negative balance qty |
| 197 | qty = self.stack[-1][QTY] + qty |
| 198 | if qty > 0: # new balance qty is positive |
| 199 | self.stack[-1] = [qty, rate] |
| 200 | else: # new balance qty is still negative, maintain same rate |
| 201 | self.stack[-1][QTY] = qty |
Ankush Menat | b855030 | 2022-01-15 12:36:56 +0530 | [diff] [blame] | 202 | |
Ankush Menat | b855030 | 2022-01-15 12:36:56 +0530 | [diff] [blame] | 203 | def remove_stock( |
| 204 | self, qty: float, outgoing_rate: float = 0.0, rate_generator: Callable[[], float] = None |
| 205 | ) -> List[StockBin]: |
| 206 | """Remove stock from the stack and return popped bins. |
| 207 | |
| 208 | args: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 209 | qty: quantity to remove |
| 210 | rate: outgoing rate - ignored. Kept for backwards compatibility. |
| 211 | rate_generator: function to be called if stack is not found and rate is required. |
Ankush Menat | b855030 | 2022-01-15 12:36:56 +0530 | [diff] [blame] | 212 | """ |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 213 | if not rate_generator: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 214 | rate_generator = lambda: 0.0 # noqa |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 215 | |
| 216 | consumed_bins = [] |
| 217 | while qty: |
| 218 | if not len(self.stack): |
| 219 | # rely on rate generator. |
| 220 | self.stack.append([0, rate_generator()]) |
| 221 | |
| 222 | # start at the end. |
| 223 | index = -1 |
| 224 | |
| 225 | stock_bin = self.stack[index] |
| 226 | if qty >= stock_bin[QTY]: |
| 227 | # consume current bin |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 228 | qty = round_off_if_near_zero(qty - stock_bin[QTY]) |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 229 | to_consume = self.stack.pop(index) |
| 230 | consumed_bins.append(list(to_consume)) |
| 231 | |
| 232 | if not self.stack and qty: |
| 233 | # stock finished, qty still remains to be withdrawn |
| 234 | # negative stock, keep in as a negative bin |
| 235 | self.stack.append([-qty, outgoing_rate or stock_bin[RATE]]) |
| 236 | consumed_bins.append([qty, outgoing_rate or stock_bin[RATE]]) |
| 237 | break |
| 238 | else: |
| 239 | # qty found in current bin consume it and exit |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 240 | stock_bin[QTY] = round_off_if_near_zero(stock_bin[QTY] - qty) |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 241 | consumed_bins.append([qty, stock_bin[RATE]]) |
| 242 | qty = 0 |
| 243 | |
| 244 | return consumed_bins |
Ankush Menat | b855030 | 2022-01-15 12:36:56 +0530 | [diff] [blame] | 245 | |
| 246 | |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 247 | def round_off_if_near_zero(number: float, precision: int = 7) -> float: |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 248 | """Rounds off the number to zero only if number is close to zero for decimal |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 249 | specified in precision. Precision defaults to 7. |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 250 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 251 | if abs(0.0 - flt(number)) < (1.0 / (10**precision)): |
Ankush Menat | e6e679c | 2021-12-18 20:36:47 +0530 | [diff] [blame] | 252 | return 0.0 |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 253 | |
| 254 | return flt(number) |