From 71b505fbdcd7089e6cf771ad10101cbea8ad8fab Mon Sep 17 00:00:00 2001 From: jacopobacci Date: Tue, 17 Dec 2024 16:35:43 +0100 Subject: [PATCH] feat(sustainability): enhance carbon tracking with supplier info integration Integrate product supplier information into carbon tracking models to improve carbon emission calculations. Add support for carbon factor management in supplier info views, allowing manual and automatic modes. Enhance carbon distribution validation and computation logic to include product and supplier data, ensuring more accurate carbon footprint assessments. --- sustainability/models/carbon_line_mixin.py | 1 + sustainability/models/carbon_mixin.py | 4 +- sustainability_purchase/__manifest__.py | 2 +- sustainability_purchase/models/__init__.py | 3 +- .../models/account_move_line.py | 10 +++- .../models/product_product.py | 15 ++++++ .../models/product_supplierinfo.py | 23 +++++++-- .../views/product_supplierinfo.xml | 50 ++++++++++++------- 8 files changed, 79 insertions(+), 29 deletions(-) create mode 100644 sustainability_purchase/models/product_product.py diff --git a/sustainability/models/carbon_line_mixin.py b/sustainability/models/carbon_line_mixin.py index 1e600515..bbbec683 100644 --- a/sustainability/models/carbon_line_mixin.py +++ b/sustainability/models/carbon_line_mixin.py @@ -117,6 +117,7 @@ def _get_computation_levels_mapping(self) -> dict: "product.product": _("Product"), "product.category": _("Product category"), "product.template": _("Product template"), + "product.supplierinfo": _("Product supplier"), "res.partner": _("Partner"), "account.account": _("Account"), "res.company": _("Company fallback"), diff --git a/sustainability/models/carbon_mixin.py b/sustainability/models/carbon_mixin.py index cd69f544..2334ed73 100644 --- a/sustainability/models/carbon_mixin.py +++ b/sustainability/models/carbon_mixin.py @@ -40,6 +40,7 @@ # Todo: make this extendable from sub modules CARBON_MODELS = [ "carbon.factor", + "product.supplierinfo", "product.category", "product.product", "product.template", @@ -402,6 +403,8 @@ def has_valid_carbon_value(self, carbon_type: str): def has_valid_carbon_distribution(self, carbon_type: str): self.ensure_one() + if not self[f"carbon_{carbon_type}_use_distribution"]: + return True total_percentage = sum( [line.percentage for line in self._get_distribution_lines(carbon_type)] ) @@ -445,7 +448,6 @@ def action_recompute_carbon(self, carbon_type: str = None): carbon_type = carbon_type or self.env.context.get("carbon_type", "carbon_in") if not hasattr(self, f"{carbon_type}_value"): return False - getattr(self, f"_compute_{carbon_type}_mode")() return True diff --git a/sustainability_purchase/__manifest__.py b/sustainability_purchase/__manifest__.py index ca96b7cf..31cd2fd7 100644 --- a/sustainability_purchase/__manifest__.py +++ b/sustainability_purchase/__manifest__.py @@ -16,7 +16,7 @@ # Data "data/ir_cron.xml", # Views - # "views/product_supplierinfo.xml", + "views/product_supplierinfo.xml", "views/carbon_factor.xml", "views/carbon_line_origin.xml", "views/purchase_order.xml", diff --git a/sustainability_purchase/models/__init__.py b/sustainability_purchase/models/__init__.py index fee66e46..18ad15e0 100644 --- a/sustainability_purchase/models/__init__.py +++ b/sustainability_purchase/models/__init__.py @@ -1,4 +1,5 @@ -# from . import product_supplierinfo +from . import product_supplierinfo +from . import product_product from . import account_move_line from . import product_template from . import purchase_order diff --git a/sustainability_purchase/models/account_move_line.py b/sustainability_purchase/models/account_move_line.py index 280f4b6f..b69f2237 100644 --- a/sustainability_purchase/models/account_move_line.py +++ b/sustainability_purchase/models/account_move_line.py @@ -11,7 +11,11 @@ class AccountMoveLine(models.Model): @api.model def _get_carbon_compute_possible_fields(self) -> list[str]: res = super()._get_carbon_compute_possible_fields() - res = ["partner_id", "move_partner_id"] + res + res = [ + "product_id", + "partner_id", + "move_partner_id", + ] + res return res # --- Partner --- @@ -29,7 +33,9 @@ def can_use_move_partner_id_carbon_value(self) -> bool: ) @api.depends( - "partner_id.carbon_in_factor_id", "move_partner_id.carbon_in_factor_id" + "partner_id.carbon_in_factor_id", + "move_partner_id.carbon_in_factor_id", + "product_id.carbon_in_factor_id", ) def _compute_carbon_debt(self, force_compute=None): return super()._compute_carbon_debt(force_compute) diff --git a/sustainability_purchase/models/product_product.py b/sustainability_purchase/models/product_product.py new file mode 100644 index 00000000..e02e82c6 --- /dev/null +++ b/sustainability_purchase/models/product_product.py @@ -0,0 +1,15 @@ +from odoo import api, models + + +class ProductProduct(models.Model): + _inherit = "product.product" + + def _get_carbon_in_fallback_records(self) -> list: + res = super()._get_carbon_in_fallback_records() + return [x for x in self.seller_ids] + res + + @api.depends( + "seller_ids", + ) + def _compute_carbon_in_mode(self): + return super()._compute_carbon_in_mode() diff --git a/sustainability_purchase/models/product_supplierinfo.py b/sustainability_purchase/models/product_supplierinfo.py index 03664310..fb85066f 100644 --- a/sustainability_purchase/models/product_supplierinfo.py +++ b/sustainability_purchase/models/product_supplierinfo.py @@ -1,4 +1,4 @@ -from odoo import models +from odoo import api, models class ProductSupplierInfo(models.Model): @@ -10,7 +10,20 @@ def _get_carbon_in_fallback_records(self) -> list: res = super()._get_carbon_in_fallback_records() return res + [self.partner_id] - def _get_carbon_out_fallback_records(self) -> list: - self.ensure_one() - res = super()._get_carbon_out_fallback_records() - return res + [self.partner_id] + @api.depends("partner_id.carbon_in_factor_id") + def _compute_carbon_in_mode(self): + return super()._compute_carbon_in_mode() + + @api.onchange("carbon_in_factor_id") + def _onchange_carbon_in_factor_id(self): + """ + If carbon_in_factor_id is selected, set carbon_in_is_manual to True + and carbon_in_mode to 'manual'. Otherwise, reset to default values. + """ + for record in self: + if record.carbon_in_factor_id: + record.carbon_in_is_manual = True + record.carbon_in_mode = "manual" + else: + record.carbon_in_is_manual = False + record.carbon_in_mode = "auto" diff --git a/sustainability_purchase/views/product_supplierinfo.xml b/sustainability_purchase/views/product_supplierinfo.xml index 0a8af568..f27a5c23 100644 --- a/sustainability_purchase/views/product_supplierinfo.xml +++ b/sustainability_purchase/views/product_supplierinfo.xml @@ -1,29 +1,41 @@ - - product_supplierinfo_tree_view2 + + product_supplierinfo_form_view + product.supplierinfo + + + + + + + + + + + + + + sustainability.product.supplierinfo.tree.view product.supplierinfo - -