| 1 |
|
| 2 |
|
| 3 |
from unittest import skip |
| 4 |
|
| 5 |
from odoo.addons.stock_account.tests.test_anglo_saxon_valuation_reconciliation_common import ValuationReconciliationTestCommon |
| 6 |
from odoo.tests import tagged, Form |
| 7 |
|
| 8 |
|
| 9 |
@tagged('post_install', '-at_install') |
| 10 |
@skip('Temporary to fast merge new valuation') |
| 11 |
class TestStockLandedCostsMrp(ValuationReconciliationTestCommon): |
| 12 |
|
| 13 |
@classmethod |
| 14 |
def setUpClass(cls): |
| 15 |
super(TestStockLandedCostsMrp, cls).setUpClass() |
| 16 |
|
| 17 |
cls.supplier_id = cls.env['res.partner'].create({'name': 'My Test Supplier'}).id |
| 18 |
cls.customer_id = cls.env['res.partner'].create({'name': 'My Test Customer'}).id |
| 19 |
cls.picking_type_in_id = cls.env.ref('stock.picking_type_in') |
| 20 |
cls.picking_type_out_id = cls.env.ref('stock.picking_type_out') |
| 21 |
cls.supplier_location_id = cls.env.ref('stock.stock_location_suppliers') |
| 22 |
cls.stock_location_id = cls.company_data['default_warehouse'].lot_stock_id |
| 23 |
cls.customer_location_id = cls.env.ref('stock.stock_location_customers') |
| 24 |
|
| 25 |
cls.product_component1 = cls.env['product.product'].create({ |
| 26 |
'name': 'Component1', |
| 27 |
'is_storable': True, |
| 28 |
'standard_price': 1.0, |
| 29 |
}) |
| 30 |
cls.product_component2 = cls.env['product.product'].create({ |
| 31 |
'name': 'Component2', |
| 32 |
'is_storable': True, |
| 33 |
'standard_price': 2.0, |
| 34 |
}) |
| 35 |
cls.product_refrigerator = cls.env['product.product'].create({ |
| 36 |
'name': 'Refrigerator', |
| 37 |
'is_storable': True, |
| 38 |
'categ_id': cls.env.ref('product.product_category_goods').id, |
| 39 |
}) |
| 40 |
cls.uom_unit = cls.env.ref('uom.product_uom_unit') |
| 41 |
cls.bom_refri = cls.env['mrp.bom'].create({ |
| 42 |
'product_id': cls.product_refrigerator.id, |
| 43 |
'product_tmpl_id': cls.product_refrigerator.product_tmpl_id.id, |
| 44 |
'product_uom_id': cls.uom_unit.id, |
| 45 |
'product_qty': 1.0, |
| 46 |
'type': 'normal', |
| 47 |
}) |
| 48 |
cls.bom_refri_line1 = cls.env['mrp.bom.line'].create({ |
| 49 |
'bom_id': cls.bom_refri.id, |
| 50 |
'product_id': cls.product_component1.id, |
| 51 |
'product_qty': 3, |
| 52 |
}) |
| 53 |
cls.bom_refri_line2 = cls.env['mrp.bom.line'].create({ |
| 54 |
'bom_id': cls.bom_refri.id, |
| 55 |
'product_id': cls.product_component2.id, |
| 56 |
'product_qty': 1, |
| 57 |
}) |
| 58 |
|
| 59 |
cls.warehouse_1 = cls.env['stock.warehouse'].create({ |
| 60 |
'name': 'Base Warehouse', |
| 61 |
'reception_steps': 'one_step', |
| 62 |
'delivery_steps': 'ship_only', |
| 63 |
'code': 'BWH'}) |
| 64 |
|
| 65 |
cls.product_refrigerator.categ_id.property_cost_method = 'fifo' |
| 66 |
cls.product_refrigerator.categ_id.property_valuation = 'real_time' |
| 67 |
|
| 68 |
|
| 69 |
cls.landed_cost = cls.env['product.product'].create({ |
| 70 |
'name': 'Landed Cost', |
| 71 |
'type': 'service', |
| 72 |
'categ_id': cls.env.ref('product.product_category_services').id, |
| 73 |
}) |
| 74 |
cls.allow_user = cls.env['res.users'].with_context({'no_reset_password': True}).create({ |
| 75 |
'name': "Adviser", |
| 76 |
'login': "fm", |
| 77 |
'email': "[email protected]", |
| 78 |
'group_ids': [(6, 0, [cls.env.ref('account.group_account_manager').id, cls.env.ref('mrp.group_mrp_user').id, cls.env.ref('stock.group_stock_manager').id])] |
| 79 |
}) |
| 80 |
|
| 81 |
def test_landed_cost_on_mrp(self): |
| 82 |
|
| 83 |
quants = self.env['stock.quant'].with_context(inventory_mode=True).create({ |
| 84 |
'product_id': self.product_component1.id, |
| 85 |
'inventory_quantity': 500, |
| 86 |
'location_id': self.warehouse_1.lot_stock_id.id, |
| 87 |
}) |
| 88 |
quants |= self.env['stock.quant'].with_context(inventory_mode=True).create({ |
| 89 |
'product_id': self.product_component2.id, |
| 90 |
'inventory_quantity': 500, |
| 91 |
'location_id': self.warehouse_1.lot_stock_id.id, |
| 92 |
}) |
| 93 |
quants.action_apply_inventory() |
| 94 |
|
| 95 |
man_order_form = Form(self.env['mrp.production'].with_user(self.allow_user)) |
| 96 |
man_order_form.product_id = self.product_refrigerator |
| 97 |
man_order_form.bom_id = self.bom_refri |
| 98 |
man_order_form.product_qty = 2.0 |
| 99 |
man_order = man_order_form.save() |
| 100 |
|
| 101 |
self.assertEqual(man_order.state, 'draft', "Production order should be in draft state.") |
| 102 |
man_order.action_confirm() |
| 103 |
self.assertEqual(man_order.state, 'confirmed', "Production order should be in confirmed state.") |
| 104 |
|
| 105 |
|
| 106 |
production_move = man_order.move_finished_ids |
| 107 |
self.assertEqual(production_move.product_id, self.product_refrigerator) |
| 108 |
|
| 109 |
first_move = man_order.move_raw_ids.filtered(lambda move: move.product_id == self.product_component1) |
| 110 |
self.assertEqual(first_move.product_qty, 6.0) |
| 111 |
first_move = man_order.move_raw_ids.filtered(lambda move: move.product_id == self.product_component2) |
| 112 |
self.assertEqual(first_move.product_qty, 2.0) |
| 113 |
|
| 114 |
|
| 115 |
mo_form = Form(man_order.with_user(self.allow_user)) |
| 116 |
mo_form.qty_producing = 2 |
| 117 |
man_order = mo_form.save() |
| 118 |
man_order.button_mark_done() |
| 119 |
|
| 120 |
landed_cost = Form(self.env['stock.landed.cost'].with_user(self.allow_user)).save() |
| 121 |
landed_cost.target_model = 'manufacturing' |
| 122 |
|
| 123 |
|
| 124 |
self.assertTrue(man_order in self.env['mrp.production'].search([ |
| 125 |
('move_finished_ids.stock_valuation_layer_ids', '!=', False), ('company_id', '=', landed_cost.company_id.id)])) |
| 126 |
|
| 127 |
landed_cost.mrp_production_ids = [(6, 0, [man_order.id])] |
| 128 |
landed_cost.cost_lines = [(0, 0, {'product_id': self.landed_cost.id, 'price_unit': 5.0, 'split_method': 'equal'})] |
| 129 |
landed_cost.button_validate() |
| 130 |
|
| 131 |
self.assertEqual(landed_cost.state, 'done') |
| 132 |
self.assertTrue(landed_cost.account_move_id) |
| 133 |
|
| 134 |
self.assertEqual(len(landed_cost.stock_valuation_layer_ids), 1) |
| 135 |
self.assertEqual(landed_cost.stock_valuation_layer_ids.product_id, self.product_refrigerator) |
| 136 |
self.assertEqual(landed_cost.stock_valuation_layer_ids.value, 5.0) |
| 137 |
|
| 138 |
def test_landed_cost_on_mrp_02(self): |
| 139 |
""" |
| 140 |
Test that a user who has manager access to stock can create and validate a landed cost linked |
| 141 |
to a Manufacturing order without the need for MRP access |
| 142 |
""" |
| 143 |
|
| 144 |
stock_manager = self.env['res.users'].with_context({'no_reset_password': True}).create({ |
| 145 |
'name': "Stock Manager", |
| 146 |
'login': "test", |
| 147 |
'email': "[email protected]", |
| 148 |
'group_ids': [(6, 0, [self.env.ref('stock.group_stock_manager').id])] |
| 149 |
}) |
| 150 |
|
| 151 |
self.env['stock.quant']._update_available_quantity(self.product_component1, self.warehouse_1.lot_stock_id, 10) |
| 152 |
self.env['stock.quant']._update_available_quantity(self.product_component2, self.warehouse_1.lot_stock_id, 10) |
| 153 |
|
| 154 |
|
| 155 |
man_order_form = Form(self.env['mrp.production'].with_user(self.allow_user)) |
| 156 |
man_order_form.product_id = self.product_refrigerator |
| 157 |
man_order_form.bom_id = self.bom_refri |
| 158 |
man_order_form.product_qty = 1.0 |
| 159 |
man_order = man_order_form.save() |
| 160 |
man_order.action_confirm() |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
man_order_form = Form(man_order) |
| 165 |
man_order_form.qty_producing = 1 |
| 166 |
man_order_form.save() |
| 167 |
man_order.button_mark_done() |
| 168 |
|
| 169 |
|
| 170 |
landed_cost = Form(self.env['stock.landed.cost'].with_user(stock_manager)).save() |
| 171 |
landed_cost.target_model = 'manufacturing' |
| 172 |
|
| 173 |
|
| 174 |
self.assertTrue(man_order in self.env['mrp.production'].search([ |
| 175 |
('move_finished_ids.stock_valuation_layer_ids', '!=', False), ('company_id', '=', landed_cost.company_id.id)])) |
| 176 |
landed_cost.mrp_production_ids = [(6, 0, [man_order.id])] |
| 177 |
|
| 178 |
|
| 179 |
landed_cost.with_user(stock_manager).button_validate() |
| 180 |
self.assertEqual(landed_cost.state, 'done') |
| 181 |
|
| 182 |
def test_landed_cost_on_mrp_03(self): |
| 183 |
""" |
| 184 |
Do not apply landed costs to byproducts without cost_share |
| 185 |
""" |
| 186 |
|
| 187 |
byproduct1 = self.env['product.product'].create({ |
| 188 |
'name': 'Byproduct1', |
| 189 |
'is_storable': True, |
| 190 |
'categ_id': self.env.ref('product.product_category_goods').id, |
| 191 |
}) |
| 192 |
byproduct2 = self.env['product.product'].create({ |
| 193 |
'name': 'Byproduct2', |
| 194 |
'is_storable': True, |
| 195 |
'categ_id': self.env.ref('product.product_category_goods').id, |
| 196 |
}) |
| 197 |
self.bom_refri.write({ |
| 198 |
'byproduct_ids': [ |
| 199 |
(0, 0, {'product_id': byproduct1.id, 'product_qty': 1, 'cost_share': 100}), |
| 200 |
(0, 0, {'product_id': byproduct2.id, 'product_qty': 1, 'cost_share': 0}), |
| 201 |
], |
| 202 |
}) |
| 203 |
man_order_form = Form(self.env['mrp.production'].with_user(self.allow_user)) |
| 204 |
man_order_form.product_id = self.product_refrigerator |
| 205 |
man_order_form.bom_id = self.bom_refri |
| 206 |
man_order_form.product_qty = 2.0 |
| 207 |
man_order = man_order_form.save() |
| 208 |
man_order.action_confirm() |
| 209 |
|
| 210 |
mo_form = Form(man_order.with_user(self.allow_user)) |
| 211 |
mo_form.qty_producing = 2 |
| 212 |
man_order = mo_form.save() |
| 213 |
man_order.button_mark_done() |
| 214 |
|
| 215 |
landed_cost = Form(self.env['stock.landed.cost'].with_user(self.allow_user)).save() |
| 216 |
landed_cost.target_model = 'manufacturing' |
| 217 |
landed_cost.mrp_production_ids = [(6, 0, [man_order.id])] |
| 218 |
landed_cost.cost_lines = [(0, 0, {'product_id': self.landed_cost.id, 'price_unit': 5.0, 'split_method': 'equal'})] |
| 219 |
landed_cost.compute_landed_cost() |
| 220 |
|
| 221 |
|
| 222 |
self.assertFalse(byproduct2 in landed_cost.valuation_adjustment_lines.product_id) |