From 36eb62bb3f7ed4c0bd406ecd22313bc688b4cb30 Mon Sep 17 00:00:00 2001 From: "chafique.delli" Date: Thu, 21 Jul 2022 13:21:05 +0200 Subject: [PATCH 1/4] [14.0][ADD] account_fiscal_product_rule --- account_fiscal_product_rule/README.rst | 77 ++++ account_fiscal_product_rule/__init__.py | 1 + account_fiscal_product_rule/__manifest__.py | 18 + .../models/__init__.py | 3 + .../models/account_fiscal_position.py | 59 +++ .../models/account_move.py | 52 +++ account_fiscal_product_rule/models/product.py | 36 ++ .../readme/CONTRIBUTORS.rst | 2 + .../readme/DESCRIPTION.rst | 1 + .../security/ir.model.access.csv | 2 + .../static/description/index.html | 415 ++++++++++++++++++ account_fiscal_product_rule/tests/__init__.py | 1 + .../tests/test_account_fiscal_product_rule.py | 62 +++ .../views/account_fiscal_position.xml | 85 ++++ .../views/account_menuitem.xml | 11 + account_fiscal_product_rule/views/product.xml | 39 ++ .../odoo/addons/account_fiscal_product_rule | 1 + setup/account_fiscal_product_rule/setup.py | 6 + 18 files changed, 871 insertions(+) create mode 100644 account_fiscal_product_rule/README.rst create mode 100644 account_fiscal_product_rule/__init__.py create mode 100644 account_fiscal_product_rule/__manifest__.py create mode 100644 account_fiscal_product_rule/models/__init__.py create mode 100644 account_fiscal_product_rule/models/account_fiscal_position.py create mode 100644 account_fiscal_product_rule/models/account_move.py create mode 100644 account_fiscal_product_rule/models/product.py create mode 100644 account_fiscal_product_rule/readme/CONTRIBUTORS.rst create mode 100644 account_fiscal_product_rule/readme/DESCRIPTION.rst create mode 100644 account_fiscal_product_rule/security/ir.model.access.csv create mode 100644 account_fiscal_product_rule/static/description/index.html create mode 100644 account_fiscal_product_rule/tests/__init__.py create mode 100644 account_fiscal_product_rule/tests/test_account_fiscal_product_rule.py create mode 100644 account_fiscal_product_rule/views/account_fiscal_position.xml create mode 100644 account_fiscal_product_rule/views/account_menuitem.xml create mode 100644 account_fiscal_product_rule/views/product.xml create mode 120000 setup/account_fiscal_product_rule/odoo/addons/account_fiscal_product_rule create mode 100644 setup/account_fiscal_product_rule/setup.py diff --git a/account_fiscal_product_rule/README.rst b/account_fiscal_product_rule/README.rst new file mode 100644 index 000000000..279186eda --- /dev/null +++ b/account_fiscal_product_rule/README.rst @@ -0,0 +1,77 @@ +=========================== +Account Fiscal Product Rule +=========================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Faccount--fiscal--rule-lightgray.png?logo=github + :target: https://github.com/OCA/account-fiscal-rule/tree/14.0/account_fiscal_product_rule + :alt: OCA/account-fiscal-rule +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/account-fiscal-rule-14-0/account-fiscal-rule-14-0-account_fiscal_product_rule + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/251/14.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows to map tax and account depending of the product. + +**Table of contents** + +.. contents:: + :local: + +Known issues / Roadmap +====================== + +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 +~~~~~~~ + +* Akretion + +Contributors +~~~~~~~~~~~~ + +* Sébastien Beau +* Chafique Delli + +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/account-fiscal-rule `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/account_fiscal_product_rule/__init__.py b/account_fiscal_product_rule/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/account_fiscal_product_rule/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/account_fiscal_product_rule/__manifest__.py b/account_fiscal_product_rule/__manifest__.py new file mode 100644 index 000000000..843004ed2 --- /dev/null +++ b/account_fiscal_product_rule/__manifest__.py @@ -0,0 +1,18 @@ +# Copyright 2022 Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +{ + "name": "Account Fiscal Product Rule", + "version": "14.0.1.0.0", + "category": "Accounting & Finance", + "author": "Akretion, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/account-fiscal-rule", + "license": "AGPL-3", + "depends": ["account"], + "data": [ + "security/ir.model.access.csv", + "views/product.xml", + "views/account_fiscal_position.xml", + "views/account_menuitem.xml", + ], + "installable": True, +} diff --git a/account_fiscal_product_rule/models/__init__.py b/account_fiscal_product_rule/models/__init__.py new file mode 100644 index 000000000..420776ce8 --- /dev/null +++ b/account_fiscal_product_rule/models/__init__.py @@ -0,0 +1,3 @@ +from . import account_fiscal_position +from . import product +from . import account_move diff --git a/account_fiscal_product_rule/models/account_fiscal_position.py b/account_fiscal_product_rule/models/account_fiscal_position.py new file mode 100644 index 000000000..4d846ac27 --- /dev/null +++ b/account_fiscal_product_rule/models/account_fiscal_position.py @@ -0,0 +1,59 @@ +# Copyright 2022 Akretion France (http://www.akretion.com) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class AccountFiscalPositionProductRule(models.Model): + _name = "account.fiscal.position.product.rule" + _description = "Account Fiscal Position Rule in Product" + + name = fields.Char(required=True) + fiscal_position_id = fields.Many2one("account.fiscal.position", required=True) + product_tmpl_ids = fields.Many2many("product.template") + product_category_ids = fields.Many2many("product.category") + account_income_id = fields.Many2one("account.account") + account_expense_id = fields.Many2one("account.account") + seller_tax_ids = fields.Many2many("account.tax", "account_tax_seller") + supplier_tax_ids = fields.Many2many( + "account.tax", + "account_tax_supplier", + ) + company_id = fields.Many2one(related="fiscal_position_id.company_id") + + +class AccountFiscalPosition(models.Model): + _inherit = "account.fiscal.position" + + fiscal_position_product_rule_ids = fields.One2many( + "account.fiscal.position.product.rule", + "fiscal_position_id", + string="Product Fiscal Rules", + ) + + def map_tax(self, taxes, product=None, partner=None): + if product or self.env.context.get("product_id", False): + if not product: + product = self.env["product.product"].browse( + self.env.context.get("product_id", False) + ) + for fp in self: + fiscal_product_rules = fp.fiscal_position_product_rule_ids.filtered( + lambda r: product.product_tmpl_id in r.product_tmpl_ids + or product.categ_id in r.product_category_ids + ) + if fiscal_product_rules: + res = self.env["account.tax"] + if ( + taxes[0].type_tax_use == "sale" + and fiscal_product_rules[0].seller_tax_ids + ): + res = fiscal_product_rules[0].seller_tax_ids[0] + if ( + taxes[0].type_tax_use == "purchase" + and fiscal_product_rules[0].supplier_tax_ids + ): + res = fiscal_product_rules[0].supplier_tax_ids[0] + if res: + return res + return super().map_tax(taxes=taxes, product=product, partner=partner) diff --git a/account_fiscal_product_rule/models/account_move.py b/account_fiscal_product_rule/models/account_move.py new file mode 100644 index 000000000..4d95bc4ef --- /dev/null +++ b/account_fiscal_product_rule/models/account_move.py @@ -0,0 +1,52 @@ +# Copyright 2022 Akretion France (http://www.akretion.com) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, models + + +class AccountMoveLine(models.Model): + _inherit = "account.move.line" + + @api.onchange("product_id") + def _onchange_product_id(self): + super()._onchange_product_id() + for line in self: + if not line.product_id or line.display_type in ( + "line_section", + "line_note", + ): + continue + + taxes = line._get_computed_taxes() + if ( + taxes + and line.move_id.fiscal_position_id.fiscal_position_product_rule_ids + ): + taxes = line.move_id.fiscal_position_id.map_tax( + taxes, line.product_id, line.partner_id + ) + line.tax_ids = taxes + line.product_uom_id = line._get_computed_uom() + line.price_unit = line.with_context( + product_id=line.product_id.id + )._get_computed_price_unit() + + @api.onchange("product_uom_id") + def _onchange_uom_id(self): + super()._onchange_uom_id() + for line in self: + if line.display_type in ("line_section", "line_note"): + continue + + taxes = line._get_computed_taxes() + if ( + taxes + and line.move_id.fiscal_position_id.fiscal_position_product_rule_ids + ): + taxes = line.move_id.fiscal_position_id.map_tax( + taxes, line.product_id, line.partner_id + ) + line.tax_ids = taxes + line.price_unit = line.with_context( + product_id=line.product_id.id + )._get_computed_price_unit() diff --git a/account_fiscal_product_rule/models/product.py b/account_fiscal_product_rule/models/product.py new file mode 100644 index 000000000..e46e4e0e0 --- /dev/null +++ b/account_fiscal_product_rule/models/product.py @@ -0,0 +1,36 @@ +# Copyright 2022 Akretion France (http://www.akretion.com) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ProductCategory(models.Model): + _inherit = "product.category" + + fiscal_position_product_rule_ids = fields.Many2many( + "account.fiscal.position.product.rule", string="Fiscal Rule" + ) + + +class ProductTemplate(models.Model): + _inherit = "product.template" + + fiscal_position_product_rule_ids = fields.Many2many( + "account.fiscal.position.product.rule", string="Fiscal Rule" + ) + + def get_product_accounts(self, fiscal_pos=None): + for product in self: + if fiscal_pos: + fiscal_product_rules = ( + fiscal_pos.fiscal_position_product_rule_ids.filtered( + lambda r: product in r.product_tmpl_ids + or product.categ_id in r.product_category_ids + ) + ) + if fiscal_product_rules: + accounts = {} + accounts["income"] = fiscal_product_rules[0].account_income_id + accounts["expense"] = fiscal_product_rules[0].account_expense_id + return accounts + return super().get_product_accounts(fiscal_pos=fiscal_pos) diff --git a/account_fiscal_product_rule/readme/CONTRIBUTORS.rst b/account_fiscal_product_rule/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..eb429ce84 --- /dev/null +++ b/account_fiscal_product_rule/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* Sébastien Beau +* Chafique Delli diff --git a/account_fiscal_product_rule/readme/DESCRIPTION.rst b/account_fiscal_product_rule/readme/DESCRIPTION.rst new file mode 100644 index 000000000..baf806eb5 --- /dev/null +++ b/account_fiscal_product_rule/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module allows to map tax and account depending of the product. diff --git a/account_fiscal_product_rule/security/ir.model.access.csv b/account_fiscal_product_rule/security/ir.model.access.csv new file mode 100644 index 000000000..b6a4e89e4 --- /dev/null +++ b/account_fiscal_product_rule/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_account_fiscal_position_product_rule,access on account.fiscal.position.product.rule,model_account_fiscal_position_product_rule,account.group_account_user,1,1,1,1 diff --git a/account_fiscal_product_rule/static/description/index.html b/account_fiscal_product_rule/static/description/index.html new file mode 100644 index 000000000..e5a97438c --- /dev/null +++ b/account_fiscal_product_rule/static/description/index.html @@ -0,0 +1,415 @@ + + + + + + +Account Fiscal Product Rule + + + +
+

