diff --git a/product_main_supplierinfo/README.rst b/product_main_supplierinfo/README.rst new file mode 100644 index 00000000000..b4722016923 --- /dev/null +++ b/product_main_supplierinfo/README.rst @@ -0,0 +1,83 @@ +=================== +Product Main Vendor +=================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/licence-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%2Fproduct--attribute-lightgray.png?logo=github + :target: https://github.com/OCA/product-attribute/tree/14.0/product_main_supplierinfo + :alt: OCA/product-attribute +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/product-attribute-14-0/product-attribute-14-0-product_main_supplierinfo + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/135/14.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module shows the main vendor of a product. + +**Table of contents** + +.. contents:: + :local: + +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 +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Camptocamp + +Contributors +~~~~~~~~~~~~ + +* `Camptocamp `_ + + * Sébastien Alix + +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-sebalix| image:: https://github.com/sebalix.png?size=40px + :target: https://github.com/sebalix + :alt: sebalix + +Current `maintainer `__: + +|maintainer-sebalix| + +This module is part of the `OCA/product-attribute `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/product_main_supplierinfo/__init__.py b/product_main_supplierinfo/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/product_main_supplierinfo/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/product_main_supplierinfo/__manifest__.py b/product_main_supplierinfo/__manifest__.py new file mode 100644 index 00000000000..adc72368956 --- /dev/null +++ b/product_main_supplierinfo/__manifest__.py @@ -0,0 +1,17 @@ +# Copyright 2021 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) +{ + "name": "Product Main Vendor", + "version": "14.0.1.0.0", + "summary": "Display the main vendor of a product.", + "website": "https://github.com/OCA/product-attribute", + "author": "Camptocamp, Odoo Community Association (OCA)", + "maintainers": ["sebalix"], + "development_status": "Beta", + "license": "AGPL-3", + "category": "Product", + "depends": ["product", "purchase"], + "data": ["views/product_product.xml"], + "auto_install": False, + "installable": True, +} diff --git a/product_main_supplierinfo/models/__init__.py b/product_main_supplierinfo/models/__init__.py new file mode 100644 index 00000000000..5c74c8c30f1 --- /dev/null +++ b/product_main_supplierinfo/models/__init__.py @@ -0,0 +1 @@ +from . import product_product diff --git a/product_main_supplierinfo/models/product_product.py b/product_main_supplierinfo/models/product_product.py new file mode 100644 index 00000000000..04bfdef451a --- /dev/null +++ b/product_main_supplierinfo/models/product_product.py @@ -0,0 +1,53 @@ +# Copyright 2021 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +from odoo import api, fields, models + + +class ProductProduct(models.Model): + _inherit = "product.product" + + main_seller_id = fields.Many2one( + comodel_name="product.supplierinfo", + string="Main Vendor", + compute="_compute_main_seller_id", + ) + + @api.depends( + "seller_ids.name.active", + "seller_ids.sequence", + "seller_ids.min_qty", + "seller_ids.price", + "seller_ids.company_id", + "seller_ids.product_id", + "seller_ids.date_start", + "seller_ids.date_end", + ) + @api.depends_context("company") + def _compute_main_seller_id(self): + for product in self: + sellers = product._get_sellers() + product.main_seller_id = fields.first(sellers) + + def _get_sellers(self): + """Returns all available sellers of a product based on some constraints. + + They are ordered and filtered like it is done in the standard 'product' addon. + """ + self.ensure_one() + all_sellers = self._prepare_sellers().filtered( + lambda s: not s.company_id or s.company_id.id == self.env.company.id + ) + today = fields.Date.context_today(self) + sellers = all_sellers.filtered( + lambda s: ( + (s.product_id == self or not s.product_id) + and ( + (s.date_start <= today if s.date_start else True) + and (s.date_end >= today if s.date_end else True) + ) + ) + ) + if not sellers: + sellers = all_sellers + return sellers diff --git a/product_main_supplierinfo/readme/CONTRIBUTORS.rst b/product_main_supplierinfo/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000000..046ba7c569e --- /dev/null +++ b/product_main_supplierinfo/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `Camptocamp `_ + + * Sébastien Alix diff --git a/product_main_supplierinfo/readme/DESCRIPTION.rst b/product_main_supplierinfo/readme/DESCRIPTION.rst new file mode 100644 index 00000000000..158e07c5435 --- /dev/null +++ b/product_main_supplierinfo/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module shows the main vendor of a product. diff --git a/product_main_supplierinfo/static/description/index.html b/product_main_supplierinfo/static/description/index.html new file mode 100644 index 00000000000..aadad70ef82 --- /dev/null +++ b/product_main_supplierinfo/static/description/index.html @@ -0,0 +1,424 @@ + + + + + + +Product Main Vendor + + + +
+

