Skip to content

Commit

Permalink
[ADD] Invoice app statistics (e.g. storage usage)
Browse files Browse the repository at this point in the history
I want to refactor this eventually with changes in OCA modules (product_pack, subscription_oca, and more). Didn't want to spend too much time on this. This works and is fine if we eventually want to refactor it.
  • Loading branch information
tarteo committed Aug 30, 2024
1 parent b81b531 commit e624cbe
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions argocd_sale/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"subscription_oca",
"argocd_deployer",
"sale_recurring_payment",
"account_invoice_pricelist",
],
"demo": [
"demo/sale_subscription_template_demo.xml",
Expand Down
8 changes: 8 additions & 0 deletions argocd_sale/demo/product_template_demo.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="demo_s3_storage_product" model="product.template">
<field name="name">S3 Storage</field>
<field name="application_stat_type_id" ref="argocd_deployer.demo_storage_usage_application_stat_type" />
<field name="list_price">0.20</field>
<field name="invoice_policy">order</field>
</record>

<record id="demo_curq_basis_product_template" model="product.template">
<field name="name">Curq Basis</field>
<field name="application_template_id" ref="argocd_deployer.demo_curq_basis_application_template" />
Expand All @@ -8,6 +15,7 @@
<field name="list_price">30.0</field>
<field name="invoice_policy">order</field>
<field name="application_set_id" ref="argocd_deployer.application_set_default"/>
<field name="stat_product_ids" eval="[Command.set([ref('argocd_sale.demo_s3_storage_product')])]" />
</record>
<record id="demo_pos_product_template" model="product.template">
<field name="name">Point of Sales</field>
Expand Down
9 changes: 9 additions & 0 deletions argocd_sale/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,12 @@ class ProductTemplate(models.Model):
column1="product_template_id",
column2="partner_id",
)

application_stat_type_id = fields.Many2one(
string="Statistics Type", comodel_name="argocd.application.stat.type"
)

# We use this for now because product.pack doesn't work really for products with variants
stat_product_ids = fields.Many2many(
comodel_name="product.product", string="Products on Statistics"
)
73 changes: 73 additions & 0 deletions argocd_sale/models/subscription.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from datetime import timedelta

from dateutil.relativedelta import relativedelta

from odoo import Command, _, fields, models


Expand Down Expand Up @@ -106,3 +108,74 @@ def view_applications(self):
"type": "ir.actions.act_window",
"domain": [("id", "in", self.application_ids.ids)],
}

def _prepare_account_move_line_stat_product(
self, app, stat_product, sequence, date_from, date_to
):
self.ensure_one()
account = (
stat_product.property_account_income_id
or stat_product.categ_id.property_account_income_categ_id
)
quantity = 0
stats = app.stat_ids.filtered(
lambda l: date_from < l.date.date() < date_to
).mapped("value")
if stats:
quantity = max(stats)
return {
"product_id": stat_product.id,
"name": "> %s" % stat_product.name,
"quantity": quantity,
"price_unit": stat_product.list_price,
"tax_ids": [Command.set(stat_product.taxes_id.ids)],
"product_uom_id": stat_product.uom_id.id,
"account_id": account.id,
"sequence": sequence,
}

def _prepare_account_move(self, line_ids):
# TODO: Only used in draft, invoice, invoice_send make it also work for sale_and_invoice
if not self.account_invoice_ids_count: # First time don't invoice stat products
return super()._prepare_account_move(line_ids)

# TODO: Fix this with a sequence in sale.subscription.line
sequence = 0
for cmd in line_ids:
cmd[2]["sequence"] = sequence
sequence += 10

type_interval = self.template_id.recurring_rule_type
interval = int(self.template_id.recurring_interval)
recurring_previous_date = self.recurring_next_date - relativedelta(
**{type_interval: interval}
)

additional_invoice_line_ids = []
for line in self.sale_subscription_line_ids.filtered(
lambda l: l.product_id.stat_product_ids and l.application_ids
):
corresponding_invoice_line = list(
filter(lambda l: l[2]["product_id"] == line.product_id.id, line_ids)
)[0][
2
] # TODO: Add field subscription_line_id to account.move.line
sequence = corresponding_invoice_line["sequence"] + 1
app = line.application_ids
for stat_product in line.product_id.stat_product_ids:
line_values = self._prepare_account_move_line_stat_product(
app,
stat_product,
sequence,
recurring_previous_date,
self.recurring_next_date,
)
additional_invoice_line_ids.append(Command.create(line_values))
line_ids += additional_invoice_line_ids
return super()._prepare_account_move(line_ids)

def create_invoice(self):
# TODO: Only used in draft, invoice, invoice_send make it also work for sale_and_invoice
res = super().create_invoice()
res.button_update_prices_from_pricelist()
return res
5 changes: 5 additions & 0 deletions argocd_sale/views/product_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
<field name="application_template_id" />
<field name="application_tag_ids" widget="many2many_tags"/>
<field name="reseller_partner_ids" options="{'no_create_edit': True}" />

</group>
<group>
<field name="application_stat_type_id" options="{'no_create_edit': True}" />
<field name="stat_product_ids" widget="many2many_tags" domain="[('application_stat_type_id', '!=', False)]" />
</group>
</page>
</xpath>
Expand Down
11 changes: 11 additions & 0 deletions argocd_website/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ def get_subscription_details(self):
}
for variant in line.product_id.product_template_variant_value_ids
],
"stat_products": [
{
"name": stat_product.name,
"uom": stat_product.uom_id.name,
"price": stat_product.list_price,
"price_base_formatted": currency.format(
stat_product.list_price
),
}
for stat_product in line.product_id.stat_product_ids
],
}
for line in sub.sale_subscription_line_ids
],
Expand Down
16 changes: 16 additions & 0 deletions argocd_website/static/src/xml/website.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@
t-esc="variant_value.price_extra_formatted"
/>
</div>
<t t-if="line.stat_products">
<div class="row">
<div class="col-12 fw-bold small pt-2">
<span class="fa fa-fw fa-caret-right" /> Extra costs
</div>
</div>
<div class="row" t-as="stat_product" t-foreach="line.stat_products">
<div class="col-12 small">
<span class="fa fa-fw" />
<span t-esc="stat_product.name" />:
<span class="badge text-bg-light">
<t t-esc="price_base_formatted" /> per <t t-esc="stat_product.uom" />
</span>
</div>
</div>
</t>
</li>
</ul>

Expand Down

0 comments on commit e624cbe

Please sign in to comment.