Skip to content
Snippets Groups Projects
Commit eff4eef8 authored by Emanuel Buzey's avatar Emanuel Buzey Committed by Daniil Digtyar Vasilieva
Browse files

[IMP] energy_selfconsumption: create product

parent 91b1ca0b
No related branches found
No related tags found
2 merge requests!253[REL] Release 06/11/23,!211[IMP] energy_selfconsumption: invoicing acquired power
......@@ -43,3 +43,7 @@ class Project(models.Model):
required=True,
default=lambda self: self.env.ref("base.es"),
)
product_ids = fields.One2many(
"product.product",
"project_id",
)
......@@ -6,3 +6,4 @@ from . import supply_point_assignation
from . import project
from . import contract_line
from . import contract
from . import product
from odoo import fields, models
class Product(models.Model):
_inherit = "product.product"
project_id = fields.Many2one(
"energy_project.project",
required=True,
ondelete="restrict",
string="Energy Project",
check_company=True,
)
......@@ -46,6 +46,10 @@ class Selfconsumption(models.Model):
else:
record.report_distribution_table = False
def _compute_invoicing_name(self):
for record in self:
record.invoicing_mode = record.project_id.product_ids[0].name.split("-")[0]
project_id = fields.Many2one(
"energy_project.project", required=True, ondelete="cascade"
)
......@@ -76,6 +80,7 @@ class Selfconsumption(models.Model):
)
inscription_count = fields.Integer(compute=_compute_inscription_count)
contracts_count = fields.Integer(compute=_compute_contract_count)
invoicing_mode = fields.Char(compute=_compute_invoicing_name)
def get_distribution_tables(self):
self.ensure_one()
......@@ -110,6 +115,17 @@ class Selfconsumption(models.Model):
"context": {"create": True, "default_project_id": self.id},
}
def get_product(self):
self.ensure_one()
return {
"type": "ir.actions.act_window",
"name": "Product",
"view_mode": "tree,form",
"res_model": "product.product",
"domain": [("project_id", "=", self.id)],
"context": {"create": True, "default_project_id": self.id},
}
def distribution_table_state(self, actual_state, new_state):
distribution_table_to_activate = self.distribution_table_ids.filtered(
lambda table: table.state == actual_state
......
......@@ -81,6 +81,12 @@
widget="statusbar"
readonly="True"
statusbar_visible="draft,inscription,activation,active"
/>
<field
name="invoicing_mode"
widget="statusbar"
readonly="True"
statusbar_visible="draft,inscription,activation,active"
/>
</header>
<sheet>
......@@ -127,6 +133,19 @@
/>
</button>
<button
class="oe_stat_button"
type="object"
name="get_product"
icon="fa-file-text"
attrs="{'visible': [('invoicing_mode', '!=', False)]}"
>
<field
string="Invoicing Mode"
name="invoicing_mode"
widget="statinfo"
/>
</button>
</div>
<widget
......
......@@ -5,11 +5,11 @@ class ContractGenerationWizard(models.TransientModel):
_name = "energy_selfconsumption.define_invoicing_mode.wizard"
INVOICING_VALUES = [
("power_acquired", _("By power acquired")),
("energy_delivered", _("By energy delivered")),
("power_acquired", _("(PA) Power Acquired")),
("energy_delivered", _("(ED) Energy Delivered")),
(
"energy_delivered_hourly",
_("For energy delivered hourly variable coefficients (CSV)"),
"energy_delivered_variable",
_("(VHC) Energy Delivered Variable Hourly Coefficient"),
),
]
......@@ -43,3 +43,80 @@ class ContractGenerationWizard(models.TransientModel):
string=_("Recurrence"),
help=_("Specify Interval for automatic invoice generation."),
)
selfconsumption_id = fields.Many2one(
"energy_selfconsumption.selfconsumption", readonly=True
)
def _get_invoicing_mode_value(self):
"""
This method return the invoicing_mode label.
"""
for mode_value, mode_label in self.INVOICING_VALUES:
if mode_value == self.invoicing_mode:
return _(mode_label)
return ""
def save_data_to_selfconsumption(self):
# Create product
self.env["product.product"].create(
{
"name": f"{self._get_invoicing_mode_value()} - {self.selfconsumption_id.name}",
"lst_price": self.price,
"company_id": self.env.company.id,
"project_id": self.selfconsumption_id.project_id.id,
}
)
# Create contract formula
# TODO:actualizar formula energy_delivered y energy_delivered_variable.
# 4.Contract.template (en ivoicing --> configuracion --> create contract tmplate
if self.invoicing_mode == "power_acquired":
self.env["contract.line.qty.formula"].create(
{
"name": _("Formula - %s") % (self.selfconsumption_id.name),
"code": f"""
days_timedelta = line.next_period_date_end - line.next_period_date_start
if days_timedelta:
# Add one so it counts the same day too (month = 29 + 1)
days_between = days_timedelta.days + 1
else:
days_between = 0
result = line.supply_point_assignation_id.distribution_table_id.selfconsumption_project_id.power * line.supply_point_assignation_id.coefficient * {self.price} * days_between
""",
}
)
elif self.invoicing_mode == "energy_delivered":
self.env["contract.line.qty.formula"].create(
{
"name": _("Formula - %s") % (self.selfconsumption_id.name),
"code": f"""
days_timedelta = line.next_period_date_end - line.next_period_date_start
if days_timedelta:
# Add one so it counts the same day too (month = 29 + 1)
days_between = days_timedelta.days + 1
else:
days_between = 0
result = line.supply_point_assignation_id.distribution_table_id.selfconsumption_project_id.power * line.supply_point_assignation_id.coefficient * days_between
""",
}
)
elif self.invoicing_mode == "energy_delivered_variable":
self.env["contract.line.qty.formula"].create(
{
"name": _("Formula - %s") % (self.selfconsumption_id.name),
"code": """
days_timedelta = line.next_period_date_end - line.next_period_date_start
if days_timedelta:
# Add one so it counts the same day too (month = 29 + 1)
days_between = days_timedelta.days + 1
else:
days_between = 0
result = line.supply_point_assignation_id.distribution_table_id.selfconsumption_project_id.power * line.supply_point_assignation_id.coefficient * days_between
""",
}
)
return {
"type": "ir.actions.act_window_close",
}
......@@ -31,6 +31,14 @@
</group>
</group>
</sheet>
<footer>
<button
name="save_data_to_selfconsumption"
string="Save Data"
type="object"
class="btn-primary"
/>
</footer>
</form>
</field>
</record>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment