diff --git a/pricelist_brand/README.rst b/pricelist_brand/README.rst new file mode 100644 index 000000000..8b1da6bb2 --- /dev/null +++ b/pricelist_brand/README.rst @@ -0,0 +1,88 @@ +=============== +Pricelist Brand +=============== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:1da3e6535ebeeecede9d443275f9f2f0e2e2dcfd6309f236f3e961bfe38522f7 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fbrand-lightgray.png?logo=github + :target: https://github.com/OCA/brand/tree/16.0/pricelist_brand + :alt: OCA/brand +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/brand-16-0/brand-16-0-pricelist_brand + :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/brand&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows to configure pricelist items so that they are applied on products of specific brands. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To apply pricelist items on branded products: + +#. See 'product_brand' module to learn how to configure brands on products. +#. Go to *Sales > Pricelists* and open the pricelist of your choice or create a new one. +#. Edit the pricelist and add a new item to it. +#. Select *Brand* in the field *Applied On*. +#. Select the product brand of your choice in the field *Brand*. +#. Configure the rest of the rule the way you want, this rule will apply on any product from the brand you selected. + +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 +~~~~~~~ + +* ACSONE SA/NV + +Contributors +~~~~~~~~~~~~ + +* Quentin Groulard + +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. + +This module is part of the `OCA/brand `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/pricelist_brand/__init__.py b/pricelist_brand/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/pricelist_brand/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/pricelist_brand/__manifest__.py b/pricelist_brand/__manifest__.py new file mode 100644 index 000000000..49fdd4771 --- /dev/null +++ b/pricelist_brand/__manifest__.py @@ -0,0 +1,13 @@ +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Pricelist Brand", + "summary": """This module allows to apply pricelist items on brand""", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/brand", + "depends": ["product_brand"], + "data": ["views/product_pricelist.xml"], +} diff --git a/pricelist_brand/i18n/pricelist_brand.pot b/pricelist_brand/i18n/pricelist_brand.pot new file mode 100644 index 000000000..adeea94d4 --- /dev/null +++ b/pricelist_brand/i18n/pricelist_brand.pot @@ -0,0 +1,59 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * pricelist_brand +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 13.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: pricelist_brand +#: model:ir.model.fields,field_description:pricelist_brand.field_product_pricelist_item__applied_on +msgid "Apply On" +msgstr "" + +#. module: pricelist_brand +#: model:ir.model.fields,field_description:pricelist_brand.field_product_pricelist_item__product_brand_id +#: model:ir.model.fields.selection,name:pricelist_brand.selection__product_pricelist_item__applied_on__25_brand +msgid "Brand" +msgstr "" + +#. module: pricelist_brand +#: code:addons/pricelist_brand/models/product_pricelist.py:0 +#, python-format +msgid "Brand: %s" +msgstr "" + +#. module: pricelist_brand +#: code:addons/pricelist_brand/models/product_pricelist.py:0 +#, python-format +msgid "Please specify the brand for which this rule should be applied" +msgstr "" + +#. module: pricelist_brand +#: model:ir.model,name:pricelist_brand.model_product_pricelist +msgid "Pricelist" +msgstr "" + +#. module: pricelist_brand +#: model:ir.model.fields,help:pricelist_brand.field_product_pricelist_item__applied_on +msgid "Pricelist Item applicable on selected option" +msgstr "" + +#. module: pricelist_brand +#: model:ir.model,name:pricelist_brand.model_product_pricelist_item +msgid "Pricelist Rule" +msgstr "" + +#. module: pricelist_brand +#: model:ir.model.fields,help:pricelist_brand.field_product_pricelist_item__product_brand_id +msgid "" +"Specify a brand if this rule only applies to productsbelonging to this " +"brand. Keep empty otherwise." +msgstr "" diff --git a/pricelist_brand/models/__init__.py b/pricelist_brand/models/__init__.py new file mode 100644 index 000000000..9880677a7 --- /dev/null +++ b/pricelist_brand/models/__init__.py @@ -0,0 +1 @@ +from . import product_pricelist diff --git a/pricelist_brand/models/product_pricelist.py b/pricelist_brand/models/product_pricelist.py new file mode 100644 index 000000000..256046f6b --- /dev/null +++ b/pricelist_brand/models/product_pricelist.py @@ -0,0 +1,119 @@ +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError +from odoo.osv import expression + + +class ProductPricelist(models.Model): + + _inherit = "product.pricelist" + + def _get_applicable_rules_domain(self, products, date, **kwargs): + res = super()._get_applicable_rules_domain(products, date) + if products._name == "product.template": + brand_domain = ("product_brand_id", "in", products.product_brand_id.ids) + else: + brand_domain = ( + "product_brand_id", + "in", + products.product_tmpl_id.product_brand_id.ids, + ) + res = expression.AND( + [ + res, + [ + ("|"), + ("product_brand_id", "=", False), + (brand_domain), + ], + ] + ) + return res + + +class ProductPricelistItem(models.Model): + + _inherit = "product.pricelist.item" + + product_brand_id = fields.Many2one( + comodel_name="product.brand", + string="Brand", + ondelete="cascade", + help="Specify a brand if this rule only applies to products" + "belonging to this brand. Keep empty otherwise.", + ) + applied_on = fields.Selection( + selection_add=[("25_brand", "Brand")], ondelete={"25_brand": "set default"} + ) + + @api.constrains("product_id", "product_tmpl_id", "categ_id", "product_brand_id") + def _check_product_consistency(self): + for item in self: + if item.applied_on == "25_brand" and not item.product_brand_id: + raise ValidationError( + _("Please specify the brand for which this rule should be applied") + ) + return super()._check_product_consistency() + + @api.depends( + "applied_on", + "categ_id", + "product_tmpl_id", + "product_id", + "compute_price", + "fixed_price", + "pricelist_id", + "percent_price", + "price_discount", + "price_surcharge", + ) + def _compute_name_and_price(self): + res = super()._compute_name_and_price() + for item in self: + if item.product_brand_id and item.applied_on == "25_brand": + item.name = _("Brand: %s") % (item.product_brand_id.display_name) + return res + + @api.onchange("product_id", "product_tmpl_id", "categ_id", "product_brand_id") + def _onchange_rule_content(self): + return super()._onchange_rule_content() + + @api.model_create_multi + def create(self, vals_list): + for values in vals_list: + if values.get("applied_on", False): + # Ensure item consistency for later searches. + applied_on = values["applied_on"] + if applied_on == "25_brand": + values.update( + dict(product_id=None, product_tmpl_id=None, categ_id=None) + ) + elif applied_on == "3_global": + values.update(dict(product_brand_id=None)) + elif applied_on == "2_product_category": + values.update(dict(product_brand_id=None)) + elif applied_on == "1_product": + values.update(dict(product_brand_id=None)) + elif applied_on == "0_product_variant": + values.update(dict(product_brand_id=None)) + return super().create(vals_list) + + def write(self, values): + if values.get("applied_on", False): + # Ensure item consistency for later searches. + applied_on = values["applied_on"] + if applied_on == "25_brand": + values.update( + dict(product_id=None, product_tmpl_id=None, categ_id=None) + ) + elif applied_on == "3_global": + values.update(dict(product_brand_id=None)) + elif applied_on == "2_product_category": + values.update(dict(product_brand_id=None)) + elif applied_on == "1_product": + values.update(dict(product_brand_id=None)) + elif applied_on == "0_product_variant": + values.update(dict(product_brand_id=None)) + return super().write(values) diff --git a/pricelist_brand/readme/CONTRIBUTORS.rst b/pricelist_brand/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..5914f5529 --- /dev/null +++ b/pricelist_brand/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Quentin Groulard diff --git a/pricelist_brand/readme/DESCRIPTION.rst b/pricelist_brand/readme/DESCRIPTION.rst new file mode 100644 index 000000000..a6fa92014 --- /dev/null +++ b/pricelist_brand/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module allows to configure pricelist items so that they are applied on products of specific brands. diff --git a/pricelist_brand/readme/USAGE.rst b/pricelist_brand/readme/USAGE.rst new file mode 100644 index 000000000..1bffe67cf --- /dev/null +++ b/pricelist_brand/readme/USAGE.rst @@ -0,0 +1,8 @@ +To apply pricelist items on branded products: + +#. See 'product_brand' module to learn how to configure brands on products. +#. Go to *Sales > Pricelists* and open the pricelist of your choice or create a new one. +#. Edit the pricelist and add a new item to it. +#. Select *Brand* in the field *Applied On*. +#. Select the product brand of your choice in the field *Brand*. +#. Configure the rest of the rule the way you want, this rule will apply on any product from the brand you selected. diff --git a/pricelist_brand/static/description/icon.png b/pricelist_brand/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/pricelist_brand/static/description/icon.png differ diff --git a/pricelist_brand/static/description/index.html b/pricelist_brand/static/description/index.html new file mode 100644 index 000000000..3e2d8b8de --- /dev/null +++ b/pricelist_brand/static/description/index.html @@ -0,0 +1,437 @@ + + + + + + +Pricelist Brand + + + +
+

