|
| 1 | +# Copyright 2021-2024 Tecnativa - Víctor Martínez |
| 2 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) |
| 3 | + |
| 4 | +from odoo.tests.common import users |
| 5 | + |
| 6 | +from odoo.addons.stock_landed_costs_purchase_auto.tests.common import ( |
| 7 | + TestPurchaseOrderBase, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +class TestPurchaseOrder(TestPurchaseOrderBase): |
| 12 | + @classmethod |
| 13 | + def setUpClass(cls): |
| 14 | + super().setUpClass() |
| 15 | + cls.env = cls.env( |
| 16 | + context=dict( |
| 17 | + cls.env.context, |
| 18 | + test_stock_landed_costs_delivery=True, |
| 19 | + ) |
| 20 | + ) |
| 21 | + product_carrier = cls.env["product.product"].create( |
| 22 | + {"name": "Carrier", "type": "service", "categ_id": cls.category.id} |
| 23 | + ) |
| 24 | + cls.carrier = cls.env["delivery.carrier"].create( |
| 25 | + { |
| 26 | + "name": "Test carrier", |
| 27 | + "product_id": product_carrier.id, |
| 28 | + "fixed_price": 10, |
| 29 | + } |
| 30 | + ) |
| 31 | + cls.order.carrier_id = cls.carrier |
| 32 | + cls.purchase_user.write( |
| 33 | + {"groups_id": [(4, cls.env.ref("stock.group_stock_user").id)]} |
| 34 | + ) |
| 35 | + |
| 36 | + @users("test_purchase_user") |
| 37 | + def test_order_with_lc_basic_user(self): |
| 38 | + self.order = self.order.with_user(self.env.user) |
| 39 | + self.order.button_confirm() |
| 40 | + lc = self.order.landed_cost_ids |
| 41 | + self.assertTrue(lc.state, "draft") |
| 42 | + picking = self.order.picking_ids |
| 43 | + self._action_picking_validate(picking) |
| 44 | + self.assertTrue(lc.state, "done") |
| 45 | + |
| 46 | + def test_order_with_lc_carrier_id(self): |
| 47 | + self.order.button_confirm() |
| 48 | + picking = self.order.picking_ids |
| 49 | + lc = self.order.landed_cost_ids |
| 50 | + self.assertEqual(len(lc.cost_lines), 0) |
| 51 | + self.assertEqual(lc.state, "draft") |
| 52 | + self._action_picking_validate(picking) |
| 53 | + self.assertEqual(len(lc.cost_lines), 1) |
| 54 | + self.assertEqual(lc.state, "done") |
| 55 | + self.assertIn(self.carrier.product_id, lc.cost_lines.mapped("product_id")) |
| 56 | + lc_cost_line = lc.cost_lines.filtered( |
| 57 | + lambda x: x.product_id == self.carrier.product_id |
| 58 | + ) |
| 59 | + self.assertEqual(lc_cost_line.price_unit, 10) |
| 60 | + self.assertEqual( |
| 61 | + lc_cost_line.split_method, self.carrier.split_method_landed_cost_line |
| 62 | + ) |
| 63 | + |
| 64 | + def test_order_with_lc_carrier_id_multi_01(self): |
| 65 | + """Order > Carrier. Picking 1 > Carrier. Picking 2 > Carrier.""" |
| 66 | + self.order.order_line.product_qty = 2 |
| 67 | + self.order.button_confirm() |
| 68 | + picking = self.order.picking_ids |
| 69 | + self.assertEqual(len(self.order.landed_cost_ids), 1) |
| 70 | + lc = self.order.landed_cost_ids |
| 71 | + self.assertEqual(len(lc.cost_lines), 0) |
| 72 | + self.assertEqual(lc.state, "draft") |
| 73 | + for move in picking.move_ids_without_package: |
| 74 | + move.quantity_done = 1 |
| 75 | + self._action_picking_validate(picking) |
| 76 | + self.assertEqual(len(self.order.landed_cost_ids), 2) |
| 77 | + extra_lc = self.order.landed_cost_ids - lc |
| 78 | + self.assertEqual(len(lc.cost_lines), 1) |
| 79 | + self.assertEqual(lc.state, "done") |
| 80 | + self.assertEqual(extra_lc.state, "draft") |
| 81 | + self.assertEqual(lc.cost_lines.price_unit, 10) |
| 82 | + new_picking = self.order.picking_ids - picking |
| 83 | + self._action_picking_validate(new_picking) |
| 84 | + self.assertEqual(len(extra_lc.cost_lines), 1) |
| 85 | + self.assertEqual(extra_lc.state, "done") |
| 86 | + self.assertEqual(self.carrier.product_id, extra_lc.cost_lines.product_id) |
| 87 | + self.assertEqual(extra_lc.cost_lines.price_unit, 10) |
| 88 | + self.assertEqual( |
| 89 | + self.carrier.split_method_landed_cost_line, |
| 90 | + extra_lc.cost_lines.split_method, |
| 91 | + ) |
| 92 | + |
| 93 | + def test_order_with_lc_carrier_id_multi_02(self): |
| 94 | + """Order > No Carrier. Picking 1 > No Carrier. Picking 2 > Carrier.""" |
| 95 | + self.order.carrier_id = False |
| 96 | + self.order.order_line.product_qty = 2 |
| 97 | + self.order.button_confirm() |
| 98 | + picking = self.order.picking_ids |
| 99 | + self.assertEqual(len(self.order.landed_cost_ids), 1) |
| 100 | + lc = self.order.landed_cost_ids |
| 101 | + self.assertEqual(len(lc.cost_lines), 0) |
| 102 | + self.assertEqual(lc.state, "draft") |
| 103 | + for move in picking.move_ids_without_package: |
| 104 | + move.quantity_done = 1 |
| 105 | + self._action_picking_validate(picking) |
| 106 | + # Picking without carrier and LC without cost lines and draft state |
| 107 | + self.assertFalse(picking.carrier_id) |
| 108 | + self.assertEqual(len(self.order.landed_cost_ids), 2) |
| 109 | + extra_lc = self.order.landed_cost_ids - lc |
| 110 | + self.assertEqual(len(lc.cost_lines), 0) |
| 111 | + self.assertEqual(lc.state, "draft") |
| 112 | + self.assertEqual(extra_lc.state, "draft") |
| 113 | + new_picking = self.order.picking_ids - picking |
| 114 | + new_picking.carrier_id = self.carrier |
| 115 | + self._action_picking_validate(new_picking) |
| 116 | + # Order with carrier, delivery price and delivery line |
| 117 | + self.assertEqual(self.order.carrier_id, self.carrier) |
| 118 | + self.assertEqual(self.order.delivery_price, 10) |
| 119 | + self.assertEqual( |
| 120 | + len(self.order.order_line.filtered(lambda x: x.is_delivery)), 1 |
| 121 | + ) |
| 122 | + # LC keep draft state |
| 123 | + self.assertEqual(lc.state, "draft") |
| 124 | + # Extra LC done (cost line from delivery) |
| 125 | + self.assertEqual(len(extra_lc.cost_lines), 1) |
| 126 | + self.assertEqual(extra_lc.state, "done") |
| 127 | + self.assertEqual(self.carrier.product_id, extra_lc.cost_lines.product_id) |
| 128 | + self.assertEqual(extra_lc.cost_lines.price_unit, 10) |
| 129 | + self.assertEqual( |
| 130 | + self.carrier.split_method_landed_cost_line, |
| 131 | + extra_lc.cost_lines.split_method, |
| 132 | + ) |
0 commit comments