| 1 |
|
| 2 |
|
| 3 |
from odoo import Command, fields |
| 4 |
from odoo.tests import Form, tagged |
| 5 |
from odoo.tools.float_utils import float_round, float_compare |
| 6 |
|
| 7 |
from odoo.addons.mrp_account.tests.common import TestBomPriceCommon |
| 8 |
from odoo.addons.mrp_subcontracting.tests.common import TestMrpSubcontractingCommon |
| 9 |
from odoo.addons.stock_account.tests.common import TestStockValuationCommon |
| 10 |
|
| 11 |
|
| 12 |
@tagged('post_install', '-at_install') |
| 13 |
class TestAccountSubcontractingFlows(TestMrpSubcontractingCommon, TestStockValuationCommon): |
| 14 |
@classmethod |
| 15 |
def setUpClass(cls): |
| 16 |
super().setUpClass() |
| 17 |
cls.account_production = cls.env['account.account'].create({ |
| 18 |
'name': 'Production Account', |
| 19 |
'code': '100102', |
| 20 |
'account_type': 'asset_current', |
| 21 |
}) |
| 22 |
cls.prod_location = cls.env['stock.location'].search([('usage', '=', 'production'), ('company_id', '=', cls.company.id)]) |
| 23 |
cls.prod_location.valuation_account_id = cls.account_production.id |
| 24 |
cls.comp1.standard_price = 10.0 |
| 25 |
cls.comp2.standard_price = 20.0 |
| 26 |
|
| 27 |
def test_subcontracting_account_flow_1(self): |
| 28 |
(self.comp1 | self.comp2 | self.finished).categ_id = self.category_fifo_auto |
| 29 |
self._make_in_move(self.comp1, 10, unit_cost=10, location_dest_id=self.env.company.subcontracting_location_id.id) |
| 30 |
self._make_in_move(self.comp2, 10, unit_cost=20, location_dest_id=self.env.company.subcontracting_location_id.id) |
| 31 |
all_amls_ids = self.env['account.move.line'].search([]).ids |
| 32 |
|
| 33 |
move = self._make_in_move( |
| 34 |
self.finished, |
| 35 |
1, |
| 36 |
unit_cost=30, |
| 37 |
create_picking=True, |
| 38 |
partner_id=self.subcontractor_partner1.id, |
| 39 |
) |
| 40 |
|
| 41 |
picking_receipt = move.picking_id |
| 42 |
mo1 = move.picking_id._get_subcontract_production() |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
self.assertEqual(mo1.move_finished_ids.value, 60) |
| 51 |
self.assertEqual(picking_receipt.move_ids.value, 0) |
| 52 |
self.assertEqual(picking_receipt.move_ids.product_id.total_value, 60) |
| 53 |
|
| 54 |
amls = self.env['account.move.line'].search([('id', 'not in', all_amls_ids)]) |
| 55 |
all_amls_ids += amls.ids |
| 56 |
|
| 57 |
self.assertRecordValues(amls.sorted('product_id'), [ |
| 58 |
|
| 59 |
{'account_id': self.account_stock_valuation.id, 'product_id': self.comp1.id, 'debit': 0.0, 'credit': 10.0}, |
| 60 |
{'account_id': self.account_production.id, 'product_id': self.comp1.id, 'debit': 10.0, 'credit': 0.0}, |
| 61 |
|
| 62 |
{'account_id': self.account_stock_valuation.id, 'product_id': self.comp2.id, 'debit': 0.0, 'credit': 20.0}, |
| 63 |
{'account_id': self.account_production.id, 'product_id': self.comp2.id, 'debit': 20.0, 'credit': 0.0}, |
| 64 |
|
| 65 |
{'account_id': self.account_production.id, 'product_id': self.finished.id, 'debit': 0.0, 'credit': 30.0}, |
| 66 |
{'account_id': self.account_stock_valuation.id, 'product_id': self.finished.id, 'debit': 30.0, 'credit': 0.0}, |
| 67 |
]) |
| 68 |
|
| 69 |
|
| 70 |
scrap = self.env['stock.scrap'].create({ |
| 71 |
'product_id': self.finished.id, |
| 72 |
'product_uom_id': self.finished.uom_id.id, |
| 73 |
'scrap_qty': 1, |
| 74 |
'production_id': mo1.id, |
| 75 |
'location_id': self.stock_location.id, |
| 76 |
}) |
| 77 |
scrap.do_scrap() |
| 78 |
|
| 79 |
self.assertEqual(self.finished.total_value, 0) |
| 80 |
|
| 81 |
def test_subcontracting_account_backorder(self): |
| 82 |
""" This test uses tracked (serial and lot) component and tracked (serial) finished product |
| 83 |
The original subcontracting production order will be split into 4 backorders. This test |
| 84 |
ensure the extra cost asked from the subcontractor is added correctly on all the finished |
| 85 |
product valuation layer. Not only the first one. """ |
| 86 |
todo_nb = 4 |
| 87 |
self.comp2.tracking = 'lot' |
| 88 |
self.comp1.tracking = 'serial' |
| 89 |
self.comp2.standard_price = 100 |
| 90 |
self.finished.tracking = 'serial' |
| 91 |
self.finished.categ_id = self.category_fifo |
| 92 |
|
| 93 |
picking_receipt = self.env['stock.picking'].create({ |
| 94 |
'picking_type_id': self.picking_type_in.id, |
| 95 |
'partner_id': self.subcontractor_partner1.id, |
| 96 |
'move_ids': [Command.create({ |
| 97 |
'product_id': self.finished.id, |
| 98 |
'product_uom_qty': todo_nb, |
| 99 |
'price_unit': 50, |
| 100 |
})], |
| 101 |
}) |
| 102 |
picking_receipt.action_confirm() |
| 103 |
picking_receipt.do_unreserve() |
| 104 |
|
| 105 |
lot_comp2 = self.env['stock.lot'].create({ |
| 106 |
'name': 'lot_comp2', |
| 107 |
'product_id': self.comp2.id, |
| 108 |
}) |
| 109 |
serials_finished = [] |
| 110 |
serials_comp1 = [] |
| 111 |
for i in range(todo_nb): |
| 112 |
serials_finished.append(self.env['stock.lot'].create({ |
| 113 |
'name': 'serial_fin_%s' % i, |
| 114 |
'product_id': self.finished.id, |
| 115 |
})) |
| 116 |
serials_comp1.append(self.env['stock.lot'].create({ |
| 117 |
'name': 'serials_comp1_%s' % i, |
| 118 |
'product_id': self.comp1.id, |
| 119 |
})) |
| 120 |
|
| 121 |
action = picking_receipt.move_ids.action_show_details() |
| 122 |
with Form(picking_receipt.move_ids.with_context(action['context']), view=action['view_id']) as move_form: |
| 123 |
for serial in serials_finished: |
| 124 |
with move_form.move_line_ids.new() as move_line: |
| 125 |
move_line.lot_id = serial |
| 126 |
move_line.picked = True |
| 127 |
move_line.quantity = 1 |
| 128 |
move_form.save() |
| 129 |
|
| 130 |
for mo, compo_1_serial in zip(picking_receipt._get_subcontract_production(), serials_comp1): |
| 131 |
action = mo.move_raw_ids[0].action_show_details() |
| 132 |
with Form(mo.move_raw_ids[0].with_context(action['context']), view=action['view_id']) as move_form: |
| 133 |
with move_form.move_line_ids.new() as move_line: |
| 134 |
self.assertEqual(move_line.product_id, self.comp1) |
| 135 |
move_line.lot_id = compo_1_serial |
| 136 |
move_line.picked = True |
| 137 |
move_line.quantity = 1 |
| 138 |
move_form.save() |
| 139 |
action = mo.move_raw_ids[1].action_show_details() |
| 140 |
with Form(mo.move_raw_ids[1].with_context(action['context']), view=action['view_id']) as move_form: |
| 141 |
with move_form.move_line_ids.new() as move_line: |
| 142 |
self.assertEqual(move_line.product_id, self.comp2) |
| 143 |
move_line.lot_id = lot_comp2 |
| 144 |
move_line.picked = True |
| 145 |
move_line.quantity = 1 |
| 146 |
move_form.save() |
| 147 |
|
| 148 |
|
| 149 |
picking_receipt.move_ids.picked = True |
| 150 |
picking_receipt.button_validate() |
| 151 |
self.assertEqual(picking_receipt.state, 'done') |
| 152 |
|
| 153 |
f_move = self.env['stock.move'].search([( |
| 154 |
'product_id', '=', self.finished.id, |
| 155 |
)]) |
| 156 |
self.assertEqual(len(f_move), 5) |
| 157 |
self.assertRecordValues(f_move, [ |
| 158 |
{'is_in': False, 'value': 0, 'quantity': 4}, |
| 159 |
{'is_in': True, 'value': 160, 'quantity': 1}, |
| 160 |
{'is_in': True, 'value': 160, 'quantity': 1}, |
| 161 |
{'is_in': True, 'value': 160, 'quantity': 1}, |
| 162 |
{'is_in': True, 'value': 160, 'quantity': 1}, |
| 163 |
]) |
| 164 |
|
| 165 |
def test_tracked_compo_and_backorder(self): |
| 166 |
""" |
| 167 |
Suppose a subcontracted product P with two tracked components, P is FIFO |
| 168 |
Create a receipt for 10 x P, receive 5, then 3 and then 2 |
| 169 |
""" |
| 170 |
self.product_category.property_cost_method = 'fifo' |
| 171 |
self.comp1.tracking = 'lot' |
| 172 |
self.comp1.standard_price = 10 |
| 173 |
self.comp2.tracking = 'lot' |
| 174 |
self.comp2.standard_price = 20 |
| 175 |
|
| 176 |
lot01, lot02 = self.env['stock.lot'].create([{ |
| 177 |
'name': "Lot of %s" % product.name, |
| 178 |
'product_id': product.id, |
| 179 |
} for product in (self.comp1, self.comp2)]) |
| 180 |
|
| 181 |
receipt = self.env['stock.picking'].create({ |
| 182 |
'picking_type_id': self.picking_type_in.id, |
| 183 |
'partner_id': self.subcontractor_partner1.id, |
| 184 |
'move_ids': [Command.create({ |
| 185 |
'product_id': self.finished.id, |
| 186 |
'product_uom_qty': 10, |
| 187 |
'price_unit': 50, |
| 188 |
})], |
| 189 |
}) |
| 190 |
receipt.action_confirm() |
| 191 |
|
| 192 |
for qty_producing in (5, 3, 2): |
| 193 |
receipt.move_ids.quantity = qty_producing |
| 194 |
mo = receipt._get_subcontract_production() |
| 195 |
action = mo.move_raw_ids[0].action_show_details() |
| 196 |
with Form(mo.move_raw_ids[0].with_context(action['context']), view=action['view_id']) as move_form: |
| 197 |
with move_form.move_line_ids.new() as move_line: |
| 198 |
move_line.lot_id = lot01 |
| 199 |
move_line.quantity = qty_producing |
| 200 |
move_form.save() |
| 201 |
action = mo.move_raw_ids[1].action_show_details() |
| 202 |
with Form(mo.move_raw_ids[1].with_context(action['context']), view=action['view_id']) as move_form: |
| 203 |
with move_form.move_line_ids.new() as move_line: |
| 204 |
move_line.lot_id = lot02 |
| 205 |
move_line.quantity = qty_producing |
| 206 |
move_form.save() |
| 207 |
action = receipt.button_validate() |
| 208 |
if isinstance(action, dict): |
| 209 |
Form.from_action(self.env, action).save().process() |
| 210 |
receipt = receipt.backorder_ids |
| 211 |
|
| 212 |
f_move = self.env['stock.move'].search([( |
| 213 |
'product_id', '=', self.finished.id, |
| 214 |
)]) |
| 215 |
self.assertRecordValues(f_move, [ |
| 216 |
{'quantity': 5, 'value': 0, 'state': 'done'}, |
| 217 |
{'quantity': 5, 'value': 5 * (10 + 20 + 50), 'state': 'done'}, |
| 218 |
{'quantity': 3, 'value': 0, 'state': 'done'}, |
| 219 |
{'quantity': 3, 'value': 3 * (10 + 20 + 50), 'state': 'done'}, |
| 220 |
{'quantity': 2, 'value': 0, 'state': 'done'}, |
| 221 |
{'quantity': 2, 'value': 2 * (10 + 20 + 50), 'state': 'done'}, |
| 222 |
]) |
| 223 |
|
| 224 |
def test_subcontract_cost_different_when_standard_price(self): |
| 225 |
"""Test when subcontracting with standard price when |
| 226 |
Final product cost != Components cost + Subcontracting cost |
| 227 |
When posting the account entries for receiving final product, the |
| 228 |
subcontracting cost will be adjusted based on the difference of the cost. |
| 229 |
""" |
| 230 |
(self.comp1 | self.comp2 | self.finished).categ_id = self.category_standard_auto |
| 231 |
self.comp1.standard_price = 10 |
| 232 |
self.comp2.standard_price = 20 |
| 233 |
self.finished.standard_price = 40 |
| 234 |
|
| 235 |
all_amls_ids = self.env['account.move.line'].search([]).ids |
| 236 |
|
| 237 |
self._make_in_move(self.finished, 1, unit_cost=15, |
| 238 |
create_picking=True, |
| 239 |
partner_id=self.subcontractor_partner1.id) |
| 240 |
|
| 241 |
amls = self.env['account.move.line'].search([('id', 'not in', all_amls_ids)]) |
| 242 |
self.assertRecordValues(amls.sorted("account_id, product_id"), [ |
| 243 |
|
| 244 |
{'account_id': self.account_production.id, 'product_id': self.comp1.id, 'debit': 10.0, 'credit': 0.0}, |
| 245 |
{'account_id': self.account_production.id, 'product_id': self.comp2.id, 'debit': 20.0, 'credit': 0.0}, |
| 246 |
{'account_id': self.account_production.id, 'product_id': self.finished.id, 'debit': 0.0, 'credit': 40.0}, |
| 247 |
{'account_id': self.account_stock_valuation.id, 'product_id': self.comp1.id, 'debit': 0.0, 'credit': 10.0}, |
| 248 |
{'account_id': self.account_stock_valuation.id, 'product_id': self.comp2.id, 'debit': 0.0, 'credit': 20.0}, |
| 249 |
{'account_id': self.account_stock_valuation.id, 'product_id': self.finished.id, 'debit': 40.0, 'credit': 0.0}, |
| 250 |
]) |
| 251 |
|
| 252 |
def test_subcontract_without_prod_account(self): |
| 253 |
""" |
| 254 |
Test that the production stock account is optional, and we will fallback on input/output accounts. |
| 255 |
""" |
| 256 |
(self.comp1 | self.comp2 | self.finished).categ_id = self.category_fifo_auto |
| 257 |
self.comp1.standard_price = 1.0 |
| 258 |
self.prod_location.valuation_account_id = False |
| 259 |
all_amls_ids = self.env['account.move.line'].search([]).ids |
| 260 |
self._make_in_move(self.finished, 1, create_picking=True, |
| 261 |
partner_id=self.subcontractor_partner1.id).picking_id |
| 262 |
|
| 263 |
amls = self.env['account.move.line'].search([('id', 'not in', all_amls_ids)]) |
| 264 |
|
| 265 |
self.assertFalse(amls) |
| 266 |
|
| 267 |
|
| 268 |
class TestSubcontractingBOMCost(TestBomPriceCommon): |
| 269 |
@classmethod |
| 270 |
def setUpClass(cls): |
| 271 |
super().setUpClass() |
| 272 |
cls.partner = cls.env['res.partner'].create({ |
| 273 |
'name': 'A name can be a Many2one...' |
| 274 |
}) |
| 275 |
(cls.bom_1 | cls.bom_2).write({ |
| 276 |
'type': 'subcontract', |
| 277 |
'subcontractor_ids': [Command.link(cls.partner.id)], |
| 278 |
}) |
| 279 |
|
| 280 |
def test_01_compute_price_subcontracting_cost(self): |
| 281 |
"""Test calculation of bom cost with subcontracting.""" |
| 282 |
suppliers = self.env['product.supplierinfo'].create([ |
| 283 |
{ |
| 284 |
'partner_id': self.partner.id, |
| 285 |
'product_tmpl_id': self.dining_table.product_tmpl_id.id, |
| 286 |
'price': 150.0, |
| 287 |
}, { |
| 288 |
'partner_id': self.partner.id, |
| 289 |
'product_tmpl_id': self.table_head.product_tmpl_id.id, |
| 290 |
'price': 120.0, |
| 291 |
'product_uom_id': self.dozen.id, |
| 292 |
}, |
| 293 |
]) |
| 294 |
self.assertEqual(suppliers.mapped('is_subcontractor'), [True, True]) |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
self.assertEqual(self.dining_table.standard_price, 1000, "Initial price of the Product should be 1000") |
| 307 |
self.dining_table.button_bom_cost() |
| 308 |
self.assertEqual(float_round(self.dining_table.standard_price, precision_digits=2), 700.0, "After computing price from BoM price should be 700") |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
self.assertEqual(self.table_head.standard_price, 300, "Initial price of the Product should be 300") |
| 322 |
self.Product.browse([self.dining_table.id, self.table_head.id]).action_bom_cost() |
| 323 |
self.assertEqual(float_compare(self.table_head.standard_price, 478.75, precision_digits=2), 0, "After computing price from BoM price should be 878.75") |
| 324 |
self.assertEqual(float_compare(self.dining_table.standard_price, 878.75, precision_digits=2), 0, "After computing price from BoM price should be 878.75") |
| 325 |
|
| 326 |
def test_02_compute_price_subcontracting_cost(self): |
| 327 |
"""Test calculation of bom cost with subcontracting and supplier in different currency.""" |
| 328 |
currency_a = self.env['res.currency'].create({ |
| 329 |
'name': 'ZEN', |
| 330 |
'symbol': 'Z', |
| 331 |
'rounding': 0.01, |
| 332 |
'currency_unit_label': 'Zenny', |
| 333 |
'rate_ids': [Command.create({ |
| 334 |
'name': fields.Date.today(), |
| 335 |
'company_rate': 0.5, |
| 336 |
})], |
| 337 |
}) |
| 338 |
|
| 339 |
self.env['product.supplierinfo'].create([{ |
| 340 |
'partner_id': self.partner.id, |
| 341 |
'product_tmpl_id': self.dining_table.product_tmpl_id.id, |
| 342 |
'price': 120.0, |
| 343 |
'currency_id': currency_a.id, |
| 344 |
}]) |
| 345 |
self.assertEqual(self.dining_table.standard_price, 1000) |
| 346 |
self.dining_table.button_bom_cost() |
| 347 |
self.assertEqual(self.dining_table.standard_price, 790) |