Product Main Vendor

+ + +

Beta License: AGPL-3 OCA/product-attribute Translate me on Weblate Try me on Runbot

+

This module shows the main vendor of a product.

+

Table of contents

+ +
+

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 +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Camptocamp
  • +
+
+
+

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:

+

sebalix

+

This module is part of the OCA/product-attribute project on GitHub.

+

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

+
+
+
+ + diff --git a/product_main_supplierinfo/tests/__init__.py b/product_main_supplierinfo/tests/__init__.py new file mode 100644 index 00000000000..f827dc0fbd8 --- /dev/null +++ b/product_main_supplierinfo/tests/__init__.py @@ -0,0 +1 @@ +from . import test_product_main_supplier diff --git a/product_main_supplierinfo/tests/test_product_main_supplier.py b/product_main_supplierinfo/tests/test_product_main_supplier.py new file mode 100644 index 00000000000..1c5649af4a9 --- /dev/null +++ b/product_main_supplierinfo/tests/test_product_main_supplier.py @@ -0,0 +1,47 @@ +# Copyright 2021 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +from odoo import fields +from odoo.tests.common import SavepointCase + + +class TestProductMainSupplierInfo(SavepointCase): + @classmethod + def setUpClass(cls): + super(TestProductMainSupplierInfo, cls).setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) + cls.product = cls.env.ref("product.product_product_6") + cls.seller_1 = cls.env.ref("product.product_supplierinfo_1") + cls.seller_1.sequence = 1 + cls.seller_2 = cls.env.ref("product.product_supplierinfo_2") + cls.seller_2.sequence = 2 + cls.seller_2bis = cls.env.ref("product.product_supplierinfo_2bis") + cls.seller_2bis.sequence = 3 + cls.company_2 = cls.env.company.create({"name": "Company2"}) + + def test_main_seller_1(self): + """"Case 1: all the sellers share the same company.""" + self.assertEqual(self.product.main_seller_id, self.seller_1) + self.seller_2.sequence = -1 + self.assertEqual(self.product.main_seller_id, self.seller_2) + + def test_main_seller_2(self): + """"Case 2: the sellers do not share the same company.""" + # Assign 'seller_1' to the second company, so the main vendor computed + # for the main company is now 'seller_2' + self.seller_1.company_id = self.company_2 + self.assertEqual(self.product.main_seller_id, self.seller_2) + # Check that the main vendor for the second company is 'seller_1' + self.assertEqual( + self.product.with_company(self.company_2).main_seller_id, + self.seller_1, + ) + + def test_main_seller_3(self): + """"Case 3: the sellers have different start/end dates.""" + today = fields.Date.today() + tomorrow = fields.Date.add(today, days=1) + yesterday = fields.Date.subtract(today, days=1) + self.seller_1.date_start = tomorrow + self.seller_2.date_end = yesterday + self.assertEqual(self.product.main_seller_id, self.seller_2bis) diff --git a/product_main_supplierinfo/views/product_product.xml b/product_main_supplierinfo/views/product_product.xml new file mode 100644 index 00000000000..c95c72e1ead --- /dev/null +++ b/product_main_supplierinfo/views/product_product.xml @@ -0,0 +1,30 @@ + + + + + + product.product.form.inherit + product.product + + + + + + + + + + + + product.product.tree.inherit + product.product + + + + + + + + +