| 1 |
|
| 2 |
|
| 3 |
from unittest import skip |
| 4 |
|
| 5 |
from odoo.fields import Command |
| 6 |
from odoo.tests import Form, tagged |
| 7 |
|
| 8 |
from odoo.addons.sale.tests.common import TestSaleCommon |
| 9 |
from odoo.addons.stock_account.tests.test_anglo_saxon_valuation_reconciliation_common import ValuationReconciliationTestCommon |
| 10 |
|
| 11 |
|
| 12 |
@tagged('post_install', '-at_install') |
| 13 |
@skip('Temporary to fast merge new valuation') |
| 14 |
class TestSaleMRPAngloSaxonValuation(TestSaleCommon, ValuationReconciliationTestCommon): |
| 15 |
|
| 16 |
@classmethod |
| 17 |
def setUpClass(cls): |
| 18 |
super().setUpClass() |
| 19 |
|
| 20 |
cls.env.user.company_id.anglo_saxon_accounting = True |
| 21 |
|
| 22 |
@classmethod |
| 23 |
def _create_product(cls, **create_vals): |
| 24 |
if create_vals.get('is_storable'): |
| 25 |
create_vals['categ_id'] = cls.stock_account_product_categ.id |
| 26 |
return super()._create_product(**create_vals) |
| 27 |
|
| 28 |
def test_sale_mrp_kit_bom_cogs(self): |
| 29 |
"""Check invoice COGS aml after selling and delivering a product |
| 30 |
with Kit BoM having another product with Kit BoM as component""" |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
self.component_a = self._create_product(name='Component A', is_storable=True, standard_price=3.00) |
| 48 |
self.component_b = self._create_product(name='Component B', is_storable=True, standard_price=4.00) |
| 49 |
self.component_bb = self._create_product(name='Component BB', is_storable=False, standard_price=5.00) |
| 50 |
self.kit_a = self._create_product(name='Kit A', is_storable=True, standard_price=0.00) |
| 51 |
self.kit_b = self._create_product(name='Kit B', is_storable=False, standard_price=0.00) |
| 52 |
pack_2 = self.env['uom.uom'].create({'name': 'Pack of 2', 'relative_factor': 2, 'relative_uom_id': self.kit_a.uom_id.id}) |
| 53 |
|
| 54 |
self.kit_a.write({ |
| 55 |
'property_account_expense_id': self.company_data['default_account_expense'].id, |
| 56 |
'property_account_income_id': self.company_data['default_account_revenue'].id, |
| 57 |
}) |
| 58 |
|
| 59 |
|
| 60 |
bom_product_form = Form(self.env['mrp.bom']) |
| 61 |
bom_product_form.product_tmpl_id = self.kit_a.product_tmpl_id |
| 62 |
bom_product_form.product_qty = 3.0 |
| 63 |
bom_product_form.type = 'phantom' |
| 64 |
with bom_product_form.bom_line_ids.new() as bom_line: |
| 65 |
bom_line.product_id = self.kit_b |
| 66 |
bom_line.product_qty = 2.0 |
| 67 |
with bom_product_form.bom_line_ids.new() as bom_line: |
| 68 |
bom_line.product_id = self.component_a |
| 69 |
bom_line.product_qty = 1.0 |
| 70 |
self.bom_a = bom_product_form.save() |
| 71 |
|
| 72 |
|
| 73 |
bom_product_form = Form(self.env['mrp.bom']) |
| 74 |
bom_product_form.product_tmpl_id = self.kit_b.product_tmpl_id |
| 75 |
bom_product_form.product_qty = 10.0 |
| 76 |
bom_product_form.type = 'phantom' |
| 77 |
with bom_product_form.bom_line_ids.new() as bom_line: |
| 78 |
bom_line.product_id = self.component_b |
| 79 |
bom_line.product_qty = 2.0 |
| 80 |
with bom_product_form.bom_line_ids.new() as bom_line: |
| 81 |
bom_line.product_id = self.component_bb |
| 82 |
bom_line.product_qty = 3.0 |
| 83 |
self.bom_b = bom_product_form.save() |
| 84 |
|
| 85 |
so = self.env['sale.order'].create({ |
| 86 |
'partner_id': self.partner_a.id, |
| 87 |
'order_line': [ |
| 88 |
(0, 0, { |
| 89 |
'name': self.kit_a.name, |
| 90 |
'product_id': self.kit_a.id, |
| 91 |
'product_uom_qty': 0.5, |
| 92 |
'product_uom_id': pack_2.id, |
| 93 |
'price_unit': 1, |
| 94 |
'tax_ids': False, |
| 95 |
})], |
| 96 |
}) |
| 97 |
so.action_confirm() |
| 98 |
so.picking_ids.move_ids.write({'quantity': 1, 'picked': True}) |
| 99 |
so.picking_ids.button_validate() |
| 100 |
|
| 101 |
invoice = so.with_context(default_journal_id=self.company_data['default_journal_sale'].id)._create_invoices() |
| 102 |
invoice.action_post() |
| 103 |
|
| 104 |
|
| 105 |
amls = invoice.line_ids |
| 106 |
self.assertEqual(len(amls), 4) |
| 107 |
stock_out_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_stock_out']) |
| 108 |
self.assertEqual(stock_out_aml.debit, 0) |
| 109 |
self.assertAlmostEqual(stock_out_aml.credit, 1.53, msg="Should not include the value of consumable component") |
| 110 |
cogs_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_expense']) |
| 111 |
self.assertAlmostEqual(cogs_aml.debit, 1.53, msg="Should not include the value of consumable component") |
| 112 |
self.assertEqual(cogs_aml.credit, 0) |
| 113 |
|
| 114 |
def test_sale_mrp_anglo_saxon_variant(self): |
| 115 |
"""Test the price unit of kit with variants""" |
| 116 |
|
| 117 |
|
| 118 |
self.env.company.currency_id = self.env.ref('base.USD') |
| 119 |
|
| 120 |
|
| 121 |
self.prod_att_1 = self.env['product.attribute'].create({'name': 'Color'}) |
| 122 |
self.prod_attr1_v1 = self.env['product.attribute.value'].create({'name': 'red', 'attribute_id': self.prod_att_1.id, 'sequence': 1}) |
| 123 |
self.prod_attr1_v2 = self.env['product.attribute.value'].create({'name': 'blue', 'attribute_id': self.prod_att_1.id, 'sequence': 2}) |
| 124 |
|
| 125 |
|
| 126 |
self.product_template = self.env['product.template'].create({ |
| 127 |
'name': 'Product Template', |
| 128 |
'is_storable': True, |
| 129 |
'uom_id': self.uom_unit.id, |
| 130 |
'invoice_policy': 'delivery', |
| 131 |
'categ_id': self.stock_account_product_categ.id, |
| 132 |
'attribute_line_ids': [(0, 0, { |
| 133 |
'attribute_id': self.prod_att_1.id, |
| 134 |
'value_ids': [(6, 0, [self.prod_attr1_v1.id, self.prod_attr1_v2.id])] |
| 135 |
})] |
| 136 |
}) |
| 137 |
|
| 138 |
|
| 139 |
self.pt_attr1_v1 = self.product_template.attribute_line_ids[0].product_template_value_ids[0] |
| 140 |
self.pt_attr1_v2 = self.product_template.attribute_line_ids[0].product_template_value_ids[1] |
| 141 |
self.variant_1 = self.product_template._get_variant_for_combination(self.pt_attr1_v1) |
| 142 |
self.variant_2 = self.product_template._get_variant_for_combination(self.pt_attr1_v2) |
| 143 |
|
| 144 |
def create_simple_bom_for_product(product, name, price): |
| 145 |
component = self.env['product.product'].create({ |
| 146 |
'name': 'Component ' + name, |
| 147 |
'is_storable': True, |
| 148 |
'uom_id': self.uom_unit.id, |
| 149 |
'categ_id': self.stock_account_product_categ.id, |
| 150 |
'standard_price': price |
| 151 |
}) |
| 152 |
self.env['stock.quant'].sudo().create({ |
| 153 |
'product_id': component.id, |
| 154 |
'location_id': self.company_data['default_warehouse'].lot_stock_id.id, |
| 155 |
'quantity': 10.0, |
| 156 |
}) |
| 157 |
bom = self.env['mrp.bom'].create({ |
| 158 |
'product_tmpl_id': self.product_template.id, |
| 159 |
'product_id': product.id, |
| 160 |
'product_qty': 1.0, |
| 161 |
'type': 'phantom' |
| 162 |
}) |
| 163 |
self.env['mrp.bom.line'].create({ |
| 164 |
'product_id': component.id, |
| 165 |
'product_qty': 1.0, |
| 166 |
'bom_id': bom.id |
| 167 |
}) |
| 168 |
|
| 169 |
create_simple_bom_for_product(self.variant_1, "V1", 20) |
| 170 |
create_simple_bom_for_product(self.variant_2, "V2", 10) |
| 171 |
|
| 172 |
def create_post_sale_order(product): |
| 173 |
so_vals = { |
| 174 |
'partner_id': self.partner_a.id, |
| 175 |
'partner_invoice_id': self.partner_a.id, |
| 176 |
'partner_shipping_id': self.partner_a.id, |
| 177 |
'order_line': [(0, 0, { |
| 178 |
'name': product.name, |
| 179 |
'product_id': product.id, |
| 180 |
'product_uom_qty': 2, |
| 181 |
'price_unit': product.list_price |
| 182 |
})], |
| 183 |
'company_id': self.company_data['company'].id, |
| 184 |
} |
| 185 |
so = self.env['sale.order'].create(so_vals) |
| 186 |
|
| 187 |
so.action_confirm() |
| 188 |
|
| 189 |
pick = so.picking_ids |
| 190 |
|
| 191 |
pick.button_validate() |
| 192 |
|
| 193 |
so._create_invoices() |
| 194 |
invoice = so.invoice_ids |
| 195 |
invoice.action_post() |
| 196 |
return invoice |
| 197 |
|
| 198 |
|
| 199 |
self.invoice_1 = create_post_sale_order(self.variant_1) |
| 200 |
self.invoice_2 = create_post_sale_order(self.variant_2) |
| 201 |
|
| 202 |
def check_cogs_entry_values(invoice, expected_value): |
| 203 |
aml = invoice.line_ids |
| 204 |
aml_expense = aml.filtered(lambda l: l.display_type == 'cogs' and l.debit > 0) |
| 205 |
aml_output = aml.filtered(lambda l: l.display_type == 'cogs' and l.credit > 0) |
| 206 |
self.assertEqual(aml_expense.debit, expected_value, "Cost of Good Sold entry missing or mismatching for variant") |
| 207 |
self.assertEqual(aml_output.credit, expected_value, "Cost of Good Sold entry missing or mismatching for variant") |
| 208 |
|
| 209 |
|
| 210 |
check_cogs_entry_values(self.invoice_1, 40) |
| 211 |
|
| 212 |
check_cogs_entry_values(self.invoice_2, 20) |
| 213 |
|
| 214 |
def test_anglo_saxo_return_and_credit_note(self): |
| 215 |
""" |
| 216 |
When posting a credit note for a returned kit, the value of the anglo-saxo lines |
| 217 |
should be based on the returned component's value |
| 218 |
""" |
| 219 |
self.stock_account_product_categ.property_cost_method = 'fifo' |
| 220 |
|
| 221 |
kit = self._create_product(name='Simple Kit', is_storable=True, standard_price=0) |
| 222 |
component = self._create_product(name='Compo A', is_storable=True, standard_price=0) |
| 223 |
kit.property_account_expense_id = self.company_data['default_account_expense'] |
| 224 |
|
| 225 |
self.env['mrp.bom'].create({ |
| 226 |
'product_tmpl_id': kit.product_tmpl_id.id, |
| 227 |
'product_qty': 1.0, |
| 228 |
'type': 'phantom', |
| 229 |
'bom_line_ids': [(0, 0, {'product_id': component.id, 'product_qty': 1.0})] |
| 230 |
}) |
| 231 |
|
| 232 |
|
| 233 |
in_moves = self.env['stock.move'].create([{ |
| 234 |
'product_id': component.id, |
| 235 |
'location_id': self.env.ref('stock.stock_location_suppliers').id, |
| 236 |
'location_dest_id': self.company_data['default_warehouse'].lot_stock_id.id, |
| 237 |
'product_uom': component.uom_id.id, |
| 238 |
'product_uom_qty': 1, |
| 239 |
'price_unit': p, |
| 240 |
} for p in [10, 20, 60]]) |
| 241 |
in_moves._action_confirm() |
| 242 |
in_moves.write({'quantity': 1, 'picked': True}) |
| 243 |
in_moves._action_done() |
| 244 |
|
| 245 |
|
| 246 |
so = self.env['sale.order'].create({ |
| 247 |
'partner_id': self.env['res.partner'].create({'name': 'Test Partner'}).id, |
| 248 |
'order_line': [ |
| 249 |
(0, 0, { |
| 250 |
'name': kit.name, |
| 251 |
'product_id': kit.id, |
| 252 |
'product_uom_qty': 3.0, |
| 253 |
'price_unit': 100, |
| 254 |
'tax_ids': False, |
| 255 |
})], |
| 256 |
}) |
| 257 |
so.action_confirm() |
| 258 |
|
| 259 |
|
| 260 |
pickings = [] |
| 261 |
picking = so.picking_ids |
| 262 |
while picking: |
| 263 |
pickings.append(picking) |
| 264 |
picking.move_ids.write({'quantity': 1, 'picked': True}) |
| 265 |
action = picking.button_validate() |
| 266 |
if isinstance(action, dict): |
| 267 |
Form.from_action(self.env, action).save().process() |
| 268 |
picking = picking.backorder_ids |
| 269 |
|
| 270 |
invoice = so._create_invoices() |
| 271 |
invoice.action_post() |
| 272 |
|
| 273 |
|
| 274 |
in_moves = self.env['stock.move'].create({ |
| 275 |
'product_id': component.id, |
| 276 |
'location_id': self.env.ref('stock.stock_location_suppliers').id, |
| 277 |
'location_dest_id': self.company_data['default_warehouse'].lot_stock_id.id, |
| 278 |
'product_uom': component.uom_id.id, |
| 279 |
'product_uom_qty': 1, |
| 280 |
'price_unit': 100, |
| 281 |
}) |
| 282 |
in_moves._action_confirm() |
| 283 |
in_moves.write({'quantity': 1, 'picked': True}) |
| 284 |
in_moves._action_done() |
| 285 |
|
| 286 |
|
| 287 |
ctx = {'active_id': pickings[1].id, 'active_model': 'stock.picking'} |
| 288 |
return_wizard = Form(self.env['stock.return.picking'].with_context(ctx)).save() |
| 289 |
return_wizard.product_return_moves.quantity = 1 |
| 290 |
return_picking = return_wizard._create_return() |
| 291 |
return_picking.move_ids.write({'quantity': 1, 'picked': True}) |
| 292 |
return_picking.button_validate() |
| 293 |
|
| 294 |
|
| 295 |
ctx = {'active_model': 'account.move', 'active_ids': invoice.ids} |
| 296 |
refund_wizard = self.env['account.move.reversal'].with_context(ctx).create({ |
| 297 |
'journal_id': invoice.journal_id.id, |
| 298 |
}) |
| 299 |
action = refund_wizard.refund_moves() |
| 300 |
reverse_invoice = self.env['account.move'].browse(action['res_id']) |
| 301 |
with Form(reverse_invoice) as reverse_invoice_form: |
| 302 |
with reverse_invoice_form.invoice_line_ids.edit(0) as line: |
| 303 |
line.quantity = 1 |
| 304 |
reverse_invoice.action_post() |
| 305 |
|
| 306 |
amls = reverse_invoice.line_ids |
| 307 |
stock_out_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_stock_out']) |
| 308 |
self.assertEqual(stock_out_aml.debit, 20, 'Should be to the value of the returned component') |
| 309 |
self.assertEqual(stock_out_aml.credit, 0) |
| 310 |
cogs_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_expense']) |
| 311 |
self.assertEqual(cogs_aml.debit, 0) |
| 312 |
self.assertEqual(cogs_aml.credit, 20, 'Should be to the value of the returned component') |
| 313 |
|
| 314 |
def test_anglo_saxo_return_and_create_invoice(self): |
| 315 |
""" |
| 316 |
When creating an invoice for a returned kit, the value of the anglo-saxo lines |
| 317 |
should be based on the returned component's value |
| 318 |
""" |
| 319 |
self.stock_account_product_categ.property_cost_method = 'fifo' |
| 320 |
|
| 321 |
kit = self._create_product(name='Simple Kit', is_storable=True, standard_price=0) |
| 322 |
component = self._create_product(name='Compo A', is_storable=True, standard_price=0) |
| 323 |
(kit + component).invoice_policy = 'delivery' |
| 324 |
kit.property_account_expense_id = self.company_data['default_account_expense'] |
| 325 |
|
| 326 |
self.env['mrp.bom'].create({ |
| 327 |
'product_tmpl_id': kit.product_tmpl_id.id, |
| 328 |
'product_qty': 1.0, |
| 329 |
'type': 'phantom', |
| 330 |
'bom_line_ids': [(0, 0, {'product_id': component.id, 'product_qty': 1.0})] |
| 331 |
}) |
| 332 |
|
| 333 |
|
| 334 |
in_moves = self.env['stock.move'].create([{ |
| 335 |
'product_id': component.id, |
| 336 |
'location_id': self.env.ref('stock.stock_location_suppliers').id, |
| 337 |
'location_dest_id': self.company_data['default_warehouse'].lot_stock_id.id, |
| 338 |
'product_uom': component.uom_id.id, |
| 339 |
'product_uom_qty': 1, |
| 340 |
'price_unit': p, |
| 341 |
} for p in [10, 20, 60]]) |
| 342 |
in_moves._action_confirm() |
| 343 |
in_moves.write({'quantity': 1, 'picked': True}) |
| 344 |
in_moves._action_done() |
| 345 |
|
| 346 |
|
| 347 |
so = self.env['sale.order'].create({ |
| 348 |
'partner_id': self.env['res.partner'].create({'name': 'Test Partner'}).id, |
| 349 |
'order_line': [ |
| 350 |
(0, 0, { |
| 351 |
'name': kit.name, |
| 352 |
'product_id': kit.id, |
| 353 |
'product_uom_qty': 3.0, |
| 354 |
'price_unit': 100, |
| 355 |
'tax_ids': False, |
| 356 |
})], |
| 357 |
}) |
| 358 |
so.action_confirm() |
| 359 |
|
| 360 |
|
| 361 |
pickings = [] |
| 362 |
picking = so.picking_ids |
| 363 |
while picking: |
| 364 |
pickings.append(picking) |
| 365 |
picking.move_ids.write({'quantity': 1, 'picked': True}) |
| 366 |
action = picking.button_validate() |
| 367 |
if isinstance(action, dict): |
| 368 |
Form.from_action(self.env, action).save().process() |
| 369 |
picking = picking.backorder_ids |
| 370 |
|
| 371 |
invoice = so._create_invoices() |
| 372 |
invoice.action_post() |
| 373 |
|
| 374 |
|
| 375 |
in_moves = self.env['stock.move'].create({ |
| 376 |
'product_id': component.id, |
| 377 |
'location_id': self.env.ref('stock.stock_location_suppliers').id, |
| 378 |
'location_dest_id': self.company_data['default_warehouse'].lot_stock_id.id, |
| 379 |
'product_uom': component.uom_id.id, |
| 380 |
'product_uom_qty': 1, |
| 381 |
'price_unit': 100, |
| 382 |
}) |
| 383 |
in_moves._action_confirm() |
| 384 |
in_moves.write({'quantity': 1, 'picked': True}) |
| 385 |
in_moves._action_done() |
| 386 |
|
| 387 |
|
| 388 |
ctx = {'active_id': pickings[1].id, 'active_model': 'stock.picking'} |
| 389 |
return_wizard = Form(self.env['stock.return.picking'].with_context(ctx)).save() |
| 390 |
return_wizard.product_return_moves.quantity = 1 |
| 391 |
return_picking = return_wizard._create_return() |
| 392 |
return_picking.move_ids.write({'quantity': 1, 'picked': True}) |
| 393 |
return_picking.button_validate() |
| 394 |
|
| 395 |
|
| 396 |
ctx = {'active_model': 'sale.order', 'active_ids': so.ids} |
| 397 |
create_invoice_wizard = self.env['sale.advance.payment.inv'].with_context(ctx).create( |
| 398 |
{'advance_payment_method': 'delivered'}) |
| 399 |
create_invoice_wizard.create_invoices() |
| 400 |
reverse_invoice = so.invoice_ids[-1] |
| 401 |
with Form(reverse_invoice) as reverse_invoice_form: |
| 402 |
with reverse_invoice_form.invoice_line_ids.edit(0) as line: |
| 403 |
line.quantity = 1 |
| 404 |
reverse_invoice.action_post() |
| 405 |
|
| 406 |
amls = reverse_invoice.line_ids |
| 407 |
stock_out_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_stock_out']) |
| 408 |
self.assertEqual(stock_out_aml.debit, 20, 'Should be to the value of the returned component') |
| 409 |
self.assertEqual(stock_out_aml.credit, 0) |
| 410 |
cogs_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_expense']) |
| 411 |
self.assertEqual(cogs_aml.debit, 0) |
| 412 |
self.assertEqual(cogs_aml.credit, 20, 'Should be to the value of the returned component') |
| 413 |
|
| 414 |
def test_kit_avco_fully_owned_and_delivered_invoice_post_delivery(self): |
| 415 |
self.stock_account_product_categ.property_cost_method = 'average' |
| 416 |
|
| 417 |
compo01 = self._create_product(name='Compo 01', is_storable=True, standard_price=10) |
| 418 |
compo02 = self._create_product(name='Compo 02', is_storable=True, standard_price=20) |
| 419 |
kit = self._create_product(name='Kit', is_storable=True, standard_price=0) |
| 420 |
|
| 421 |
(compo01 + compo02 + kit).invoice_policy = 'delivery' |
| 422 |
|
| 423 |
self.env['stock.quant']._update_available_quantity(compo01, self.company_data['default_warehouse'].lot_stock_id, 1, owner_id=self.partner_b) |
| 424 |
self.env['stock.quant']._update_available_quantity(compo02, self.company_data['default_warehouse'].lot_stock_id, 1, owner_id=self.partner_b) |
| 425 |
|
| 426 |
self.env['mrp.bom'].create({ |
| 427 |
'product_id': kit.id, |
| 428 |
'product_tmpl_id': kit.product_tmpl_id.id, |
| 429 |
'product_uom_id': kit.uom_id.id, |
| 430 |
'product_qty': 1.0, |
| 431 |
'type': 'phantom', |
| 432 |
'bom_line_ids': [ |
| 433 |
(0, 0, {'product_id': compo01.id, 'product_qty': 1.0}), |
| 434 |
(0, 0, {'product_id': compo02.id, 'product_qty': 1.0}), |
| 435 |
], |
| 436 |
}) |
| 437 |
|
| 438 |
so = self.env['sale.order'].create({ |
| 439 |
'partner_id': self.partner_a.id, |
| 440 |
'order_line': [ |
| 441 |
(0, 0, { |
| 442 |
'name': kit.name, |
| 443 |
'product_id': kit.id, |
| 444 |
'product_uom_qty': 1.0, |
| 445 |
'price_unit': 5, |
| 446 |
'tax_ids': False, |
| 447 |
})], |
| 448 |
}) |
| 449 |
so.action_confirm() |
| 450 |
so.picking_ids.move_ids.write({'quantity': 1, 'picked': True}) |
| 451 |
so.picking_ids.button_validate() |
| 452 |
|
| 453 |
invoice = so._create_invoices() |
| 454 |
invoice.action_post() |
| 455 |
|
| 456 |
|
| 457 |
amls = invoice.line_ids |
| 458 |
self.assertRecordValues(amls, [ |
| 459 |
|
| 460 |
{'account_id': self.company_data['default_account_revenue'].id, 'debit': 0, 'credit': 5}, |
| 461 |
{'account_id': self.company_data['default_account_receivable'].id, 'debit': 5, 'credit': 0}, |
| 462 |
]) |
| 463 |
|
| 464 |
def test_kit_avco_partially_owned_and_delivered_invoice_post_delivery(self): |
| 465 |
self.stock_account_product_categ.property_cost_method = 'average' |
| 466 |
|
| 467 |
compo01 = self._create_product(name='Compo 01', is_storable=True, standard_price=10) |
| 468 |
compo02 = self._create_product(name='Compo 02', is_storable=True, standard_price=20) |
| 469 |
kit = self._create_product(name='Kit', is_storable=True, standard_price=0) |
| 470 |
|
| 471 |
(compo01 + compo02 + kit).invoice_policy = 'delivery' |
| 472 |
|
| 473 |
self.env['stock.quant']._update_available_quantity(compo01, self.company_data['default_warehouse'].lot_stock_id, 1, owner_id=self.partner_b) |
| 474 |
self.env['stock.quant']._update_available_quantity(compo01, self.company_data['default_warehouse'].lot_stock_id, 1) |
| 475 |
self.env['stock.quant']._update_available_quantity(compo02, self.company_data['default_warehouse'].lot_stock_id, 1, owner_id=self.partner_b) |
| 476 |
self.env['stock.quant']._update_available_quantity(compo02, self.company_data['default_warehouse'].lot_stock_id, 1) |
| 477 |
|
| 478 |
self.env['mrp.bom'].create({ |
| 479 |
'product_id': kit.id, |
| 480 |
'product_tmpl_id': kit.product_tmpl_id.id, |
| 481 |
'product_uom_id': kit.uom_id.id, |
| 482 |
'product_qty': 1.0, |
| 483 |
'type': 'phantom', |
| 484 |
'bom_line_ids': [ |
| 485 |
(0, 0, {'product_id': compo01.id, 'product_qty': 1.0}), |
| 486 |
(0, 0, {'product_id': compo02.id, 'product_qty': 1.0}), |
| 487 |
], |
| 488 |
}) |
| 489 |
|
| 490 |
so = self.env['sale.order'].create({ |
| 491 |
'partner_id': self.partner_a.id, |
| 492 |
'order_line': [ |
| 493 |
(0, 0, { |
| 494 |
'name': kit.name, |
| 495 |
'product_id': kit.id, |
| 496 |
'product_uom_qty': 2.0, |
| 497 |
'price_unit': 5, |
| 498 |
'tax_ids': False, |
| 499 |
})], |
| 500 |
}) |
| 501 |
so.action_confirm() |
| 502 |
so.picking_ids.move_line_ids.quantity = 1 |
| 503 |
so.picking_ids.move_ids.picked = True |
| 504 |
so.picking_ids.button_validate() |
| 505 |
|
| 506 |
invoice = so._create_invoices() |
| 507 |
invoice.action_post() |
| 508 |
|
| 509 |
|
| 510 |
amls = invoice.line_ids |
| 511 |
self.assertRecordValues(amls, [ |
| 512 |
|
| 513 |
{'account_id': self.company_data['default_account_revenue'].id, 'debit': 0, 'credit': 10}, |
| 514 |
{'account_id': self.company_data['default_account_receivable'].id, 'debit': 10, 'credit': 0}, |
| 515 |
{'account_id': self.company_data['default_account_stock_out'].id, 'debit': 0, 'credit': 30}, |
| 516 |
{'account_id': self.company_data['default_account_expense'].id, 'debit': 30, 'credit': 0}, |
| 517 |
]) |
| 518 |
|
| 519 |
def test_anglo_saxo_kit_subkits(self): |
| 520 |
"""Check invoice COGS aml after selling and delivering a product |
| 521 |
with Kit BoM producing 2 times the product and having |
| 522 |
2 products with Kit BoM as components""" |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
component_a = self._create_product(name='Component A', is_storable=True, standard_price=10.00) |
| 546 |
component_b = self._create_product(name='Component B', is_storable=True, standard_price=6.00) |
| 547 |
subkit_a = self._create_product(name='Subkit A', is_storable=True, standard_price=0.00) |
| 548 |
subkit_b = self._create_product(name='Subkit B', is_storable=True, standard_price=0.00) |
| 549 |
main_kit = self._create_product(name='Main kit', is_storable=True, standard_price=0.00) |
| 550 |
|
| 551 |
main_kit.write({ |
| 552 |
'property_account_expense_id': self.company_data['default_account_expense'].id, |
| 553 |
'property_account_income_id': self.company_data['default_account_revenue'].id, |
| 554 |
}) |
| 555 |
|
| 556 |
|
| 557 |
self.env['mrp.bom'].create({ |
| 558 |
'product_id': main_kit.id, |
| 559 |
'product_tmpl_id': main_kit.product_tmpl_id.id, |
| 560 |
'product_qty': 4.0, |
| 561 |
'type': 'phantom', |
| 562 |
'bom_line_ids': [ |
| 563 |
(0, 0, {'product_id': subkit_a.id, 'product_qty': 1.0}), |
| 564 |
(0, 0, {'product_id': subkit_b.id, 'product_qty': 1.0}), |
| 565 |
], |
| 566 |
}) |
| 567 |
|
| 568 |
self.env['mrp.bom'].create({ |
| 569 |
'product_id': subkit_a.id, |
| 570 |
'product_tmpl_id': subkit_a.product_tmpl_id.id, |
| 571 |
'product_qty': 1.0, |
| 572 |
'type': 'phantom', |
| 573 |
'bom_line_ids': [ |
| 574 |
(0, 0, {'product_id': component_a.id, 'product_qty': 2.0}), |
| 575 |
], |
| 576 |
}) |
| 577 |
|
| 578 |
self.env['mrp.bom'].create({ |
| 579 |
'product_id': subkit_b.id, |
| 580 |
'product_tmpl_id': subkit_b.product_tmpl_id.id, |
| 581 |
'product_qty': 1.0, |
| 582 |
'type': 'phantom', |
| 583 |
'bom_line_ids': [ |
| 584 |
(0, 0, {'product_id': component_b.id, 'product_qty': 2.0}), |
| 585 |
], |
| 586 |
}) |
| 587 |
|
| 588 |
so = self.env['sale.order'].create({ |
| 589 |
'partner_id': self.partner_a.id, |
| 590 |
'order_line': [ |
| 591 |
(0, 0, { |
| 592 |
'name': main_kit.name, |
| 593 |
'product_id': main_kit.id, |
| 594 |
'product_uom_qty': 1.0, |
| 595 |
'price_unit': 1, |
| 596 |
'tax_ids': False, |
| 597 |
})], |
| 598 |
}) |
| 599 |
so.action_confirm() |
| 600 |
for move in so.picking_ids.move_ids: |
| 601 |
move.quantity = move.product_uom_qty |
| 602 |
so.picking_ids.button_validate() |
| 603 |
|
| 604 |
invoice = so.with_context(default_journal_id=self.company_data['default_journal_sale'].id)._create_invoices() |
| 605 |
invoice.action_post() |
| 606 |
|
| 607 |
|
| 608 |
amls = invoice.line_ids |
| 609 |
self.assertEqual(len(amls), 4) |
| 610 |
stock_out_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_stock_out']) |
| 611 |
self.assertEqual(stock_out_aml.debit, 0) |
| 612 |
self.assertAlmostEqual(stock_out_aml.credit, 8.00, msg="Should include include the components from all subkits, with the price adapted for 1 Main kit") |
| 613 |
cogs_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_expense']) |
| 614 |
self.assertAlmostEqual(cogs_aml.debit, 8.00, msg="Should include include the components from all subkits, with the price adapted for 1 Main kit") |
| 615 |
self.assertEqual(cogs_aml.credit, 0) |
| 616 |
|
| 617 |
def test_sell_kit_invoice_before_delivery(self): |
| 618 |
""" When a kit product is invoiced prior to delivery, we want to make sure to reconcile all |
| 619 |
the AMLs from its explosion together, else we risk re-reconciliation attempts (which will |
| 620 |
block certain actions from being performed altogether). |
| 621 |
""" |
| 622 |
self.stock_account_product_categ.property_cost_method = 'average' |
| 623 |
|
| 624 |
compo01 = self._create_product(name="Compo 01", is_storable=True, standard_price=10) |
| 625 |
compo02 = self._create_product(name="Compo 02", is_storable=True, standard_price=20) |
| 626 |
kit = self._create_product(name="Kit", is_storable=True, standard_price=30) |
| 627 |
(compo01 + compo02 + kit).write({'invoice_policy': 'order'}) |
| 628 |
warehouse = self.company_data['default_warehouse'] |
| 629 |
self.env['stock.quant']._update_available_quantity(compo01, warehouse.lot_stock_id, 1.0) |
| 630 |
self.env['stock.quant']._update_available_quantity(compo02, warehouse.lot_stock_id, 2.0) |
| 631 |
self.env['mrp.bom'].create({ |
| 632 |
'type': 'phantom', |
| 633 |
'product_id': kit.id, |
| 634 |
'product_tmpl_id': kit.product_tmpl_id.id, |
| 635 |
'product_qty': 1, |
| 636 |
'bom_line_ids': [ |
| 637 |
Command.create({'product_id': compo01.id, 'product_qty': 1}), |
| 638 |
Command.create({'product_id': compo02.id, 'product_qty': 1}), |
| 639 |
], |
| 640 |
}) |
| 641 |
sale_order = self.env['sale.order'].create({ |
| 642 |
'partner_id': self.partner_a.id, |
| 643 |
'order_line': [ |
| 644 |
Command.create({ |
| 645 |
'product_id': kit.id, |
| 646 |
'product_uom_qty': 1, |
| 647 |
'price_unit': 10, |
| 648 |
}), |
| 649 |
Command.create({ |
| 650 |
'product_id': compo02.id, |
| 651 |
'product_uom_qty': 1, |
| 652 |
'price_unit': 5, |
| 653 |
}), |
| 654 |
], |
| 655 |
}) |
| 656 |
sale_order.action_confirm() |
| 657 |
invoice = sale_order.with_context(default_journal_id=self.company_data['default_journal_sale'].id)._create_invoices() |
| 658 |
invoice.action_post() |
| 659 |
delivery = sale_order.picking_ids |
| 660 |
|
| 661 |
delivery.button_validate() |
| 662 |
stock_output_amls = self.env['account.move.line'].search([('account_id', '=', self.company_data['default_account_stock_out'].id)], order='id asc') |
| 663 |
self.assertRecordValues(stock_output_amls, |
| 664 |
[ |
| 665 |
{'product_id': kit.id, 'reconciled': True, 'debit': 0.0, 'credit': 30.0}, |
| 666 |
{'product_id': compo02.id, 'reconciled': True, 'debit': 0.0, 'credit': 20.0}, |
| 667 |
{'product_id': compo01.id, 'reconciled': True, 'debit': 10.0, 'credit': 0.0}, |
| 668 |
{'product_id': compo02.id, 'reconciled': True, 'debit': 20.0, 'credit': 0.0}, |
| 669 |
{'product_id': compo02.id, 'reconciled': True, 'debit': 20.0, 'credit': 0.0}, |
| 670 |
] |
| 671 |
) |