From 37f506662b74ccd621b4455bc60b94f6d0f31640 Mon Sep 17 00:00:00 2001 From: tarteo Date: Thu, 14 Mar 2024 11:53:06 +0100 Subject: [PATCH] [IMP] subscription_oca: Various improvements oca_subscription: Open subscription button in invoice view --- subscription_oca/README.rst | 2 +- subscription_oca/__manifest__.py | 1 + .../data/sale_subscription_data.xml | 18 ++++++++--- subscription_oca/models/account_move.py | 8 +++++ subscription_oca/models/sale_subscription.py | 30 ++++++++++++------- .../models/sale_subscription_stage.py | 7 ++++- .../static/description/index.html | 3 +- .../tests/test_subscription_oca.py | 7 ++++- subscription_oca/views/account_move_views.xml | 20 +++++++++++++ .../wizard/close_subscription_wizard.py | 9 +----- 10 files changed, 79 insertions(+), 26 deletions(-) create mode 100644 subscription_oca/views/account_move_views.xml diff --git a/subscription_oca/README.rst b/subscription_oca/README.rst index 4511f0c682..9793d834bc 100644 --- a/subscription_oca/README.rst +++ b/subscription_oca/README.rst @@ -7,7 +7,7 @@ Subscription management !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:7f85e1d860d2d5332db014c46c57b9bece8f76a849049bda632499d422ba6dd8 + !! source digest: sha256:a5b7e0d3ce65f481fcf7e2c49d76f35cf3e07e7b3d06fe49051f4c5eaaa34de4 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png diff --git a/subscription_oca/__manifest__.py b/subscription_oca/__manifest__.py index fa4400f482..f34b423f93 100644 --- a/subscription_oca/__manifest__.py +++ b/subscription_oca/__manifest__.py @@ -12,6 +12,7 @@ "depends": ["sale_management", "account"], "data": [ "views/product_template_views.xml", + "views/account_move_views.xml", "views/sale_subscription_views.xml", "views/sale_subscription_stage_views.xml", "views/sale_subscription_tag_views.xml", diff --git a/subscription_oca/data/sale_subscription_data.xml b/subscription_oca/data/sale_subscription_data.xml index 6ee56a620e..eebea52b07 100644 --- a/subscription_oca/data/sale_subscription_data.xml +++ b/subscription_oca/data/sale_subscription_data.xml @@ -13,11 +13,21 @@ - Ready to start + Draft 0 + draft + + Draft, still working on the specifics. + + + + + + Ready to start + 1 pre - Draft equivalent, a subscription is ready to start when is not marked as in progress but it can be at any moment. If there's no 'Closed'-type stage defined, when a subscription comes to an end by automatic means, it will be marked with this stage. + A subscription is ready to start when is not marked as in progress but it can be at any moment. If there's no 'Closed'-type stage defined, when a subscription comes to an end by automatic means, it will be marked with this stage. @@ -25,7 +35,7 @@ In progress - 1 + 2 in_progress @@ -36,7 +46,7 @@ Closed - 2 + 3 post diff --git a/subscription_oca/models/account_move.py b/subscription_oca/models/account_move.py index 570a02f29a..7b3679697f 100644 --- a/subscription_oca/models/account_move.py +++ b/subscription_oca/models/account_move.py @@ -10,3 +10,11 @@ class AccountMove(models.Model): subscription_id = fields.Many2one( comodel_name="sale.subscription", string="Subscription" ) + + def action_open_subscription(self): + self.ensure_one() + action = self.env["ir.actions.act_window"]._for_xml_id( + "subscription_oca.sale_subscription_action" + ) + action["domain"] = [("id", "=", self.subscription_id.id)] + return action diff --git a/subscription_oca/models/sale_subscription.py b/subscription_oca/models/sale_subscription.py index 6845706840..b984bcf490 100644 --- a/subscription_oca/models/sale_subscription.py +++ b/subscription_oca/models/sale_subscription.py @@ -142,21 +142,22 @@ def cron_subscription_management(self): for subscription in self.search([]): if subscription.in_progress: if ( - subscription.recurring_next_date == today + subscription.recurring_next_date <= today and subscription.sale_subscription_line_ids ): try: subscription.generate_invoice() - except Exception: + except Exception: # pragma: no cover logger.exception("Error on subscription invoice generate") if not subscription.recurring_rule_boundary: - if subscription.date == today: + if subscription.date <= today: subscription.action_close_subscription() - else: - if subscription.date_start == today: - subscription.action_start_subscription() - subscription.generate_invoice() + elif ( + subscription.date_start <= today and subscription.stage_id.type == "pre" + ): + subscription.action_start_subscription() + subscription.generate_invoice() @api.depends("sale_subscription_line_ids") def _compute_total(self): @@ -239,7 +240,6 @@ def action_start_subscription(self): self.stage_id = in_progress_stage def action_close_subscription(self): - self.recurring_next_date = False return { "view_type": "form", "view_mode": "form", @@ -249,6 +249,16 @@ def action_close_subscription(self): "res_id": False, } + def close_subscription(self, close_reason_id=False): + self.ensure_one() + self.recurring_next_date = False + closed_stage = self.env["sale.subscription.stage"].search( + [("type", "=", "post")], limit=1 + ) + self.close_reason_id = close_reason_id + if self.stage_id != closed_stage: + self.stage_id = closed_stage + def _prepare_sale_order(self, line_ids=False): self.ensure_one() return { @@ -271,6 +281,7 @@ def _prepare_account_move(self, line_ids): "invoice_user_id": self.user_id.id, "partner_bank_id": self.company_id.partner_id.bank_ids[:1].id, "invoice_line_ids": line_ids, + "subscription_id": self.id, } if self.journal_id: values["journal_id"] = self.journal_id.id @@ -294,7 +305,6 @@ def create_invoice(self): .with_context(default_move_type="out_invoice", journal_type="sale") .create(invoice_values) ) - self.write({"invoice_ids": [(4, invoice_id.id)]}) return invoice_id def create_sale_order(self): @@ -464,7 +474,7 @@ def create(self, values): values["date_start"] = values["recurring_next_date"] values["stage_id"] = ( self.env["sale.subscription.stage"] - .search([("type", "=", "pre")], order="sequence desc", limit=1) + .search([("type", "=", "draft")], order="sequence desc", limit=1) .id ) return super(SaleSubscription, self).create(values) diff --git a/subscription_oca/models/sale_subscription_stage.py b/subscription_oca/models/sale_subscription_stage.py index 8bc3961788..02a88ac4a9 100644 --- a/subscription_oca/models/sale_subscription_stage.py +++ b/subscription_oca/models/sale_subscription_stage.py @@ -16,7 +16,12 @@ class SaleSubscriptionStage(models.Model): fold = fields.Boolean(string="Kanban folded") description = fields.Text(translate=True) type = fields.Selection( - [("pre", "Ready to start"), ("in_progress", "In progress"), ("post", "Closed")], + [ + ("draft", "Draft"), + ("pre", "Ready to start"), + ("in_progress", "In progress"), + ("post", "Closed"), + ], default="pre", ) diff --git a/subscription_oca/static/description/index.html b/subscription_oca/static/description/index.html index 2f09cbdd62..c13921a7e8 100644 --- a/subscription_oca/static/description/index.html +++ b/subscription_oca/static/description/index.html @@ -1,3 +1,4 @@ + @@ -366,7 +367,7 @@

Subscription management

!! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -!! source digest: sha256:7f85e1d860d2d5332db014c46c57b9bece8f76a849049bda632499d422ba6dd8 +!! source digest: sha256:a5b7e0d3ce65f481fcf7e2c49d76f35cf3e07e7b3d06fe49051f4c5eaaa34de4 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

Beta License: AGPL-3 OCA/contract Translate me on Weblate Try me on Runboat

This module allows creating subscriptions that generate recurring invoices or orders. It also enables the sale of products that generate subscriptions.

diff --git a/subscription_oca/tests/test_subscription_oca.py b/subscription_oca/tests/test_subscription_oca.py index 0169960633..0008341a60 100644 --- a/subscription_oca/tests/test_subscription_oca.py +++ b/subscription_oca/tests/test_subscription_oca.py @@ -531,7 +531,7 @@ def test_x_subscription_oca_pricelist_related(self): wiz = self.env["close.reason.wizard"].create({}) wiz.with_context(active_id=self.sub1.id).button_confirm() self.assertEqual(self.sub1.stage_id.name, "Closed") - self.assertFalse(self.sub1.active) + self.assertTrue(self.sub1.active) self.tmpl1.action_view_subscription_ids() self.tmpl1.action_view_product_ids() self.tmpl1.read(["product_ids_count", "subscription_count"]) @@ -607,6 +607,11 @@ def test_compute_display_name(self): stage.display_name, "Updated Test Stage", "display_name not computed" ) + def test_open_subscription(self): + invoice = self.sub1.create_invoice() + action = invoice.action_open_subscription() + self.assertEqual(action["domain"], [("id", "=", self.sub1.id)]) + def _collect_all_sub_test_results(self, subscription): test_res = [] sale_order = subscription.create_sale_order() diff --git a/subscription_oca/views/account_move_views.xml b/subscription_oca/views/account_move_views.xml new file mode 100644 index 0000000000..0af0012123 --- /dev/null +++ b/subscription_oca/views/account_move_views.xml @@ -0,0 +1,20 @@ + + + + account.move + + + + +