diff --git a/purchase_order_supplierinfo_update/README.rst b/purchase_order_supplierinfo_update/README.rst new file mode 100644 index 00000000000..d9fdc965dea --- /dev/null +++ b/purchase_order_supplierinfo_update/README.rst @@ -0,0 +1,124 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +================================== +Purchase Order Supplierinfo Update +================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:7e804f3344c4e3f15a5415eab410a29ebf023c707aa02ca6903a7434dbb8f277 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fpurchase--workflow-lightgray.png?logo=github + :target: https://github.com/OCA/purchase-workflow/tree/19.0/purchase_order_supplierinfo_update + :alt: OCA/purchase-workflow +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/purchase-workflow-19-0/purchase-workflow-19-0-purchase_order_supplierinfo_update + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/purchase-workflow&target_branch=19.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module extends the functionality of purchase to allow the price and +the discount of the supplier-info to be updated automatically for each +product of the purchase when it is confirmed. This will allow that when +a purchase order is created, the suggested price and suggested discount +for those products is the last one purchased. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module, you need to: + +1. Go to *Purchase > Orders > Requests for Quotation* and create a new + Requests for Quotation with several products. +2. Confirm that quotation and the unit prices defined on the lines will + be stored in the supplier-info (related to the quotation supplier) of + the products. +3. By default the suggested price and the suggested discount for the + products is the one stored in supplier-info, so in this way, if you + create another quotation with the same supplier and the same + products, the suggested price and the suggested discount for each + product will be the same price and same discount at which the product + was last purchased. + +Known issues / Roadmap +====================== + +- Refactor that module to share algorithm with similar module + account_invoice_supplierinfo_update. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Tecnativa + +Contributors +------------ + +- `Tecnativa `__: + + - Ernesto Tejeda + - Carlos Dauden + +- Daniel Reis , `Open Source + Integrators `__: +- `Heliconia Solutions Pvt. Ltd. `__ + + - Bhavesh Heliconia + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-ernestotejeda| image:: https://github.com/ernestotejeda.png?size=40px + :target: https://github.com/ernestotejeda + :alt: ernestotejeda + +Current `maintainer `__: + +|maintainer-ernestotejeda| + +This module is part of the `OCA/purchase-workflow `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/purchase_order_supplierinfo_update/__init__.py b/purchase_order_supplierinfo_update/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/purchase_order_supplierinfo_update/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/purchase_order_supplierinfo_update/__manifest__.py b/purchase_order_supplierinfo_update/__manifest__.py new file mode 100644 index 00000000000..897548f9aa5 --- /dev/null +++ b/purchase_order_supplierinfo_update/__manifest__.py @@ -0,0 +1,13 @@ +# Copyright 2021 Tecnativa - Ernesto Tejeda +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Purchase Order Supplierinfo Update", + "summary": "Update product supplierinfo with the last purchase price", + "version": "19.0.1.0.0", + "category": "Purchase", + "website": "https://github.com/OCA/purchase-workflow", + "author": "Tecnativa, Odoo Community Association (OCA)", + "maintainers": ["ernestotejeda"], + "license": "AGPL-3", + "depends": ["purchase"], +} diff --git a/purchase_order_supplierinfo_update/i18n/it.po b/purchase_order_supplierinfo_update/i18n/it.po new file mode 100644 index 00000000000..655da4683aa --- /dev/null +++ b/purchase_order_supplierinfo_update/i18n/it.po @@ -0,0 +1,27 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * purchase_order_supplierinfo_update +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2023-12-24 15:34+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 4.17\n" + +#. module: purchase_order_supplierinfo_update +#: model:ir.model,name:purchase_order_supplierinfo_update.model_purchase_order +msgid "Purchase Order" +msgstr "Ordine di acquisto" + +#. module: purchase_order_supplierinfo_update +#: model:ir.model,name:purchase_order_supplierinfo_update.model_purchase_order_line +msgid "Purchase Order Line" +msgstr "Riga ordine di acquisto" diff --git a/purchase_order_supplierinfo_update/i18n/pt_BR.po b/purchase_order_supplierinfo_update/i18n/pt_BR.po new file mode 100644 index 00000000000..0f06533e7f8 --- /dev/null +++ b/purchase_order_supplierinfo_update/i18n/pt_BR.po @@ -0,0 +1,27 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * purchase_order_supplierinfo_update +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2023-10-23 14:32+0000\n" +"Last-Translator: Adriano Prado \n" +"Language-Team: none\n" +"Language: pt_BR\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n > 1;\n" +"X-Generator: Weblate 4.17\n" + +#. module: purchase_order_supplierinfo_update +#: model:ir.model,name:purchase_order_supplierinfo_update.model_purchase_order +msgid "Purchase Order" +msgstr "Pedido de Compra" + +#. module: purchase_order_supplierinfo_update +#: model:ir.model,name:purchase_order_supplierinfo_update.model_purchase_order_line +msgid "Purchase Order Line" +msgstr "Linha Pedido de Compra" diff --git a/purchase_order_supplierinfo_update/i18n/purchase_order_supplierinfo_update.pot b/purchase_order_supplierinfo_update/i18n/purchase_order_supplierinfo_update.pot new file mode 100644 index 00000000000..4d912443859 --- /dev/null +++ b/purchase_order_supplierinfo_update/i18n/purchase_order_supplierinfo_update.pot @@ -0,0 +1,24 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * purchase_order_supplierinfo_update +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 18.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: purchase_order_supplierinfo_update +#: model:ir.model,name:purchase_order_supplierinfo_update.model_purchase_order +msgid "Purchase Order" +msgstr "" + +#. module: purchase_order_supplierinfo_update +#: model:ir.model,name:purchase_order_supplierinfo_update.model_purchase_order_line +msgid "Purchase Order Line" +msgstr "" diff --git a/purchase_order_supplierinfo_update/models/__init__.py b/purchase_order_supplierinfo_update/models/__init__.py new file mode 100644 index 00000000000..9a32d2efa63 --- /dev/null +++ b/purchase_order_supplierinfo_update/models/__init__.py @@ -0,0 +1,2 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from . import purchase_order diff --git a/purchase_order_supplierinfo_update/models/purchase_order.py b/purchase_order_supplierinfo_update/models/purchase_order.py new file mode 100644 index 00000000000..e29c7860181 --- /dev/null +++ b/purchase_order_supplierinfo_update/models/purchase_order.py @@ -0,0 +1,74 @@ +# Copyright 2021 Tecnativa - Ernesto Tejeda +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + + +class PurchaseOrder(models.Model): + _inherit = "purchase.order" + + def write(self, vals): + no_updated = self.filtered(lambda r: r.state not in ["purchase", "done"]) + res = super().write(vals) + if vals.get("state", "") in ["purchase", "done"]: + no_updated.mapped("order_line").update_supplierinfo_price() + return res + + +class PurchaseOrderLine(models.Model): + _inherit = "purchase.order.line" + + @api.model_create_multi + def create(self, vals_list): + res = super().create(vals_list) + res.update_supplierinfo_price() + return res + + def write(self, vals): + res = super().write(vals) + if vals.get("price_unit") or vals.get("discount"): + self.update_supplierinfo_price() + return res + + def update_supplierinfo_price(self): + for line in self.filtered( + lambda r: not r.display_type and r.order_id.state in ["purchase", "done"] + ): + domain = [ + ("partner_id", "=", line.partner_id.id), + ("product_id", "=", line.product_id.id), + ("date_order", ">", line.date_order), + ] + if not self.env["purchase.order.line"].search_count(domain, limit=1): + params = {"order_id": line.order_id} + seller = line.product_id._select_seller( + partner_id=line.partner_id, + quantity=line.product_qty, + date=line.order_id.date_order and line.order_id.date_order.date(), + uom_id=line.product_uom_id, + params=params, + ) + if seller: + line._update_supplierinfo(seller) + + def _update_supplierinfo(self, seller): + self.ensure_one() + new_seller_price = self.price_unit + # convert according to the currency if necessary + if self.currency_id and self.currency_id != seller.currency_id: + new_seller_price = self.currency_id._convert( + new_seller_price, + seller.currency_id, + seller.company_id, + self.date_order or fields.Date.today(), + ) + # convert according to the UoM if necessary + if self.product_uom_id and self.product_uom_id != seller.product_uom_id: + new_seller_price = self.product_uom_id._compute_price( + new_seller_price, seller.product_uom_id + ) + # Set price + if new_seller_price != seller.price: + seller.sudo().price = new_seller_price + if self.discount != seller.discount: + seller.sudo().discount = self.discount diff --git a/purchase_order_supplierinfo_update/pyproject.toml b/purchase_order_supplierinfo_update/pyproject.toml new file mode 100644 index 00000000000..4231d0cccb3 --- /dev/null +++ b/purchase_order_supplierinfo_update/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/purchase_order_supplierinfo_update/readme/CONTRIBUTORS.md b/purchase_order_supplierinfo_update/readme/CONTRIBUTORS.md new file mode 100644 index 00000000000..a6929b21e22 --- /dev/null +++ b/purchase_order_supplierinfo_update/readme/CONTRIBUTORS.md @@ -0,0 +1,7 @@ +- [Tecnativa](https://www.tecnativa.com): + - Ernesto Tejeda + - Carlos Dauden +- Daniel Reis \<\>, [Open Source + Integrators](https://www.opensourceintegrators.eu): +- [Heliconia Solutions Pvt. Ltd.](https://www.heliconia.io) + - Bhavesh Heliconia diff --git a/purchase_order_supplierinfo_update/readme/DESCRIPTION.md b/purchase_order_supplierinfo_update/readme/DESCRIPTION.md new file mode 100644 index 00000000000..d2f373a4eb8 --- /dev/null +++ b/purchase_order_supplierinfo_update/readme/DESCRIPTION.md @@ -0,0 +1,5 @@ +This module extends the functionality of purchase to allow the price and the discount of +the supplier-info to be updated automatically for each product of the +purchase when it is confirmed. This will allow that when a purchase +order is created, the suggested price and suggested discount for those products is the last one +purchased. diff --git a/purchase_order_supplierinfo_update/readme/ROADMAP.md b/purchase_order_supplierinfo_update/readme/ROADMAP.md new file mode 100644 index 00000000000..cb1006086a4 --- /dev/null +++ b/purchase_order_supplierinfo_update/readme/ROADMAP.md @@ -0,0 +1,2 @@ +- Refactor that module to share algorithm with similar module + account_invoice_supplierinfo_update. diff --git a/purchase_order_supplierinfo_update/readme/USAGE.md b/purchase_order_supplierinfo_update/readme/USAGE.md new file mode 100644 index 00000000000..879422a62ef --- /dev/null +++ b/purchase_order_supplierinfo_update/readme/USAGE.md @@ -0,0 +1,12 @@ +To use this module, you need to: + +1. Go to *Purchase \> Orders \> Requests for Quotation* and create a + new Requests for Quotation with several products. +2. Confirm that quotation and the unit prices defined on the lines will + be stored in the supplier-info (related to the quotation supplier) + of the products. +3. By default the suggested price and the suggested discount for the products is the one stored in + supplier-info, so in this way, if you create another quotation with + the same supplier and the same products, the suggested price and the suggested discount for + each product will be the same price and same discount at which the product was last + purchased. diff --git a/purchase_order_supplierinfo_update/static/description/icon.png b/purchase_order_supplierinfo_update/static/description/icon.png new file mode 100644 index 00000000000..3a0328b516c Binary files /dev/null and b/purchase_order_supplierinfo_update/static/description/icon.png differ diff --git a/purchase_order_supplierinfo_update/static/description/index.html b/purchase_order_supplierinfo_update/static/description/index.html new file mode 100644 index 00000000000..711f8650a8f --- /dev/null +++ b/purchase_order_supplierinfo_update/static/description/index.html @@ -0,0 +1,471 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Purchase Order Supplierinfo Update

