|
| 1 | +# Copyright 2025 Camptocamp SA |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) |
| 3 | +from datetime import datetime |
| 4 | + |
| 5 | +from .common import PromiseReleaseCommonCase |
| 6 | + |
| 7 | + |
| 8 | +class TestAvailableToPromiseReleaseCancel(PromiseReleaseCommonCase): |
| 9 | + @classmethod |
| 10 | + def setUpClass(cls): |
| 11 | + super().setUpClass() |
| 12 | + |
| 13 | + cls.wh.delivery_steps = "pick_pack_ship" |
| 14 | + cls._update_qty_in_location(cls.loc_bin1, cls.product1, 15.0) |
| 15 | + |
| 16 | + delivery_route = cls.wh.delivery_route_id |
| 17 | + ship_rule = delivery_route.rule_ids.filtered( |
| 18 | + lambda r: r.location_id == cls.loc_customer |
| 19 | + ) |
| 20 | + cls.loc_output = ship_rule.location_src_id |
| 21 | + pack_rule = delivery_route.rule_ids.filtered( |
| 22 | + lambda r: r.location_id == cls.loc_output |
| 23 | + ) |
| 24 | + cls.loc_pack = pack_rule.location_src_id |
| 25 | + pick_rule = delivery_route.rule_ids.filtered( |
| 26 | + lambda r: r.location_id == cls.loc_pack |
| 27 | + ) |
| 28 | + cls.pick_type = pick_rule.picking_type_id |
| 29 | + cls.pack_type = pack_rule.picking_type_id |
| 30 | + |
| 31 | + cls.picking_chain = cls._create_picking_chain( |
| 32 | + cls.wh, [(cls.product1, 10)], date=datetime(2019, 9, 2, 16, 0) |
| 33 | + ) |
| 34 | + cls.ship_picking = cls._out_picking(cls.picking_chain) |
| 35 | + cls.pack_picking = cls._prev_picking(cls.ship_picking) |
| 36 | + cls.pick_picking = cls._prev_picking(cls.pack_picking) |
| 37 | + |
| 38 | + # Why is this not working when creating picking after enabling this setting? |
| 39 | + delivery_route.write( |
| 40 | + { |
| 41 | + "available_to_promise_defer_pull": True, |
| 42 | + "allow_unrelease_on_cancel": True, |
| 43 | + } |
| 44 | + ) |
| 45 | + cls.ship_picking.release_available_to_promise() |
| 46 | + cls.cleanup_type = cls.env["stock.picking.type"].create( |
| 47 | + { |
| 48 | + "name": "Cancel Cleanup", |
| 49 | + "default_location_dest_id": cls.loc_stock.id, |
| 50 | + "sequence_code": "CCP", |
| 51 | + "code": "internal", |
| 52 | + } |
| 53 | + ) |
| 54 | + cls.pick_type.return_picking_type_id = cls.cleanup_type |
| 55 | + cls.pack_type.return_picking_type_id = cls.cleanup_type |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def _get_cleanup_picking(cls): |
| 59 | + return cls.env["stock.picking"].search( |
| 60 | + [("picking_type_id", "=", cls.cleanup_type.id)] |
| 61 | + ) |
| 62 | + |
| 63 | + def test_unrelease_picked(self): |
| 64 | + # In this case, we should get 1 return picking from |
| 65 | + # WH/PACK to WH/STOCK |
| 66 | + self._deliver(self.pick_picking) |
| 67 | + self.ship_picking.unrelease() |
| 68 | + self.assertTrue(self.ship_picking.need_release) |
| 69 | + self.assertEqual(self.pack_picking.state, "cancel") |
| 70 | + self.assertEqual(self.pick_picking.state, "done") |
| 71 | + cancel_picking = self._get_cleanup_picking() |
| 72 | + self.assertEqual(len(cancel_picking), 1) |
| 73 | + self.assertEqual(cancel_picking.location_id, self.loc_pack) |
| 74 | + self.assertEqual(cancel_picking.location_dest_id, self.loc_stock) |
| 75 | + |
| 76 | + def test_unrelease_packed(self): |
| 77 | + # In this case, we should get 1 return picking from |
| 78 | + # WH/OUT to WH/STOCK |
| 79 | + self._deliver(self.pick_picking) |
| 80 | + self._deliver(self.pack_picking) |
| 81 | + self.ship_picking.unrelease() |
| 82 | + self.assertTrue(self.ship_picking.need_release) |
| 83 | + self.assertEqual(self.pack_picking.state, "done") |
| 84 | + self.assertEqual(self.pick_picking.state, "done") |
| 85 | + cancel_picking = self._get_cleanup_picking() |
| 86 | + self.assertEqual(len(cancel_picking), 1) |
| 87 | + self.assertEqual(cancel_picking.location_id, self.loc_output) |
| 88 | + self.assertEqual(cancel_picking.location_dest_id, self.loc_stock) |
| 89 | + |
| 90 | + def test_unrelease_picked_partial(self): |
| 91 | + qty_picked = [(self.product1, 5.0)] |
| 92 | + self._deliver(self.pick_picking, product_qty=qty_picked) |
| 93 | + pick_backorder = self._get_backorder_for_pickings(self.pick_picking) |
| 94 | + self.assertTrue(pick_backorder) |
| 95 | + self.ship_picking.unrelease() |
| 96 | + self.assertTrue(self.ship_picking.need_release) |
| 97 | + self.assertEqual(self.pack_picking.state, "cancel") |
| 98 | + self.assertEqual(self.pick_picking.state, "done") |
| 99 | + cancel_picking = self._get_cleanup_picking() |
| 100 | + # In the end, we cancelled 5 units for the pick backorder, and returned |
| 101 | + # 5 units from pack -> stock |
| 102 | + self.assertEqual(pick_backorder.state, "cancel") |
| 103 | + self.assertEqual(cancel_picking.location_id, self.loc_pack) |
| 104 | + self.assertEqual(cancel_picking.location_dest_id, self.loc_stock) |
| 105 | + self.assertEqual(cancel_picking.move_lines.product_uom_qty, 5.0) |
| 106 | + |
| 107 | + def test_unrelease_packed_partial(self): |
| 108 | + self._deliver(self.pick_picking) |
| 109 | + qty_packed = [(self.product1, 5.0)] |
| 110 | + self._deliver(self.pack_picking, product_qty=qty_packed) |
| 111 | + pack_backorder = self._get_backorder_for_pickings(self.pack_picking) |
| 112 | + self.assertTrue(pack_backorder) |
| 113 | + self.ship_picking.unrelease() |
| 114 | + self.assertTrue(self.ship_picking.need_release) |
| 115 | + self.assertEqual(self.pack_picking.state, "done") |
| 116 | + self.assertEqual(self.pick_picking.state, "done") |
| 117 | + cancel_pickings = self._get_cleanup_picking() |
| 118 | + self.assertEqual(len(cancel_pickings), 2) |
| 119 | + # In the end, we cancelled 5 units for the pack backorder, returned |
| 120 | + # 5 units from pack -> stock, and 5 units from output -> stock |
| 121 | + pack_cancel = cancel_pickings.filtered(lambda p: p.location_id == self.loc_pack) |
| 122 | + ship_cancel = cancel_pickings.filtered( |
| 123 | + lambda p: p.location_id == self.loc_output |
| 124 | + ) |
| 125 | + self.assertEqual(pack_cancel.move_lines.product_uom_qty, 5.0) |
| 126 | + self.assertEqual(ship_cancel.move_lines.product_uom_qty, 5.0) |
| 127 | + |
| 128 | + def test_unrelease_shipped(self): |
| 129 | + self._deliver(self.pick_picking) |
| 130 | + self._deliver(self.pack_picking) |
| 131 | + self._deliver(self.ship_picking) |
| 132 | + self.ship_picking.unrelease() |
| 133 | + # Did nothing |
| 134 | + self.assertEqual(self.ship_picking.state, "done") |
| 135 | + self.assertEqual(self.pack_picking.state, "done") |
| 136 | + self.assertEqual(self.pick_picking.state, "done") |
0 commit comments