Skip to content

Commit

Permalink
[IMP] subscription_oca: Various improvements
Browse files Browse the repository at this point in the history
[IMP] subscription_oca: Various improvements
  • Loading branch information
tarteo committed Mar 14, 2024
1 parent df881dd commit 3851561
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
18 changes: 14 additions & 4 deletions subscription_oca/data/sale_subscription_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,29 @@
<data noupdate="1">
<record id="subscription_stage_draft" model="sale.subscription.stage">
<!-- <field eval="True" name="active"/> -->
<field name="name">Ready to start</field>
<field name="name">Draft</field>
<field name="sequence">0</field>
<field name="type">draft</field>
<field name="description">
Draft, still working on the specifics.
</field>
<field eval="False" name="fold" />
</record>
<record id="subscription_stage_ready" model="sale.subscription.stage">
<!-- <field eval="True" name="active"/> -->
<field name="name">Ready to start</field>
<field name="sequence">1</field>
<field name="type">pre</field>
<field name="description">
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.
</field>
<field eval="False" name="fold" />
</record>

<record id="subscription_stage_in_progress" model="sale.subscription.stage">
<!-- <field eval="True" name="active"/> -->
<field name="name">In progress</field>
<field name="sequence">1</field>
<field name="sequence">2</field>
<field name="type">in_progress</field>
<field eval="False" name="fold" />
<field name="description">
Expand All @@ -36,7 +46,7 @@
<record id="subscription_stage_closed" model="sale.subscription.stage">
<!-- <field eval="True" name="active"/> -->
<field name="name">Closed</field>
<field name="sequence">2</field>
<field name="sequence">3</field>
<field name="type">post</field>
<field eval="False" name="fold" />
<field name="description">
Expand Down
28 changes: 19 additions & 9 deletions subscription_oca/models/sale_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
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):
Expand Down Expand Up @@ -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",
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)
7 changes: 6 additions & 1 deletion subscription_oca/models/sale_subscription_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

Expand Down
2 changes: 1 addition & 1 deletion subscription_oca/tests/test_subscription_oca.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
9 changes: 1 addition & 8 deletions subscription_oca/wizard/close_subscription_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 3851561

Please sign in to comment.