Pricelist Brand

+ + +

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

+

This module allows to configure pricelist items so that they are applied on products of specific brands.

+

Table of contents

+ +
+

Usage

+

To apply pricelist items on branded products:

+
    +
  1. See ‘product_brand’ module to learn how to configure brands on products.
  2. +
  3. Go to Sales > Pricelists and open the pricelist of your choice or create a new one.
  4. +
  5. Edit the pricelist and add a new item to it.
  6. +
  7. Select Brand in the field Applied On.
  8. +
  9. Select the product brand of your choice in the field Brand.
  10. +
  11. Configure the rest of the rule the way you want, this rule will apply on any product from the brand you selected.
  12. +
+
+
+

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

+
    +
  • ACSONE SA/NV
  • +
+
+
+

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.

+

This module is part of the OCA/brand project on GitHub.

+

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

+
+
+
+ + diff --git a/pricelist_brand/tests/__init__.py b/pricelist_brand/tests/__init__.py new file mode 100644 index 000000000..48b4ef970 --- /dev/null +++ b/pricelist_brand/tests/__init__.py @@ -0,0 +1 @@ +from . import test_product_pricelist diff --git a/pricelist_brand/tests/test_product_pricelist.py b/pricelist_brand/tests/test_product_pricelist.py new file mode 100644 index 000000000..b9ba6c952 --- /dev/null +++ b/pricelist_brand/tests/test_product_pricelist.py @@ -0,0 +1,179 @@ +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import _ +from odoo.exceptions import ValidationError +from odoo.tests.common import TransactionCase +from odoo.tools import float_compare + + +class TestProductPricelist(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.product_brand_obj = cls.env["product.brand"] + cls.product_brand = cls.env["product.brand"].create( + {"name": "Test Brand", "description": "Test brand description"} + ) + cls.product = cls.env.ref("product.product_product_4") + cls.product.write({"product_brand_id": cls.product_brand.id}) + cls.product_2 = cls.env.ref("product.product_product_5") + + cls.list0 = cls.env.ref("product.list0") + cls.pricelist = cls.env["product.pricelist"].create( + { + "name": "Test Pricelist", + "item_ids": [ + ( + 0, + 0, + { + "name": "Default pricelist", + "compute_price": "formula", + "base": "pricelist", + "base_pricelist_id": cls.list0.id, + }, + ), + ( + 0, + 0, + { + "name": "10% Discount on Test Brand", + "applied_on": "25_brand", + "product_brand_id": cls.product_brand.id, + "compute_price": "formula", + "base": "list_price", + "price_discount": 10, + }, + ), + ], + } + ) + cls.product_categ = cls.env.ref("product.product_category_2") + + def test_ensure_pricelist_item_consistency(self): + with self.assertRaises(ValidationError): + self.env["product.pricelist.item"].create( + { + "pricelist_id": self.pricelist.id, + "base": "list_price", + "compute_price": "formula", + "applied_on": "25_brand", + } + ) + pricelist_item = self.env["product.pricelist.item"].create( + { + "pricelist_id": self.pricelist.id, + "base": "list_price", + "compute_price": "formula", + "applied_on": "25_brand", + "product_brand_id": self.product_brand.id, + } + ) + pricelist_item.write({"product_brand_id": self.product_brand.id}) + pricelist_item._compute_name_and_price() + self.assertEqual( + pricelist_item.name, _("Brand: %s") % (self.product_brand.display_name) + ) + pricelist_item_2 = self.env["product.pricelist.item"].create( + { + "pricelist_id": self.pricelist.id, + "base": "list_price", + "compute_price": "formula", + "applied_on": "2_product_category", + "categ_id": self.product_categ.id, + "product_brand_id": self.product_brand.id, + } + ) + self.assertFalse(pricelist_item_2.product_brand_id) + pricelist_item_3 = self.env["product.pricelist.item"].create( + { + "pricelist_id": self.pricelist.id, + "base": "list_price", + "compute_price": "formula", + "applied_on": "1_product", + "product_tmpl_id": self.product.product_tmpl_id.id, + "product_brand_id": self.product_brand.id, + } + ) + self.assertFalse(pricelist_item_3.product_brand_id) + pricelist_item_4 = self.env["product.pricelist.item"].create( + { + "pricelist_id": self.pricelist.id, + "base": "list_price", + "compute_price": "formula", + "applied_on": "0_product_variant", + "product_id": self.product.id, + "product_brand_id": self.product_brand.id, + } + ) + self.assertFalse(pricelist_item_4.product_brand_id) + self.assertFalse( + any( + [ + pricelist_item.product_id, + pricelist_item.product_tmpl_id, + pricelist_item.categ_id, + ] + ) + ) + pricelist_item.write( + {"applied_on": "0_product_variant", "product_id": self.product.id} + ) + self.assertFalse(pricelist_item.product_brand_id) + pricelist_item.write( + {"applied_on": "25_brand", "product_brand_id": self.product_brand.id} + ) + self.assertFalse(pricelist_item.product_id) + pricelist_item.write( + { + "applied_on": "1_product", + "product_tmpl_id": self.product.product_tmpl_id.id, + } + ) + self.assertFalse(pricelist_item.product_brand_id) + pricelist_item.write( + {"applied_on": "25_brand", "product_brand_id": self.product_brand.id} + ) + self.assertFalse(pricelist_item.product_tmpl_id) + pricelist_item.write( + {"applied_on": "2_product_category", "categ_id": self.product.categ_id.id} + ) + self.assertFalse(pricelist_item.product_brand_id) + pricelist_item.write( + {"applied_on": "25_brand", "product_brand_id": self.product_brand.id} + ) + self.assertFalse(pricelist_item.categ_id) + pricelist_item.write({"applied_on": "3_global"}) + self.assertFalse(pricelist_item.product_brand_id) + + def test_calculation_price_of_products_pricelist(self): + """Test calculation of product price based on pricelist""" + # Check sale price of branded product + product_with_context = self.product.with_context( + pricelist=self.pricelist.id, quantity=1 + ) + self.assertEqual( + float_compare( + product_with_context._get_contextual_price(), + ( + product_with_context.lst_price + - product_with_context.lst_price * (0.10) + ), + precision_digits=2, + ), + 0, + ) + + # Check sale price of not branded product (should not change) + product_2_with_context = self.product_2.with_context( + pricelist=self.pricelist.id, quantity=1 + ) + self.assertEqual( + float_compare( + product_2_with_context._get_contextual_price(), + product_2_with_context.lst_price, + precision_digits=2, + ), + 0, + ) diff --git a/pricelist_brand/views/product_pricelist.xml b/pricelist_brand/views/product_pricelist.xml new file mode 100644 index 000000000..f35812258 --- /dev/null +++ b/pricelist_brand/views/product_pricelist.xml @@ -0,0 +1,21 @@ + + + + + product.pricelist.item.form (in pricelist_brand) + product.pricelist.item + + + + + + + + diff --git a/setup/pricelist_brand/odoo/addons/pricelist_brand b/setup/pricelist_brand/odoo/addons/pricelist_brand new file mode 120000 index 000000000..e6b0989a2 --- /dev/null +++ b/setup/pricelist_brand/odoo/addons/pricelist_brand @@ -0,0 +1 @@ +../../../../pricelist_brand \ No newline at end of file diff --git a/setup/pricelist_brand/setup.py b/setup/pricelist_brand/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/pricelist_brand/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)