Account Fiscal Product Rule

+ + +

Beta License: AGPL-3 OCA/account-fiscal-rule

+

This module allows to map tax and account depending of the 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

+
    +
  • Akretion
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is part of the OCA/account-fiscal-rule project on GitHub.

+

You are welcome to contribute.

+
+
+
+ + diff --git a/account_fiscal_product_rule/tests/__init__.py b/account_fiscal_product_rule/tests/__init__.py new file mode 100644 index 000000000..902df7676 --- /dev/null +++ b/account_fiscal_product_rule/tests/__init__.py @@ -0,0 +1 @@ +from . import test_account_fiscal_product_rule diff --git a/account_fiscal_product_rule/tests/test_account_fiscal_product_rule.py b/account_fiscal_product_rule/tests/test_account_fiscal_product_rule.py new file mode 100644 index 000000000..5d3bcb8ae --- /dev/null +++ b/account_fiscal_product_rule/tests/test_account_fiscal_product_rule.py @@ -0,0 +1,62 @@ +# Copyright 2022 Akretion France (http://www.akretion.com) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.tests import tagged + +from odoo.addons.account.tests.common import AccountTestInvoicingCommon + + +@tagged("post_install", "-at_install") +class TestAccountFiscalProductRule(AccountTestInvoicingCommon): + @classmethod + def setUpClass(cls, chart_template_ref=None): + super().setUpClass(chart_template_ref=chart_template_ref) + # ===== Taxes ===== + cls.tax_sale = cls.company_data["default_tax_sale"].copy(default={"amount": 30}) + # ===== Accounts ===== + cls.account_income = cls.copy_account( + cls.company_data["default_account_revenue"] + ) + cls.account_income.code = "123456" + # ===== Fiscal Position Product Rules ===== + cls.fiscal_product_rule = cls.env[ + "account.fiscal.position.product.rule" + ].create( + { + "name": "Fiscal Product Template Rule", + "fiscal_position_id": cls.fiscal_pos_a.id, + "seller_tax_ids": [(6, 0, cls.tax_sale.ids)], + "account_income_id": cls.account_income.id, + } + ) + + def test_no_rule(self): + invoice = self.init_invoice( + "out_invoice", partner=self.partner_b, products=[self.product_a] + ) + line = invoice.line_ids.filtered(lambda r: r.product_id == self.product_a) + # check the tax/account + self.assertEqual(line.tax_ids[0].amount, 15.0) + self.assertEqual(line.account_id.code, "400000 (1)") + + def test_rule_on_categ(self): + self.product_a.categ_id.fiscal_position_product_rule_ids = ( + self.fiscal_product_rule + ) + invoice = self.init_invoice( + "out_invoice", partner=self.partner_b, products=[self.product_a] + ) + line = invoice.line_ids.filtered(lambda r: r.product_id == self.product_a) + # check the tax/account is the on define by the rule + self.assertEqual(line.tax_ids[0].amount, 30.0) + self.assertEqual(line.account_id.code, "123456") + + def test_rule_on_product(self): + self.product_a.fiscal_position_product_rule_ids = self.fiscal_product_rule + invoice = self.init_invoice( + "out_invoice", partner=self.partner_b, products=[self.product_a] + ) + line = invoice.line_ids.filtered(lambda r: r.product_id == self.product_a) + # check the tax/account is the on define by the rule + self.assertEqual(line.tax_ids[0].amount, 30.0) + self.assertEqual(line.account_id.code, "123456") diff --git a/account_fiscal_product_rule/views/account_fiscal_position.xml b/account_fiscal_product_rule/views/account_fiscal_position.xml new file mode 100644 index 000000000..0c5b7a642 --- /dev/null +++ b/account_fiscal_product_rule/views/account_fiscal_position.xml @@ -0,0 +1,85 @@ + + + + account.fiscal.position + + + + + + + + + + + + + + + + + + + + + + + + account.fiscal.product.rule.filter + account.fiscal.position.product.rule + + + + + + + + + account.fiscal.product.rule.tree + account.fiscal.position.product.rule + + + + + + + + + + + + + + + + + Product Fiscal Rules + account.fiscal.position.product.rule + tree + + +