+ +

Beta License: AGPL-3 OCA/purchase-workflow Translate me on Weblate Try me on Runboat

+

This module extends the functionality of purchase to allow the price and +the discount of the supplier-info to be updated automatically for each +product of the purchase when it is confirmed. This will allow that when +a purchase order is created, the suggested price and suggested discount +for those products is the last one purchased.

+

Table of contents

+ +
+

Usage

+

To use this module, you need to:

+
    +
  1. Go to Purchase > Orders > Requests for Quotation and create a new +Requests for Quotation with several products.
  2. +
  3. Confirm that quotation and the unit prices defined on the lines will +be stored in the supplier-info (related to the quotation supplier) of +the products.
  4. +
  5. By default the suggested price and the suggested discount for the +products is the one stored in supplier-info, so in this way, if you +create another quotation with the same supplier and the same +products, the suggested price and the suggested discount for each +product will be the same price and same discount at which the product +was last purchased.
  6. +
+
+
+

Known issues / Roadmap

+
    +
  • Refactor that module to share algorithm with similar module +account_invoice_supplierinfo_update.
  • +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Tecnativa
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

ernestotejeda

+

This module is part of the OCA/purchase-workflow project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+
+ + diff --git a/purchase_order_supplierinfo_update/tests/__init__.py b/purchase_order_supplierinfo_update/tests/__init__.py new file mode 100644 index 00000000000..a21274de57c --- /dev/null +++ b/purchase_order_supplierinfo_update/tests/__init__.py @@ -0,0 +1,2 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from . import test_purchase_order_supplierinfo_update diff --git a/purchase_order_supplierinfo_update/tests/test_purchase_order_supplierinfo_update.py b/purchase_order_supplierinfo_update/tests/test_purchase_order_supplierinfo_update.py new file mode 100644 index 00000000000..f259f16272f --- /dev/null +++ b/purchase_order_supplierinfo_update/tests/test_purchase_order_supplierinfo_update.py @@ -0,0 +1,132 @@ +# Copyright 2021 Tecnativa - Ernesto Tejeda +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from dateutil.relativedelta import relativedelta + +from odoo import fields +from odoo.fields import Command +from odoo.tests import Form + +from odoo.addons.base.tests.common import BaseCommon + + +class TestPurchaseOrderSupplierinfoUpdate(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.product = cls.env["product.product"].create( + {"name": "Product Test", "type": "consu"} # do not depend on stock module + ) + cls.supplier = cls.env["res.partner"].create({"name": "Supplier Test"}) + cls.supplierinfo = cls.env["product.supplierinfo"].create( + { + "partner_id": cls.supplier.id, + "product_tmpl_id": cls.product.product_tmpl_id.id, + "price": 100, + } + ) + cls.product_2 = cls.env["product.product"].create( + {"name": "Product Test 2", "type": "consu"} + ) + cls.supplierinfo_2 = cls.env["product.supplierinfo"].create( + { + "partner_id": cls.supplier.id, + "product_tmpl_id": cls.product_2.product_tmpl_id.id, + "price": 10, + } + ) + + def test_confirn_purchase_order(self): + # Create a PO, confirm it and check the supplierinfo is updated + po_form = Form(self.env["purchase.order"]) + po_form.partner_id = self.supplier + with po_form.order_line.new() as po_line_form: + po_line_form.product_id = self.product + self.assertEqual(po_line_form.price_unit, 100) + po_line_form.price_unit = 150 + po_line_form.tax_ids.clear() + purchase_order = po_form.save() + purchase_order.button_confirm() + self.assertEqual(self.supplierinfo.price, 150) + # Create another PO, confirm it and check the supplierinfo is updated + po_form = Form(self.env["purchase.order"]) + po_form.partner_id = self.supplier + with po_form.order_line.new() as po_line_form: + po_line_form.product_id = self.product + self.assertEqual(po_line_form.price_unit, 150) + po_line_form.price_unit = 200 + po_line_form.tax_ids.clear() + purchase_order = po_form.save() + purchase_order.button_confirm() + self.assertEqual(self.supplierinfo.price, 200) + + def test_change_price_in_confirmed_po(self): + # Create first purchase + po_form_1 = Form(self.env["purchase.order"]) + po_form_1.partner_id = self.supplier + now = fields.Datetime.now() + po_form_1.date_order = now - relativedelta(days=1) + with po_form_1.order_line.new() as po_line_form: + po_line_form.product_id = self.product + po_line_form.tax_ids.clear() + purchase_order_1 = po_form_1.save() + purchase_order_1.button_confirm() + # Create second purchase + po_form_2 = Form(self.env["purchase.order"]) + po_form_2.partner_id = self.supplier + with po_form_2.order_line.new() as po_line_form: + po_line_form.product_id = self.product + po_line_form.price_unit = 200 + po_line_form.tax_ids.clear() + purchase_order_2 = po_form_2.save() + purchase_order_2.button_confirm() + # Change price in second purchase + with Form(purchase_order_2) as po_form_2: + with po_form_2.order_line.edit(0) as po_line_form: + po_line_form.price_unit = 300 + self.assertEqual(self.supplierinfo.price, 300) + # Change price in first purchase. This doesn't update supplierinfo + # because it isn't the last purchase of this product by this supplier + with Form(purchase_order_1) as po_form_1: + with po_form_1.order_line.edit(0) as po_line_form: + po_line_form.price_unit = 400 + self.assertEqual(self.supplierinfo.price, 300) + + def test_create_new_line_in_a_confirmed_po(self): + # Create first purchase + po_form_1 = Form(self.env["purchase.order"]) + po_form_1.partner_id = self.supplier + with po_form_1.order_line.new() as po_line_form: + po_line_form.product_id = self.product + po_line_form.tax_ids.clear() + purchase_order_1 = po_form_1.save() + purchase_order_1.button_confirm() + # Create second purchase + po_form_2 = Form(self.env["purchase.order"]) + po_form_2.partner_id = self.supplier + with po_form_2.order_line.new() as po_line_form: + po_line_form.product_id = self.product + po_line_form.price_unit = 200 + po_line_form.tax_ids.clear() + purchase_order_2 = po_form_2.save() + purchase_order_2.button_confirm() + # Create a new line in the first purchase (that is already confirmed) + # with another product. + # We can not use a Form due to a modifier restriction on purchase order view + # purchase/views/purchase_views.xml#L230 + purchase_order_1.write( + { + "order_line": [ + Command.create( + { + "product_id": self.product_2.id, + "product_uom_id": self.product_2.uom_id.id, + "price_unit": 20.00, + "discount": 10.00, + } + ) + ] + } + ) + self.assertEqual(self.supplierinfo_2.price, 20) + self.assertEqual(self.supplierinfo_2.discount, 10)