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