+ Create a new product fiscal rule +

+
+
+ + +
diff --git a/account_fiscal_product_rule/views/account_menuitem.xml b/account_fiscal_product_rule/views/account_menuitem.xml new file mode 100644 index 000000000..bcf4a856a --- /dev/null +++ b/account_fiscal_product_rule/views/account_menuitem.xml @@ -0,0 +1,11 @@ + + + + + diff --git a/account_fiscal_product_rule/views/product.xml b/account_fiscal_product_rule/views/product.xml new file mode 100644 index 000000000..644c27e11 --- /dev/null +++ b/account_fiscal_product_rule/views/product.xml @@ -0,0 +1,39 @@ + + + + product.template + + + + + + + + + + + + product.category + + + + + + + + + + + diff --git a/setup/account_fiscal_product_rule/odoo/addons/account_fiscal_product_rule b/setup/account_fiscal_product_rule/odoo/addons/account_fiscal_product_rule new file mode 120000 index 000000000..a299b8d6a --- /dev/null +++ b/setup/account_fiscal_product_rule/odoo/addons/account_fiscal_product_rule @@ -0,0 +1 @@ +../../../../account_fiscal_product_rule \ No newline at end of file diff --git a/setup/account_fiscal_product_rule/setup.py b/setup/account_fiscal_product_rule/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/account_fiscal_product_rule/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) From 0d9218ec24d25e8d304b8bebcaf1bfcd9f000aa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20BEAU?= Date: Wed, 7 Sep 2022 20:59:04 +0200 Subject: [PATCH 2/4] account_fiscal_product_rule: support recursivity on category --- .../models/account_fiscal_position.py | 5 ++- account_fiscal_product_rule/models/product.py | 35 ++++++++++++------- .../tests/test_account_fiscal_product_rule.py | 16 +++++++++ 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/account_fiscal_product_rule/models/account_fiscal_position.py b/account_fiscal_product_rule/models/account_fiscal_position.py index 4d846ac27..897b08837 100644 --- a/account_fiscal_product_rule/models/account_fiscal_position.py +++ b/account_fiscal_product_rule/models/account_fiscal_position.py @@ -38,9 +38,8 @@ def map_tax(self, taxes, product=None, partner=None): self.env.context.get("product_id", False) ) for fp in self: - fiscal_product_rules = fp.fiscal_position_product_rule_ids.filtered( - lambda r: product.product_tmpl_id in r.product_tmpl_ids - or product.categ_id in r.product_category_ids + fiscal_product_rules = ( + product.product_tmpl_id.get_matching_product_fiscal_rule(fp) ) if fiscal_product_rules: res = self.env["account.tax"] diff --git a/account_fiscal_product_rule/models/product.py b/account_fiscal_product_rule/models/product.py index e46e4e0e0..93f3ba0cc 100644 --- a/account_fiscal_product_rule/models/product.py +++ b/account_fiscal_product_rule/models/product.py @@ -11,6 +11,15 @@ class ProductCategory(models.Model): "account.fiscal.position.product.rule", string="Fiscal Rule" ) + def get_matching_product_fiscal_rule(self, fiscal_pos): + self.ensure_one() + return self.fiscal_position_product_rule_ids.filtered( + lambda r: r.fiscal_position_id == fiscal_pos + ) or ( + self.parent_id + and self.parent_id.get_matching_product_fiscal_rule(fiscal_pos) + ) + class ProductTemplate(models.Model): _inherit = "product.template" @@ -19,18 +28,18 @@ class ProductTemplate(models.Model): "account.fiscal.position.product.rule", string="Fiscal Rule" ) + def get_matching_product_fiscal_rule(self, fiscal_pos): + self.ensure_one() + return self.fiscal_position_product_rule_ids.filtered( + lambda r: r.fiscal_position_id == fiscal_pos + ) or self.categ_id.get_matching_product_fiscal_rule(fiscal_pos) + def get_product_accounts(self, fiscal_pos=None): - for product in self: - if fiscal_pos: - fiscal_product_rules = ( - fiscal_pos.fiscal_position_product_rule_ids.filtered( - lambda r: product in r.product_tmpl_ids - or product.categ_id in r.product_category_ids - ) - ) - if fiscal_product_rules: - accounts = {} - accounts["income"] = fiscal_product_rules[0].account_income_id - accounts["expense"] = fiscal_product_rules[0].account_expense_id - return accounts + if fiscal_pos: + rule = self.get_matching_product_fiscal_rule(fiscal_pos) + if rule: + return { + "income": rule.account_income_id, + "expense": rule.account_expense_id, + } return super().get_product_accounts(fiscal_pos=fiscal_pos) diff --git a/account_fiscal_product_rule/tests/test_account_fiscal_product_rule.py b/account_fiscal_product_rule/tests/test_account_fiscal_product_rule.py index 5d3bcb8ae..ddeecc92f 100644 --- a/account_fiscal_product_rule/tests/test_account_fiscal_product_rule.py +++ b/account_fiscal_product_rule/tests/test_account_fiscal_product_rule.py @@ -36,9 +36,23 @@ def test_no_rule(self): ) line = invoice.line_ids.filtered(lambda r: r.product_id == self.product_a) # check the tax/account + self.assertEqual(len(line.tax_ids), 1) self.assertEqual(line.tax_ids[0].amount, 15.0) self.assertEqual(line.account_id.code, "400000 (1)") + def test_rule_on_parent_categ(self): + categ = self.env.ref("product.product_category_1") + categ.parent_id.fiscal_position_product_rule_ids = self.fiscal_product_rule + self.product_a.categ_id = categ + invoice = self.init_invoice( + "out_invoice", partner=self.partner_b, products=[self.product_a] + ) + line = invoice.line_ids.filtered(lambda r: r.product_id == self.product_a) + # check the tax/account is the on define by the rule + self.assertEqual(len(line.tax_ids), 1) + self.assertEqual(line.tax_ids[0].amount, 30.0) + self.assertEqual(line.account_id.code, "123456") + def test_rule_on_categ(self): self.product_a.categ_id.fiscal_position_product_rule_ids = ( self.fiscal_product_rule @@ -48,6 +62,7 @@ def test_rule_on_categ(self): ) line = invoice.line_ids.filtered(lambda r: r.product_id == self.product_a) # check the tax/account is the on define by the rule + self.assertEqual(len(line.tax_ids), 1) self.assertEqual(line.tax_ids[0].amount, 30.0) self.assertEqual(line.account_id.code, "123456") @@ -58,5 +73,6 @@ def test_rule_on_product(self): ) line = invoice.line_ids.filtered(lambda r: r.product_id == self.product_a) # check the tax/account is the on define by the rule + self.assertEqual(len(line.tax_ids), 1) self.assertEqual(line.tax_ids[0].amount, 30.0) self.assertEqual(line.account_id.code, "123456") From 2fd3dc9d177a78a6f7ced42544c8008425e7a5a2 Mon Sep 17 00:00:00 2001 From: "chafique.delli" Date: Thu, 8 Sep 2022 18:16:16 +0200 Subject: [PATCH 3/4] account_fiscal_product_rule: refactor and cleaner code --- account_fiscal_product_rule/__manifest__.py | 1 - .../models/account_fiscal_position.py | 60 +++++++++++------- .../models/account_move.py | 42 ++----------- account_fiscal_product_rule/models/product.py | 16 ++--- .../tests/test_account_fiscal_product_rule.py | 13 +++- .../views/account_fiscal_position.xml | 62 +++---------------- 6 files changed, 67 insertions(+), 127 deletions(-) diff --git a/account_fiscal_product_rule/__manifest__.py b/account_fiscal_product_rule/__manifest__.py index 843004ed2..ba155db00 100644 --- a/account_fiscal_product_rule/__manifest__.py +++ b/account_fiscal_product_rule/__manifest__.py @@ -12,7 +12,6 @@ "security/ir.model.access.csv", "views/product.xml", "views/account_fiscal_position.xml", - "views/account_menuitem.xml", ], "installable": True, } diff --git a/account_fiscal_product_rule/models/account_fiscal_position.py b/account_fiscal_product_rule/models/account_fiscal_position.py index 897b08837..22b9de761 100644 --- a/account_fiscal_product_rule/models/account_fiscal_position.py +++ b/account_fiscal_product_rule/models/account_fiscal_position.py @@ -1,7 +1,8 @@ # Copyright 2022 Akretion France (http://www.akretion.com) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import fields, models +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError class AccountFiscalPositionProductRule(models.Model): @@ -22,6 +23,31 @@ class AccountFiscalPositionProductRule(models.Model): company_id = fields.Many2one(related="fiscal_position_id.company_id") +class ProductRuleMixin(models.AbstractModel): + _name = "product.rule.mixin" + _description = "Product Rule Mixin" + + fiscal_position_product_rule_ids = fields.Many2many( + "account.fiscal.position.product.rule", string="Product Fiscal Rules" + ) + + @api.constrains("fiscal_position_product_rule_ids") + def _check_no_duplicate_fiscal_position(self): + for record in self: + fps = [] + # import pdb;pdb.set_trace() + for rule in record.fiscal_position_product_rule_ids: + if rule.fiscal_position_id in fps: + raise ValidationError( + _( + "A Fiscal Position Product Rule already exists for this product " + "or product category with this fiscal position !" + ) + ) + else: + fps.append(rule.fiscal_position_id) + + class AccountFiscalPosition(models.Model): _inherit = "account.fiscal.position" @@ -32,27 +58,13 @@ class AccountFiscalPosition(models.Model): ) def map_tax(self, taxes, product=None, partner=None): - if product or self.env.context.get("product_id", False): - if not product: - product = self.env["product.product"].browse( - self.env.context.get("product_id", False) - ) - for fp in self: - fiscal_product_rules = ( - product.product_tmpl_id.get_matching_product_fiscal_rule(fp) - ) - if fiscal_product_rules: - res = self.env["account.tax"] - if ( - taxes[0].type_tax_use == "sale" - and fiscal_product_rules[0].seller_tax_ids - ): - res = fiscal_product_rules[0].seller_tax_ids[0] - if ( - taxes[0].type_tax_use == "purchase" - and fiscal_product_rules[0].supplier_tax_ids - ): - res = fiscal_product_rules[0].supplier_tax_ids[0] - if res: - return res + prod = product or self._context.get("product") + if prod: + rule = prod.product_tmpl_id.get_matching_product_fiscal_rule(self) + if rule and taxes: + tax_use = taxes[0].type_tax_use + if tax_use == "sale" and rule.seller_tax_ids: + return rule.seller_tax_ids + elif tax_use == "purchase" and rule.supplier_tax_ids: + return rule.supplier_tax_ids return super().map_tax(taxes=taxes, product=product, partner=partner) diff --git a/account_fiscal_product_rule/models/account_move.py b/account_fiscal_product_rule/models/account_move.py index 4d95bc4ef..88409c54e 100644 --- a/account_fiscal_product_rule/models/account_move.py +++ b/account_fiscal_product_rule/models/account_move.py @@ -9,44 +9,14 @@ class AccountMoveLine(models.Model): @api.onchange("product_id") def _onchange_product_id(self): - super()._onchange_product_id() for line in self: - if not line.product_id or line.display_type in ( - "line_section", - "line_note", - ): - continue - - taxes = line._get_computed_taxes() - if ( - taxes - and line.move_id.fiscal_position_id.fiscal_position_product_rule_ids - ): - taxes = line.move_id.fiscal_position_id.map_tax( - taxes, line.product_id, line.partner_id - ) - line.tax_ids = taxes - line.product_uom_id = line._get_computed_uom() - line.price_unit = line.with_context( - product_id=line.product_id.id - )._get_computed_price_unit() + super( + AccountMoveLine, line.with_context(product=line.product_id) + )._onchange_product_id() @api.onchange("product_uom_id") def _onchange_uom_id(self): - super()._onchange_uom_id() for line in self: - if line.display_type in ("line_section", "line_note"): - continue - - taxes = line._get_computed_taxes() - if ( - taxes - and line.move_id.fiscal_position_id.fiscal_position_product_rule_ids - ): - taxes = line.move_id.fiscal_position_id.map_tax( - taxes, line.product_id, line.partner_id - ) - line.tax_ids = taxes - line.price_unit = line.with_context( - product_id=line.product_id.id - )._get_computed_price_unit() + super( + AccountMoveLine, line.with_context(product=line.product_id) + )._onchange_uom_id() diff --git a/account_fiscal_product_rule/models/product.py b/account_fiscal_product_rule/models/product.py index 93f3ba0cc..bb151382c 100644 --- a/account_fiscal_product_rule/models/product.py +++ b/account_fiscal_product_rule/models/product.py @@ -1,15 +1,12 @@ # Copyright 2022 Akretion France (http://www.akretion.com) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import fields, models +from odoo import models class ProductCategory(models.Model): - _inherit = "product.category" - - fiscal_position_product_rule_ids = fields.Many2many( - "account.fiscal.position.product.rule", string="Fiscal Rule" - ) + _name = "product.category" + _inherit = ["product.category", "product.rule.mixin"] def get_matching_product_fiscal_rule(self, fiscal_pos): self.ensure_one() @@ -22,11 +19,8 @@ def get_matching_product_fiscal_rule(self, fiscal_pos): class ProductTemplate(models.Model): - _inherit = "product.template" - - fiscal_position_product_rule_ids = fields.Many2many( - "account.fiscal.position.product.rule", string="Fiscal Rule" - ) + _name = "product.template" + _inherit = ["product.template", "product.rule.mixin"] def get_matching_product_fiscal_rule(self, fiscal_pos): self.ensure_one() diff --git a/account_fiscal_product_rule/tests/test_account_fiscal_product_rule.py b/account_fiscal_product_rule/tests/test_account_fiscal_product_rule.py index ddeecc92f..a242ed412 100644 --- a/account_fiscal_product_rule/tests/test_account_fiscal_product_rule.py +++ b/account_fiscal_product_rule/tests/test_account_fiscal_product_rule.py @@ -1,6 +1,7 @@ # Copyright 2022 Akretion France (http://www.akretion.com) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo.exceptions import ValidationError from odoo.tests import tagged from odoo.addons.account.tests.common import AccountTestInvoicingCommon @@ -23,12 +24,15 @@ def setUpClass(cls, chart_template_ref=None): "account.fiscal.position.product.rule" ].create( { - "name": "Fiscal Product Template Rule", + "name": "Fiscal Product Rule", "fiscal_position_id": cls.fiscal_pos_a.id, "seller_tax_ids": [(6, 0, cls.tax_sale.ids)], "account_income_id": cls.account_income.id, } ) + cls.copy_fiscal_product_rule = cls.fiscal_product_rule.copy( + default={"name": "Fiscal Product Rule (copy)"} + ) def test_no_rule(self): invoice = self.init_invoice( @@ -76,3 +80,10 @@ def test_rule_on_product(self): self.assertEqual(len(line.tax_ids), 1) self.assertEqual(line.tax_ids[0].amount, 30.0) self.assertEqual(line.account_id.code, "123456") + + def test_no_duplicate_fiscal_position(self): + self.product_a.fiscal_position_product_rule_ids = self.fiscal_product_rule + with self.assertRaises(ValidationError): + self.product_a.fiscal_position_product_rule_ids += ( + self.copy_fiscal_product_rule + ) diff --git a/account_fiscal_product_rule/views/account_fiscal_position.xml b/account_fiscal_product_rule/views/account_fiscal_position.xml index 0c5b7a642..e3b03c100 100644 --- a/account_fiscal_product_rule/views/account_fiscal_position.xml +++ b/account_fiscal_product_rule/views/account_fiscal_position.xml @@ -12,7 +12,12 @@ widget="one2many" nolabel="1" > - + - - + + @@ -31,55 +36,4 @@ - - account.fiscal.product.rule.filter - account.fiscal.position.product.rule - - - - - - - - - account.fiscal.product.rule.tree - account.fiscal.position.product.rule - - - - - - - - - - - - - - - - - Product Fiscal Rules - account.fiscal.position.product.rule - tree - - -

