1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.fields import Date, Datetime
from odoo.tools import mute_logger
from odoo.tests import Form, tagged
from odoo.addons.account.tests.common import AccountTestInvoicingCommon
from odoo.addons.stock_account.tests.test_stockvaluation import _create_accounting_data
@tagged('post_install', '-at_install')
class TestAngloSaxonValuationPurchaseMRP(AccountTestInvoicingCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.vendor01 = cls.env['res.partner'].create({'name': "Super Vendor"})
cls.stock_input_account, cls.stock_output_account, cls.stock_valuation_account, cls.expense_account, cls.stock_journal = _create_accounting_data(cls.env)
cls.avco_category = cls.env['product.category'].create({
'name': 'AVCO',
'property_cost_method': 'average',
'property_valuation': 'real_time',
'property_stock_account_input_categ_id': cls.stock_input_account.id,
'property_stock_account_output_categ_id': cls.stock_output_account.id,
'property_stock_journal': cls.stock_journal.id,
'property_stock_valuation_account_id': cls.stock_valuation_account.id,
})
currency_grp = cls.env.ref('base.group_multi_currency')
cls.env.user.write({'groups_id': [(4, currency_grp.id)]})
cls.env.company.anglo_saxon_accounting = True
def test_kit_anglo_saxo_price_diff(self):
"""
Suppose an automated-AVCO configuration and a Price Difference Account defined on
the product category. When buying a kit of that category at a higher price than its
cost, the difference should be published on the Price Difference Account
"""
kit, compo01, compo02 = self.env['product.product'].create([{
'name': name,
'standard_price': price,
'is_storable': True,
'categ_id': self.avco_category.id,
} for name, price in [('Kit', 0), ('Compo 01', 10), ('Compo 02', 20)]])
self.env['mrp.bom'].create({
'product_tmpl_id': kit.product_tmpl_id.id,
'type': 'phantom',
'bom_line_ids': [(0, 0, {
'product_id': p.id,
'product_qty': 1,
}) for p in [compo01, compo02]]
})
kit.button_bom_cost()
po_form = Form(self.env['purchase.order'])
po_form.partner_id = self.vendor01
with po_form.order_line.new() as pol_form:
pol_form.product_id = kit
pol_form.price_unit = 100
po = po_form.save()
po.button_confirm()
po.picking_ids.button_validate()
action = po.action_create_invoice()
invoice = self.env['account.move'].browse(action['res_id'])
invoice.invoice_date = Date.today()
invoice.action_post()
svls = po.order_line.move_ids.stock_valuation_layer_ids
self.assertEqual(len(svls), 2, "The invoice should have created two SVL (one by kit's component) for the price diff")
self.assertEqual(sum(svls.mapped('value')), 100, "Should be the standard price of both components")
input_amls = self.env['account.move.line'].search([('account_id', '=', self.stock_input_account.id)])
self.assertEqual(sum(input_amls.mapped('balance')), 0)
def test_buy_deliver_and_return_kit_with_auto_avco_components(self):
"""
A kit K with two AVCO components
- C01, cost share 25%
- C02, cost share 75%
K in Units
C01, C02 in Litres
Buy and receive 1 kit @ 100
Deliver the kit
Update the cost shares
Return the delivery
"""
stock_location = self.env['stock.location'].search([
('company_id', '=', self.env.company.id),
('name', '=', 'Stock'),
])
customer_location = self.env.ref('stock.stock_location_customers')
type_out = self.env['stock.picking.type'].search([
('company_id', '=', self.env.company.id),
('name', '=', 'Delivery Orders')])
uom_unit = self.env.ref('uom.product_uom_unit')
uom_litre = self.env.ref('uom.product_uom_litre')
component01, component02 = self.env['product.product'].create([{
'name': 'Component %s' % name,
'is_storable': True,
'categ_id': self.avco_category.id,
'uom_id': uom_litre.id,
'uom_po_id': uom_litre.id,
} for name in ['01', '02']])
kit = self.env['product.product'].create({
'name': 'Super Kit',
'type': 'consu',
'uom_id': uom_unit.id,
'uom_po_id': uom_unit.id,
})
bom_kit = self.env['mrp.bom'].create({
'product_tmpl_id': kit.product_tmpl_id.id,
'type': 'phantom',
'bom_line_ids': [(0, 0, {
'product_id': component01.id,
'product_qty': 1,
'cost_share': 25,
}), (0, 0, {
'product_id': component02.id,
'product_qty': 1,
'cost_share': 75,
})],
})
po_form = Form(self.env['purchase.order'])
po_form.partner_id = self.vendor01
with po_form.order_line.new() as pol_form:
pol_form.product_id = kit
pol_form.price_unit = 100
pol_form.taxes_id.clear()
po = po_form.save()
po.button_confirm()
receipt = po.picking_ids
receipt.move_line_ids.quantity = 1
receipt.button_validate()
self.assertEqual(receipt.state, 'done')
self.assertEqual(receipt.move_line_ids.product_id, component01 | component02)
self.assertEqual(po.order_line.qty_received, 1)
self.assertEqual(component01.stock_valuation_layer_ids.value, 25)
self.assertEqual(component02.stock_valuation_layer_ids.value, 75)
delivery = self.env['stock.picking'].create({
'picking_type_id': type_out.id,
'location_id': stock_location.id,
'location_dest_id': customer_location.id,
'move_ids': [(0, 0, {
'name': kit.name,
'product_id': kit.id,
'product_uom': kit.uom_id.id,
'product_uom_qty': 1.0,
'location_id': stock_location.id,
'location_dest_id': customer_location.id,
})],
})
delivery.action_confirm()
delivery.move_ids.move_line_ids.quantity = 1
delivery.button_validate()
self.assertEqual(component01.stock_valuation_layer_ids.mapped('value'), [25, -25])
self.assertEqual(component02.stock_valuation_layer_ids.mapped('value'), [75, -75])
with mute_logger('odoo.tests.form.onchange'):
with Form(bom_kit) as kit_form:
with kit_form.bom_line_ids.edit(0) as line:
line.cost_share = 30
with kit_form.bom_line_ids.edit(1) as line:
line.cost_share = 70
wizard_form = Form(self.env['stock.return.picking'].with_context(active_id=delivery.id, active_model='stock.picking'))
wizard = wizard_form.save()
wizard.product_return_moves.quantity = 1
action = wizard.action_create_returns()
return_picking = self.env["stock.picking"].browse(action["res_id"])
return_picking.move_ids.move_line_ids.quantity = 1
return_picking.button_validate()
self.assertEqual(component01.stock_valuation_layer_ids.mapped('value'), [25, -25, 25])
self.assertEqual(component02.stock_valuation_layer_ids.mapped('value'), [75, -75, 75])
def test_valuation_multicurrency_with_kits(self):
""" Purchase a Kit in multi-currency and verify that the amount_currency is correctly computed.
"""
# Setup Kit
kit, cmp = self.env['product.product'].create([{
'name': name,
'standard_price': 0,
'is_storable': True,
'categ_id': self.avco_category.id,
} for name in ['Kit', 'Cmp']])
self.env['mrp.bom'].create({
'product_tmpl_id': kit.product_tmpl_id.id,
'type': 'phantom',
'bom_line_ids': [(0, 0, {'product_id': cmp.id, 'product_qty': 5})]
})
# Setup Currency
usd = self.env.ref('base.USD')
eur = self.env.ref('base.EUR')
self.env['res.currency.rate'].create({
'name': Datetime.today(),
'currency_id': usd.id,
'rate': 1})
self.env['res.currency.rate'].create({
'name': Datetime.today(),
'currency_id': eur.id,
'rate': 2})
# Create Purchase
po_form = Form(self.env['purchase.order'])
po_form.partner_id = self.vendor01
po_form.currency_id = eur
with po_form.order_line.new() as pol_form:
pol_form.product_id = kit
pol_form.price_unit = 100 # $50
po = po_form.save()
po.button_confirm()
po.picking_ids.button_validate()
svl = po.order_line.move_ids.stock_valuation_layer_ids.ensure_one()
input_aml = self.env['account.move.line'].search([('account_id', '=', self.stock_valuation_account.id)])
self.assertEqual(svl.value, 50) # USD
self.assertEqual(input_aml.amount_currency, 100) # EUR
self.assertEqual(input_aml.balance, 50) # USD
def test_fifo_cost_adjust_mo_quantity(self):
""" An MO using a FIFO cost method product as a component should not zero-out the std cost
of the product if we unlock it once it is in a validated state and adjust the quantity of
component used to be smaller than originally entered.
"""
self.product_a.categ_id = self.env['product.category'].create({
'name': 'FIFO',
'property_cost_method': 'fifo',
'property_valuation': 'real_time'
})
purchase_order = self.env['purchase.order'].create({
'partner_id': self.partner_a.id,
'order_line': [(0, 0, {
'product_id': self.product_a.id,
'product_qty': 10,
'price_unit': 100,
})],
})
purchase_order.button_confirm()
purchase_order.picking_ids[0].button_validate()
manufacturing_order = self.env['mrp.production'].create({
'product_id': self.product_b.id,
'product_qty': 1,
'move_raw_ids': [(0, 0, {
'product_id': self.product_a.id,
'product_uom_qty': 100,
})],
})
manufacturing_order.action_confirm()
manufacturing_order.move_raw_ids.write({
'quantity': 100,
'picked': True,
})
manufacturing_order.button_mark_done()
manufacturing_order.action_toggle_is_locked()
manufacturing_order.move_raw_ids.quantity = 1
self.assertEqual(self.product_a.standard_price, 100)
|