From 3a6629783a4fd14e87f45da4400180372b121485 Mon Sep 17 00:00:00 2001 From: trisdoan Date: Thu, 6 Jun 2024 17:08:49 +0700 Subject: [PATCH] [IMP] purchase_vendor_promotion: auto assign supplier with promotion --- purchase_vendor_promotion/README.rst | 16 ++++-- purchase_vendor_promotion/__manifest__.py | 2 + .../data/stock_route.xml | 5 ++ purchase_vendor_promotion/models/__init__.py | 1 + .../models/product_supplierinfo.py | 11 ++++ .../models/stock_route.py | 10 ++++ .../models/stock_warehouse_orderpoint.py | 16 ++++++ .../readme/CONTRIBUTORS.rst | 2 + .../readme/DESCRIPTION.rst | 5 +- .../static/description/index.html | 48 +++++++++-------- .../tests/test_vendor_promotion.py | 52 ++++++++++++++----- .../views/stock_route.xml | 11 ++++ 12 files changed, 138 insertions(+), 41 deletions(-) create mode 100644 purchase_vendor_promotion/data/stock_route.xml create mode 100644 purchase_vendor_promotion/models/stock_route.py create mode 100644 purchase_vendor_promotion/views/stock_route.xml diff --git a/purchase_vendor_promotion/README.rst b/purchase_vendor_promotion/README.rst index 8daec701322..cae5922b035 100644 --- a/purchase_vendor_promotion/README.rst +++ b/purchase_vendor_promotion/README.rst @@ -2,10 +2,13 @@ Purchase Vendor Promotion ========================= -.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:d9b1b88cbcd184f9d7419231e529d3ca7b29ad1e59a9a0cdb3842a60ddc46126 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png :target: https://odoo-community.org/page/development-status @@ -20,13 +23,14 @@ Purchase Vendor Promotion :target: https://translation.odoo-community.org/projects/purchase-workflow-16-0/purchase-workflow-16-0-purchase_vendor_promotion :alt: Translate me on Weblate .. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png - :target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/purchase-workflow&target_branch=16.0 + :target: https://runboat.odoo-community.org/builds?repo=OCA/purchase-workflow&target_branch=16.0 :alt: Try me on Runboat -|badge1| |badge2| |badge3| |badge4| |badge5| +|badge1| |badge2| |badge3| |badge4| |badge5| -This module allows you to visualise the start and end date of a vendor promotion during replenishment. +This module allows you to visualise the start and end date of a vendor promotion during replenishment, in `Inventory > Operations > Replenishment` or `Inventory > Configuration > Products > Reordering Rules`. A vendor price is considered as a promotion if product supplier info is flagged as a `promotion`. +If "Buy" is chosen as the Preferred Route, the vendor having the best active or upcoming promotion for the product will be automatically chosen. If no promotion is available, then first vendor will be chosen. **Table of contents** @@ -38,7 +42,7 @@ 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 smashing it by providing a detailed and welcomed +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. @@ -55,6 +59,8 @@ Contributors ~~~~~~~~~~~~ * Telmo Santos +* Tris Doan + Maintainers ~~~~~~~~~~~ diff --git a/purchase_vendor_promotion/__manifest__.py b/purchase_vendor_promotion/__manifest__.py index b4023a5bcb8..b613d74109a 100644 --- a/purchase_vendor_promotion/__manifest__.py +++ b/purchase_vendor_promotion/__manifest__.py @@ -12,9 +12,11 @@ "license": "AGPL-3", "depends": ["purchase_stock"], "data": [ + "data/stock_route.xml", "views/product_views.xml", "views/stock_orderpoint_views.xml", "views/purchase_order_views.xml", + "views/stock_route.xml", ], "installable": True, "auto_install": False, diff --git a/purchase_vendor_promotion/data/stock_route.xml b/purchase_vendor_promotion/data/stock_route.xml new file mode 100644 index 00000000000..645aa84f4ba --- /dev/null +++ b/purchase_vendor_promotion/data/stock_route.xml @@ -0,0 +1,5 @@ + + + True + + diff --git a/purchase_vendor_promotion/models/__init__.py b/purchase_vendor_promotion/models/__init__.py index 7d3cc1b5201..098323eec63 100644 --- a/purchase_vendor_promotion/models/__init__.py +++ b/purchase_vendor_promotion/models/__init__.py @@ -1,3 +1,4 @@ +from . import stock_route from . import purchase_order_line from . import product_supplierinfo from . import stock_warehouse_orderpoint diff --git a/purchase_vendor_promotion/models/product_supplierinfo.py b/purchase_vendor_promotion/models/product_supplierinfo.py index b3b4b9ccea0..922df0721a1 100644 --- a/purchase_vendor_promotion/models/product_supplierinfo.py +++ b/purchase_vendor_promotion/models/product_supplierinfo.py @@ -22,3 +22,14 @@ def _check_promotion_dates(self): raise ValidationError( _("Promotion start date must be before end date.") ) + + def _is_promotion_active_or_upcoming(self, date=None): + """ + Consider Promotion is active if it's within start and end date or it's in the future + """ + self.ensure_one() + if not self.is_promotion: + return False + if not date: + date = fields.Date.today() + return date <= self.date_start or (self.date_start <= date <= self.date_end) diff --git a/purchase_vendor_promotion/models/stock_route.py b/purchase_vendor_promotion/models/stock_route.py new file mode 100644 index 00000000000..466b2636f4c --- /dev/null +++ b/purchase_vendor_promotion/models/stock_route.py @@ -0,0 +1,10 @@ +# Copyright 2024 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import fields, models + + +class StockRoute(models.Model): + _inherit = "stock.route" + + force_vendor_with_best_promotion = fields.Boolean(default=False) diff --git a/purchase_vendor_promotion/models/stock_warehouse_orderpoint.py b/purchase_vendor_promotion/models/stock_warehouse_orderpoint.py index 2fd217adeba..0dd24b19d72 100644 --- a/purchase_vendor_promotion/models/stock_warehouse_orderpoint.py +++ b/purchase_vendor_promotion/models/stock_warehouse_orderpoint.py @@ -12,6 +12,22 @@ class StockWarehouseOrderpoint(models.Model): ) promotion_date_start = fields.Date(compute="_compute_promotion", store=True) promotion_date_end = fields.Date(compute="_compute_promotion", store=True) + supplier_id = fields.Many2one( + compute="_compute_supplier_id", readonly=False, store=True + ) + + @api.depends("route_id", "route_id.force_vendor_with_best_promotion") + def _compute_supplier_id(self): + for rec in self: + if rec.route_id and rec.route_id.force_vendor_with_best_promotion: + suppliers = rec.product_id._prepare_sellers(False) + promotion_suppliers = suppliers.filtered( + lambda x: x._is_promotion_active_or_upcoming() + ) + if promotion_suppliers: + rec.supplier_id = promotion_suppliers[0].id + elif suppliers: + rec.supplier_id = suppliers[0].id @api.depends("supplier_id") def _compute_promotion(self): diff --git a/purchase_vendor_promotion/readme/CONTRIBUTORS.rst b/purchase_vendor_promotion/readme/CONTRIBUTORS.rst index 8ed3aebaca0..bff50efc42b 100644 --- a/purchase_vendor_promotion/readme/CONTRIBUTORS.rst +++ b/purchase_vendor_promotion/readme/CONTRIBUTORS.rst @@ -1 +1,3 @@ * Telmo Santos +* Tris Doan + diff --git a/purchase_vendor_promotion/readme/DESCRIPTION.rst b/purchase_vendor_promotion/readme/DESCRIPTION.rst index 251d9c9c07d..aac1b9c22be 100644 --- a/purchase_vendor_promotion/readme/DESCRIPTION.rst +++ b/purchase_vendor_promotion/readme/DESCRIPTION.rst @@ -1,2 +1,3 @@ -This module allows you to visualise the start and end date of a vendor promotion during replenishment. -A vendor price is considered as a promotion if product supplier info is flagged as a `promotion`. \ No newline at end of file +This module allows you to visualise the start and end date of a vendor promotion during replenishment, in `Inventory > Operations > Replenishment` or `Inventory > Configuration > Products > Reordering Rules`. +A vendor price is considered as a promotion if product supplier info is flagged as a `promotion`. +If "Buy" is chosen as the Preferred Route, the vendor having the best active or upcoming promotion for the product will be automatically chosen. If no promotion is available, then first vendor will be chosen. diff --git a/purchase_vendor_promotion/static/description/index.html b/purchase_vendor_promotion/static/description/index.html index 8707a3e9d17..6c50fe75213 100644 --- a/purchase_vendor_promotion/static/description/index.html +++ b/purchase_vendor_promotion/static/description/index.html @@ -1,20 +1,20 @@ - - + Purchase Vendor Promotion