| 1 |
|
| 2 |
|
| 3 |
import datetime |
| 4 |
from freezegun import freeze_time |
| 5 |
|
| 6 |
from odoo import fields |
| 7 |
from odoo.tests import Form, tagged |
| 8 |
from odoo.addons.stock_account.tests.common import TestStockValuationCommon |
| 9 |
|
| 10 |
|
| 11 |
@tagged('post_install', '-at_install') |
| 12 |
class TestSaleStockMargin(TestStockValuationCommon): |
| 13 |
|
| 14 |
@classmethod |
| 15 |
def setUpClass(cls): |
| 16 |
super().setUpClass() |
| 17 |
cls.pricelist = cls.env['product.pricelist'].create({ |
| 18 |
'name': 'Simple Pricelist', |
| 19 |
'company_id': False, |
| 20 |
}) |
| 21 |
cls.env['res.currency.rate'].search([]).unlink() |
| 22 |
cls.customer = cls.env['res.partner'].create({ |
| 23 |
'name': 'Customer', |
| 24 |
}) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
def _create_sale_order(self): |
| 31 |
return self.env['sale.order'].create({ |
| 32 |
'name': 'Sale order', |
| 33 |
'partner_id': self.customer.id, |
| 34 |
'partner_invoice_id': self.customer.id, |
| 35 |
'pricelist_id': self.pricelist.id, |
| 36 |
}) |
| 37 |
|
| 38 |
def _create_sale_order_line(self, sale_order, product, quantity, price_unit=0): |
| 39 |
return self.env['sale.order.line'].create({ |
| 40 |
'name': 'Sale order', |
| 41 |
'order_id': sale_order.id, |
| 42 |
'price_unit': price_unit, |
| 43 |
'product_id': product.id, |
| 44 |
'product_uom_qty': quantity, |
| 45 |
}) |
| 46 |
|
| 47 |
def _create_product(self): |
| 48 |
product_template = self.env['product.template'].create({ |
| 49 |
'name': 'Super product', |
| 50 |
'is_storable': True, |
| 51 |
'categ_id': self.env.ref('product.product_category_goods').id, |
| 52 |
}) |
| 53 |
product_template.categ_id.property_cost_method = 'fifo' |
| 54 |
return product_template.product_variant_ids |
| 55 |
|
| 56 |
def _setup_multicurrency(self): |
| 57 |
usd = self.env.ref('base.USD') |
| 58 |
self.company_currency = self.env.company.currency_id |
| 59 |
self.other_currency = self.env.ref('base.EUR') if self.company_currency == usd else usd |
| 60 |
date = fields.Date.today() |
| 61 |
self.env['res.currency.rate'].create([ |
| 62 |
{'currency_id': self.company_currency.id, 'rate': 1, 'name': date}, |
| 63 |
{'currency_id': self.other_currency.id, 'rate': 2, 'name': date}, |
| 64 |
]) |
| 65 |
return self.company_currency, self.other_currency |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
def test_sale_stock_margin_1(self): |
| 72 |
sale_order = self._create_sale_order() |
| 73 |
product = self._create_product() |
| 74 |
|
| 75 |
self._make_in_move(product, 2, 35) |
| 76 |
self._make_out_move(product, 1) |
| 77 |
|
| 78 |
order_line = self._create_sale_order_line(sale_order, product, 1, 50) |
| 79 |
sale_order.action_confirm() |
| 80 |
|
| 81 |
self.assertEqual(order_line.purchase_price, 35) |
| 82 |
self.assertEqual(sale_order.margin, 15) |
| 83 |
|
| 84 |
sale_order.picking_ids.move_ids.write({'quantity': 1, 'picked': True}) |
| 85 |
sale_order.picking_ids.button_validate() |
| 86 |
|
| 87 |
self.assertEqual(order_line.purchase_price, 35) |
| 88 |
self.assertEqual(order_line.margin, 15) |
| 89 |
self.assertEqual(sale_order.margin, 15) |
| 90 |
|
| 91 |
def test_sale_stock_margin_2(self): |
| 92 |
sale_order = self._create_sale_order() |
| 93 |
product = self._create_product() |
| 94 |
|
| 95 |
self._make_in_move(product, 2, 32) |
| 96 |
self._make_in_move(product, 5, 17) |
| 97 |
self._make_out_move(product, 1) |
| 98 |
|
| 99 |
order_line = self._create_sale_order_line(sale_order, product, 2, 50) |
| 100 |
sale_order.action_confirm() |
| 101 |
|
| 102 |
self.assertEqual(order_line.purchase_price, 19.5) |
| 103 |
self.assertAlmostEqual(sale_order.margin, 61) |
| 104 |
|
| 105 |
sale_order.picking_ids.move_ids.write({'quantity': 2, 'picked': True}) |
| 106 |
sale_order.picking_ids.button_validate() |
| 107 |
|
| 108 |
self.assertAlmostEqual(order_line.purchase_price, 24.5) |
| 109 |
self.assertAlmostEqual(order_line.margin, 51) |
| 110 |
self.assertAlmostEqual(sale_order.margin, 51) |
| 111 |
|
| 112 |
def test_sale_stock_margin_3(self): |
| 113 |
sale_order = self._create_sale_order() |
| 114 |
product = self._create_product() |
| 115 |
|
| 116 |
self._make_in_move(product, 2, 10) |
| 117 |
self._make_out_move(product, 1) |
| 118 |
|
| 119 |
order_line = self._create_sale_order_line(sale_order, product, 2, 20) |
| 120 |
sale_order.action_confirm() |
| 121 |
|
| 122 |
self.assertEqual(order_line.purchase_price, 10) |
| 123 |
self.assertAlmostEqual(sale_order.margin, 20) |
| 124 |
|
| 125 |
sale_order.picking_ids.move_ids.write({'quantity': 1, 'picked': True}) |
| 126 |
sale_order.picking_ids.button_validate() |
| 127 |
|
| 128 |
self.assertAlmostEqual(order_line.purchase_price, 10) |
| 129 |
self.assertAlmostEqual(order_line.margin, 20) |
| 130 |
self.assertAlmostEqual(sale_order.margin, 20) |
| 131 |
|
| 132 |
def test_sale_stock_margin_4(self): |
| 133 |
sale_order = self._create_sale_order() |
| 134 |
product = self._create_product() |
| 135 |
|
| 136 |
self._make_in_move(product, 2, 10) |
| 137 |
self._make_in_move(product, 1, 20) |
| 138 |
self._make_out_move(product, 1) |
| 139 |
|
| 140 |
order_line = self._create_sale_order_line(sale_order, product, 2, 20) |
| 141 |
sale_order.action_confirm() |
| 142 |
|
| 143 |
self.assertEqual(order_line.purchase_price, 15) |
| 144 |
self.assertAlmostEqual(sale_order.margin, 10) |
| 145 |
|
| 146 |
sale_order.picking_ids.move_ids.write({'quantity': 1, 'picked': True}) |
| 147 |
Form.from_action(self.env, sale_order.picking_ids.button_validate()).save().process() |
| 148 |
|
| 149 |
self.assertAlmostEqual(order_line.purchase_price, 15) |
| 150 |
self.assertAlmostEqual(order_line.margin, 10) |
| 151 |
self.assertAlmostEqual(sale_order.margin, 10) |
| 152 |
|
| 153 |
def test_sale_stock_margin_5(self): |
| 154 |
sale_order = self._create_sale_order() |
| 155 |
product_1 = self._create_product() |
| 156 |
product_2 = self._create_product() |
| 157 |
|
| 158 |
self._make_in_move(product_1, 2, 35) |
| 159 |
self._make_in_move(product_1, 1, 51) |
| 160 |
self._make_out_move(product_1, 1) |
| 161 |
|
| 162 |
self._make_in_move(product_2, 2, 17) |
| 163 |
self._make_in_move(product_2, 1, 11) |
| 164 |
self._make_out_move(product_2, 1) |
| 165 |
|
| 166 |
order_line_1 = self._create_sale_order_line(sale_order, product_1, 2, 60) |
| 167 |
order_line_2 = self._create_sale_order_line(sale_order, product_2, 4, 20) |
| 168 |
sale_order.action_confirm() |
| 169 |
|
| 170 |
self.assertAlmostEqual(order_line_1.purchase_price, 43) |
| 171 |
self.assertAlmostEqual(order_line_2.purchase_price, 14) |
| 172 |
self.assertAlmostEqual(order_line_1.margin, 17 * 2) |
| 173 |
self.assertAlmostEqual(order_line_2.margin, 6 * 4) |
| 174 |
self.assertAlmostEqual(sale_order.margin, 58) |
| 175 |
|
| 176 |
sale_order.picking_ids.move_ids[0].write({'quantity': 2, 'picked': True}) |
| 177 |
sale_order.picking_ids.move_ids[1].write({'quantity': 3, 'picked': True}) |
| 178 |
|
| 179 |
Form.from_action(self.env, sale_order.picking_ids.button_validate()).save().process() |
| 180 |
|
| 181 |
self.assertAlmostEqual(order_line_1.purchase_price, 43) |
| 182 |
self.assertAlmostEqual(order_line_2.purchase_price, 12.5) |
| 183 |
self.assertAlmostEqual(order_line_1.margin, 34) |
| 184 |
self.assertAlmostEqual(order_line_2.margin, 30) |
| 185 |
self.assertAlmostEqual(sale_order.margin, 64) |
| 186 |
|
| 187 |
def test_sale_stock_margin_6(self): |
| 188 |
""" Test that the purchase price doesn't change when there is a service product in the SO""" |
| 189 |
product = self.product_standard |
| 190 |
service = self.env['product.product'].create({ |
| 191 |
'name': 'Service', |
| 192 |
'type': 'service', |
| 193 |
'list_price': 100.0, |
| 194 |
'standard_price': 50.0}) |
| 195 |
product.list_price = 80.0 |
| 196 |
product.standard_price = 40.0 |
| 197 |
sale_order = self._create_sale_order() |
| 198 |
order_line_1 = self._create_sale_order_line(sale_order, service, 1, 100) |
| 199 |
order_line_2 = self._create_sale_order_line(sale_order, product, 1, 80) |
| 200 |
|
| 201 |
self.assertEqual(order_line_1.purchase_price, 50, "Sales order line cost should be 50.00") |
| 202 |
self.assertEqual(order_line_2.purchase_price, 40, "Sales order line cost should be 40.00") |
| 203 |
|
| 204 |
self.assertEqual(order_line_1.margin, 50, "Sales order line profit should be 50.00") |
| 205 |
self.assertEqual(order_line_2.margin, 40, "Sales order line profit should be 40.00") |
| 206 |
self.assertEqual(sale_order.margin, 90, "Sales order profit should be 90.00") |
| 207 |
|
| 208 |
|
| 209 |
order_line_1.purchase_price = 100.0 |
| 210 |
self.assertEqual(order_line_1.purchase_price, 100, "Sales order line cost should be 100.00") |
| 211 |
|
| 212 |
|
| 213 |
sale_order.action_confirm() |
| 214 |
|
| 215 |
self.assertEqual(order_line_1.purchase_price, 100, "Sales order line cost should be 100.00") |
| 216 |
self.assertEqual(order_line_2.purchase_price, 40, "Sales order line cost should be 40.00") |
| 217 |
|
| 218 |
def test_so_and_multicurrency(self): |
| 219 |
_company_currency, other_currency = self._setup_multicurrency() |
| 220 |
so = self._create_sale_order() |
| 221 |
so.pricelist_id = self.env['product.pricelist'].create({ |
| 222 |
'name': 'Super Pricelist', |
| 223 |
'currency_id': other_currency.id, |
| 224 |
}) |
| 225 |
|
| 226 |
product = self._create_product() |
| 227 |
product.standard_price = 100 |
| 228 |
product.list_price = 200 |
| 229 |
|
| 230 |
so_form = Form(so) |
| 231 |
with so_form.order_line.new() as line: |
| 232 |
line.product_id = product |
| 233 |
so = so_form.save() |
| 234 |
so_line = so.order_line |
| 235 |
|
| 236 |
self.assertEqual(so_line.purchase_price, 200) |
| 237 |
self.assertEqual(so_line.price_unit, 400) |
| 238 |
so.action_confirm() |
| 239 |
self.assertEqual(so_line.purchase_price, 200) |
| 240 |
self.assertEqual(so_line.price_unit, 400) |
| 241 |
|
| 242 |
def test_so_and_multicompany(self): |
| 243 |
""" In a multicompany environnement, when the user is on company C01 and confirms a SO that |
| 244 |
belongs to a second company C02, this test ensures that the computations will be based on |
| 245 |
C02's data""" |
| 246 |
main_company = self.env['res.company']._get_main_company() |
| 247 |
main_company_currency = main_company.currency_id |
| 248 |
new_company_currency = self.env.ref('base.EUR') if main_company_currency == self.env.ref('base.USD') else self.env.ref('base.USD') |
| 249 |
|
| 250 |
date = fields.Date.today() |
| 251 |
self.env['res.currency.rate'].create([ |
| 252 |
{'currency_id': main_company_currency.id, 'rate': 1, 'name': date, 'company_id': False}, |
| 253 |
{'currency_id': new_company_currency.id, 'rate': 3, 'name': date, 'company_id': False}, |
| 254 |
]) |
| 255 |
|
| 256 |
new_company = self.env['res.company'].create({ |
| 257 |
'name': 'Super Company', |
| 258 |
'currency_id': new_company_currency.id, |
| 259 |
}) |
| 260 |
self.env.user.company_id = new_company.id |
| 261 |
|
| 262 |
self.pricelist.currency_id = new_company_currency.id |
| 263 |
|
| 264 |
product = self._create_product() |
| 265 |
product.categ_id.property_cost_method = 'fifo' |
| 266 |
product.standard_price = 100 |
| 267 |
|
| 268 |
incoming_picking_type = self.env['stock.picking.type'].search([('company_id', '=', new_company.id), ('code', '=', 'incoming')], limit=1) |
| 269 |
production_location = self.env['stock.location'].search([('company_id', '=', new_company.id), ('usage', '=', 'production')]) |
| 270 |
|
| 271 |
picking = self.env['stock.picking'].create({ |
| 272 |
'picking_type_id': incoming_picking_type.id, |
| 273 |
'location_id': production_location.id, |
| 274 |
'location_dest_id': incoming_picking_type.default_location_dest_id.id, |
| 275 |
}) |
| 276 |
self.env['stock.move'].create({ |
| 277 |
'product_id': product.id, |
| 278 |
'location_id': production_location.id, |
| 279 |
'location_dest_id': incoming_picking_type.default_location_dest_id.id, |
| 280 |
'product_uom': product.uom_id.id, |
| 281 |
'product_uom_qty': 1, |
| 282 |
'picking_type_id': incoming_picking_type.id, |
| 283 |
'picking_id': picking.id, |
| 284 |
}) |
| 285 |
picking.action_confirm() |
| 286 |
picking.button_validate() |
| 287 |
self.pricelist.currency_id = new_company_currency.id |
| 288 |
partner = self.env['res.partner'].create({'name': 'Super Partner'}) |
| 289 |
so = self.env['sale.order'].create({ |
| 290 |
'name': 'Sale order', |
| 291 |
'partner_id': partner.id, |
| 292 |
'partner_invoice_id': partner.id, |
| 293 |
'pricelist_id': self.pricelist.id, |
| 294 |
}) |
| 295 |
sol = self._create_sale_order_line(so, product, 1, price_unit=200) |
| 296 |
|
| 297 |
self.env.user.company_id = main_company.id |
| 298 |
so.action_confirm() |
| 299 |
|
| 300 |
self.assertEqual(sol.purchase_price, 100) |
| 301 |
self.assertEqual(sol.margin, 100) |
| 302 |
|
| 303 |
def test_purchase_price_changes(self): |
| 304 |
self._setup_multicurrency() |
| 305 |
so = self._create_sale_order() |
| 306 |
product = self._create_product() |
| 307 |
product.categ_id.property_cost_method = 'standard' |
| 308 |
product.standard_price = 20 |
| 309 |
self._create_sale_order_line(so, product, 1, product.list_price) |
| 310 |
|
| 311 |
so_form = Form(so) |
| 312 |
with so_form.order_line.edit(0) as line: |
| 313 |
line.purchase_price = 15 |
| 314 |
so = so_form.save() |
| 315 |
email_act = so.action_quotation_send() |
| 316 |
email_ctx = email_act.get('context', {}) |
| 317 |
so.with_context(**email_ctx).message_post_with_source( |
| 318 |
self.env['mail.template'].browse(email_ctx.get('default_template_id')), |
| 319 |
subtype_id=self.env['ir.model.data']._xmlid_to_res_id('mail.mt_comment'), |
| 320 |
) |
| 321 |
|
| 322 |
self.assertEqual(so.state, 'sent') |
| 323 |
self.assertEqual(so.order_line[0].purchase_price, 15) |
| 324 |
so.action_confirm() |
| 325 |
self.assertEqual(so.order_line[0].purchase_price, 15) |
| 326 |
|
| 327 |
|
| 328 |
so.with_context(disable_cancel_warning=True).action_cancel() |
| 329 |
so.action_draft() |
| 330 |
so.currency_id = self.other_currency |
| 331 |
self.assertEqual(so.order_line.move_ids.state, 'cancel') |
| 332 |
self.assertEqual(so.order_line.purchase_price, 40) |
| 333 |
|
| 334 |
def test_add_product_on_delivery_price_unit_on_sale(self): |
| 335 |
""" Adding a product directly on a sale order's delivery should result in the new SOL |
| 336 |
having its `purchase_price` and `margin` + `margin_percent` fields correctly calculated. |
| 337 |
""" |
| 338 |
products = [self._create_product() for _ in range(2)] |
| 339 |
for product, cost, price in zip(products, [20, 10], [25, 20]): |
| 340 |
product.categ_id.property_cost_method = 'standard' |
| 341 |
product.write({ |
| 342 |
'standard_price': cost, |
| 343 |
'list_price': price, |
| 344 |
'invoice_policy': 'delivery', |
| 345 |
}) |
| 346 |
sale_order = self._create_sale_order() |
| 347 |
self._create_sale_order_line(sale_order, products[0], 10, products[0].list_price) |
| 348 |
sale_order.action_confirm() |
| 349 |
delivery = sale_order.picking_ids[0] |
| 350 |
with Form(delivery) as delivery_form: |
| 351 |
with delivery_form.move_ids.new() as move: |
| 352 |
move.product_id = products[1] |
| 353 |
move.product_uom_qty = 10 |
| 354 |
delivery.move_ids.quantity = 10 |
| 355 |
delivery.button_validate() |
| 356 |
self.assertRecordValues( |
| 357 |
sale_order.order_line.filtered(lambda sol: sol.product_id == products[1]), |
| 358 |
[{ |
| 359 |
'price_unit': products[1].list_price, |
| 360 |
'purchase_price': products[1].standard_price, |
| 361 |
'margin': 100, |
| 362 |
'margin_percent': 0.5, |
| 363 |
}] |
| 364 |
) |
| 365 |
|
| 366 |
def test_add_standard_product_on_delivery_cost_on_sale_order(self): |
| 367 |
""" test that if product with standard cost method is added in delivery, the cost is computed.""" |
| 368 |
product = self.product_standard |
| 369 |
product.write({ |
| 370 |
'standard_price': 20, |
| 371 |
'list_price': 25, |
| 372 |
'invoice_policy': 'order', |
| 373 |
}) |
| 374 |
product2 = self.env['product.product'].create({ |
| 375 |
'name': 'product2', |
| 376 |
'type': 'consu', |
| 377 |
'is_storable': True, |
| 378 |
'standard_price': 10, |
| 379 |
'list_price': 20, |
| 380 |
'invoice_policy': 'order', |
| 381 |
}) |
| 382 |
sale_order = self._create_sale_order() |
| 383 |
self._create_sale_order_line(sale_order, product, 10, product.list_price) |
| 384 |
sale_order.action_confirm() |
| 385 |
delivery = sale_order.picking_ids[0] |
| 386 |
with Form(delivery) as delivery_form: |
| 387 |
with delivery_form.move_ids.new() as move: |
| 388 |
move.product_id = product2 |
| 389 |
move.product_uom_qty = 10 |
| 390 |
delivery.move_ids.quantity = 10 |
| 391 |
delivery.button_validate() |
| 392 |
self.assertEqual(sale_order.order_line.filtered(lambda sol: sol.product_id == product2).purchase_price, 10) |
| 393 |
|
| 394 |
def test_add_avco_product_on_delivery_cost_on_sale_order(self): |
| 395 |
""" test that if product with avco cost method and an order "invoice_policy" is added in delivery, the cost is computed.""" |
| 396 |
categ_average = self.env['product.category'].create({ |
| 397 |
'name': 'AVERAGE', |
| 398 |
'property_cost_method': 'average' |
| 399 |
}) |
| 400 |
self.product = self.product_avco |
| 401 |
self.product.write({ |
| 402 |
'standard_price': 20, |
| 403 |
'list_price': 25, |
| 404 |
'invoice_policy': 'order', |
| 405 |
}) |
| 406 |
product2 = self.env['product.product'].create({ |
| 407 |
'name': 'product2', |
| 408 |
'type': 'consu', |
| 409 |
'is_storable': True, |
| 410 |
'categ_id': categ_average.id, |
| 411 |
'standard_price': 10, |
| 412 |
'list_price': 20, |
| 413 |
'invoice_policy': 'order', |
| 414 |
}) |
| 415 |
sale_order = self._create_sale_order() |
| 416 |
self._create_sale_order_line(sale_order, self.product, 10, self.product.list_price) |
| 417 |
sale_order.action_confirm() |
| 418 |
delivery = sale_order.picking_ids[0] |
| 419 |
with Form(delivery) as delivery_form: |
| 420 |
with delivery_form.move_ids.new() as move: |
| 421 |
move.product_id = product2 |
| 422 |
move.product_uom_qty = 10 |
| 423 |
delivery.move_ids.quantity = 10 |
| 424 |
delivery.button_validate() |
| 425 |
self.assertEqual(sale_order.order_line.filtered(lambda sol: sol.product_id == product2).purchase_price, 10) |
| 426 |
|
| 427 |
def test_avco_does_not_mix_products_on_compute_avg_price(self): |
| 428 |
""" |
| 429 |
Ensure that when stock moves are duplicated and their product changed, |
| 430 |
the sale line linkage is cleared correctly, preventing average price |
| 431 |
computation from mixing valuation layers of different products. |
| 432 |
This test verifies that: |
| 433 |
- The duplicated delivery's moves lose the original sale_line_id when the product changes. |
| 434 |
- A new sale order line is created for the new product, increasing the total order lines. |
| 435 |
- Validations of deliveries and return pickings proceed without errors. |
| 436 |
- The purchase price on the original sale line remains accurate (unchanged). |
| 437 |
""" |
| 438 |
self.product_avco_auto.uom_id = self.env.ref('uom.product_uom_dozen').id |
| 439 |
sale_order = self._create_sale_order() |
| 440 |
sale_order_line = self._create_sale_order_line(sale_order, self.product_avco, 1) |
| 441 |
sale_order.action_confirm() |
| 442 |
|
| 443 |
first_delivery = sale_order.picking_ids |
| 444 |
second_delivery = first_delivery.copy() |
| 445 |
self.assertEqual(second_delivery.move_ids.sale_line_id, sale_order_line) |
| 446 |
second_delivery.move_ids.product_id = self.product_avco_auto |
| 447 |
self.assertFalse(second_delivery.move_ids.sale_line_id) |
| 448 |
self.assertTrue(len(sale_order.order_line), 2) |
| 449 |
second_delivery.action_confirm() |
| 450 |
second_delivery.move_ids.quantity = 1 |
| 451 |
second_delivery.button_validate() |
| 452 |
self.assertEqual(second_delivery.move_ids.sale_line_id, sale_order.order_line - sale_order_line) |
| 453 |
stock_picking_return = self.env['stock.return.picking'].create({ |
| 454 |
'picking_id': second_delivery.id, |
| 455 |
}) |
| 456 |
stock_picking_return.product_return_moves.quantity = 1 |
| 457 |
return_picking = stock_picking_return._create_return() |
| 458 |
return_picking.move_ids.quantity = 1 |
| 459 |
return_picking.button_validate() |
| 460 |
self.assertEqual(return_picking.state, 'done') |
| 461 |
|
| 462 |
first_delivery.move_ids.quantity = 1 |
| 463 |
first_delivery.button_validate() |
| 464 |
self.assertEqual(first_delivery.state, 'done') |
| 465 |
self.assertEqual(sale_order_line.purchase_price, 10) |
| 466 |
|
| 467 |
def test_avco_different_uom(self): |
| 468 |
pack_of_6 = self.ref('uom.product_uom_pack_6') |
| 469 |
self.product_avco.write({ |
| 470 |
'standard_price': 1, |
| 471 |
'list_price': 3, |
| 472 |
'uom_ids': [pack_of_6], |
| 473 |
}) |
| 474 |
sale_order = self._create_sale_order() |
| 475 |
sale_order_line = self.env['sale.order.line'].create({ |
| 476 |
'name': 'Sale order', |
| 477 |
'order_id': sale_order.id, |
| 478 |
'product_id': self.product_avco.id, |
| 479 |
'product_uom_qty': 1, |
| 480 |
'product_uom_id': pack_of_6, |
| 481 |
}) |
| 482 |
sale_order.action_confirm() |
| 483 |
self.assertEqual(sale_order_line.margin, 12.0) |
| 484 |
|
| 485 |
def test_avco_calc(self): |
| 486 |
""" test purchase_price and margin correct calculation for avco product""" |
| 487 |
|
| 488 |
with freeze_time() as freeze: |
| 489 |
so = self._create_sale_order() |
| 490 |
self.product_avco_auto.list_price = 100 |
| 491 |
self._make_in_move(self.product_avco_auto, 2, 20) |
| 492 |
self._make_in_move(self.product_avco_auto, 2, 40) |
| 493 |
self.assertEqual(self.product_avco_auto.standard_price, 30, "standard_price for avco = (2 * 20 + 2 * 40) / (2 + 2) = 30: 4 in stock") |
| 494 |
freeze.tick(delta=datetime.timedelta(seconds=2)) |
| 495 |
|
| 496 |
|
| 497 |
sol = self._create_sale_order_line(so, self.product_avco_auto, 2, 100) |
| 498 |
self.assertEqual(sol.product_uom_qty, 2) |
| 499 |
self.assertEqual(sol.qty_delivered, 0) |
| 500 |
self.assertEqual(sol.purchase_price, 30, "purchase_price should match product's standard_price") |
| 501 |
self.assertEqual(sol.margin, 140, "margin = (sale price - purchase_price) * SOL quantity = (100 - 30) * 2 = 140") |
| 502 |
|
| 503 |
|
| 504 |
so.action_confirm() |
| 505 |
move = sol.move_ids |
| 506 |
move.quantity = 1 |
| 507 |
delivery = move.picking_id |
| 508 |
backorder_wizard_values = delivery.button_validate() |
| 509 |
backorder_wizard = self.env[(backorder_wizard_values.get('res_model'))].browse(backorder_wizard_values.get('res_id')).with_context(backorder_wizard_values['context']) |
| 510 |
backorder_wizard.process() |
| 511 |
|
| 512 |
|
| 513 |
self.assertEqual(sol.purchase_price, 30, "purchase_price = (qty_delivered * purchase_unit_from_delivery + qty_from_std_price * standard_price)/(qty_from_delivery + qty_from_std_price) = (1 * 30 + 1 * 30)/ (1 + 1) = 30") |
| 514 |
self.assertEqual(sol.margin, 140, "margin = (sale price - purchase_price) * SOL quantity = (100 - 30) * 2 = 140") |
| 515 |
freeze.tick(delta=datetime.timedelta(seconds=2)) |
| 516 |
|
| 517 |
|
| 518 |
self._make_in_move(self.product_avco_auto, 2, 142.5) |
| 519 |
self.assertEqual(self.product_avco_auto.standard_price, 75, "standard_price for avco = (3 * 30 + 2 * 142.5) / (3 + 2) = 75: 3 remaining + 2 added to stock") |
| 520 |
self.assertEqual(sol.purchase_price, 30, "purchase_price shouldn't have changed") |
| 521 |
freeze.tick(delta=datetime.timedelta(seconds=2)) |
| 522 |
move = sol.move_ids.filtered(lambda m: m.state != 'done') |
| 523 |
move.quantity = 2 |
| 524 |
delivery = move.picking_id |
| 525 |
delivery.button_validate() |
| 526 |
|
| 527 |
|
| 528 |
self.assertEqual(sol.purchase_price, 60, "purchase_price = (qty_delivered * purchase_unit_from_delivery + qty_from_std_price * standard_price)/(qty_from_delivery + qty_from_std_price) = (3 * 60 + 0 * 75)/ (3 + 0) = 60") |
| 529 |
self.assertEqual(sol.margin, 80, "margin = (sale price - purchase_price) * SOL quantity = (100 - 60) * 2 = 80") |
| 530 |
|
| 531 |
def test_avco_zero_quantity(self): |
| 532 |
""" test that the purchase_price and margin are still calculated correctly when 0 quantity SOL |
| 533 |
including when a return is done for avco valuated product""" |
| 534 |
|
| 535 |
with freeze_time() as freeze: |
| 536 |
so = self._create_sale_order() |
| 537 |
self.product_avco_auto.list_price = 100 |
| 538 |
self._make_in_move(self.product_avco_auto, 2, 20) |
| 539 |
self._make_in_move(self.product_avco_auto, 2, 40) |
| 540 |
self.assertEqual(self.product_avco_auto.standard_price, 30, "standard_price for avco = (2 * 20 + 2 * 40) / (2 + 2) = 30: 4 in stock") |
| 541 |
sol = self._create_sale_order_line(so, self.product_avco_auto, 1, 100) |
| 542 |
|
| 543 |
self.assertEqual(sol.product_uom_qty, 1) |
| 544 |
self.assertEqual(sol.qty_delivered, 0) |
| 545 |
self.assertEqual(sol.purchase_price, self.product_avco_auto.standard_price, "purchase_price should match product's standard_price") |
| 546 |
self.assertEqual(sol.margin, 70, "margin = (sale price - purchase_price) * SOL quantity = (100 - 30) * 1 = 70") |
| 547 |
so.action_confirm() |
| 548 |
|
| 549 |
|
| 550 |
sol2 = self._create_sale_order_line(so, self.product_avco_auto, 0, 90) |
| 551 |
self.assertEqual(sol2.product_uom_qty, 0) |
| 552 |
self.assertEqual(sol2.qty_delivered, 0) |
| 553 |
self.assertEqual(sol2.purchase_price, self.product_avco_auto.standard_price, "0 Quantity and 0 Delivered => default to standard price") |
| 554 |
self.assertEqual(sol2.margin, 0, "margin = 0 if no quantities sold/delivered") |
| 555 |
|
| 556 |
|
| 557 |
self._make_in_move(self.product_avco_auto, 2, 60) |
| 558 |
self.assertEqual(self.product_avco_auto.standard_price, 40, "standard_price for avco = (4 * 30 + 2 * 60) / (4 + 2) = 40: 4 existing + 2 added to stock") |
| 559 |
freeze.tick(delta=datetime.timedelta(seconds=2)) |
| 560 |
move = sol.move_ids |
| 561 |
move.quantity = sol.product_uom_qty |
| 562 |
delivery = move.picking_id |
| 563 |
delivery.button_validate() |
| 564 |
self.assertEqual(sol.product_uom_qty, 1) |
| 565 |
self.assertEqual(sol.qty_delivered, 1) |
| 566 |
self.assertEqual(sol.purchase_price, self.product_avco_auto.standard_price, "purchase_price should match product's standard_price") |
| 567 |
self.assertEqual(sol.margin, 60, "margin = (sale price - purchase_price) * SOL quantity = (100 - 40) * 1 = 60") |
| 568 |
freeze.tick(delta=datetime.timedelta(seconds=2)) |
| 569 |
|
| 570 |
|
| 571 |
self._make_in_move(self.product_avco_auto, 2, 5) |
| 572 |
self.assertEqual(self.product_avco_auto.standard_price, 30, "standard_price for avco = (5 * 40 + 2 * 5) / (5 + 2) = 30: 1 delivered, 5 remaining + 2 added to stock") |
| 573 |
freeze.tick(delta=datetime.timedelta(seconds=2)) |
| 574 |
stock_return_picking_form = Form(self.env['stock.return.picking'].with_context(active_id=delivery.id, active_model='stock.picking')) |
| 575 |
stock_return_picking = stock_return_picking_form.save() |
| 576 |
stock_return_picking.product_return_moves.quantity = 3.0 |
| 577 |
stock_return_picking_action = stock_return_picking.action_create_returns() |
| 578 |
return_pick = self.env['stock.picking'].browse(stock_return_picking_action['res_id']) |
| 579 |
return_pick.button_validate() |
| 580 |
self.assertEqual(sol.product_uom_qty, 1) |
| 581 |
self.assertEqual(sol.qty_delivered, -2) |
| 582 |
self.assertEqual(self.product_avco_auto.standard_price, 33, "standard_price for avco = (7 * 30 + 3 * 40) / (7 + 3)) = 33: 7 remaining + 3 returned") |
| 583 |
self.assertEqual(sol.purchase_price, self.product_avco_auto.standard_price, "< 0 Delivered => default to standard price") |
| 584 |
self.assertEqual(sol.margin, 67, "margin = (sale price - purchase_price) * SOL quantity = (100 - 33) * 1 = 67") |
| 585 |
freeze.tick(delta=datetime.timedelta(seconds=2)) |
| 586 |
|
| 587 |
|
| 588 |
self._make_in_move(self.product_avco_auto, 2, 30) |
| 589 |
self.assertEqual(self.product_avco_auto.standard_price, 32.5, "standard_price for avco = (10 * 33 + 2 * 30) / 12 = 32.5: 10 remaining + 2 added to stock") |
| 590 |
freeze.tick(delta=datetime.timedelta(seconds=2)) |
| 591 |
sol.product_uom_qty = 0 |
| 592 |
self.assertEqual(sol.purchase_price, self.product_avco_auto.standard_price, "< 0 Delivered => default to standard price") |
| 593 |
self.assertEqual(sol.margin, -135, "margin = (sale price - purchase_price) * qty_delivered = (100 - 32.5) * -2 = -135") |
| 594 |
|
| 595 |
|
| 596 |
so2 = self._create_sale_order() |
| 597 |
|
| 598 |
throwaway_sol = self._create_sale_order_line(so2, self.product_standard, 1, 100) |
| 599 |
so2.action_confirm() |
| 600 |
move = throwaway_sol.move_ids |
| 601 |
move.quantity = throwaway_sol.product_uom_qty |
| 602 |
delivery = move.picking_id |
| 603 |
with Form(delivery) as delivery_form: |
| 604 |
with delivery_form.move_ids.new() as extra_move: |
| 605 |
extra_move.product_id = self.product_avco_auto |
| 606 |
extra_move.quantity = 2 |
| 607 |
delivery.button_validate() |
| 608 |
sol3 = so2.order_line - throwaway_sol |
| 609 |
self.assertEqual(sol3.product_uom_qty, 0) |
| 610 |
self.assertEqual(sol3.qty_delivered, 2) |
| 611 |
self.assertEqual(self.product_avco_auto.standard_price, 32.5, 'no new incoming moves, std price should be unchanged') |
| 612 |
|
| 613 |
self.assertEqual(sol3.purchase_price, self.product_avco_auto.standard_price, "purchase_price should match product's standard_price") |
| 614 |
self.assertEqual(sol3.margin, -65, "margin = SOL qty * sale price - purchase_price * qty_delivered = (0 - 32.5) * 2 = -65") |
| 615 |
freeze.tick(delta=datetime.timedelta(seconds=2)) |
| 616 |
|
| 617 |
|
| 618 |
self._make_in_move(self.product_avco_auto, 2, 17.5) |
| 619 |
self.assertEqual(self.product_avco_auto.standard_price, 30, "standard_price for avco = (10 * 32.5 + 2 * 17.5) / (10 + 2) = 30: 2 delivered, 10 remaining + 2 added to stock") |
| 620 |
freeze.tick(delta=datetime.timedelta(seconds=2)) |
| 621 |
stock_return_picking_form = Form(self.env['stock.return.picking'].with_context(active_id=delivery.id, active_model='stock.picking')) |
| 622 |
stock_return_picking = stock_return_picking_form.save() |
| 623 |
stock_return_picking.product_return_moves.quantity = 1.0 |
| 624 |
stock_return_picking_action = stock_return_picking.action_create_returns() |
| 625 |
return_pick = self.env['stock.picking'].browse(stock_return_picking_action['res_id']) |
| 626 |
return_pick.button_validate() |
| 627 |
self.assertEqual(sol3.product_uom_qty, 0) |
| 628 |
self.assertEqual(sol3.qty_delivered, 1) |
| 629 |
|
| 630 |
self.assertEqual(sol3.purchase_price, 32.5, "purchase_price = 2 * 32.5 + 1 * 32.5) / (2 + 1) = 32.5") |
| 631 |
self.assertEqual(sol3.margin, -32.5, "margin = SOL qty * sale price - purchase_price * qty_delivered = (0 - 32.5) * 1 = -32.5") |