- Create a new product fiscal rule -

-
-
- - From b9b6906d98cc9776f509ae466c0ed28cfc48157e Mon Sep 17 00:00:00 2001 From: "chafique.delli" Date: Thu, 8 Sep 2022 18:59:11 +0200 Subject: [PATCH 4/4] account_fiscal_product_rule: have constraint when create new fiscal product rule in the fiscal position --- .../models/account_fiscal_position.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/account_fiscal_product_rule/models/account_fiscal_position.py b/account_fiscal_product_rule/models/account_fiscal_position.py index 22b9de761..4a46e489e 100644 --- a/account_fiscal_product_rule/models/account_fiscal_position.py +++ b/account_fiscal_product_rule/models/account_fiscal_position.py @@ -22,6 +22,14 @@ class AccountFiscalPositionProductRule(models.Model): ) company_id = fields.Many2one(related="fiscal_position_id.company_id") + @api.constrains("product_tmpl_ids") + def _check_no_duplicate_fiscal_position_on_product(self): + self.product_tmpl_ids._check_no_duplicate_fiscal_position() + + @api.constrains("product_category_ids") + def _check_no_duplicate_fiscal_position_on_category(self): + self.product_category_ids._check_no_duplicate_fiscal_position() + class ProductRuleMixin(models.AbstractModel): _name = "product.rule.mixin" @@ -35,7 +43,6 @@ class ProductRuleMixin(models.AbstractModel): def _check_no_duplicate_fiscal_position(self): for record in self: fps = [] - # import pdb;pdb.set_trace() for rule in record.fiscal_position_product_rule_ids: if rule.fiscal_position_id in fps: raise ValidationError(