Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][IMP] subscription_oca: Various improvements #1058

Open
wants to merge 2 commits into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion subscription_oca/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions subscription_oca/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
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
7 changes: 7 additions & 0 deletions subscription_oca/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@
subscription_id = fields.Many2one(
comodel_name="sale.subscription", string="Subscription"
)

def action_open_subscription(self):
self.ensure_one()
action = self.env.ref("subscription_oca.sale_subscription_action")
action = action.read()[0]
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use action = self.env["ir.actions.act_window"]._for_xml_id("subscription_oca.sale_subscription_action") instead.

action["domain"] = [("id", "=", self.subscription_id.id)]
return action

Check warning on line 19 in subscription_oca/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

subscription_oca/models/account_move.py#L15-L19

Added lines #L15 - L19 were not covered by tests
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(
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like selection with a limit=1. Is there a mean to restrict stage selection in a better way ?

[("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
3 changes: 2 additions & 1 deletion subscription_oca/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -366,7 +367,7 @@ <h1 class="title">Subscription management</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:7f85e1d860d2d5332db014c46c57b9bece8f76a849049bda632499d422ba6dd8
!! source digest: sha256:a5b7e0d3ce65f481fcf7e2c49d76f35cf3e07e7b3d06fe49051f4c5eaaa34de4
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/contract/tree/16.0/subscription_oca"><img alt="OCA/contract" src="https://img.shields.io/badge/github-OCA%2Fcontract-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/contract-16-0/contract-16-0-subscription_oca"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/contract&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module allows creating subscriptions that generate recurring invoices or orders. It also enables the sale of products that generate subscriptions.</p>
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
20 changes: 20 additions & 0 deletions subscription_oca/views/account_move_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" ?>
<odoo>
<record id="view_move_form" model="ir.ui.view">
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form" />
<field name="arch" type="xml">
<xpath expr="//div[@name='button_box']" position="inside">
<field name="subscription_id" invisible="1" />
<button
name="action_open_subscription"
type="object"
class="oe_stat_button"
icon="fa-calendar"
string="Subscription"
attrs="{'invisible': [('subscription_id', '=', False)]}"
/>
</xpath>
</field>
</record>
</odoo>
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)
Loading