Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][ADD] account_fiscal_product_rule #305

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions account_fiscal_product_rule/README.rst
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/OCA/account-fiscal-rule/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 <https://github.com/OCA/account-fiscal-rule/issues/new?body=module:%20account_fiscal_product_rule%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
~~~~~~~

* Akretion

Contributors
~~~~~~~~~~~~

* Sébastien Beau <sebastien.beau@akretion.com>
* Chafique Delli <chafique.delli@akretion.com>

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 <https://github.com/OCA/account-fiscal-rule/tree/14.0/account_fiscal_product_rule>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions account_fiscal_product_rule/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
18 changes: 18 additions & 0 deletions account_fiscal_product_rule/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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,
}
3 changes: 3 additions & 0 deletions account_fiscal_product_rule/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import account_fiscal_position
from . import product
from . import account_move
58 changes: 58 additions & 0 deletions account_fiscal_product_rule/models/account_fiscal_position.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the rule is not dependent anymore from the (product) source tax, what about price included or not?
I think you need to ensure the new tax has the same price_include state

if product or self.env.context.get("product_id", False):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just do

prod = product or self._context.get("product")

avoid to set in the variable "product" the value passed by the context, as if we call super we will inject the product and this may produce error (as odoo do not pass it)

if not product:
product = self.env["product.product"].browse(
self.env.context.get("product_id", False)
)
for fp in self:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/!\ BE CAREFUL /!\
When doing a loop for and inside this loop you use return, in most of the time you do something really wrong !

Because if somebody call with method with several record it will return after processing the first element.

In that case it do not produce a bug because we always call map_tax with only one fiscal position. But adding a loop for mean that you expect several and make harder to understand the code (and also will avoid to raise an error if somewhere the code use an ensure_one)

So remove the loop and use self direct as there is only one element

fiscal_product_rules = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can rename rule instead of fiscal_product_rules

Suggested change
fiscal_product_rules = (
rule = (

Note we should have only one rule (see python constraint in product.py)

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can write shorter/cleaner code

Something like

Suggested change
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
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

And please use if/elif and not only if

return super().map_tax(taxes=taxes, product=product, partner=partner)
52 changes: 52 additions & 0 deletions account_fiscal_product_rule/models/account_move.py
Original file line number Diff line number Diff line change
@@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code is toooooo complex

just do this and the taxes will be the right one directly in the native code, we should avoid to call twice map taxes

Suggested change
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()
for record in self:
super(
AccountMoveLine, record.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()
45 changes: 45 additions & 0 deletions account_fiscal_product_rule/models/product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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"
)

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)
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a python constraint to be sure to not put 2 account.fiscal.position.rule related to the same fiscal position

something like

def check_no_duplicate_fiscal_position(self):
    for record in self:
        fps = []
        for rule in record.fiscal_position_product_rule_ids:
            if rule.fiscal_position_id in fps:
                raise ValidationError(...)
            else:
                fps.append(rule.fiscal_position_id)

To avoid duplicated code for this constraint. You can create a mixin (fiscal.position.product.rule.owner) and put the code for the field definition "fiscal_position_product_rule_ids" and the constrainte

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • add the test that add 2 fiscal rule with the same fiscal position, it should raise an error (you can test only the product or the categ as the code is the same).


class ProductTemplate(models.Model):
_inherit = "product.template"

fiscal_position_product_rule_ids = fields.Many2many(
"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):
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)
2 changes: 2 additions & 0 deletions account_fiscal_product_rule/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Sébastien Beau <sebastien.beau@akretion.com>
* Chafique Delli <chafique.delli@akretion.com>
1 change: 1 addition & 0 deletions account_fiscal_product_rule/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module allows to map tax and account depending of the product.
2 changes: 2 additions & 0 deletions account_fiscal_product_rule/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -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
Loading