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