From 60f7943a5900c7bd7ba2a7da38aeb139de1235c9 Mon Sep 17 00:00:00 2001 From: daniquilez <dani.quilez@gmail.com> Date: Fri, 7 Mar 2025 18:46:58 +0100 Subject: [PATCH] =?UTF-8?q?[IMP]=20=E2=9C=A8=20Define=20config=20journal?= =?UTF-8?q?=20and=20pack=5Ftype=20mixin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__manifest__.py | 1 + .../components/contract_utils.py | 10 +-- .../components/sale_order_utils.py | 2 +- .../models/__init__.py | 1 + .../models/account_move.py | 40 ++++++++---- .../models/contract_template.py | 43 ++---------- .../models/pack_type_mixin.py | 65 +++++++++++++++++++ .../models/res_company.py | 12 ++-- energy_communities_service_invoicing/utils.py | 15 +++-- .../views/account_move_views.xml | 2 +- .../views/contract_views.xml | 1 + .../views/res_company_views.xml | 3 +- energy_selfconsumption/models/__init__.py | 1 + energy_selfconsumption/models/account_move.py | 3 +- .../models/contract_template.py | 13 ++-- .../models/pack_type_mixin.py | 11 ++++ 16 files changed, 146 insertions(+), 77 deletions(-) create mode 100644 energy_communities_service_invoicing/models/pack_type_mixin.py create mode 100644 energy_selfconsumption/models/pack_type_mixin.py diff --git a/energy_communities_service_invoicing/__manifest__.py b/energy_communities_service_invoicing/__manifest__.py index 38cac035e..1a6ac870a 100644 --- a/energy_communities_service_invoicing/__manifest__.py +++ b/energy_communities_service_invoicing/__manifest__.py @@ -22,6 +22,7 @@ "contract_variable_quantity", "energy_communities", "energy_communities_cooperator", # TODO: This dependency is needed for active members formula. Need to refactor this. + "account_invoice_inter_company", ], # always loaded "data": [ diff --git a/energy_communities_service_invoicing/components/contract_utils.py b/energy_communities_service_invoicing/components/contract_utils.py index 3269860b8..bd18f5196 100644 --- a/energy_communities_service_invoicing/components/contract_utils.py +++ b/energy_communities_service_invoicing/components/contract_utils.py @@ -55,10 +55,12 @@ class ContractUtils(Component): for line in self.work.record.contract_line_ids: line.write({"discount": discount}) - def set_configuration_service_invoicing_journal_if_defined(self): - journal_id = self.work.record.company_id.service_invoicing_journal_id - if journal_id: - self.work.record.write({"journal_id": journal_id.id}) + # method to be extended if using component for another pack_type + def set_configuration_journal_if_defined(self): + if self.work.record.pack_type == "platform_pack": + journal_id = self.work.record.company_id.service_invoicing_sale_journal_id + if journal_id: + self.work.record.write({"journal_id": journal_id.id}) def clean_non_service_lines(self): for line in self.work.record.contract_line_ids: diff --git a/energy_communities_service_invoicing/components/sale_order_utils.py b/energy_communities_service_invoicing/components/sale_order_utils.py index 24c5ae56f..e3e241ddc 100644 --- a/energy_communities_service_invoicing/components/sale_order_utils.py +++ b/energy_communities_service_invoicing/components/sale_order_utils.py @@ -77,7 +77,7 @@ class SaleOrderUtils(Component): component.clean_non_service_lines() component.set_start_date(start_date) component.set_discount(discount) - component.set_configuration_service_invoicing_journal_if_defined() + component.set_configuration_journal_if_defined() return service_invoicing_id def create_service_invoicing_initial( diff --git a/energy_communities_service_invoicing/models/__init__.py b/energy_communities_service_invoicing/models/__init__.py index 7ba9edcc6..8d39612f6 100644 --- a/energy_communities_service_invoicing/models/__init__.py +++ b/energy_communities_service_invoicing/models/__init__.py @@ -1,3 +1,4 @@ +from . import pack_type_mixin from . import abstract_contract from . import account_move from . import contract diff --git a/energy_communities_service_invoicing/models/account_move.py b/energy_communities_service_invoicing/models/account_move.py index 381e6c69b..fdfc3e71e 100644 --- a/energy_communities_service_invoicing/models/account_move.py +++ b/energy_communities_service_invoicing/models/account_move.py @@ -1,28 +1,26 @@ from odoo import api, fields, models +from ..utils import PACK_VALUES + class AccountMove(models.Model): - _inherit = "account.move" + _name = "account.move" + _inherit = ["account.move", "pack.type.mixin"] ref_invoice_id = fields.Many2one( comodel_name="account.move", - compute="_compute_ref_invoice_id_related_contract_id_pack_type_is_contract", + compute="_compute_ref_invoice_id_related_contract_id_is_contract", compute_sudo=True, store=False, ) related_contract_id = fields.Many2one( comodel_name="contract.contract", - compute="_compute_ref_invoice_id_related_contract_id_pack_type_is_contract", + compute="_compute_ref_invoice_id_related_contract_id_is_contract", compute_sudo=True, store=False, ) - pack_type = fields.Boolean( - compute="_compute_ref_invoice_id_related_contract_id_pack_type_is_contract", - compute_sudo=True, - store=True, - ) is_contract = fields.Boolean( - compute="_compute_ref_invoice_id_related_contract_id_pack_type_is_contract", + compute="_compute_ref_invoice_id_related_contract_id_is_contract", compute_sudo=True, store=True, ) @@ -34,11 +32,10 @@ class AccountMove(models.Model): ) @api.depends("invoice_line_ids", "ref") - def _compute_ref_invoice_id_related_contract_id_pack_type_is_contract(self): + def _compute_ref_invoice_id_related_contract_id_is_contract(self): for record in self: record.ref_invoice_id = False record.related_contract_id = False - record.pack_type = 'none' record.is_contract = False rel_inv = False if record.ref: @@ -49,7 +46,6 @@ class AccountMove(models.Model): ) if rel_inv: record.ref_invoice_id = rel_inv.id - record.pack_type = rel_inv.pack_type record.is_contract = rel_inv.is_contract if rel_inv.related_contract_id: record.related_contract_id = rel_inv.related_contract_id.id @@ -58,6 +54,24 @@ class AccountMove(models.Model): first_move_line = record.invoice_line_ids[0] if first_move_line.contract_line_id: rel_contract = first_move_line.contract_line_id.contract_id - record.pack_type = rel_contract.pack_type record.related_contract_id = rel_contract.id record.is_contract = True + + def custom_compute_pack_type(self): + self._set_custom_pack_type_on_invoice() + + @api.depends("ref", "invoice_line_ids") + def _compute_pack_type(self): + super()._compute_pack_type() + + # define configuration intercompany journal + def _prepare_invoice_data(self, dest_company): + inv_data = super()._prepare_invoice_data(dest_company) + if ( + self.pack_type == "platform_pack" + and dest_company.sudo().service_invoicing_purchase_journal_id + ): + inv_data[ + "journal_id" + ] = dest_company.sudo().service_invoicing_purchase_journal_id.id + return inv_data diff --git a/energy_communities_service_invoicing/models/contract_template.py b/energy_communities_service_invoicing/models/contract_template.py index 440855b79..e39c38163 100644 --- a/energy_communities_service_invoicing/models/contract_template.py +++ b/energy_communities_service_invoicing/models/contract_template.py @@ -1,45 +1,14 @@ -from odoo import api, fields, models, _ +from odoo import _, api, fields, models -PACK_VALUES = [ - ("platform_pack", _("Platform Pack")), - ("none", _("None")), -] class ContractTemplate(models.Model): _name = "contract.template" - _inherit = "contract.template" + _inherit = ["contract.template", "pack.type.mixin"] is_free_pack = fields.Boolean(string="Is a free pack") - pack_type = fields.Selection(PACK_VALUES, compute="_compute_pack_type", string="Pack Type", store=True) - - def _get_pack_product_from_category(self, category_id, value): - return value if bool( - self.env["product.template"].search( - [ - ("property_contract_template_id", "=", self.id), - ( - "categ_id", - "=", - category_id, - ), - ] - ) - ) else 'none' - - def _set_custom_pack_type(self, ref_category, value): - try: - categ_id = self.env.ref( - ref_category - ).id - except: - categ_id = False - if categ_id: - self.pack_type = self._get_pack_product_from_category(categ_id, value) def custom_compute_pack_type(self): - self._set_custom_pack_type("energy_communities_service_invoicing.product_category_platform_pack", 'platform_pack') - - def _compute_pack_type(self): - for record in self: - record.custom_compute_pack_type() - + self._set_custom_pack_type_on_contract_template( + "platform_pack", + "energy_communities_service_invoicing.product_category_platform_pack", + ) diff --git a/energy_communities_service_invoicing/models/pack_type_mixin.py b/energy_communities_service_invoicing/models/pack_type_mixin.py new file mode 100644 index 000000000..7d3ce9e2a --- /dev/null +++ b/energy_communities_service_invoicing/models/pack_type_mixin.py @@ -0,0 +1,65 @@ +from odoo import fields, models + +from ..utils import PACK_VALUES + + +class PackTypeMixin(models.AbstractModel): + _name = "pack.type.mixin" + _description = "Add pack_type to any model" + + pack_type = fields.Selection( + PACK_VALUES, + compute="_compute_pack_type", + compute_sudo=True, + string="Pack Type", + store=True, + ) + + def _get_pack_type_from_product_category(self, pack_type, category_id): + return ( + pack_type + if bool( + self.env["product.template"].search( + [ + ("property_contract_template_id", "=", self.id), + ("categ_id", "=", category_id), + ] + ) + ) + else "none" + ) + + def _set_custom_pack_type_on_contract_template(self, pack_type, ref_category): + try: + categ_id = self.env.ref(ref_category).id + except: + categ_id = False + if categ_id: + self.pack_type = self._get_pack_type_from_product_category( + pack_type, categ_id + ) + + def _set_custom_pack_type_on_invoice(self): + if self.ref: + rel_inv = ( + self.env["account.move"] + .sudo() + .search([("name", "=", self.ref)], limit=1) + ) + if rel_inv: + self.pack_type = rel_inv.pack_type + else: + if self.invoice_line_ids: + first_move_line = self.invoice_line_ids[0] + if first_move_line.contract_line_id: + rel_contract = first_move_line.contract_line_id.contract_id + self.pack_type = rel_contract.pack_type + + # method to be overwriten on implementations + def custom_compute_pack_type(self): + pass + + def _compute_pack_type(self): + for record in self: + record.pack_type = "none" + record.custom_compute_pack_type() diff --git a/energy_communities_service_invoicing/models/res_company.py b/energy_communities_service_invoicing/models/res_company.py index 0264c69bb..7ce76cc08 100644 --- a/energy_communities_service_invoicing/models/res_company.py +++ b/energy_communities_service_invoicing/models/res_company.py @@ -6,14 +6,14 @@ class ResCompany(models.Model): _name = "res.company" _inherit = ["res.company"] - service_invoicing_journal_id = fields.Many2one( + service_invoicing_sale_journal_id = fields.Many2one( comodel_name="account.journal", - string="Service invoicing journal", + string="Service invoicing sale journal", + ) + service_invoicing_purchase_journal_id = fields.Many2one( + comodel_name="account.journal", + string="Service invoicing purchase journal", ) - # service_invoicing_payment_mode_id = fields.Many2one( - # comodel_name="account.payment.mode", - # string="Service invoicing payment mode", - # ) service_invoicing_sale_team_id = fields.Many2one( comodel_name="crm.team", string="Service invoicing sales team", diff --git a/energy_communities_service_invoicing/utils.py b/energy_communities_service_invoicing/utils.py index 2f63628b5..a99e7c972 100644 --- a/energy_communities_service_invoicing/utils.py +++ b/energy_communities_service_invoicing/utils.py @@ -4,6 +4,11 @@ from odoo.exceptions import ValidationError from odoo.addons.contract.models.contract import ContractContract +PACK_VALUES = [ + ("platform_pack", _("Platform Pack")), + ("none", _("None")), +] + _CONTRACT_STATUS_VALUES = [ ("paused", _("Paused")), ("in_progress", _("In progress")), @@ -77,19 +82,21 @@ def raise_existing_same_open_platform_pack_contract_error(existing_contract): def get_existing_open_pack_contract( - env, partner_id, pack_type, contract_id=False, custom_query=[] + env, partner_id, pack_type, contract_id=False, custom_query=False ): - #("community_company_id", "=", community_company_id.id), + # ("community_company_id", "=", community_company_id.id), query = [ - ("partner_id", "=", partner_id.id), + ("partner_id", "=", partner_id.id), ("pack_type", "=", pack_type), ("status", "in", ["paused", "in_progress"]), ] if contract_id: query.append(("id", "!=", contract_id.id)) - query = custom_query + query + if custom_query: + query = custom_query + query return env["contract.contract"].search(query, limit=1) + def get_existing_last_closed_pack_contract( env, partner_id, community_company_id, contract_id=False ): diff --git a/energy_communities_service_invoicing/views/account_move_views.xml b/energy_communities_service_invoicing/views/account_move_views.xml index 8778184dd..6b2f82309 100644 --- a/energy_communities_service_invoicing/views/account_move_views.xml +++ b/energy_communities_service_invoicing/views/account_move_views.xml @@ -5,7 +5,7 @@ <field name="inherit_id" ref="account.view_move_form" /> <field name="arch" type="xml"> <xpath expr="//field[@name='partner_id']" position="after"> - <field name="pack_type" invisible="True" /> + <field name="pack_type" /> <field name="is_contract" invisible="True" /> <field name="invoice_origin" attrs="{'invisible': [('is_contract','=',False)]}" /> <field name="related_contract_id" attrs="{'invisible': [('is_contract','=',False)]}" /> diff --git a/energy_communities_service_invoicing/views/contract_views.xml b/energy_communities_service_invoicing/views/contract_views.xml index 6e79bb912..3feaa068a 100644 --- a/energy_communities_service_invoicing/views/contract_views.xml +++ b/energy_communities_service_invoicing/views/contract_views.xml @@ -137,6 +137,7 @@ <group> <field name="name" /> <field name="pack_id" options="{'no_open': True}" /> + <field name="pack_type" /> <field name="related_contract_product_ids" widget="one2many" > <tree editable="bottom"> <field name="name" readonly="1"/> diff --git a/energy_communities_service_invoicing/views/res_company_views.xml b/energy_communities_service_invoicing/views/res_company_views.xml index 7c49d6fa3..97e3c0b2c 100644 --- a/energy_communities_service_invoicing/views/res_company_views.xml +++ b/energy_communities_service_invoicing/views/res_company_views.xml @@ -19,7 +19,8 @@ <page string="Services Invoicing"> <group> <field name="id" invisible="1"/> - <field name="service_invoicing_journal_id" domain="[('company_id', '=', id)]" /> + <field name="service_invoicing_sale_journal_id" domain="[('company_id', '=', id)]" /> + <field name="service_invoicing_purchase_journal_id" domain="[('company_id', '=', id)]" /> <field name="service_invoicing_sale_team_id" domain="[('company_id', '=', id)]" /> </group> </page> diff --git a/energy_selfconsumption/models/__init__.py b/energy_selfconsumption/models/__init__.py index 17238e412..85657e9a7 100644 --- a/energy_selfconsumption/models/__init__.py +++ b/energy_selfconsumption/models/__init__.py @@ -1,3 +1,4 @@ +from . import pack_type_mixin from . import account_move from . import contract from . import contract_csv_report diff --git a/energy_selfconsumption/models/account_move.py b/energy_selfconsumption/models/account_move.py index 760be4ee4..110e8e4bb 100644 --- a/energy_selfconsumption/models/account_move.py +++ b/energy_selfconsumption/models/account_move.py @@ -11,7 +11,8 @@ class AccountMoveLine(models.Model): class AccountMove(models.Model): - _inherit = "account.move" + _name = "account.move" + _inherit = ["account.move", "pack.type.mixin"] selfconsumption_invoicing_mode = fields.Selection( [ diff --git a/energy_selfconsumption/models/contract_template.py b/energy_selfconsumption/models/contract_template.py index 76734f7a3..e45bdbfd4 100644 --- a/energy_selfconsumption/models/contract_template.py +++ b/energy_selfconsumption/models/contract_template.py @@ -1,19 +1,14 @@ from odoo import _, api, fields, models -PACK_VALUES = [ - ("selfconsumption_pack", _("Selfconsumption Pack")), -] - class ContractTemplate(models.Model): - _inherit = "contract.template" - - pack_type = fields.Selection(selection_add=PACK_VALUES) + _name = "contract.template" + _inherit = ["contract.template", "pack.type.mixin"] def custom_compute_pack_type(self): super().custom_compute_pack_type() if self.pack_type == "none": - self._set_custom_pack_type( - "selfconsumption.product_category_selfconsumption_pack", + self._set_custom_pack_type_on_contract_template( "selfconsumption_pack", + "selfconsumption.product_category_selfconsumption_pack", ) diff --git a/energy_selfconsumption/models/pack_type_mixin.py b/energy_selfconsumption/models/pack_type_mixin.py new file mode 100644 index 000000000..30bad5f15 --- /dev/null +++ b/energy_selfconsumption/models/pack_type_mixin.py @@ -0,0 +1,11 @@ +from odoo import _, fields, models + +PACK_VALUES = [ + ("selfconsumption_pack", _("Selfconsumption Pack")), +] + + +class PackTypeMixin(models.AbstractModel): + _inherit = "pack.type.mixin" + + pack_type = fields.Selection(selection_add=PACK_VALUES) -- GitLab