Skip to content

Commit

Permalink
[ADD] fermente_custom_import_product_margin_classification
Browse files Browse the repository at this point in the history
  • Loading branch information
legalsylvain committed Nov 25, 2024
1 parent 96ea26a commit 96eb2e7
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 0 deletions.
9 changes: 9 additions & 0 deletions fermente_custom_import_base/models/custom_import_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,20 @@ def _custom_import_get_or_create(

# Overload Section
def _load_records_create(self, vals_list):
# This function is called in many places in odoo Core
# to create records, specially when loading
# 'demo' or 'data' from xml files.
# We only want to alter data in a 'import' process.
if not self.env.context.get("import_file"):
return super()._load_records_create(vals_list)

new_vals_list = []
for vals in vals_list:
new_vals = vals.copy()
self._custom_import_hook_vals(vals, new_vals)
new_vals_list.append(new_vals)

# TODO, move this check in another part
self._custom_import_check_duplicates_new_vals(new_vals_list)

return super()._load_records_create(new_vals_list)
1 change: 1 addition & 0 deletions fermente_custom_import_demo/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# OCA
"account_product_fiscal_classification",
"l10n_fr_department_product_origin",
"product_margin_classification",
"product_net_weight",
"product_origin",
"product_supplierinfo_qty_multiplier",
Expand Down
Binary file modified fermente_custom_import_demo/static/xlsx/template_product.xlsx
Binary file not shown.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (C) 2024 - 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).

{
"name": "GRAP - Custom Product Import - Product Margin Classification Module",
"summary": "Extra GRAP Tools to import product data for"
" Product Margin Classification module",
"version": "16.0.1.0.0",
"category": "Tools",
"author": "GRAP",
"website": "https://github.com/grap/grap-odoo-import",
"license": "AGPL-3",
"depends": ["fermente_custom_import_product", "product_margin_classification"],
"auto_install": True,
"installable": True,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import custom_import_product_mixin
from . import product_template
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (C) 2024 - 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 import _, models
from odoo.tools import float_compare


class CustomImportProductMixin(models.AbstractModel):
_name = "custom.import.product.mixin"
_inherit = ["custom.import.product.mixin"]

# pylint: disable=missing-return
def _custom_import_hook_vals(self, old_vals, new_vals):
super()._custom_import_hook_vals(old_vals, new_vals)
self._custom_import_handle_margin_classification_vals(old_vals, new_vals)

def _custom_import_handle_margin_classification_vals(self, old_vals, new_vals):
profit_margin = (
"grap_import_margin_rate" in old_vals
and old_vals["grap_import_margin_rate"] * 100
)
if profit_margin:
# Look for existing classification
classifications = self.env["product.margin.classification"].search([])
product_classification = False
for classification in classifications:
if not float_compare(
profit_margin,
classification.profit_margin,
precision_rounding=self.env["decimal.precision"].precision_get(
"Margin Rate"
),
):
product_classification = classification
break

if not product_classification:
# If not found, create a new one
product_classification = self.env[
"product.margin.classification"
].create(
{
"name": _(
f"Margin Rate {old_vals['grap_import_margin_rate']} %"
),
"markup": 100 * (profit_margin / (100 - profit_margin)),
}
)
new_vals["margin_classification_id"] = product_classification.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (C) 2024 - 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 import fields, models


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

grap_import_margin_rate = fields.Float(
string="Margin Rate (For import)", store=False
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Sylvain LE GAL <https://twitter.com/legalsylvain>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This module improve the "import" features provided by Odoo.

* ``product.product``:

* Create a margin classification if it doens't exist,
with the correct margin rate amount.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_module
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name,uom_id,categ_id,grap_import_margin_rate
Product 1,Units,All / Saleable,0.333333
Product 2,Units,All / Saleable,0.5
Product 3,Units,All / Saleable,0.4123
Product 4,Units,All / Saleable,0.4123
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright (C) 2024 - 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.tests import tagged

from odoo.addons.fermente_custom_import_product.tests.test_module import (
TestModuleProduct,
)


@tagged("post_install", "-at_install")
class TestModulePurchaseDiscount(TestModuleProduct):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.ProductProduct = cls.env["product.product"]
cls.margin_33 = cls.env.ref(
"product_margin_classification.classification_normal_margin"
)
cls.margin_50 = cls.env.ref(
"product_margin_classification.classification_big_margin"
)

def _test_import_product_margin_classification(self, model):
products, messages = self._test_import_file(
"fermente_custom_import_product_margin_classification",
model,
"product.csv",
folder="product",
)
self.assertFalse(messages)
self.assertEqual(len(products), 4)

product_1 = products.filtered(lambda x: x.name == "Product 1")
self.assertEqual(product_1.margin_classification_id, self.margin_33)

product_2 = products.filtered(lambda x: x.name == "Product 2")
self.assertEqual(product_2.margin_classification_id, self.margin_50)

product_3 = products.filtered(lambda x: x.name == "Product 3")
product_4 = products.filtered(lambda x: x.name == "Product 4")
self.assertEqual(
product_3.margin_classification_id, product_4.margin_classification_id
)

def test_01_import_product_margin_classification_product(self):
self._test_import_product_margin_classification("product.product")

def test_02_import_product_margin_classification_template(self):
self._test_import_product_margin_classification("product.template")
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 96eb2e7

Please sign in to comment.