diff --git a/account_invoice_supplierinfo_update/models/account_move_line.py b/account_invoice_supplierinfo_update/models/account_move_line.py index dc0b6a26c6a..18d6220ff8d 100644 --- a/account_invoice_supplierinfo_update/models/account_move_line.py +++ b/account_invoice_supplierinfo_update/models/account_move_line.py @@ -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 diff --git a/account_invoice_supplierinfo_update/tests/test_module.py b/account_invoice_supplierinfo_update/tests/test_module.py index 18ffa4523e8..17097e5a277 100644 --- a/account_invoice_supplierinfo_update/tests/test_module.py +++ b/account_invoice_supplierinfo_update/tests/test_module.py @@ -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