| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 3 |
|
| 4 |
from odoo.addons.sale_mrp.tests import test_sale_mrp_flow |
| 5 |
from odoo.fields import Command |
| 6 |
from odoo.tests import common, Form |
| 7 |
|
| 8 |
|
| 9 |
@common.tagged('post_install', '-at_install') |
| 10 |
class TestSaleMrpFlow(test_sale_mrp_flow.TestSaleMrpFlowCommon): |
| 11 |
|
| 12 |
def test_kit_cost_calculation(self): |
| 13 |
""" Check that the average cost price is computed correctly after SO confirmation: |
| 14 |
BOM 1: |
| 15 |
- 1 unit of “super kit”: |
| 16 |
- 2 units of “component a” |
| 17 |
BOM 2: |
| 18 |
- 1 unit of “component a”: |
| 19 |
- 3 units of "component b" |
| 20 |
1 unit of "component b" = $10 |
| 21 |
1 unit of "super kit" = 2 * 3 * $10 = *$60 |
| 22 |
""" |
| 23 |
super_kit = self._cls_create_product('Super Kit', self.uom_unit) |
| 24 |
(super_kit + self.component_a + self.component_b).categ_id.property_cost_method = 'average' |
| 25 |
self.env['mrp.bom'].create({ |
| 26 |
'product_tmpl_id': self.component_a.product_tmpl_id.id, |
| 27 |
'product_qty': 1.0, |
| 28 |
'type': 'phantom', |
| 29 |
'bom_line_ids': [(0, 0, { |
| 30 |
'product_id': self.component_b.id, |
| 31 |
'product_qty': 3.0, |
| 32 |
})] |
| 33 |
}) |
| 34 |
self.env['mrp.bom'].create({ |
| 35 |
'product_tmpl_id': super_kit.product_tmpl_id.id, |
| 36 |
'product_qty': 1.0, |
| 37 |
'type': 'phantom', |
| 38 |
'bom_line_ids': [(0, 0, { |
| 39 |
'product_id': self.component_a.id, |
| 40 |
'product_qty': 2.0, |
| 41 |
})] |
| 42 |
}) |
| 43 |
self.component_b.standard_price = 10 |
| 44 |
self.component_a.button_bom_cost() |
| 45 |
super_kit.button_bom_cost() |
| 46 |
so_form = Form(self.env['sale.order']) |
| 47 |
so_form.partner_id = self.partner_a |
| 48 |
with so_form.order_line.new() as line: |
| 49 |
line.product_id = super_kit |
| 50 |
so = so_form.save() |
| 51 |
self.assertEqual(so.order_line.purchase_price, 60) |
| 52 |
so.action_confirm() |
| 53 |
self.assertEqual(so.order_line.purchase_price, 60) |
| 54 |
|
| 55 |
def test_kit_cost_calculation_2(self): |
| 56 |
""" Check that the average cost price is computed correctly after receipt validation: |
| 57 |
Lovely KIT BOM for 10: |
| 58 |
- 1O unit of Kit $50 |
| 59 |
- 10 units of “component b” $10 |
| 60 |
-> $60 per Lovely Kit |
| 61 |
SUB KIT BOM: |
| 62 |
- 1 units of “component a” 1 x $30 = $30 |
| 63 |
- 2 units of “component b” 2 x $10 = $20 |
| 64 |
-> $50 per SUB Kit |
| 65 |
""" |
| 66 |
sub_kit, kit = self._cls_create_product('Sub Kit', self.uom_unit), self._cls_create_product('Lovely Kit', self.uom_ten) |
| 67 |
kit.uom_ids = [Command.set([self.uom_unit.id, self.uom_ten.id])] |
| 68 |
self.product_category.property_cost_method = 'average' |
| 69 |
(kit + sub_kit + self.component_a + self.component_b).categ_id = self.product_category |
| 70 |
self.env['mrp.bom'].create([ |
| 71 |
{ |
| 72 |
'product_tmpl_id': sub_kit.product_tmpl_id.id, |
| 73 |
'product_qty': 1.0, |
| 74 |
'type': 'phantom', |
| 75 |
'bom_line_ids': [ |
| 76 |
Command.create({ |
| 77 |
'product_id': self.component_a.id, |
| 78 |
'product_qty': 1.0, |
| 79 |
}), |
| 80 |
Command.create({ |
| 81 |
'product_id': self.component_b.id, |
| 82 |
'product_qty': 2.0, |
| 83 |
}), |
| 84 |
], |
| 85 |
}, |
| 86 |
{ |
| 87 |
'product_tmpl_id': kit.product_tmpl_id.id, |
| 88 |
'product_uom_id': kit.uom_id.id, |
| 89 |
'product_qty': 1.0, |
| 90 |
'type': 'phantom', |
| 91 |
'bom_line_ids': [ |
| 92 |
Command.create({ |
| 93 |
'product_id': sub_kit.id, |
| 94 |
'product_qty': 10.0, |
| 95 |
}), |
| 96 |
Command.create({ |
| 97 |
'product_id': self.component_b.id, |
| 98 |
'product_qty': 10.0, |
| 99 |
}), |
| 100 |
], |
| 101 |
}, |
| 102 |
]) |
| 103 |
self.component_a.standard_price = 30 |
| 104 |
self.component_b.standard_price = 10 |
| 105 |
sub_kit.action_bom_cost() |
| 106 |
kit.action_bom_cost() |
| 107 |
so = self.env['sale.order'].create({ |
| 108 |
'partner_id': self.partner_a.id, |
| 109 |
'order_line': [Command.create({ |
| 110 |
'product_id': kit.id, |
| 111 |
'product_uom_qty': 3, |
| 112 |
'product_uom_id': self.uom_ten.id |
| 113 |
})] |
| 114 |
}) |
| 115 |
self.assertEqual(so.order_line.purchase_price, 600) |
| 116 |
so.action_confirm() |
| 117 |
self.assertEqual(so.order_line.purchase_price, 600) |
| 118 |
for move in so.picking_ids.move_ids: |
| 119 |
move.quantity = move.product_uom_qty |
| 120 |
self.assertRecordValues(so.order_line, [{'purchase_price': 600, 'qty_delivered': 0.0}]) |
| 121 |
so.picking_ids.button_validate() |
| 122 |
self.assertEqual(so.picking_ids.state, 'done') |
| 123 |
self.assertRecordValues(so.order_line, [{'purchase_price': 600, 'qty_delivered': 3.0}]) |
| 124 |
self.assertEqual(so.order_line.purchase_price, 600) |