| 1 |
|
| 2 |
|
| 3 |
from unittest import skip |
| 4 |
|
| 5 |
from odoo.addons.stock_account.tests.test_lot_valuation import TestLotValuation |
| 6 |
from odoo.tests import tagged, Form |
| 7 |
from odoo import Command |
| 8 |
|
| 9 |
|
| 10 |
@tagged('post_install', '-at_install') |
| 11 |
@skip('Temporary to fast merge new valuation') |
| 12 |
class TestStockLandedCostsLots(TestLotValuation): |
| 13 |
@classmethod |
| 14 |
def setUpClass(cls): |
| 15 |
super().setUpClass() |
| 16 |
cls.productlc1 = cls.env['product.product'].create({ |
| 17 |
'name': 'product1', |
| 18 |
'type': 'service', |
| 19 |
'landed_cost_ok': True, |
| 20 |
'categ_id': cls.env.ref('product.product_category_goods').id, |
| 21 |
}) |
| 22 |
|
| 23 |
def test_stock_landed_costs_lots(self): |
| 24 |
self.product1.product_tmpl_id.categ_id.property_valuation = 'real_time' |
| 25 |
picking_1 = self.env['stock.picking'].create({ |
| 26 |
'picking_type_id': self.env.ref('stock.picking_type_in').id, |
| 27 |
'move_ids': [Command.create({ |
| 28 |
'product_id': self.product1.id, |
| 29 |
'product_uom_qty': 15, |
| 30 |
'product_uom': self.ref('uom.product_uom_unit'), |
| 31 |
'location_id': self.supplier_location.id, |
| 32 |
'location_dest_id': self.env.ref('stock.stock_location_stock').id, |
| 33 |
'price_unit': 10, |
| 34 |
})], |
| 35 |
}) |
| 36 |
product2 = self.env['product.product'].create({ |
| 37 |
'name': 'product2', |
| 38 |
'is_storable': True, |
| 39 |
'tracking': 'lot', |
| 40 |
'lot_valuated': True, |
| 41 |
'categ_id': self.env.ref('product.product_category_goods').id, |
| 42 |
}) |
| 43 |
picking_2 = self.env['stock.picking'].create({ |
| 44 |
'picking_type_id': self.env.ref('stock.picking_type_in').id, |
| 45 |
'move_ids': [Command.create({ |
| 46 |
'product_id': product2.id, |
| 47 |
'product_uom_qty': 10, |
| 48 |
'product_uom': self.ref('uom.product_uom_unit'), |
| 49 |
'location_id': self.supplier_location.id, |
| 50 |
'location_dest_id': self.env.ref('stock.stock_location_stock').id, |
| 51 |
'price_unit': 11, |
| 52 |
})], |
| 53 |
}) |
| 54 |
|
| 55 |
|
| 56 |
(picking_1 | picking_2).action_confirm() |
| 57 |
picking_1.move_ids.move_line_ids = [Command.clear()] + [Command.create({ |
| 58 |
'product_id': self.product1.id, |
| 59 |
'lot_name': lot_name, |
| 60 |
'quantity': 5, |
| 61 |
'location_id': self.supplier_location.id, |
| 62 |
'location_dest_id': self.env.ref('stock.stock_location_stock').id, |
| 63 |
}) for lot_name in ['LClotA1', 'LClotA2', 'LClotA3']] |
| 64 |
picking_2.move_ids.move_line_ids = [Command.clear()] + [Command.create({ |
| 65 |
'product_id': product2.id, |
| 66 |
'lot_name': lot_name, |
| 67 |
'quantity': 5, |
| 68 |
'location_id': self.supplier_location.id, |
| 69 |
'location_dest_id': self.env.ref('stock.stock_location_stock').id, |
| 70 |
}) for lot_name in ['LClotB1', 'LClotB2']] |
| 71 |
(picking_1 | picking_2).move_ids.picked = True |
| 72 |
(picking_1 | picking_2).button_validate() |
| 73 |
|
| 74 |
og_p1_layers = picking_1.move_ids.stock_valuation_layer_ids |
| 75 |
og_p2_layers = picking_2.move_ids.stock_valuation_layer_ids |
| 76 |
lc_form = Form(self.env['stock.landed.cost']) |
| 77 |
lc_form.picking_ids = (picking_1 | picking_2) |
| 78 |
with lc_form.cost_lines.new() as cost_line: |
| 79 |
cost_line.product_id = self.productlc1 |
| 80 |
cost_line.price_unit = 6 |
| 81 |
lc = lc_form.save() |
| 82 |
lc.compute_landed_cost() |
| 83 |
lc.button_validate() |
| 84 |
for valuation in lc.valuation_adjustment_lines: |
| 85 |
if valuation.cost_line_id.name == 'equal split': |
| 86 |
self.assertEqual(valuation.additional_landed_cost, 5) |
| 87 |
|
| 88 |
|
| 89 |
self.assertEqual(lc.state, "done") |
| 90 |
self.assertTrue(lc.account_move_id) |
| 91 |
self.assertEqual(len(lc.account_move_id.line_ids), 4) |
| 92 |
|
| 93 |
lc_value = sum(lc.account_move_id.line_ids.filtered(lambda aml: aml.account_id.name.startswith('Expenses')).mapped('debit')) |
| 94 |
product_value = abs(self.productlc1.value_svl) |
| 95 |
self.assertEqual(lc_value, product_value) |
| 96 |
lot = self.env['stock.lot'].search([('name', 'ilike', 'LClot')]) |
| 97 |
lot_product_a = lot.filtered(lambda l: l.product_id == self.product1) |
| 98 |
lot_product_b = lot - lot_product_a |
| 99 |
self.assertRecordValues(lc.stock_valuation_layer_ids.sorted(lambda svl: svl.product_id.id), [ |
| 100 |
{'lot_id': lot_product_a[0].id, 'product_id': self.product1.id, 'stock_valuation_layer_id': og_p1_layers[0].id, 'quantity': 0, 'value': 1}, |
| 101 |
{'lot_id': lot_product_a[1].id, 'product_id': self.product1.id, 'stock_valuation_layer_id': og_p1_layers[1].id, 'quantity': 0, 'value': 1}, |
| 102 |
{'lot_id': lot_product_a[2].id, 'product_id': self.product1.id, 'stock_valuation_layer_id': og_p1_layers[2].id, 'quantity': 0, 'value': 1}, |
| 103 |
{'lot_id': lot_product_b[0].id, 'product_id': product2.id, 'stock_valuation_layer_id': og_p2_layers[0].id, 'quantity': 0, 'value': 1.5}, |
| 104 |
{'lot_id': lot_product_b[1].id, 'product_id': product2.id, 'stock_valuation_layer_id': og_p2_layers[1].id, 'quantity': 0, 'value': 1.5}, |
| 105 |
]) |
| 106 |
|
| 107 |
for l, price in zip(lot_product_a, [10.2, 10.2, 10.2]): |
| 108 |
self.assertEqual(l.standard_price, price) |
| 109 |
for l, price in zip(lot_product_b, [11.3, 11.3]): |
| 110 |
self.assertEqual(l.standard_price, price) |
| 111 |
outs = self._make_out_move(self.product1, 9, lot_ids=[lot_product_a[0], lot_product_a[1], lot_product_a[2]]) |
| 112 |
self.assertRecordValues(outs.stock_valuation_layer_ids.sorted('id'), [ |
| 113 |
{'lot_id': lot_product_a[0].id, 'product_id': self.product1.id, 'quantity': -3, 'value': -30.6}, |
| 114 |
{'lot_id': lot_product_a[1].id, 'product_id': self.product1.id, 'quantity': -3, 'value': -30.6}, |
| 115 |
{'lot_id': lot_product_a[2].id, 'product_id': self.product1.id, 'quantity': -3, 'value': -30.6}, |
| 116 |
]) |
| 117 |
|
| 118 |
outs = self._make_out_move(product2, 4, lot_ids=[lot_product_b[0], lot_product_b[1]]) |
| 119 |
self.assertRecordValues(outs.stock_valuation_layer_ids.sorted('id'), [ |
| 120 |
{'lot_id': lot_product_b[0].id, 'product_id': product2.id, 'quantity': -2, 'value': -22.6}, |
| 121 |
{'lot_id': lot_product_b[1].id, 'product_id': product2.id, 'quantity': -2, 'value': -22.6}, |
| 122 |
]) |
| 123 |
|
| 124 |
def test_landed_cost_when_partially_sold(self): |
| 125 |
""" |
| 126 |
check that the landed costs split correctly between lot/ serial numbers |
| 127 |
when some lot/serial number are empty (no share of the landed cost for those) |
| 128 |
or when some have a portion of their quantity already sold (check that it uses the |
| 129 |
remaining quantity) |
| 130 |
""" |
| 131 |
product1 = self.env['product.product'].create({ |
| 132 |
'name': 'product2', |
| 133 |
'is_storable': True, |
| 134 |
'tracking': 'lot', |
| 135 |
'lot_valuated': True, |
| 136 |
'categ_id': self.env.ref('product.product_category_goods').id, |
| 137 |
}) |
| 138 |
product1.categ_id.property_cost_method = 'fifo' |
| 139 |
|
| 140 |
picking_1 = self.env['stock.picking'].create({ |
| 141 |
'picking_type_id': self.env.ref('stock.picking_type_in').id, |
| 142 |
'move_ids': [Command.create({ |
| 143 |
'product_id': product1.id, |
| 144 |
'product_uom_qty': 5, |
| 145 |
'product_uom': self.ref('uom.product_uom_unit'), |
| 146 |
'location_id': self.supplier_location.id, |
| 147 |
'location_dest_id': self.env.ref('stock.stock_location_stock').id, |
| 148 |
'price_unit': 10000, |
| 149 |
})], |
| 150 |
}) |
| 151 |
picking_1.action_confirm() |
| 152 |
|
| 153 |
picking_1.move_ids.move_line_ids = [Command.clear()] + [Command.create({ |
| 154 |
'product_id': product1.id, |
| 155 |
'lot_name': lot_name, |
| 156 |
'quantity': lot_quantity, |
| 157 |
'location_id': self.supplier_location.id, |
| 158 |
'location_dest_id': self.env.ref('stock.stock_location_stock').id, |
| 159 |
}) for (lot_name, lot_quantity) in [('L1', 1), ('L2', 2), ('L3', 1), ('L4', 1)]] |
| 160 |
picking_1.move_ids.picked = True |
| 161 |
picking_1.button_validate() |
| 162 |
|
| 163 |
(lot1, lot2, lot3, lot4) = picking_1.move_ids.move_line_ids.mapped('lot_id').sorted('id') |
| 164 |
self._make_out_move(product1, quantity=2, lot_ids=[lot1, lot2]) |
| 165 |
|
| 166 |
|
| 167 |
lc_form = Form(self.env['stock.landed.cost']) |
| 168 |
lc_form.picking_ids = picking_1 |
| 169 |
with lc_form.cost_lines.new() as cost_line: |
| 170 |
cost_line.product_id = self.productlc1 |
| 171 |
cost_line.price_unit = 5000 |
| 172 |
lc = lc_form.save() |
| 173 |
lc.compute_landed_cost() |
| 174 |
lc.button_validate() |
| 175 |
|
| 176 |
self.assertRecordValues(lc.stock_valuation_layer_ids.sorted('id'), [ |
| 177 |
{'lot_id': lot2.id, 'value': 1000}, |
| 178 |
{'lot_id': lot3.id, 'value': 1000}, |
| 179 |
{'lot_id': lot4.id, 'value': 1000}, |
| 180 |
]) |