| 1 |
|
| 2 |
import unittest |
| 3 |
from unittest import skip |
| 4 |
|
| 5 |
from odoo.addons.stock_landed_costs.tests.common import TestStockLandedCostsCommon |
| 6 |
from odoo.addons.stock_landed_costs.tests.test_stockvaluationlayer import TestStockValuationLCCommon |
| 7 |
|
| 8 |
from odoo import fields |
| 9 |
from odoo.fields import Command, Date |
| 10 |
from odoo.tests import tagged, Form |
| 11 |
|
| 12 |
|
| 13 |
@tagged('post_install', '-at_install') |
| 14 |
@skip('Temporary to fast merge new valuation') |
| 15 |
class TestLandedCosts(TestStockLandedCostsCommon): |
| 16 |
|
| 17 |
@classmethod |
| 18 |
def setUpClass(cls): |
| 19 |
super().setUpClass() |
| 20 |
|
| 21 |
cls.picking_in = cls.Picking.create({ |
| 22 |
'partner_id': cls.supplier_id, |
| 23 |
'picking_type_id': cls.warehouse.in_type_id.id, |
| 24 |
'location_id': cls.supplier_location_id, |
| 25 |
'state': 'draft', |
| 26 |
'location_dest_id': cls.warehouse.lot_stock_id.id}) |
| 27 |
cls.Move.create({ |
| 28 |
'product_id': cls.product_refrigerator.id, |
| 29 |
'product_uom_qty': 5, |
| 30 |
'product_uom': cls.product_refrigerator.uom_id.id, |
| 31 |
'picking_id': cls.picking_in.id, |
| 32 |
'location_id': cls.supplier_location_id, |
| 33 |
'location_dest_id': cls.warehouse.lot_stock_id.id}) |
| 34 |
cls.Move.create({ |
| 35 |
'product_id': cls.product_oven.id, |
| 36 |
'product_uom_qty': 10, |
| 37 |
'product_uom': cls.product_oven.uom_id.id, |
| 38 |
'picking_id': cls.picking_in.id, |
| 39 |
'location_id': cls.supplier_location_id, |
| 40 |
'location_dest_id': cls.warehouse.lot_stock_id.id}) |
| 41 |
|
| 42 |
cls.picking_out = cls.Picking.create({ |
| 43 |
'partner_id': cls.customer_id, |
| 44 |
'picking_type_id': cls.warehouse.out_type_id.id, |
| 45 |
'location_id': cls.warehouse.lot_stock_id.id, |
| 46 |
'state': 'draft', |
| 47 |
'location_dest_id': cls.customer_location_id}) |
| 48 |
cls.Move.create({ |
| 49 |
'product_id': cls.product_refrigerator.id, |
| 50 |
'product_uom_qty': 2, |
| 51 |
'product_uom': cls.product_refrigerator.uom_id.id, |
| 52 |
'picking_id': cls.picking_out.id, |
| 53 |
'location_id': cls.warehouse.lot_stock_id.id, |
| 54 |
'location_dest_id': cls.customer_location_id}) |
| 55 |
|
| 56 |
def test_00_landed_costs_on_incoming_shipment(self): |
| 57 |
""" Test landed cost on incoming shipment """ |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
self.landed_cost.categ_id.property_valuation = 'real_time' |
| 76 |
|
| 77 |
|
| 78 |
income_ship = self._process_incoming_shipment() |
| 79 |
|
| 80 |
stock_landed_cost = self._create_landed_costs({ |
| 81 |
'equal_price_unit': 10, |
| 82 |
'quantity_price_unit': 150, |
| 83 |
'weight_price_unit': 250, |
| 84 |
'volume_price_unit': 20}, income_ship) |
| 85 |
|
| 86 |
stock_landed_cost.compute_landed_cost() |
| 87 |
|
| 88 |
valid_vals = { |
| 89 |
'equal': 5.0, |
| 90 |
'by_quantity_refrigerator': 50.0, |
| 91 |
'by_quantity_oven': 100.0, |
| 92 |
'by_weight_refrigerator': 50.0, |
| 93 |
'by_weight_oven': 200, |
| 94 |
'by_volume_refrigerator': 5.0, |
| 95 |
'by_volume_oven': 15.0} |
| 96 |
|
| 97 |
|
| 98 |
self._validate_additional_landed_cost_lines(stock_landed_cost, valid_vals) |
| 99 |
|
| 100 |
stock_landed_cost.button_validate() |
| 101 |
|
| 102 |
self.assertRecordValues(stock_landed_cost.account_move_id.line_ids, [ |
| 103 |
{'name': 'equal split - Refrigerator', 'balance': 5}, |
| 104 |
{'name': 'equal split - Refrigerator', 'balance': -5}, |
| 105 |
{'name': 'split by quantity - Refrigerator', 'balance': 50}, |
| 106 |
{'name': 'split by quantity - Refrigerator', 'balance': -50}, |
| 107 |
{'name': 'split by weight - Refrigerator', 'balance': 50}, |
| 108 |
{'name': 'split by weight - Refrigerator', 'balance': -50}, |
| 109 |
{'name': 'split by volume - Refrigerator', 'balance': 5}, |
| 110 |
{'name': 'split by volume - Refrigerator', 'balance': -5}, |
| 111 |
{'name': 'equal split - Microwave Oven', 'balance': 5}, |
| 112 |
{'name': 'equal split - Microwave Oven', 'balance': -5}, |
| 113 |
{'name': 'split by quantity - Microwave Oven', 'balance': 100}, |
| 114 |
{'name': 'split by quantity - Microwave Oven', 'balance': -100}, |
| 115 |
{'name': 'split by weight - Microwave Oven', 'balance': 200}, |
| 116 |
{'name': 'split by weight - Microwave Oven', 'balance': -200}, |
| 117 |
{'name': 'split by volume - Microwave Oven', 'balance': 15}, |
| 118 |
{'name': 'split by volume - Microwave Oven', 'balance': -15}, |
| 119 |
]) |
| 120 |
|
| 121 |
def test_00_landed_costs_on_incoming_shipment_without_real_time(self): |
| 122 |
if self.env.company.chart_template != 'generic_coa': |
| 123 |
raise unittest.SkipTest('Skip this test as it works only with `generic_coa`') |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
self.product_refrigerator.write({"categ_id": self.categ_manual_periodic.id}) |
| 143 |
self.product_oven.write({"categ_id": self.categ_manual_periodic.id}) |
| 144 |
|
| 145 |
income_ship = self._process_incoming_shipment() |
| 146 |
|
| 147 |
stock_landed_cost = self._create_landed_costs({ |
| 148 |
'equal_price_unit': 10, |
| 149 |
'quantity_price_unit': 150, |
| 150 |
'weight_price_unit': 250, |
| 151 |
'volume_price_unit': 20}, income_ship) |
| 152 |
|
| 153 |
stock_landed_cost.compute_landed_cost() |
| 154 |
|
| 155 |
valid_vals = { |
| 156 |
'equal': 5.0, |
| 157 |
'by_quantity_refrigerator': 50.0, |
| 158 |
'by_quantity_oven': 100.0, |
| 159 |
'by_weight_refrigerator': 50.0, |
| 160 |
'by_weight_oven': 200, |
| 161 |
'by_volume_refrigerator': 5.0, |
| 162 |
'by_volume_oven': 15.0} |
| 163 |
|
| 164 |
|
| 165 |
self._validate_additional_landed_cost_lines(stock_landed_cost, valid_vals) |
| 166 |
|
| 167 |
stock_landed_cost.button_validate() |
| 168 |
self.assertFalse(stock_landed_cost.account_move_id) |
| 169 |
|
| 170 |
def test_01_negative_landed_costs_on_incoming_shipment(self): |
| 171 |
""" Test negative landed cost on incoming shipment """ |
| 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 |
self.landed_cost.categ_id.property_valuation = 'real_time' |
| 202 |
|
| 203 |
|
| 204 |
income_ship = self._process_incoming_shipment() |
| 205 |
|
| 206 |
self._process_outgoing_shipment() |
| 207 |
|
| 208 |
stock_landed_cost = self._create_landed_costs({ |
| 209 |
'equal_price_unit': 10, |
| 210 |
'quantity_price_unit': 150, |
| 211 |
'weight_price_unit': 250, |
| 212 |
'volume_price_unit': 20}, income_ship) |
| 213 |
|
| 214 |
stock_landed_cost.compute_landed_cost() |
| 215 |
valid_vals = { |
| 216 |
'equal': 5.0, |
| 217 |
'by_quantity_refrigerator': 50.0, |
| 218 |
'by_quantity_oven': 100.0, |
| 219 |
'by_weight_refrigerator': 50.0, |
| 220 |
'by_weight_oven': 200.0, |
| 221 |
'by_volume_refrigerator': 5.0, |
| 222 |
'by_volume_oven': 15.0} |
| 223 |
|
| 224 |
self._validate_additional_landed_cost_lines(stock_landed_cost, valid_vals) |
| 225 |
|
| 226 |
stock_landed_cost.button_validate() |
| 227 |
self.assertTrue(stock_landed_cost.account_move_id, 'Landed costs should be available account move lines') |
| 228 |
|
| 229 |
stock_negative_landed_cost = self._create_landed_costs({ |
| 230 |
'equal_price_unit': -5, |
| 231 |
'quantity_price_unit': -50, |
| 232 |
'weight_price_unit': -50, |
| 233 |
'volume_price_unit': -5}, income_ship) |
| 234 |
|
| 235 |
stock_negative_landed_cost.compute_landed_cost() |
| 236 |
valid_vals = { |
| 237 |
'equal': -2.5, |
| 238 |
'by_quantity_refrigerator': -16.67, |
| 239 |
'by_quantity_oven': -33.33, |
| 240 |
'by_weight_refrigerator': -10.00, |
| 241 |
'by_weight_oven': -40.00, |
| 242 |
'by_volume_refrigerator': -1.25, |
| 243 |
'by_volume_oven': -3.75} |
| 244 |
|
| 245 |
self._validate_additional_landed_cost_lines(stock_negative_landed_cost, valid_vals) |
| 246 |
|
| 247 |
stock_negative_landed_cost.button_validate() |
| 248 |
self.assertEqual(stock_negative_landed_cost.state, 'done', 'Negative landed costs should be in done state') |
| 249 |
self.assertTrue(stock_negative_landed_cost.account_move_id, 'Landed costs should be available account move lines') |
| 250 |
[balance] = self.env['account.move.line']._read_group( |
| 251 |
[('move_id', '=', stock_negative_landed_cost.account_move_id.id)], aggregates=['balance:sum'])[0] |
| 252 |
self.assertEqual(balance, 0, 'Move is not balanced') |
| 253 |
move_lines = [ |
| 254 |
{'name': 'split by volume - Microwave Oven', 'debit': 3.75, 'credit': 0.0}, |
| 255 |
{'name': 'split by volume - Microwave Oven', 'debit': 0.0, 'credit': 3.75}, |
| 256 |
{'name': 'split by weight - Microwave Oven', 'debit': 40.0, 'credit': 0.0}, |
| 257 |
{'name': 'split by weight - Microwave Oven', 'debit': 0.0, 'credit': 40.0}, |
| 258 |
{'name': 'split by quantity - Microwave Oven', 'debit': 33.33, 'credit': 0.0}, |
| 259 |
{'name': 'split by quantity - Microwave Oven', 'debit': 0.0, 'credit': 33.33}, |
| 260 |
{'name': 'equal split - Microwave Oven', 'debit': 2.5, 'credit': 0.0}, |
| 261 |
{'name': 'equal split - Microwave Oven', 'debit': 0.0, 'credit': 2.5}, |
| 262 |
{'name': 'split by volume - Refrigerator: 2.0 already out', 'debit': 0.5, 'credit': 0.0}, |
| 263 |
{'name': 'split by volume - Refrigerator: 2.0 already out', 'debit': 0.0, 'credit': 0.5}, |
| 264 |
{'name': 'split by weight - Refrigerator: 2.0 already out', 'debit': 4.0, 'credit': 0.0}, |
| 265 |
{'name': 'split by weight - Refrigerator: 2.0 already out', 'debit': 0.0, 'credit': 4.0}, |
| 266 |
{'name': 'split by weight - Refrigerator', 'debit': 0.0, 'credit': 10.0}, |
| 267 |
{'name': 'split by weight - Refrigerator', 'debit': 10.0, 'credit': 0.0}, |
| 268 |
{'name': 'split by volume - Refrigerator', 'debit': 0.0, 'credit': 1.25}, |
| 269 |
{'name': 'split by volume - Refrigerator', 'debit': 1.25, 'credit': 0.0}, |
| 270 |
{'name': 'split by quantity - Refrigerator: 2.0 already out', 'debit': 6.67, 'credit': 0.0}, |
| 271 |
{'name': 'split by quantity - Refrigerator: 2.0 already out', 'debit': 0.0, 'credit': 6.67}, |
| 272 |
{'name': 'split by quantity - Refrigerator', 'debit': 16.67, 'credit': 0.0}, |
| 273 |
{'name': 'split by quantity - Refrigerator', 'debit': 0.0, 'credit': 16.67}, |
| 274 |
{'name': 'equal split - Refrigerator: 2.0 already out', 'debit': 1.0, 'credit': 0.0}, |
| 275 |
{'name': 'equal split - Refrigerator: 2.0 already out', 'debit': 0.0, 'credit': 1.0}, |
| 276 |
{'name': 'equal split - Refrigerator', 'debit': 2.5, 'credit': 0.0}, |
| 277 |
{'name': 'equal split - Refrigerator', 'debit': 0.0, 'credit': 2.5} |
| 278 |
] |
| 279 |
if stock_negative_landed_cost.account_move_id.company_id.anglo_saxon_accounting: |
| 280 |
move_lines += [ |
| 281 |
{'name': 'split by volume - Refrigerator: 2.0 already out', 'debit': 0.5, 'credit': 0.0}, |
| 282 |
{'name': 'split by volume - Refrigerator: 2.0 already out', 'debit': 0.0, 'credit': 0.5}, |
| 283 |
{'name': 'split by weight - Refrigerator: 2.0 already out', 'debit': 4.0, 'credit': 0.0}, |
| 284 |
{'name': 'split by weight - Refrigerator: 2.0 already out', 'debit': 0.0, 'credit': 4.0}, |
| 285 |
{'name': 'split by quantity - Refrigerator: 2.0 already out', 'debit': 6.67, 'credit': 0.0}, |
| 286 |
{'name': 'split by quantity - Refrigerator: 2.0 already out', 'debit': 0.0, 'credit': 6.67}, |
| 287 |
{'name': 'equal split - Refrigerator: 2.0 already out', 'debit': 1.0, 'credit': 0.0}, |
| 288 |
{'name': 'equal split - Refrigerator: 2.0 already out', 'debit': 0.0, 'credit': 1.0}, |
| 289 |
] |
| 290 |
self.assertRecordValues( |
| 291 |
stock_negative_landed_cost.account_move_id.line_ids.sorted(lambda d: (d['name'], d['debit'])), |
| 292 |
sorted(move_lines, key=lambda d: (d['name'], d['debit'])), |
| 293 |
) |
| 294 |
|
| 295 |
def _process_incoming_shipment(self): |
| 296 |
""" Two product incoming shipment. """ |
| 297 |
|
| 298 |
self.picking_in.action_confirm() |
| 299 |
|
| 300 |
self.picking_in.button_validate() |
| 301 |
return self.picking_in |
| 302 |
|
| 303 |
def _process_outgoing_shipment(self): |
| 304 |
""" One product Outgoing shipment. """ |
| 305 |
|
| 306 |
self.picking_out.action_confirm() |
| 307 |
|
| 308 |
self.picking_out.action_assign() |
| 309 |
|
| 310 |
|
| 311 |
self.picking_out.button_validate() |
| 312 |
|
| 313 |
def _create_landed_costs(self, value, picking_in): |
| 314 |
return self.LandedCost.create(dict( |
| 315 |
picking_ids=[(6, 0, [picking_in.id])], |
| 316 |
account_journal_id=self.expenses_journal.id, |
| 317 |
cost_lines=[ |
| 318 |
(0, 0, { |
| 319 |
'name': 'equal split', |
| 320 |
'split_method': 'equal', |
| 321 |
'price_unit': value['equal_price_unit'], |
| 322 |
'product_id': self.landed_cost.id}), |
| 323 |
(0, 0, { |
| 324 |
'name': 'split by quantity', |
| 325 |
'split_method': 'by_quantity', |
| 326 |
'price_unit': value['quantity_price_unit'], |
| 327 |
'product_id': self.brokerage_quantity.id}), |
| 328 |
(0, 0, { |
| 329 |
'name': 'split by weight', |
| 330 |
'split_method': 'by_weight', |
| 331 |
'price_unit': value['weight_price_unit'], |
| 332 |
'product_id': self.transportation_weight.id}), |
| 333 |
(0, 0, { |
| 334 |
'name': 'split by volume', |
| 335 |
'split_method': 'by_volume', |
| 336 |
'price_unit': value['volume_price_unit'], |
| 337 |
'product_id': self.packaging_volume.id}) |
| 338 |
], |
| 339 |
)) |
| 340 |
|
| 341 |
def _validate_additional_landed_cost_lines(self, stock_landed_cost, valid_vals): |
| 342 |
for valuation in stock_landed_cost.valuation_adjustment_lines: |
| 343 |
add_cost = valuation.additional_landed_cost |
| 344 |
split_method = valuation.cost_line_id.split_method |
| 345 |
product = valuation.move_id.product_id |
| 346 |
if split_method == 'equal': |
| 347 |
self.assertEqual(add_cost, valid_vals['equal'], self._error_message(valid_vals['equal'], add_cost)) |
| 348 |
elif split_method == 'by_quantity' and product == self.product_refrigerator: |
| 349 |
self.assertEqual(add_cost, valid_vals['by_quantity_refrigerator'], self._error_message(valid_vals['by_quantity_refrigerator'], add_cost)) |
| 350 |
elif split_method == 'by_quantity' and product == self.product_oven: |
| 351 |
self.assertEqual(add_cost, valid_vals['by_quantity_oven'], self._error_message(valid_vals['by_quantity_oven'], add_cost)) |
| 352 |
elif split_method == 'by_weight' and product == self.product_refrigerator: |
| 353 |
self.assertEqual(add_cost, valid_vals['by_weight_refrigerator'], self._error_message(valid_vals['by_weight_refrigerator'], add_cost)) |
| 354 |
elif split_method == 'by_weight' and product == self.product_oven: |
| 355 |
self.assertEqual(add_cost, valid_vals['by_weight_oven'], self._error_message(valid_vals['by_weight_oven'], add_cost)) |
| 356 |
elif split_method == 'by_volume' and product == self.product_refrigerator: |
| 357 |
self.assertEqual(add_cost, valid_vals['by_volume_refrigerator'], self._error_message(valid_vals['by_volume_refrigerator'], add_cost)) |
| 358 |
elif split_method == 'by_volume' and product == self.product_oven: |
| 359 |
self.assertEqual(add_cost, valid_vals['by_volume_oven'], self._error_message(valid_vals['by_volume_oven'], add_cost)) |
| 360 |
|
| 361 |
def _error_message(self, actucal_cost, computed_cost): |
| 362 |
return 'Additional Landed Cost should be %s instead of %s' % (actucal_cost, computed_cost) |
| 363 |
|
| 364 |
|
| 365 |
@tagged('post_install', '-at_install') |
| 366 |
class TestLandedCostsWithPurchaseAndInv(TestStockValuationLCCommon): |
| 367 |
def test_invoice_after_lc(self): |
| 368 |
self.env.company.anglo_saxon_accounting = True |
| 369 |
self.product1.product_tmpl_id.categ_id.property_cost_method = 'fifo' |
| 370 |
self.product1.product_tmpl_id.categ_id.property_valuation = 'real_time' |
| 371 |
stock_valuation_account = self.company_data['default_account_stock_valuation'] |
| 372 |
|
| 373 |
|
| 374 |
po_form = Form(self.env['purchase.order']) |
| 375 |
po_form.partner_id = self.env['res.partner'].create({'name': 'vendor'}) |
| 376 |
with po_form.order_line.new() as po_line: |
| 377 |
po_line.product_id = self.product1 |
| 378 |
po_line.product_qty = 1 |
| 379 |
po_line.price_unit = 455.0 |
| 380 |
order = po_form.save() |
| 381 |
order.button_confirm() |
| 382 |
|
| 383 |
|
| 384 |
receipt = order.picking_ids[0] |
| 385 |
receipt.move_ids.quantity = 1 |
| 386 |
receipt.button_validate() |
| 387 |
|
| 388 |
|
| 389 |
svl = self.env['stock.valuation.layer'].search([('stock_move_id', '=', receipt.move_ids.id)]) |
| 390 |
self.assertAlmostEqual(svl.value, 455) |
| 391 |
aml = self.env['account.move.line'].search([('account_id', '=', stock_valuation_account.id)]) |
| 392 |
self.assertAlmostEqual(aml.debit, 455) |
| 393 |
|
| 394 |
|
| 395 |
lc = self.env['stock.landed.cost'].create(dict( |
| 396 |
picking_ids=[(6, 0, [receipt.id])], |
| 397 |
account_journal_id=self.stock_journal.id, |
| 398 |
cost_lines=[ |
| 399 |
(0, 0, { |
| 400 |
'name': 'equal split', |
| 401 |
'split_method': 'equal', |
| 402 |
'price_unit': 99, |
| 403 |
'product_id': self.productlc1.id, |
| 404 |
}), |
| 405 |
], |
| 406 |
)) |
| 407 |
lc.compute_landed_cost() |
| 408 |
lc.button_validate() |
| 409 |
|
| 410 |
|
| 411 |
self.assertAlmostEqual(lc.valuation_adjustment_lines.final_cost, 554) |
| 412 |
svl = self.env['stock.valuation.layer'].search([('stock_move_id', '=', receipt.move_ids.id)], order='id desc', limit=1) |
| 413 |
self.assertAlmostEqual(svl.value, 99) |
| 414 |
aml = self.env['account.move.line'].search([('account_id', '=', stock_valuation_account.id)], order='id desc', limit=1) |
| 415 |
self.assertAlmostEqual(aml.debit, 99) |
| 416 |
|
| 417 |
|
| 418 |
move_form = Form(self.env['account.move'].with_context(default_move_type='in_invoice')) |
| 419 |
move_form.invoice_date = move_form.date |
| 420 |
move_form.partner_id = order.partner_id |
| 421 |
move_form.purchase_vendor_bill_id = self.env['purchase.bill.union'].browse(-order.id) |
| 422 |
move = move_form.save() |
| 423 |
move.action_post() |
| 424 |
|
| 425 |
|
| 426 |
price_diff_aml = self.env['account.move.line'].search([('account_id', '=', stock_valuation_account.id), ('move_id', '=', move.id)]) |
| 427 |
self.assertEqual(len(price_diff_aml), 0, "No line should have been generated in the stock valuation account about the price difference.") |
| 428 |
|
| 429 |
def test_lc_with_avco_ordered_qty_backorder(self): |
| 430 |
""" Make sure the landed cost added in invoices are taken into account to compute product |
| 431 |
cost even in 'Ordered Quantity' invoice policy. """ |
| 432 |
self.env.company.anglo_saxon_accounting = True |
| 433 |
self.landed_cost.split_method_landed_cost = 'by_quantity' |
| 434 |
product2 = self.env['product.product'].create({ |
| 435 |
'name': 'product2', |
| 436 |
'is_storable': True, |
| 437 |
'categ_id': self.stock_account_product_categ.id, |
| 438 |
}) |
| 439 |
products = self.product1 | product2 |
| 440 |
products.purchase_method = 'purchase' |
| 441 |
products.categ_id.write({ |
| 442 |
'property_stock_account_input_categ_id': self.company_data['default_account_stock_in'].id, |
| 443 |
'property_stock_account_output_categ_id': self.company_data['default_account_stock_out'].id, |
| 444 |
'property_stock_valuation_account_id': self.company_data['default_account_stock_valuation'].id, |
| 445 |
'property_valuation': 'real_time', |
| 446 |
'property_cost_method': 'average', |
| 447 |
}) |
| 448 |
self.landed_cost.categ_id = products.categ_id.id |
| 449 |
|
| 450 |
purchase_order = self.env['purchase.order'].create({ |
| 451 |
'partner_id': self.partner_a.id, |
| 452 |
'order_line': [Command.create({ |
| 453 |
'product_id': self.product1.id, |
| 454 |
'product_qty': 100000, |
| 455 |
'price_unit': 1, |
| 456 |
}), Command.create({ |
| 457 |
'product_id': product2.id, |
| 458 |
'product_qty': 50000, |
| 459 |
'price_unit': 3, |
| 460 |
})], |
| 461 |
}) |
| 462 |
purchase_order.button_confirm() |
| 463 |
purchase_order.action_create_invoice() |
| 464 |
bill = purchase_order.invoice_ids[0] |
| 465 |
receipt = purchase_order.picking_ids[0] |
| 466 |
receipt.picking_type_id.create_backorder = 'always' |
| 467 |
receipt.move_ids[0].quantity = 50000 |
| 468 |
receipt.move_ids[1].quantity = 25000 |
| 469 |
receipt.move_ids.picked = True |
| 470 |
receipt.button_validate() |
| 471 |
self.assertEqual(self.product1.standard_price, 1) |
| 472 |
self.assertEqual(product2.standard_price, 3) |
| 473 |
|
| 474 |
bill.invoice_date = fields.Date.today() |
| 475 |
with Form(bill) as bill_form: |
| 476 |
with bill_form.invoice_line_ids.new() as inv_line: |
| 477 |
inv_line.product_id = self.landed_cost |
| 478 |
inv_line.price_unit = 75000 |
| 479 |
inv_line.is_landed_costs_line = True |
| 480 |
bill.action_post() |
| 481 |
action = bill.button_create_landed_costs() |
| 482 |
lc_form = Form(self.env[action['res_model']].browse(action['res_id'])) |
| 483 |
lc_form.picking_ids.add(receipt) |
| 484 |
lc = lc_form.save() |
| 485 |
lc.button_validate() |
| 486 |
self.assertEqual(self.product1.standard_price, 2) |
| 487 |
self.assertEqual(product2.standard_price, 4) |
| 488 |
|
| 489 |
receipt2 = purchase_order.picking_ids.filtered(lambda p: p.state == 'assigned') |
| 490 |
for move in receipt2.move_ids: |
| 491 |
move.quantity = move.product_qty |
| 492 |
move.picked = True |
| 493 |
receipt2.button_validate() |
| 494 |
self.assertEqual(self.product1.standard_price, 1.5) |
| 495 |
self.assertEqual(product2.standard_price, 3.5) |
| 496 |
|
| 497 |
def test_invoice_after_lc_amls(self): |
| 498 |
self.env.company.anglo_saxon_accounting = True |
| 499 |
self.landed_cost.landed_cost_ok = True |
| 500 |
self.landed_cost.categ_id.property_cost_method = 'fifo' |
| 501 |
self.landed_cost.categ_id.property_valuation = 'real_time' |
| 502 |
|
| 503 |
|
| 504 |
po = self.env['purchase.order'].create({ |
| 505 |
'partner_id': self.partner_a.id, |
| 506 |
'currency_id': self.company_data['currency'].id, |
| 507 |
'order_line': [ |
| 508 |
(0, 0, { |
| 509 |
'name': self.product_a.name, |
| 510 |
'product_id': self.product_a.id, |
| 511 |
'product_qty': 1.0, |
| 512 |
'product_uom_id': self.product_a.uom_id.id, |
| 513 |
'price_unit': 100.0, |
| 514 |
'tax_ids': False, |
| 515 |
}), |
| 516 |
(0, 0, { |
| 517 |
'name': self.landed_cost.name, |
| 518 |
'product_id': self.landed_cost.id, |
| 519 |
'product_qty': 1.0, |
| 520 |
'price_unit': 100.0, |
| 521 |
}), |
| 522 |
], |
| 523 |
}) |
| 524 |
po.button_confirm() |
| 525 |
|
| 526 |
receipt = po.picking_ids |
| 527 |
receipt.move_ids.quantity = 1 |
| 528 |
receipt.button_validate() |
| 529 |
po.order_line[1].qty_received = 1 |
| 530 |
|
| 531 |
po.action_create_invoice() |
| 532 |
bill = po.invoice_ids |
| 533 |
|
| 534 |
|
| 535 |
lc = self.env['stock.landed.cost'].create(dict( |
| 536 |
picking_ids=[(6, 0, [receipt.id])], |
| 537 |
account_journal_id=self.stock_journal.id, |
| 538 |
cost_lines=[ |
| 539 |
(0, 0, { |
| 540 |
'name': 'equal split', |
| 541 |
'split_method': 'equal', |
| 542 |
'price_unit': 100, |
| 543 |
'product_id': self.landed_cost.id, |
| 544 |
}), |
| 545 |
], |
| 546 |
)) |
| 547 |
lc.compute_landed_cost() |
| 548 |
lc.button_validate() |
| 549 |
|
| 550 |
user = self.env['res.users'].create({ |
| 551 |
'name': 'User h', |
| 552 |
'login': 'usher', |
| 553 |
'email': '[email protected]', |
| 554 |
'group_ids': [(6, 0, [self.env.ref('account.group_account_invoice').id])] |
| 555 |
}) |
| 556 |
|
| 557 |
bill.landed_costs_ids = [(6, 0, lc.id)] |
| 558 |
bill.invoice_date = Date.today() |
| 559 |
bill.with_user(user)._post() |
| 560 |
|
| 561 |
landed_cost_aml = bill.invoice_line_ids.filtered(lambda l: l.product_id == self.landed_cost) |
| 562 |
self.assertTrue(landed_cost_aml.reconciled) |
| 563 |
|
| 564 |
def test_lc_with_avco_ordered_qty_invoice_receipt_order(self): |
| 565 |
""" When using an invoicing policy that permits invoicing prior to reception, stock moves |
| 566 |
for products using dynamic cost methods should account for LCs associated with the order |
| 567 |
from which the move was derived. |
| 568 |
""" |
| 569 |
self.env.company.anglo_saxon_accounting = True |
| 570 |
self.product1.purchase_method = 'purchase' |
| 571 |
self.product1.categ_id.write({ |
| 572 |
'property_stock_account_input_categ_id': self.company_data['default_account_stock_in'].id, |
| 573 |
'property_stock_account_output_categ_id': self.company_data['default_account_stock_out'].id, |
| 574 |
'property_stock_valuation_account_id': self.company_data['default_account_stock_valuation'].id, |
| 575 |
'property_valuation': 'real_time', |
| 576 |
'property_cost_method': 'average', |
| 577 |
}) |
| 578 |
self.landed_cost.categ_id = self.product1.categ_id.id |
| 579 |
product = self.product1 |
| 580 |
product.uom_id = self.env.ref("uom.product_uom_kgm") |
| 581 |
|
| 582 |
purchase_order = self.env['purchase.order'].create({ |
| 583 |
'partner_id': self.partner_a.id, |
| 584 |
'order_line': [Command.create({ |
| 585 |
'product_id': product.id, |
| 586 |
'product_qty': 100000, |
| 587 |
'price_unit': 1.35, |
| 588 |
})], |
| 589 |
}) |
| 590 |
purchase_order.button_confirm() |
| 591 |
purchase_order.action_create_invoice() |
| 592 |
bill = purchase_order.invoice_ids[0] |
| 593 |
bill.invoice_line_ids[0].quantity = 23000 |
| 594 |
receipt = purchase_order.picking_ids[0] |
| 595 |
receipt.picking_type_id.create_backorder = 'always' |
| 596 |
receipt.move_ids.quantity = 23000 |
| 597 |
receipt.button_validate() |
| 598 |
self.assertEqual(product.standard_price, 1.35) |
| 599 |
|
| 600 |
bill.invoice_date = '2000-05-05' |
| 601 |
with Form(bill) as bill_form: |
| 602 |
with bill_form.invoice_line_ids.new() as inv_line: |
| 603 |
inv_line.product_id = self.landed_cost |
| 604 |
inv_line.price_unit = 23000 |
| 605 |
inv_line.is_landed_costs_line = True |
| 606 |
bill.action_post() |
| 607 |
action = bill.button_create_landed_costs() |
| 608 |
lc_form = Form(self.env[action['res_model']].browse(action['res_id'])) |
| 609 |
lc_form.picking_ids.add(receipt) |
| 610 |
lc = lc_form.save() |
| 611 |
lc.button_validate() |
| 612 |
self.assertEqual(product.standard_price, 2.35) |
| 613 |
|
| 614 |
purchase_order.action_create_invoice() |
| 615 |
bill2 = purchase_order.invoice_ids.filtered(lambda b: b.state == 'draft') |
| 616 |
bill2.invoice_line_ids[0].quantity = 27000 |
| 617 |
bill2.invoice_date = Date.today() |
| 618 |
bill2._post() |
| 619 |
receipt2 = purchase_order.picking_ids.filtered(lambda p: p.state == 'assigned') |
| 620 |
receipt2.move_ids[0].quantity = 27000 |
| 621 |
receipt2.button_validate() |
| 622 |
self.assertEqual(product.standard_price, 1.81) |
| 623 |
|
| 624 |
def test_landed_costs_avco_invoice_before_receipt(self): |
| 625 |
""" |
| 626 |
Test the application of landed costs on a product with average cost (AVCO) method |
| 627 |
when the bill is created and validated before the receipt and the landed costs are |
| 628 |
not yet created on the bill. |
| 629 |
""" |
| 630 |
|
| 631 |
self.env.company.anglo_saxon_accounting = True |
| 632 |
self.product1.purchase_method = 'purchase' |
| 633 |
self.product1.categ_id.write({ |
| 634 |
'property_stock_account_input_categ_id': self.company_data['default_account_stock_in'].id, |
| 635 |
'property_stock_account_output_categ_id': self.company_data['default_account_stock_out'].id, |
| 636 |
'property_stock_valuation_account_id': self.company_data['default_account_stock_valuation'].id, |
| 637 |
'property_valuation': 'real_time', |
| 638 |
'property_cost_method': 'average', |
| 639 |
}) |
| 640 |
self.landed_cost.categ_id = self.product1.categ_id.id |
| 641 |
product = self.product1 |
| 642 |
|
| 643 |
purchase_order = self.env['purchase.order'].create({ |
| 644 |
'partner_id': self.partner_a.id, |
| 645 |
'order_line': [Command.create({ |
| 646 |
'product_id': product.id, |
| 647 |
'product_qty': 1, |
| 648 |
'price_unit': 10, |
| 649 |
})], |
| 650 |
}) |
| 651 |
purchase_order.button_confirm() |
| 652 |
purchase_order.action_create_invoice() |
| 653 |
bill = purchase_order.invoice_ids[0] |
| 654 |
bill.invoice_line_ids[0].quantity = 1 |
| 655 |
bill.invoice_date = '2000-05-05' |
| 656 |
with Form(bill) as bill_form: |
| 657 |
with bill_form.invoice_line_ids.new() as inv_line: |
| 658 |
inv_line.product_id = self.landed_cost |
| 659 |
inv_line.price_unit = 25 |
| 660 |
inv_line.is_landed_costs_line = True |
| 661 |
bill.action_post() |
| 662 |
receipt = purchase_order.picking_ids[0] |
| 663 |
receipt.move_ids.quantity = 1 |
| 664 |
receipt.button_validate() |
| 665 |
|
| 666 |
|
| 667 |
assert receipt.move_ids[0].stock_valuation_layer_ids[0].unit_cost == 10 |
| 668 |
|
| 669 |
action = bill.button_create_landed_costs() |
| 670 |
lc_form = Form(self.env[action['res_model']].browse(action['res_id'])) |
| 671 |
lc_form.picking_ids.add(receipt) |
| 672 |
lc = lc_form.save() |
| 673 |
lc.button_validate() |
| 674 |
|
| 675 |
|
| 676 |
self.assertEqual(product.standard_price, 35) |
| 677 |
|
| 678 |
def test_refund_landed_cost_creates_negative_valuation(self): |
| 679 |
"""Ensure landed cost created from a vendor refund is negative and reduces valuation.""" |
| 680 |
self.env.company.anglo_saxon_accounting = True |
| 681 |
product = self.env['product.product'].create({ |
| 682 |
'name': 'product', |
| 683 |
'is_storable': True, |
| 684 |
'purchase_method': 'purchase', |
| 685 |
'categ_id': self.stock_account_product_categ.id, |
| 686 |
}) |
| 687 |
product.categ_id.write({ |
| 688 |
'property_stock_account_input_categ_id': self.company_data['default_account_stock_in'].id, |
| 689 |
'property_stock_account_output_categ_id': self.company_data['default_account_stock_out'].id, |
| 690 |
'property_stock_valuation_account_id': self.company_data['default_account_stock_valuation'].id, |
| 691 |
'property_valuation': 'real_time', |
| 692 |
'property_cost_method': 'average', |
| 693 |
}) |
| 694 |
landed_cost_product = self.landed_cost |
| 695 |
landed_cost_product.write({ |
| 696 |
'landed_cost_ok': True, |
| 697 |
'categ_id': product.categ_id.id, |
| 698 |
'type': 'service', |
| 699 |
'purchase_method': 'purchase', |
| 700 |
}) |
| 701 |
po = self.env['purchase.order'].create({ |
| 702 |
'partner_id': self.supplier_id, |
| 703 |
'order_line': [ |
| 704 |
Command.create({ |
| 705 |
'product_id': product.id, |
| 706 |
'product_qty': 1, |
| 707 |
'price_unit': 100, |
| 708 |
}), |
| 709 |
Command.create({ |
| 710 |
'product_id': landed_cost_product.id, |
| 711 |
'product_qty': 1, |
| 712 |
'price_unit': 20, |
| 713 |
}), |
| 714 |
], |
| 715 |
}) |
| 716 |
po.button_confirm() |
| 717 |
picking = po.picking_ids |
| 718 |
picking.move_ids.quantity = 1 |
| 719 |
picking.button_validate() |
| 720 |
po.action_create_invoice() |
| 721 |
bill = po.invoice_ids[0] |
| 722 |
bill.invoice_date = fields.Date.today() |
| 723 |
bill.action_post() |
| 724 |
action = bill.button_create_landed_costs() |
| 725 |
lc_form = Form(self.env[action['res_model']].browse(action['res_id'])) |
| 726 |
lc_form.picking_ids.add(picking) |
| 727 |
lc = lc_form.save() |
| 728 |
lc.button_validate() |
| 729 |
self.assertEqual(lc.amount_total, 20) |
| 730 |
self.assertEqual(lc.stock_valuation_layer_ids.value, 20) |
| 731 |
reverse_wizard = self.env['account.move.reversal'].with_context(active_model='account.move', active_ids=bill.ids).create({ |
| 732 |
'reason': 'Refund for landed cost', |
| 733 |
'date': fields.Date.today(), |
| 734 |
'journal_id': bill.journal_id.id, |
| 735 |
}) |
| 736 |
reversal = reverse_wizard.reverse_moves() |
| 737 |
reversed_move = self.env['account.move'].browse(reversal['res_id']) |
| 738 |
reversed_move.invoice_date = fields.Date.today() |
| 739 |
reversed_move.action_post() |
| 740 |
action = reversed_move.button_create_landed_costs() |
| 741 |
lc_form = Form(self.env[action['res_model']].browse(action['res_id'])) |
| 742 |
lc_form.picking_ids.add(picking) |
| 743 |
lc = lc_form.save() |
| 744 |
lc.button_validate() |
| 745 |
self.assertEqual(lc.amount_total, -20) |
| 746 |
self.assertEqual(lc.stock_valuation_layer_ids.value, -20) |
| 747 |
|
| 748 |
def test_landed_cost_avco_partial_bill_rounding(self): |
| 749 |
"""Tests landed cost calculation for an AVCO product with partial |
| 750 |
billing and backorders, ensuring correct stock valuation and handling |
| 751 |
of rounding with decimal precision. |
| 752 |
""" |
| 753 |
decimal_price = self.env.ref('product.decimal_price') |
| 754 |
decimal_price.digits = 5 |
| 755 |
decimal_product_uom = self.env.ref('uom.decimal_product_uom') |
| 756 |
decimal_product_uom.digits = 5 |
| 757 |
|
| 758 |
self.env.company.anglo_saxon_accounting = True |
| 759 |
self.product1.purchase_method = 'purchase' |
| 760 |
self.product1.categ_id.write({ |
| 761 |
'property_stock_account_input_categ_id': self.company_data['default_account_stock_in'].id, |
| 762 |
'property_stock_account_output_categ_id': self.company_data['default_account_stock_out'].id, |
| 763 |
'property_stock_valuation_account_id': self.company_data['default_account_stock_valuation'].id, |
| 764 |
'property_valuation': 'real_time', |
| 765 |
'property_cost_method': 'average', |
| 766 |
}) |
| 767 |
self.landed_cost.categ_id = self.product1.categ_id.id |
| 768 |
|
| 769 |
purchase_order = self.env['purchase.order'].create({ |
| 770 |
'partner_id': self.partner_a.id, |
| 771 |
'order_line': [ |
| 772 |
(0, 0, { |
| 773 |
'name': self.product1.name, |
| 774 |
'product_id': self.product1.id, |
| 775 |
'product_qty': 190.0, |
| 776 |
'product_uom_id': self.product1.uom_id.id, |
| 777 |
'price_unit': 110.0, |
| 778 |
}) |
| 779 |
], |
| 780 |
}) |
| 781 |
purchase_order.button_confirm() |
| 782 |
|
| 783 |
self.assertEqual(purchase_order.state, 'purchase') |
| 784 |
picking = purchase_order.picking_ids[0] |
| 785 |
picking.action_assign() |
| 786 |
|
| 787 |
|
| 788 |
picking.move_ids.quantity = 70 |
| 789 |
picking.button_validate() |
| 790 |
picking._action_done() |
| 791 |
|
| 792 |
backorder_picking = purchase_order.picking_ids.filtered(lambda p: p.backorder_id == picking) |
| 793 |
self.assertTrue(backorder_picking, "Backorder picking was not created or not found.") |
| 794 |
|
| 795 |
bill = self.env["account.move"].browse(purchase_order.action_create_invoice()["res_id"]) |
| 796 |
bill.invoice_date = fields.Date.today() |
| 797 |
bill.invoice_line_ids.quantity = 70 |
| 798 |
bill.action_post() |
| 799 |
|
| 800 |
svl_initial_receipt = self.env['stock.valuation.layer'].search([ |
| 801 |
('product_id', '=', self.product1.id), |
| 802 |
('stock_move_id', '=', picking.move_ids.id) |
| 803 |
]) |
| 804 |
self.assertEqual(len(svl_initial_receipt), 1) |
| 805 |
self.assertAlmostEqual(svl_initial_receipt.quantity, 70) |
| 806 |
self.assertAlmostEqual(svl_initial_receipt.unit_cost, 110, msg="SVL unit cost for initial receipt should match PO price.") |
| 807 |
self.assertAlmostEqual(svl_initial_receipt.value, 70 * 110) |
| 808 |
self.assertAlmostEqual(self.product1.standard_price, 110, msg="Product AVCO should be 110 after first receipt.") |
| 809 |
self.assertAlmostEqual(purchase_order.order_line[0].qty_invoiced, 70) |
| 810 |
|
| 811 |
|
| 812 |
landed_cost = self.env['stock.landed.cost'].create({ |
| 813 |
'picking_ids': [(6, 0, [picking.id])], |
| 814 |
'account_journal_id': self.stock_journal.id, |
| 815 |
'cost_lines': [(0, 0, { |
| 816 |
'name': 'landed cost', |
| 817 |
'split_method': 'equal', |
| 818 |
'price_unit': 95, |
| 819 |
'product_id': self.landed_cost.id, |
| 820 |
})], |
| 821 |
}) |
| 822 |
landed_cost.compute_landed_cost() |
| 823 |
landed_cost.button_validate() |
| 824 |
|
| 825 |
|
| 826 |
bill2 = self.env["account.move"].browse(purchase_order.action_create_invoice()["res_id"]) |
| 827 |
bill2.invoice_date = fields.Date.today() |
| 828 |
self.assertAlmostEqual(bill2.invoice_line_ids[0].quantity, 120, msg="Bill 2 should be for the remaining 120 units.") |
| 829 |
self.assertAlmostEqual(bill2.invoice_line_ids[0].price_unit, 110, msg="Bill 2 unit price should match PO price.") |
| 830 |
self.assertAlmostEqual(purchase_order.order_line[0].qty_invoiced, 190, msg="Total 190 units should be invoiced on PO line.") |
| 831 |
|
| 832 |
|
| 833 |
backorder_picking.action_assign() |
| 834 |
backorder_picking.move_ids[0].quantity = 120 |
| 835 |
backorder_picking.button_validate() |
| 836 |
|
| 837 |
|
| 838 |
svl_backorder_receipt = self.env['stock.valuation.layer'].search([ |
| 839 |
('product_id', '=', self.product1.id), |
| 840 |
('stock_move_id', '=', backorder_picking.move_ids[0].id) |
| 841 |
]) |
| 842 |
|
| 843 |
self.assertEqual(len(svl_backorder_receipt), 1) |
| 844 |
self.assertAlmostEqual(svl_backorder_receipt.quantity, 120) |
| 845 |
|
| 846 |
self.assertAlmostEqual(svl_backorder_receipt.unit_cost, 110, msg="SVL unit cost for backorder receipt should match PO price.") |
| 847 |
self.assertAlmostEqual(svl_backorder_receipt.value, 120 * 110) |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
self.assertAlmostEqual(self.product1.qty_available, 190) |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
self.assertAlmostEqual(self.product1.standard_price, 110.5) |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
self.assertAlmostEqual(self.product1.value_svl, 20995) |