test_onchange_product.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from datetime import datetime
  4. from odoo import fields
  5. from odoo.tests.common import TransactionCase, Form
  6. from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
  7. class TestOnchangeProductId(TransactionCase):
  8. """Test that when an included tax is mapped by a fiscal position, the included tax must be
  9. subtracted to the price of the product.
  10. """
  11. @classmethod
  12. def setUpClass(cls):
  13. super().setUpClass()
  14. cls.fiscal_position_model = cls.env['account.fiscal.position']
  15. cls.fiscal_position_tax_model = cls.env['account.fiscal.position.tax']
  16. cls.tax_model = cls.env['account.tax']
  17. cls.po_model = cls.env['purchase.order']
  18. cls.po_line_model = cls.env['purchase.order.line']
  19. cls.res_partner_model = cls.env['res.partner']
  20. cls.product_tmpl_model = cls.env['product.template']
  21. cls.product_model = cls.env['product.product']
  22. cls.product_uom_model = cls.env['uom.uom']
  23. cls.supplierinfo_model = cls.env["product.supplierinfo"]
  24. def test_onchange_product_id(self):
  25. # Required for `product_uom` to be visible in the view
  26. self.env.user.groups_id += self.env.ref('uom.group_uom')
  27. uom_id = self.product_uom_model.search([('name', '=', 'Units')])[0]
  28. partner_id = self.res_partner_model.create(dict(name="George"))
  29. tax_include_id = self.tax_model.create(dict(name="Include tax",
  30. amount='21.00',
  31. price_include=True,
  32. type_tax_use='purchase'))
  33. tax_exclude_id = self.tax_model.create(dict(name="Exclude tax",
  34. amount='0.00',
  35. type_tax_use='purchase'))
  36. supplierinfo_vals = {
  37. 'partner_id': partner_id.id,
  38. 'price': 121.0,
  39. }
  40. supplierinfo = self.supplierinfo_model.create(supplierinfo_vals)
  41. product_tmpl_id = self.product_tmpl_model.create(dict(name="Voiture",
  42. list_price=121,
  43. seller_ids=[(6, 0, [supplierinfo.id])],
  44. supplier_taxes_id=[(6, 0, [tax_include_id.id])]))
  45. product_id = product_tmpl_id.product_variant_id
  46. fp_id = self.fiscal_position_model.create(dict(name="fiscal position", sequence=1))
  47. fp_tax_id = self.fiscal_position_tax_model.create(dict(position_id=fp_id.id,
  48. tax_src_id=tax_include_id.id,
  49. tax_dest_id=tax_exclude_id.id))
  50. po_vals = {
  51. 'partner_id': partner_id.id,
  52. 'fiscal_position_id': fp_id.id,
  53. 'order_line': [
  54. (0, 0, {
  55. 'name': product_id.name,
  56. 'product_id': product_id.id,
  57. 'product_qty': 1.0,
  58. 'product_uom': uom_id.id,
  59. 'price_unit': 121.0,
  60. 'date_planned': datetime.today().strftime(DEFAULT_SERVER_DATETIME_FORMAT),
  61. })],
  62. }
  63. po = self.po_model.create(po_vals)
  64. po_line = po.order_line[0]
  65. po_line.onchange_product_id()
  66. self.assertEqual(100, po_line.price_unit, "The included tax must be subtracted to the price")
  67. supplierinfo.write({'min_qty': 24})
  68. po_line.write({'product_qty': 20})
  69. self.assertEqual(0, po_line.price_unit, "Unit price should be reset to 0 since the supplier supplies minimum of 24 quantities")
  70. po_line.write({'product_qty': 3, 'product_uom': self.ref("uom.product_uom_dozen")})
  71. self.assertEqual(1200, po_line.price_unit, "Unit price should be 1200 for one Dozen")
  72. ipad_uom = self.env['uom.category'].create({'name': 'Ipad Unit'})
  73. ipad_lot = self.env['uom.uom'].create({
  74. 'name': 'Ipad',
  75. 'category_id': ipad_uom.id,
  76. 'uom_type': 'reference',
  77. 'rounding': 0.001
  78. })
  79. ipad_lot_10 = self.env['uom.uom'].create({
  80. 'name': '10 Ipad',
  81. 'category_id': ipad_uom.id,
  82. 'uom_type': 'bigger',
  83. 'rounding': 0.001,
  84. "factor_inv": 10
  85. })
  86. product_ipad = self.env['product.product'].create({
  87. 'name': 'Conference Chair',
  88. 'standard_price': 100,
  89. 'uom_id': ipad_lot.id,
  90. 'uom_po_id': ipad_lot.id,
  91. })
  92. po_line2 = self.po_line_model.create({
  93. 'name': product_ipad.name,
  94. 'product_id': product_ipad.id,
  95. 'order_id': po.id,
  96. 'product_qty': 5,
  97. 'product_uom': ipad_uom.id,
  98. 'date_planned': fields.Date().today()
  99. })
  100. po_line2.onchange_product_id()
  101. self.assertEqual(100, po_line2.price_unit, "No vendor supplies this product, hence unit price should be set to 100")
  102. po_form = Form(po)
  103. with po_form.order_line.edit(1) as order_line:
  104. order_line.product_uom = ipad_lot_10
  105. po_form.save()
  106. self.assertEqual(1000, po_line2.price_unit, "The product_uom is multiplied by 10, hence unit price should be set to 1000")