diff --git a/subscription_oca/data/sale_subscription_data.xml b/subscription_oca/data/sale_subscription_data.xml index 6ee56a620ec..eebea52b074 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/sale_subscription.py b/subscription_oca/models/sale_subscription.py index 6845706840e..0da6d126ae3 100644 --- a/subscription_oca/models/sale_subscription.py +++ b/subscription_oca/models/sale_subscription.py @@ -142,7 +142,7 @@ 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: @@ -150,13 +150,14 @@ def cron_subscription_management(self): except Exception: 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 8bc39617884..02a88ac4a92 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/wizard/close_subscription_wizard.py b/subscription_oca/wizard/close_subscription_wizard.py index 1f38879e40d..0cafafdda92 100644 --- a/subscription_oca/wizard/close_subscription_wizard.py +++ b/subscription_oca/wizard/close_subscription_wizard.py @@ -15,11 +15,4 @@ def button_confirm(self): sale_subscription = self.env["sale.subscription"].browse( self.env.context["active_id"] ) - sale_subscription.close_reason_id = self.close_reason_id.id - stage = sale_subscription.stage_id - closed_stage = self.env["sale.subscription.stage"].search( - [("type", "=", "post")], limit=1 - ) - if stage != closed_stage: - sale_subscription.stage_id = closed_stage - sale_subscription.active = False + sale_subscription.close_subscription(self.close_reason_id.id)