Ankush Menat | 3e5f940 | 2022-01-15 18:49:46 +0530 | [diff] [blame] | 1 | import json |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 2 | import unittest |
| 3 | |
Ankush Menat | 3e5f940 | 2022-01-15 18:49:46 +0530 | [diff] [blame] | 4 | import frappe |
Ankush Menat | b0d1e6d | 2022-02-28 16:55:46 +0530 | [diff] [blame] | 5 | from frappe.tests.utils import FrappeTestCase |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 6 | from hypothesis import given |
| 7 | from hypothesis import strategies as st |
| 8 | |
Ankush Menat | 3e5f940 | 2022-01-15 18:49:46 +0530 | [diff] [blame] | 9 | from erpnext.stock.doctype.item.test_item import make_item |
| 10 | from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 11 | from erpnext.stock.valuation import FIFOValuation, LIFOValuation, round_off_if_near_zero |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 12 | |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 13 | qty_gen = st.floats(min_value=-1e6, max_value=1e6) |
| 14 | value_gen = st.floats(min_value=1, max_value=1e6) |
| 15 | stock_queue_generator = st.lists(st.tuples(qty_gen, value_gen), min_size=10) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 16 | |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 17 | |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 18 | class TestFIFOValuation(unittest.TestCase): |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 19 | def setUp(self): |
Ankush Menat | 107b404 | 2021-12-19 20:47:08 +0530 | [diff] [blame] | 20 | self.queue = FIFOValuation([]) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 21 | |
| 22 | def tearDown(self): |
| 23 | qty, value = self.queue.get_total_stock_and_value() |
| 24 | self.assertTotalQty(qty) |
| 25 | self.assertTotalValue(value) |
| 26 | |
| 27 | def assertTotalQty(self, qty): |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 28 | self.assertAlmostEqual(sum(q for q, _ in self.queue), qty, msg=f"queue: {self.queue}", places=4) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 29 | |
| 30 | def assertTotalValue(self, value): |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 31 | self.assertAlmostEqual(sum(q * r for q, r in self.queue), value, msg=f"queue: {self.queue}", places=2) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 32 | |
| 33 | def test_simple_addition(self): |
| 34 | self.queue.add_stock(1, 10) |
| 35 | self.assertTotalQty(1) |
| 36 | |
| 37 | def test_simple_removal(self): |
| 38 | self.queue.add_stock(1, 10) |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 39 | self.queue.remove_stock(1) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 40 | self.assertTotalQty(0) |
| 41 | |
| 42 | def test_merge_new_stock(self): |
| 43 | self.queue.add_stock(1, 10) |
| 44 | self.queue.add_stock(1, 10) |
| 45 | self.assertEqual(self.queue, [[2, 10]]) |
| 46 | |
| 47 | def test_adding_negative_stock_keeps_rate(self): |
Ankush Menat | 107b404 | 2021-12-19 20:47:08 +0530 | [diff] [blame] | 48 | self.queue = FIFOValuation([[-5.0, 100]]) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 49 | self.queue.add_stock(1, 10) |
| 50 | self.assertEqual(self.queue, [[-4, 100]]) |
| 51 | |
| 52 | def test_adding_negative_stock_updates_rate(self): |
Ankush Menat | 107b404 | 2021-12-19 20:47:08 +0530 | [diff] [blame] | 53 | self.queue = FIFOValuation([[-5.0, 100]]) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 54 | self.queue.add_stock(6, 10) |
| 55 | self.assertEqual(self.queue, [[1, 10]]) |
| 56 | |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 57 | def test_negative_stock(self): |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 58 | self.queue.remove_stock(1, 5) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 59 | self.assertEqual(self.queue, [[-1, 5]]) |
| 60 | |
Ankush Menat | 12c01e2 | 2022-04-04 15:22:15 +0530 | [diff] [blame] | 61 | self.queue.remove_stock(1) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 62 | self.assertTotalQty(-2) |
Ankush Menat | 12c01e2 | 2022-04-04 15:22:15 +0530 | [diff] [blame] | 63 | self.assertEqual(self.queue, [[-2, 5]]) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 64 | |
| 65 | self.queue.add_stock(2, 10) |
| 66 | self.assertTotalQty(0) |
| 67 | self.assertTotalValue(0) |
| 68 | |
| 69 | def test_removing_specified_rate(self): |
| 70 | self.queue.add_stock(1, 10) |
| 71 | self.queue.add_stock(1, 20) |
| 72 | |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 73 | self.queue.remove_stock(1, 20) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 74 | self.assertEqual(self.queue, [[1, 10]]) |
| 75 | |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 76 | def test_remove_multiple_bins(self): |
| 77 | self.queue.add_stock(1, 10) |
| 78 | self.queue.add_stock(2, 20) |
| 79 | self.queue.add_stock(1, 20) |
| 80 | self.queue.add_stock(5, 20) |
| 81 | |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 82 | self.queue.remove_stock(4) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 83 | self.assertEqual(self.queue, [[5, 20]]) |
| 84 | |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 85 | def test_remove_multiple_bins_with_rate(self): |
| 86 | self.queue.add_stock(1, 10) |
| 87 | self.queue.add_stock(2, 20) |
| 88 | self.queue.add_stock(1, 20) |
| 89 | self.queue.add_stock(5, 20) |
| 90 | |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 91 | self.queue.remove_stock(3, 20) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 92 | self.assertEqual(self.queue, [[1, 10], [5, 20]]) |
| 93 | |
Ankush Menat | 12c01e2 | 2022-04-04 15:22:15 +0530 | [diff] [blame] | 94 | def test_queue_with_unknown_rate(self): |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 95 | self.queue.add_stock(1, 1) |
| 96 | self.queue.add_stock(1, 2) |
| 97 | self.queue.add_stock(1, 3) |
| 98 | self.queue.add_stock(1, 4) |
| 99 | |
| 100 | self.assertTotalValue(10) |
| 101 | |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 102 | self.queue.remove_stock(3, 1) |
Ankush Menat | 12c01e2 | 2022-04-04 15:22:15 +0530 | [diff] [blame] | 103 | self.assertEqual(self.queue, [[1, 4]]) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 104 | |
| 105 | def test_rounding_off(self): |
| 106 | self.queue.add_stock(1.0, 1.0) |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 107 | self.queue.remove_stock(1.0 - 1e-9) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 108 | self.assertTotalQty(0) |
| 109 | |
| 110 | def test_rounding_off_near_zero(self): |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 111 | self.assertEqual(round_off_if_near_zero(0), 0) |
| 112 | self.assertEqual(round_off_if_near_zero(1), 1) |
| 113 | self.assertEqual(round_off_if_near_zero(-1), -1) |
| 114 | self.assertEqual(round_off_if_near_zero(-1e-8), 0) |
| 115 | self.assertEqual(round_off_if_near_zero(1e-8), 0) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 116 | |
| 117 | def test_totals(self): |
| 118 | self.queue.add_stock(1, 10) |
| 119 | self.queue.add_stock(2, 13) |
| 120 | self.queue.add_stock(1, 17) |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 121 | self.queue.remove_stock(1) |
| 122 | self.queue.remove_stock(1) |
| 123 | self.queue.remove_stock(1) |
Ankush Menat | 1833f7a | 2021-12-18 19:37:41 +0530 | [diff] [blame] | 124 | self.queue.add_stock(5, 17) |
| 125 | self.queue.add_stock(8, 11) |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 126 | |
| 127 | @given(stock_queue_generator) |
| 128 | def test_fifo_qty_hypothesis(self, stock_queue): |
Ankush Menat | 107b404 | 2021-12-19 20:47:08 +0530 | [diff] [blame] | 129 | self.queue = FIFOValuation([]) |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 130 | total_qty = 0 |
| 131 | |
| 132 | for qty, rate in stock_queue: |
Ankush Menat | b8a61be | 2023-03-13 15:16:30 +0530 | [diff] [blame] | 133 | if round_off_if_near_zero(qty) == 0: |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 134 | continue |
| 135 | if qty > 0: |
| 136 | self.queue.add_stock(qty, rate) |
| 137 | total_qty += qty |
| 138 | else: |
| 139 | qty = abs(qty) |
| 140 | consumed = self.queue.remove_stock(qty) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 141 | self.assertAlmostEqual( |
| 142 | qty, sum(q for q, _ in consumed), msg=f"incorrect consumption {consumed}" |
| 143 | ) |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 144 | total_qty -= qty |
| 145 | self.assertTotalQty(total_qty) |
| 146 | |
| 147 | @given(stock_queue_generator) |
| 148 | def test_fifo_qty_value_nonneg_hypothesis(self, stock_queue): |
Ankush Menat | 107b404 | 2021-12-19 20:47:08 +0530 | [diff] [blame] | 149 | self.queue = FIFOValuation([]) |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 150 | total_qty = 0.0 |
| 151 | total_value = 0.0 |
| 152 | |
| 153 | for qty, rate in stock_queue: |
| 154 | # don't allow negative stock |
Ankush Menat | b8a61be | 2023-03-13 15:16:30 +0530 | [diff] [blame] | 155 | if round_off_if_near_zero(qty) == 0 or total_qty + qty < 0 or abs(qty) < 0.1: |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 156 | continue |
| 157 | if qty > 0: |
| 158 | self.queue.add_stock(qty, rate) |
| 159 | total_qty += qty |
| 160 | total_value += qty * rate |
| 161 | else: |
| 162 | qty = abs(qty) |
| 163 | consumed = self.queue.remove_stock(qty) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 164 | self.assertAlmostEqual( |
| 165 | qty, sum(q for q, _ in consumed), msg=f"incorrect consumption {consumed}" |
| 166 | ) |
Ankush Menat | aa0e163 | 2021-12-19 17:02:04 +0530 | [diff] [blame] | 167 | total_qty -= qty |
| 168 | total_value -= sum(q * r for q, r in consumed) |
| 169 | self.assertTotalQty(total_qty) |
| 170 | self.assertTotalValue(total_value) |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 171 | |
Ankush Menat | 12c01e2 | 2022-04-04 15:22:15 +0530 | [diff] [blame] | 172 | @given(stock_queue_generator, st.floats(min_value=0.1, max_value=1e6)) |
| 173 | def test_fifo_qty_value_nonneg_hypothesis_with_outgoing_rate(self, stock_queue, outgoing_rate): |
| 174 | self.queue = FIFOValuation([]) |
| 175 | total_qty = 0.0 |
| 176 | total_value = 0.0 |
| 177 | |
| 178 | for qty, rate in stock_queue: |
| 179 | # don't allow negative stock |
Ankush Menat | b8a61be | 2023-03-13 15:16:30 +0530 | [diff] [blame] | 180 | if round_off_if_near_zero(qty) == 0 or total_qty + qty < 0 or abs(qty) < 0.1: |
Ankush Menat | 12c01e2 | 2022-04-04 15:22:15 +0530 | [diff] [blame] | 181 | continue |
| 182 | if qty > 0: |
| 183 | self.queue.add_stock(qty, rate) |
| 184 | total_qty += qty |
| 185 | total_value += qty * rate |
| 186 | else: |
| 187 | qty = abs(qty) |
| 188 | consumed = self.queue.remove_stock(qty, outgoing_rate) |
| 189 | self.assertAlmostEqual( |
| 190 | qty, sum(q for q, _ in consumed), msg=f"incorrect consumption {consumed}" |
| 191 | ) |
| 192 | total_qty -= qty |
| 193 | total_value -= sum(q * r for q, r in consumed) |
| 194 | self.assertTotalQty(total_qty) |
| 195 | self.assertTotalValue(total_value) |
Ankush Menat | 12c01e2 | 2022-04-04 15:22:15 +0530 | [diff] [blame] | 196 | |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 197 | |
| 198 | class TestLIFOValuation(unittest.TestCase): |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 199 | def setUp(self): |
| 200 | self.stack = LIFOValuation([]) |
| 201 | |
| 202 | def tearDown(self): |
| 203 | qty, value = self.stack.get_total_stock_and_value() |
| 204 | self.assertTotalQty(qty) |
| 205 | self.assertTotalValue(value) |
| 206 | |
| 207 | def assertTotalQty(self, qty): |
| 208 | self.assertAlmostEqual(sum(q for q, _ in self.stack), qty, msg=f"stack: {self.stack}", places=4) |
| 209 | |
| 210 | def assertTotalValue(self, value): |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 211 | self.assertAlmostEqual(sum(q * r for q, r in self.stack), value, msg=f"stack: {self.stack}", places=2) |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 212 | |
| 213 | def test_simple_addition(self): |
| 214 | self.stack.add_stock(1, 10) |
| 215 | self.assertTotalQty(1) |
| 216 | |
| 217 | def test_merge_new_stock(self): |
| 218 | self.stack.add_stock(1, 10) |
| 219 | self.stack.add_stock(1, 10) |
| 220 | self.assertEqual(self.stack, [[2, 10]]) |
| 221 | |
| 222 | def test_simple_removal(self): |
| 223 | self.stack.add_stock(1, 10) |
| 224 | self.stack.remove_stock(1) |
| 225 | self.assertTotalQty(0) |
| 226 | |
| 227 | def test_adding_negative_stock_keeps_rate(self): |
| 228 | self.stack = LIFOValuation([[-5.0, 100]]) |
| 229 | self.stack.add_stock(1, 10) |
| 230 | self.assertEqual(self.stack, [[-4, 100]]) |
| 231 | |
| 232 | def test_adding_negative_stock_updates_rate(self): |
| 233 | self.stack = LIFOValuation([[-5.0, 100]]) |
| 234 | self.stack.add_stock(6, 10) |
| 235 | self.assertEqual(self.stack, [[1, 10]]) |
| 236 | |
| 237 | def test_rounding_off(self): |
| 238 | self.stack.add_stock(1.0, 1.0) |
| 239 | self.stack.remove_stock(1.0 - 1e-9) |
| 240 | self.assertTotalQty(0) |
| 241 | |
| 242 | def test_lifo_consumption(self): |
| 243 | self.stack.add_stock(10, 10) |
| 244 | self.stack.add_stock(10, 20) |
| 245 | consumed = self.stack.remove_stock(15) |
| 246 | self.assertEqual(consumed, [[10, 20], [5, 10]]) |
| 247 | self.assertTotalQty(5) |
| 248 | |
| 249 | def test_lifo_consumption_going_negative(self): |
| 250 | self.stack.add_stock(10, 10) |
| 251 | self.stack.add_stock(10, 20) |
| 252 | consumed = self.stack.remove_stock(25) |
| 253 | self.assertEqual(consumed, [[10, 20], [10, 10], [5, 10]]) |
| 254 | self.assertTotalQty(-5) |
| 255 | |
| 256 | def test_lifo_consumption_multiple(self): |
| 257 | self.stack.add_stock(1, 1) |
| 258 | self.stack.add_stock(2, 2) |
| 259 | consumed = self.stack.remove_stock(1) |
| 260 | self.assertEqual(consumed, [[1, 2]]) |
| 261 | |
| 262 | self.stack.add_stock(3, 3) |
| 263 | consumed = self.stack.remove_stock(4) |
| 264 | self.assertEqual(consumed, [[3, 3], [1, 2]]) |
| 265 | |
| 266 | self.stack.add_stock(4, 4) |
| 267 | consumed = self.stack.remove_stock(5) |
| 268 | self.assertEqual(consumed, [[4, 4], [1, 1]]) |
| 269 | |
| 270 | self.stack.add_stock(5, 5) |
| 271 | consumed = self.stack.remove_stock(5) |
| 272 | self.assertEqual(consumed, [[5, 5]]) |
| 273 | |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 274 | @given(stock_queue_generator) |
| 275 | def test_lifo_qty_hypothesis(self, stock_stack): |
| 276 | self.stack = LIFOValuation([]) |
| 277 | total_qty = 0 |
| 278 | |
| 279 | for qty, rate in stock_stack: |
Ankush Menat | b8a61be | 2023-03-13 15:16:30 +0530 | [diff] [blame] | 280 | if round_off_if_near_zero(qty) == 0: |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 281 | continue |
| 282 | if qty > 0: |
| 283 | self.stack.add_stock(qty, rate) |
| 284 | total_qty += qty |
| 285 | else: |
| 286 | qty = abs(qty) |
| 287 | consumed = self.stack.remove_stock(qty) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 288 | self.assertAlmostEqual( |
| 289 | qty, sum(q for q, _ in consumed), msg=f"incorrect consumption {consumed}" |
| 290 | ) |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 291 | total_qty -= qty |
| 292 | self.assertTotalQty(total_qty) |
| 293 | |
| 294 | @given(stock_queue_generator) |
| 295 | def test_lifo_qty_value_nonneg_hypothesis(self, stock_stack): |
| 296 | self.stack = LIFOValuation([]) |
| 297 | total_qty = 0.0 |
| 298 | total_value = 0.0 |
| 299 | |
| 300 | for qty, rate in stock_stack: |
| 301 | # don't allow negative stock |
Ankush Menat | b8a61be | 2023-03-13 15:16:30 +0530 | [diff] [blame] | 302 | if round_off_if_near_zero(qty) == 0 or total_qty + qty < 0 or abs(qty) < 0.1: |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 303 | continue |
| 304 | if qty > 0: |
| 305 | self.stack.add_stock(qty, rate) |
| 306 | total_qty += qty |
| 307 | total_value += qty * rate |
| 308 | else: |
| 309 | qty = abs(qty) |
| 310 | consumed = self.stack.remove_stock(qty) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 311 | self.assertAlmostEqual( |
| 312 | qty, sum(q for q, _ in consumed), msg=f"incorrect consumption {consumed}" |
| 313 | ) |
Ankush Menat | 9c49d2d | 2022-01-15 12:52:10 +0530 | [diff] [blame] | 314 | total_qty -= qty |
| 315 | total_value -= sum(q * r for q, r in consumed) |
| 316 | self.assertTotalQty(total_qty) |
| 317 | self.assertTotalValue(total_value) |
Ankush Menat | 3e5f940 | 2022-01-15 18:49:46 +0530 | [diff] [blame] | 318 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 319 | |
Ankush Menat | b0d1e6d | 2022-02-28 16:55:46 +0530 | [diff] [blame] | 320 | class TestLIFOValuationSLE(FrappeTestCase): |
Ankush Menat | 3e5f940 | 2022-01-15 18:49:46 +0530 | [diff] [blame] | 321 | ITEM_CODE = "_Test LIFO item" |
| 322 | WAREHOUSE = "_Test Warehouse - _TC" |
| 323 | |
| 324 | @classmethod |
| 325 | def setUpClass(cls) -> None: |
| 326 | super().setUpClass() |
| 327 | make_item(cls.ITEM_CODE, {"valuation_method": "LIFO"}) |
| 328 | |
| 329 | def _make_stock_entry(self, qty, rate=None): |
| 330 | kwargs = { |
| 331 | "item_code": self.ITEM_CODE, |
| 332 | "from_warehouse" if qty < 0 else "to_warehouse": self.WAREHOUSE, |
| 333 | "rate": rate, |
| 334 | "qty": abs(qty), |
| 335 | } |
| 336 | return make_stock_entry(**kwargs) |
| 337 | |
| 338 | def assertStockQueue(self, se, expected_queue): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 339 | sle_name = frappe.db.get_value( |
| 340 | "Stock Ledger Entry", {"voucher_no": se.name, "is_cancelled": 0, "voucher_type": "Stock Entry"} |
| 341 | ) |
Ankush Menat | 3e5f940 | 2022-01-15 18:49:46 +0530 | [diff] [blame] | 342 | sle = frappe.get_doc("Stock Ledger Entry", sle_name) |
| 343 | |
| 344 | stock_queue = json.loads(sle.stock_queue) |
| 345 | |
| 346 | total_qty, total_value = LIFOValuation(stock_queue).get_total_stock_and_value() |
| 347 | self.assertEqual(sle.qty_after_transaction, total_qty) |
| 348 | self.assertEqual(sle.stock_value, total_value) |
| 349 | |
| 350 | if total_qty > 0: |
| 351 | self.assertEqual(stock_queue, expected_queue) |
| 352 | |
Ankush Menat | 3e5f940 | 2022-01-15 18:49:46 +0530 | [diff] [blame] | 353 | def test_lifo_values(self): |
Ankush Menat | 3e5f940 | 2022-01-15 18:49:46 +0530 | [diff] [blame] | 354 | in1 = self._make_stock_entry(1, 1) |
| 355 | self.assertStockQueue(in1, [[1, 1]]) |
| 356 | |
| 357 | in2 = self._make_stock_entry(2, 2) |
| 358 | self.assertStockQueue(in2, [[1, 1], [2, 2]]) |
| 359 | |
| 360 | out1 = self._make_stock_entry(-1) |
| 361 | self.assertStockQueue(out1, [[1, 1], [1, 2]]) |
| 362 | |
| 363 | in3 = self._make_stock_entry(3, 3) |
| 364 | self.assertStockQueue(in3, [[1, 1], [1, 2], [3, 3]]) |
| 365 | |
| 366 | out2 = self._make_stock_entry(-4) |
| 367 | self.assertStockQueue(out2, [[1, 1]]) |
| 368 | |
| 369 | in4 = self._make_stock_entry(4, 4) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 370 | self.assertStockQueue(in4, [[1, 1], [4, 4]]) |
Ankush Menat | 3e5f940 | 2022-01-15 18:49:46 +0530 | [diff] [blame] | 371 | |
| 372 | out3 = self._make_stock_entry(-5) |
| 373 | self.assertStockQueue(out3, []) |
| 374 | |
| 375 | in5 = self._make_stock_entry(5, 5) |
| 376 | self.assertStockQueue(in5, [[5, 5]]) |
| 377 | |
| 378 | out5 = self._make_stock_entry(-5) |
| 379 | self.assertStockQueue(out5, []) |