| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from unittest import skip |
| 5 |
|
| 6 |
from odoo import Command |
| 7 |
from odoo.addons.stock_landed_costs.tests.common import TestStockLandedCostsCommon |
| 8 |
from odoo.fields import Date |
| 9 |
from odoo.tests import tagged, Form |
| 10 |
|
| 11 |
|
| 12 |
@tagged('post_install', '-at_install') |
| 13 |
@skip('Temporary to fast merge new valuation') |
| 14 |
class TestStockLandedCostsRounding(TestStockLandedCostsCommon): |
| 15 |
|
| 16 |
def test_stock_landed_costs_rounding(self): |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
product_uom_unit_round_1 = self.env.ref('uom.product_uom_unit') |
| 21 |
product_uom_unit_round_1.write({ |
| 22 |
'name': 'Undivisible Units', |
| 23 |
}) |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
product_landed_cost_3 = self.env['product.product'].create({ |
| 28 |
'name': "LC product 3", |
| 29 |
'uom_id': product_uom_unit_round_1.id, |
| 30 |
'categ_id': self.env.ref('product.product_category_goods').id, |
| 31 |
}) |
| 32 |
product_landed_cost_3.product_tmpl_id.categ_id.property_cost_method = 'fifo' |
| 33 |
product_landed_cost_3.product_tmpl_id.categ_id.property_stock_account_input_categ_id = self.company_data['default_account_expense'] |
| 34 |
product_landed_cost_3.product_tmpl_id.categ_id.property_stock_account_output_categ_id = self.company_data['default_account_revenue'] |
| 35 |
|
| 36 |
product_landed_cost_4 = self.env['product.product'].create({ |
| 37 |
'name': "LC product 4", |
| 38 |
'uom_id': product_uom_unit_round_1.id, |
| 39 |
'categ_id': self.env.ref('product.product_category_goods').id, |
| 40 |
}) |
| 41 |
product_landed_cost_4.product_tmpl_id.categ_id.property_cost_method = 'fifo' |
| 42 |
product_landed_cost_4.product_tmpl_id.categ_id.property_valuation = 'real_time' |
| 43 |
product_landed_cost_4.product_tmpl_id.categ_id.property_stock_account_input_categ_id = self.company_data['default_account_expense'] |
| 44 |
product_landed_cost_4.product_tmpl_id.categ_id.property_stock_account_output_categ_id = self.company_data['default_account_revenue'] |
| 45 |
|
| 46 |
picking_default_vals = self.env['stock.picking'].default_get(list(self.env['stock.picking'].fields_get())) |
| 47 |
|
| 48 |
|
| 49 |
vals = dict(picking_default_vals, **{ |
| 50 |
'name': 'LC_pick_3', |
| 51 |
'picking_type_id': self.warehouse.in_type_id.id, |
| 52 |
'move_ids': [(0, 0, { |
| 53 |
'product_id': product_landed_cost_3.id, |
| 54 |
'product_uom_qty': 13, |
| 55 |
'product_uom': product_uom_unit_round_1.id, |
| 56 |
'location_id': self.ref('stock.stock_location_customers'), |
| 57 |
'location_dest_id': self.warehouse.lot_stock_id.id, |
| 58 |
})], |
| 59 |
}) |
| 60 |
picking_landed_cost_3 = self.env['stock.picking'].new(vals) |
| 61 |
picking_landed_cost_3._onchange_picking_type() |
| 62 |
vals = picking_landed_cost_3._convert_to_write(picking_landed_cost_3._cache) |
| 63 |
picking_landed_cost_3 = self.env['stock.picking'].create(vals) |
| 64 |
|
| 65 |
vals = dict(picking_default_vals, **{ |
| 66 |
'name': 'LC_pick_4', |
| 67 |
'picking_type_id': self.warehouse.in_type_id.id, |
| 68 |
'move_ids': [(0, 0, { |
| 69 |
'product_id': product_landed_cost_4.id, |
| 70 |
'product_uom_qty': 1, |
| 71 |
'product_uom': self.ref('uom.product_uom_dozen'), |
| 72 |
'location_id': self.ref('stock.stock_location_customers'), |
| 73 |
'location_dest_id': self.warehouse.lot_stock_id.id, |
| 74 |
'price_unit': 17.00 / 12.00, |
| 75 |
})], |
| 76 |
}) |
| 77 |
picking_landed_cost_4 = self.env['stock.picking'].new(vals) |
| 78 |
picking_landed_cost_4._onchange_picking_type() |
| 79 |
vals = picking_landed_cost_4._convert_to_write(picking_landed_cost_4._cache) |
| 80 |
picking_landed_cost_4 = self.env['stock.picking'].create(vals) |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
picking_landed_cost_3.move_ids.price_unit = 1.0 |
| 85 |
picking_landed_cost_3.action_confirm() |
| 86 |
picking_landed_cost_3.action_assign() |
| 87 |
picking_landed_cost_3._action_done() |
| 88 |
|
| 89 |
virtual_interior_design = self.env['product.product'].create({ |
| 90 |
'name': 'Virtual Interior Design', |
| 91 |
'categ_id': self.env.ref('product.product_category_goods').id, |
| 92 |
}) |
| 93 |
|
| 94 |
|
| 95 |
default_vals = self.env['stock.landed.cost'].default_get(list(self.env['stock.landed.cost'].fields_get())) |
| 96 |
default_vals.update({ |
| 97 |
'picking_ids': [picking_landed_cost_3.id], |
| 98 |
'account_journal_id': self.expenses_journal, |
| 99 |
'cost_lines': [(0, 0, {'product_id': virtual_interior_design.id})], |
| 100 |
'valuation_adjustment_lines': [], |
| 101 |
}) |
| 102 |
stock_landed_cost_2 = self.env['stock.landed.cost'].new(default_vals) |
| 103 |
stock_landed_cost_2.cost_lines.onchange_product_id() |
| 104 |
stock_landed_cost_2.cost_lines.name = 'equal split' |
| 105 |
stock_landed_cost_2.cost_lines.split_method = 'equal' |
| 106 |
stock_landed_cost_2.cost_lines.price_unit = 15 |
| 107 |
vals = stock_landed_cost_2._convert_to_write(stock_landed_cost_2._cache) |
| 108 |
stock_landed_cost_2 = self.env['stock.landed.cost'].create(vals) |
| 109 |
|
| 110 |
|
| 111 |
stock_landed_cost_2.compute_landed_cost() |
| 112 |
|
| 113 |
|
| 114 |
for valuation in stock_landed_cost_2.valuation_adjustment_lines: |
| 115 |
self.assertEqual(valuation.additional_landed_cost, 15) |
| 116 |
|
| 117 |
|
| 118 |
stock_landed_cost_2.button_validate() |
| 119 |
|
| 120 |
|
| 121 |
self.assertEqual(stock_landed_cost_2.state, 'done') |
| 122 |
self.assertTrue(stock_landed_cost_2.account_move_id) |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
picking_landed_cost_4.move_ids.price_unit = 17.0/12.0 |
| 127 |
picking_landed_cost_4.action_confirm() |
| 128 |
picking_landed_cost_4.action_assign() |
| 129 |
picking_landed_cost_4._action_done() |
| 130 |
|
| 131 |
|
| 132 |
default_vals = self.env['stock.landed.cost'].default_get(list(self.env['stock.landed.cost'].fields_get())) |
| 133 |
default_vals.update({ |
| 134 |
'picking_ids': [picking_landed_cost_4.id], |
| 135 |
'account_journal_id': self.expenses_journal, |
| 136 |
'cost_lines': [(0, 0, {'product_id': virtual_interior_design.id})], |
| 137 |
'valuation_adjustment_lines': [], |
| 138 |
}) |
| 139 |
stock_landed_cost_3 = self.env['stock.landed.cost'].new(default_vals) |
| 140 |
stock_landed_cost_3.cost_lines.onchange_product_id() |
| 141 |
stock_landed_cost_3.cost_lines.name = 'equal split' |
| 142 |
stock_landed_cost_3.cost_lines.split_method = 'equal' |
| 143 |
stock_landed_cost_3.cost_lines.price_unit = 11 |
| 144 |
vals = stock_landed_cost_3._convert_to_write(stock_landed_cost_3._cache) |
| 145 |
stock_landed_cost_3 = self.env['stock.landed.cost'].create(vals) |
| 146 |
|
| 147 |
|
| 148 |
stock_landed_cost_3.compute_landed_cost() |
| 149 |
|
| 150 |
|
| 151 |
for valuation in stock_landed_cost_3.valuation_adjustment_lines: |
| 152 |
self.assertEqual(valuation.additional_landed_cost, 11) |
| 153 |
|
| 154 |
|
| 155 |
stock_landed_cost_3.button_validate() |
| 156 |
|
| 157 |
|
| 158 |
self.assertEqual(stock_landed_cost_3.state, 'done') |
| 159 |
self.assertTrue(stock_landed_cost_3.account_move_id) |
| 160 |
|
| 161 |
def test_stock_landed_costs_rounding_02(self): |
| 162 |
""" The landed costs should be correctly computed, even when the decimal accuracy |
| 163 |
of the deciaml price is increased. """ |
| 164 |
self.env.ref("product.decimal_price").digits = 4 |
| 165 |
|
| 166 |
fifo_pc = self.env['product.category'].create({ |
| 167 |
'name': 'Fifo Category', |
| 168 |
'property_valuation': 'real_time', |
| 169 |
'property_cost_method': 'fifo', |
| 170 |
}) |
| 171 |
|
| 172 |
products = self.Product.create([{ |
| 173 |
'name': 'Super Product %s' % price, |
| 174 |
'categ_id': fifo_pc.id, |
| 175 |
'is_storable': True, |
| 176 |
'standard_price': price, |
| 177 |
} for price in [0.91, 0.93, 75.17, 20.54]]) |
| 178 |
|
| 179 |
landed_product = self.Product.create({ |
| 180 |
'name': 'Landed Costs', |
| 181 |
'type': 'service', |
| 182 |
'landed_cost_ok': True, |
| 183 |
'split_method_landed_cost': 'by_quantity', |
| 184 |
'standard_price': 1000.0, |
| 185 |
}) |
| 186 |
|
| 187 |
po = self.env['purchase.order'].create({ |
| 188 |
'partner_id': self.partner_a.id, |
| 189 |
'order_line': [(0, 0, { |
| 190 |
'product_id': product.id, |
| 191 |
'product_qty': qty, |
| 192 |
'price_unit': product.standard_price, |
| 193 |
}) for product, qty in zip(products, [6, 6, 3, 6])] |
| 194 |
}) |
| 195 |
po.button_confirm() |
| 196 |
|
| 197 |
po.picking_ids.button_validate() |
| 198 |
|
| 199 |
lc_form = Form(self.LandedCost) |
| 200 |
lc_form.picking_ids.add(po.picking_ids) |
| 201 |
with lc_form.cost_lines.new() as line: |
| 202 |
line.product_id = landed_product |
| 203 |
lc = lc_form.save() |
| 204 |
lc.compute_landed_cost() |
| 205 |
|
| 206 |
self.assertEqual(sum(lc.valuation_adjustment_lines.mapped('additional_landed_cost')), 1000.0) |
| 207 |
|
| 208 |
def test_stock_landed_costs_rounding_03(self): |
| 209 |
""" |
| 210 |
Storable AVCO product |
| 211 |
Receive: |
| 212 |
5 @ 5 |
| 213 |
5 @ 8 |
| 214 |
5 @ 7 |
| 215 |
20 @ 7.33 |
| 216 |
Add landed cost of $5 to each receipt (except the first one) |
| 217 |
Deliver: |
| 218 |
23 |
| 219 |
2 |
| 220 |
10 |
| 221 |
At the end, the SVL value should be zero |
| 222 |
""" |
| 223 |
self.product_a.is_storable = True |
| 224 |
self.product_a.categ_id.property_cost_method = 'average' |
| 225 |
|
| 226 |
stock_location = self.warehouse.lot_stock_id |
| 227 |
supplier_location_id = self.ref('stock.stock_location_suppliers') |
| 228 |
customer_location_id = self.ref('stock.stock_location_customers') |
| 229 |
|
| 230 |
receipts = self.env['stock.picking'].create([{ |
| 231 |
'picking_type_id': self.warehouse.in_type_id.id, |
| 232 |
'location_id': supplier_location_id, |
| 233 |
'location_dest_id': stock_location.id, |
| 234 |
'move_ids': [(0, 0, { |
| 235 |
'product_id': self.product_a.id, |
| 236 |
'price_unit': price, |
| 237 |
'product_uom': self.product_a.uom_id.id, |
| 238 |
'product_uom_qty': qty, |
| 239 |
'location_id': supplier_location_id, |
| 240 |
'location_dest_id': stock_location.id, |
| 241 |
})] |
| 242 |
} for qty, price in [ |
| 243 |
(5, 5.0), |
| 244 |
(5, 8.0), |
| 245 |
(5, 7.0), |
| 246 |
(20, 7.33), |
| 247 |
]]) |
| 248 |
|
| 249 |
receipts.action_confirm() |
| 250 |
for m in receipts.move_ids: |
| 251 |
m.quantity = m.product_uom_qty |
| 252 |
receipts.button_validate() |
| 253 |
|
| 254 |
landed_costs = self.env['stock.landed.cost'].create([{ |
| 255 |
'picking_ids': [(6, 0, picking.ids)], |
| 256 |
'account_journal_id': self.expenses_journal.id, |
| 257 |
'cost_lines': [(0, 0, { |
| 258 |
'name': 'equal split', |
| 259 |
'split_method': 'equal', |
| 260 |
'price_unit': 5.0, |
| 261 |
'product_id': self.landed_cost.id |
| 262 |
})], |
| 263 |
} for picking in receipts[1:]]) |
| 264 |
landed_costs.compute_landed_cost() |
| 265 |
landed_costs.button_validate() |
| 266 |
|
| 267 |
self.assertAlmostEqual(self.product_a.standard_price, 7.4742857) |
| 268 |
|
| 269 |
deliveries = self.env['stock.picking'].create([{ |
| 270 |
'picking_type_id': self.warehouse.out_type_id.id, |
| 271 |
'location_id': stock_location.id, |
| 272 |
'location_dest_id': customer_location_id, |
| 273 |
'move_ids': [(0, 0, { |
| 274 |
'product_id': self.product_a.id, |
| 275 |
'product_uom': self.product_a.uom_id.id, |
| 276 |
'product_uom_qty': qty, |
| 277 |
'location_id': stock_location.id, |
| 278 |
'location_dest_id': customer_location_id, |
| 279 |
})] |
| 280 |
} for qty in [23, 2, 10]]) |
| 281 |
|
| 282 |
deliveries.action_confirm() |
| 283 |
for m in deliveries.move_ids: |
| 284 |
m.quantity = m.product_uom_qty |
| 285 |
deliveries.button_validate() |
| 286 |
|
| 287 |
self.assertEqual(self.product_a.value_svl, 0) |
| 288 |
|
| 289 |
def test_lc_cost_split_cumulative_rounding_diff(self): |
| 290 |
""" Ensure that the sum total difference of all rounding operations during the splitting of |
| 291 |
an LC cost allots a sensible value to each cost line. |
| 292 |
I.e., we don't end up with one line which bears the brunt of this difference. |
| 293 |
""" |
| 294 |
product = self.env['product.product'].create({ |
| 295 |
'name': 'product', |
| 296 |
'is_storable': True, |
| 297 |
'standard_price': 10, |
| 298 |
'categ_id': self.categ_real_time.id, |
| 299 |
}) |
| 300 |
purchase_order = self.env['purchase.order'].create({ |
| 301 |
'partner_id': self.partner_a.id, |
| 302 |
'order_line': [Command.create({ |
| 303 |
'product_id': product.id, |
| 304 |
'product_qty': 1, |
| 305 |
}) for _ in range(6)], |
| 306 |
}) |
| 307 |
purchase_order.button_confirm() |
| 308 |
purchase_order.picking_ids.button_validate() |
| 309 |
purchase_order.action_create_invoice() |
| 310 |
bill = purchase_order.invoice_ids |
| 311 |
bill.invoice_date = Date.today() |
| 312 |
with Form(bill) as bill_form: |
| 313 |
with bill_form.invoice_line_ids.new() as inv_line: |
| 314 |
inv_line.product_id = self.landed_cost |
| 315 |
inv_line.price_unit = 6.85 |
| 316 |
inv_line.is_landed_costs_line = True |
| 317 |
bill.action_post() |
| 318 |
action = bill.button_create_landed_costs() |
| 319 |
lc = Form(self.env[action['res_model']].browse(action['res_id'])).save() |
| 320 |
lc.picking_ids = [Command.link(purchase_order.picking_ids.id)] |
| 321 |
lc.cost_lines.split_method = 'equal' |
| 322 |
lc.button_validate() |
| 323 |
line_costs = lc.valuation_adjustment_lines.mapped('additional_landed_cost') |
| 324 |
for line_cost, expected_cost in zip(line_costs, [1.14, 1.14, 1.14, 1.14, 1.14, 1.15]): |
| 325 |
self.assertAlmostEqual( |
| 326 |
line_cost, |
| 327 |
expected_cost, |
| 328 |
delta=lc.currency_id.rounding * 0.1 |
| 329 |
) |