Skip to content

Commit

Permalink
account_invoice_supplierinfo_update: fix updating the right supplierinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbeau committed Mar 28, 2024
1 parent f9a36d8 commit a1f2417
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ def _get_supplierinfo(self):
"""Given an invoice line, return the supplierinfo that matches
with product and supplier, if exist"""
self.ensure_one()
supplierinfos = self.product_id.seller_ids.filtered(
lambda seller: seller.partner_id == self.move_id.supplier_partner_id
supplierinfos = self.product_id._select_seller(
partner_id=self.move_id.supplier_partner_id,
quantity=self.quantity,
)
return supplierinfos and supplierinfos[0] or False

Expand Down
100 changes: 100 additions & 0 deletions account_invoice_supplierinfo_update/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,106 @@ def setUpClass(cls, chart_template_ref=None):
cls.WizardUpdateSupplierinfo = cls.env["wizard.update.invoice.supplierinfo"]
cls.ProductSupplierinfo = cls.env["product.supplierinfo"]

def test_get_the_right_variant_supplierinfo(self):
# Variant the product A and set a price on variation 1
tmpl_a = self.product_a.product_tmpl_id
tmpl_a.write(
{
"attribute_line_ids": [
(
0,
0,
{
"attribute_id": self.env.ref(
"product.product_attribute_2"
).id,
"value_ids": [
(
6,
0,
[
self.env.ref(
"product.product_attribute_value_3"
).id,
self.env.ref(
"product.product_attribute_value_4"
).id,
],
)
],
},
)
]
}
)
product_a_1, product_a_2 = tmpl_a.product_variant_ids

supplier_product_a_1 = self.env["product.supplierinfo"].create(
[
{
"partner_id": self.invoice.supplier_partner_id.id,
"product_tmpl_id": tmpl_a.id,
"product_id": product_a_1.id,
"price": 30,
}
]
)

# Set the variation 2 on the invoice and run the wizard
self.line_a.write({"product_id": product_a_2, "price_unit": 400})
vals_wizard = self.invoice.check_supplierinfo().get("context", {})
line_ids = vals_wizard.get("default_line_ids", {})

self.assertEqual(line_ids[0][2]["current_price"], False)
self.assertEqual(line_ids[0][2]["new_price"], 400.0)

wizard = self.WizardUpdateSupplierinfo.create(
{"line_ids": line_ids, "invoice_id": self.invoice.id}
)
wizard.update_supplierinfo()

# Supplier of product_a_1 should be not updated and a new supplierinfo
# have been created (to make it simple supplierinfo are always created
# on template)
self.assertEqual(supplier_product_a_1.price, 30)
self.assertEqual(len(tmpl_a.seller_ids), 2)
self.assertEqual(tmpl_a.seller_ids[1].price, 400)
self.assertFalse(tmpl_a.seller_ids[1].product_id)

def test_get_the_right_qty_supplierinfo(self):
tmpl_a = self.product_a.product_tmpl_id
self.env["product.supplierinfo"].create(
[
{
"partner_id": self.invoice.supplier_partner_id.id,
"product_tmpl_id": tmpl_a.id,
"price": 500,
"min_qty": 0,
},
{
"partner_id": self.invoice.supplier_partner_id.id,
"product_tmpl_id": tmpl_a.id,
"price": 300,
"min_qty": 20,
},
]
)

vals_wizard = self.invoice.check_supplierinfo().get("context", {})
line_ids = vals_wizard.get("default_line_ids", {})

self.assertEqual(line_ids[0][2]["current_price"], 500)
self.assertEqual(line_ids[0][2]["new_price"], 400.0)

wizard = self.WizardUpdateSupplierinfo.create(
{"line_ids": line_ids, "invoice_id": self.invoice.id}
)
wizard.update_supplierinfo()

self.assertEqual(len(tmpl_a.seller_ids), 2)
self.assertEqual(tmpl_a.seller_ids[0].price, 300)
self.assertEqual(tmpl_a.seller_ids[1].price, 400)

def test_update_pricelist_supplierinfo(self):
# supplier invoice with pricelist supplierinfo to update and
# product supplierinfo is on product_template
Expand Down

0 comments on commit a1f2417

Please sign in to comment.