From e624cbee6b8eac38a2856e38b4aac98773d68738 Mon Sep 17 00:00:00 2001 From: tarteo Date: Fri, 30 Aug 2024 14:32:48 +0200 Subject: [PATCH] [ADD] Invoice app statistics (e.g. storage usage) 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. --- argocd_sale/__manifest__.py | 1 + argocd_sale/demo/product_template_demo.xml | 8 +++ argocd_sale/models/product_template.py | 9 +++ argocd_sale/models/subscription.py | 73 ++++++++++++++++++++++ argocd_sale/views/product_template.xml | 5 ++ argocd_website/controllers/main.py | 11 ++++ argocd_website/static/src/xml/website.xml | 16 +++++ 7 files changed, 123 insertions(+) diff --git a/argocd_sale/__manifest__.py b/argocd_sale/__manifest__.py index f192a2d..2c9e36b 100644 --- a/argocd_sale/__manifest__.py +++ b/argocd_sale/__manifest__.py @@ -10,6 +10,7 @@ "subscription_oca", "argocd_deployer", "sale_recurring_payment", + "account_invoice_pricelist", ], "demo": [ "demo/sale_subscription_template_demo.xml", diff --git a/argocd_sale/demo/product_template_demo.xml b/argocd_sale/demo/product_template_demo.xml index c22607f..2454288 100644 --- a/argocd_sale/demo/product_template_demo.xml +++ b/argocd_sale/demo/product_template_demo.xml @@ -1,5 +1,12 @@ + + S3 Storage + + 0.20 + order + + Curq Basis @@ -8,6 +15,7 @@ 30.0 order + Point of Sales diff --git a/argocd_sale/models/product_template.py b/argocd_sale/models/product_template.py index 3c16194..678e101 100644 --- a/argocd_sale/models/product_template.py +++ b/argocd_sale/models/product_template.py @@ -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" + ) diff --git a/argocd_sale/models/subscription.py b/argocd_sale/models/subscription.py index 0215118..bff60e1 100644 --- a/argocd_sale/models/subscription.py +++ b/argocd_sale/models/subscription.py @@ -1,5 +1,7 @@ from datetime import timedelta +from dateutil.relativedelta import relativedelta + from odoo import Command, _, fields, models @@ -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 diff --git a/argocd_sale/views/product_template.xml b/argocd_sale/views/product_template.xml index 73f94b0..e7582e4 100644 --- a/argocd_sale/views/product_template.xml +++ b/argocd_sale/views/product_template.xml @@ -14,6 +14,11 @@ + + + + + diff --git a/argocd_website/controllers/main.py b/argocd_website/controllers/main.py index 7ea0e49..41dea45 100644 --- a/argocd_website/controllers/main.py +++ b/argocd_website/controllers/main.py @@ -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 ], diff --git a/argocd_website/static/src/xml/website.xml b/argocd_website/static/src/xml/website.xml index 6e2086c..bfed50d 100644 --- a/argocd_website/static/src/xml/website.xml +++ b/argocd_website/static/src/xml/website.xml @@ -19,6 +19,22 @@ t-esc="variant_value.price_extra_formatted" /> + +
+
+ Extra costs +
+
+
+
+ + : + + per + +
+
+