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

FIX acc_fisc_classif: find or create a classif if none #435

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
55 changes: 42 additions & 13 deletions account_product_fiscal_classification/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import json
import logging

from lxml import etree

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo import api, fields, models

_logger = logging.getLogger(__name__)


class ProductTemplate(models.Model):
Expand All @@ -32,7 +34,7 @@ class ProductTemplate(models.Model):
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
self._update_vals_fiscal_classification(vals)
self._update_vals_fiscal_classification(vals, create_mode=True)
return super().create(vals_list)

def write(self, vals):
Expand Down Expand Up @@ -60,9 +62,9 @@ def get_view(self, view_id=None, view_type="form", **options):
return result

# Custom Section
def _update_vals_fiscal_classification(self, vals):
def _update_vals_fiscal_classification(self, vals, create_mode=False):
FiscalClassification = self.env["account.product.fiscal.classification"]
if vals.get("fiscal_classification_id", False):
if vals.get("fiscal_classification_id"):
# We use sudo to have access to all the taxes, even taxes that belong
# to companies that the user can't access in the current context
classification = FiscalClassification.sudo().browse(
Expand All @@ -74,16 +76,43 @@ def _update_vals_fiscal_classification(self, vals):
"taxes_id": [(6, 0, classification.sale_tax_ids.ids)],
}
)
elif vals.get("supplier_taxes_id") or vals.get("taxes_id"):
raise ValidationError(
_(
"You can not create or write products with"
" 'Customer Taxes' or 'Supplier Taxes'\n."
"Please, use instead the 'Fiscal Classification' field."
)
)
elif create_mode or {"supplier_taxes_id", "taxes_id"} & vals.keys():
self._find_or_create_classification(vals)
bealdav marked this conversation as resolved.
Show resolved Hide resolved
return vals

@api.constrains("categ_id", "fiscal_classification_id")
def _check_rules_fiscal_classification(self):
self.env["account.product.fiscal.rule"].check_product_templates_integrity(self)

def _find_or_create_classification(self, vals):
"""Find the correct Fiscal classification,
depending of the taxes, or create a new one, if no one are found."""
# search for matching classication
purchase_tax_ids = vals.get("supplier_taxes_id", [])
sale_tax_ids = vals.get("taxes_id", [])
for elm in ("supplier_taxes_id", "taxes_id"):
if elm in vals:
del vals[elm]
domain = [
("sale_tax_ids", "in", sale_tax_ids),
("purchase_tax_ids", "in", purchase_tax_ids),
]
classification = self.env["account.product.fiscal.classification"].search(
domain, limit=1
)
if not classification:
# Create a dedicate classification for these taxes combination
classif_vals = self.env[
"account.product.fiscal.classification"
]._prepare_vals_from_taxes(
self.env["account.tax"].browse(purchase_tax_ids),
self.env["account.tax"].browse(sale_tax_ids),
)
classification = self.env["account.product.fiscal.classification"].create(
classif_vals
)
_logger.info(
f"Creating new Fiscal Classification '{classif_vals['name']}'"
f" for {self.display_name}"
)
vals["fiscal_classification_id"] = classification.id
47 changes: 46 additions & 1 deletion account_product_fiscal_classification/tests/test_module.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright (C) 2014-Today GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo.exceptions import ValidationError
from odoo.tests.common import TransactionCase

Expand Down Expand Up @@ -167,6 +166,52 @@ def test_30_rules(self):
self.fiscal_classification_B_company_1,
)

def test_no_classification_and_find_one(self):
classif = self.env.ref(
"account_product_fiscal_classification.fiscal_classification_A_company_1"
)
vals = {
"name": "Test Product",
"company_id": self.env.company.id,
"categ_id": self.category_all.id,
"taxes_id": classif.sale_tax_ids.ids,
"supplier_taxes_id": classif.purchase_tax_ids.ids,
}
product = self.ProductTemplate.with_user(self.env.user).create(vals)
self.assertEqual(product.fiscal_classification_id, classif)

def test_no_classification_and_create_one(self):
classif_co = self.env["account.product.fiscal.classification"].search_count([])
my_tax = self.env["account.tax"].create(
{"name": "my_tax", "type_tax_use": "sale", "amount": 9.99}
)
vals = {
"name": "Test Product",
"company_id": self.env.company.id,
"categ_id": self.category_all.id,
"taxes_id": my_tax.ids,
"supplier_taxes_id": [],
}
product = self.ProductTemplate.with_user(self.env.user).create(vals)
self.assertNotEqual(product.fiscal_classification_id, False)
classif_co_after = self.env[
"account.product.fiscal.classification"
].search_count([])
self.assertEqual(classif_co_after, classif_co + 1)

def test_no_tax_nor_classification_and_create_one(self):
vals = {
"name": "Test Product",
"company_id": self.env.company.id,
"categ_id": self.category_all.id,
"taxes_id": [],
"supplier_taxes_id": [],
}
product = self.ProductTemplate.with_user(self.env.user).create(vals)
classif = product.fiscal_classification_id
self.assertEqual(classif.purchase_tax_ids, self.env["account.tax"])
self.assertEqual(classif.sale_tax_ids, self.env["account.tax"])

def _create_product(self, user, category, classification):
vals = {
"name": "Test Product",
Expand Down
Loading