Skip to content

Commit e4962ff

Browse files
committed
[IMP] subscription_oca: recurrent payment
1 parent 69b97e0 commit e4962ff

File tree

8 files changed

+234
-29
lines changed

8 files changed

+234
-29
lines changed

subscription_oca/README.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
.. image:: https://odoo-community.org/readme-banner-image
2-
:target: https://odoo-community.org/get-involved?utm_source=readme
3-
:alt: Odoo Community Association
4-
51
=======================
62
Subscription management
73
=======================
@@ -17,7 +13,7 @@ Subscription management
1713
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
1814
:target: https://odoo-community.org/page/development-status
1915
:alt: Beta
20-
.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
2117
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
2218
:alt: License: AGPL-3
2319
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fcontract-lightgray.png?logo=github
@@ -91,6 +87,13 @@ Contributors
9187

9288
* Ilyas <irazor147@gmail.com>
9389

90+
91+
* `Binhex <https://binhex.cloud>`__:
92+
93+
* Adasat Torres de Leon <a.torres@binhex.cloud>
94+
95+
* Chris Mann <chrisandrewmann>
96+
9497
Maintainers
9598
~~~~~~~~~~~
9699

subscription_oca/models/sale_subscription.py

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,36 @@ class SaleSubscription(models.Model):
103103
store=True,
104104
ondelete="restrict",
105105
)
106+
payment_token_id = fields.Many2one(
107+
comodel_name="payment.token",
108+
string="Payment Token",
109+
store=True,
110+
compute="_compute_payment_token_id",
111+
domain="[('partner_id', '=', partner_id)]",
112+
)
113+
invoicing_mode = fields.Selection(related="template_id.invoicing_mode")
114+
115+
@api.depends("partner_id", "template_id")
116+
def _compute_payment_token_id(self):
117+
for record in self:
118+
if record.template_id.invoicing_mode not in [
119+
"invoice_and_payment",
120+
]:
121+
record.payment_token_id = False
122+
continue
123+
payment_token = (
124+
self.env["payment.token"]
125+
.sudo()
126+
.with_company(record.company_id)
127+
.search(
128+
[
129+
("partner_id", "=", record.partner_id.id),
130+
],
131+
limit=1,
132+
order="write_date desc",
133+
)
134+
)
135+
record.payment_token_id = payment_token.id if payment_token else False
106136

107137
@api.model
108138
def _read_group_stage_ids(self, stages, domain, order):
@@ -328,11 +358,21 @@ def create_sale_order(self):
328358
def generate_invoice(self):
329359
invoice_number = ""
330360
msg_static = _("Created invoice with reference")
331-
if self.template_id.invoicing_mode in ["draft", "invoice", "invoice_send"]:
361+
if self.template_id.invoicing_mode in [
362+
"draft",
363+
"invoice",
364+
"invoice_send",
365+
"invoice_and_payment",
366+
]:
332367
invoice = self.create_invoice()
333368
if self.template_id.invoicing_mode != "draft":
334369
invoice.action_post()
335-
if self.template_id.invoicing_mode == "invoice_send":
370+
if self.template_id.invoicing_mode == "invoice_and_payment":
371+
self.create_payment(invoice)
372+
if self.template_id.invoicing_mode in (
373+
"invoice_send",
374+
"invoice_and_payment",
375+
):
336376
mail_template = self.template_id.invoice_mail_template_id
337377
invoice.with_context(force_send=True).message_post_with_template(
338378
mail_template.id,
@@ -363,6 +403,54 @@ def generate_invoice(self):
363403
self.calculate_recurring_next_date(self.recurring_next_date)
364404
self.message_post(body=message_body)
365405

406+
def create_payment(self, invoice):
407+
invoice.ensure_one()
408+
if not self.payment_token_id:
409+
self.message_post(
410+
body=_(
411+
"No payment token found for partner %s" % invoice.partner_id.name
412+
)
413+
)
414+
return
415+
provider = self.payment_token_id.provider_id
416+
method_line = self.env["account.payment.method.line"].search(
417+
[
418+
("payment_method_id.code", "=", provider.code),
419+
("company_id", "=", invoice.company_id.id),
420+
],
421+
limit=1,
422+
)
423+
424+
if not method_line:
425+
self.message_post(
426+
body=_(
427+
"No payment method line found for payment provider %s"
428+
% provider.name
429+
)
430+
)
431+
return
432+
payment_register = self.env["account.payment.register"]
433+
payment_vals = {
434+
"currency_id": invoice.currency_id.id,
435+
"journal_id": provider.journal_id.id,
436+
"company_id": invoice.company_id.id,
437+
"partner_id": invoice.partner_id.id,
438+
"communication": invoice.name,
439+
"payment_type": "inbound",
440+
"partner_type": "customer",
441+
"payment_difference_handling": "open",
442+
"writeoff_label": "Write-Off",
443+
"payment_date": fields.Date.today(),
444+
"amount": invoice.amount_total,
445+
"payment_method_line_id": method_line.id,
446+
"payment_token_id": self.payment_token_id.id,
447+
}
448+
payment_register.with_context(
449+
active_model="account.move",
450+
active_ids=invoice.ids,
451+
active_id=invoice.id,
452+
).create(payment_vals).action_create_payments()
453+
366454
def manual_invoice(self):
367455
invoice_id = self.create_invoice()
368456
self.calculate_recurring_next_date(self.recurring_next_date)

subscription_oca/models/sale_subscription_template.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class SaleSubscriptionTemplate(models.Model):
3535
("invoice", "Invoice"),
3636
("invoice_send", "Invoice & send"),
3737
("sale_and_invoice", "Sale order & Invoice"),
38+
("invoice_and_payment", "Invoice & Recurring Payment"),
3839
],
3940
)
4041
code = fields.Char()

subscription_oca/readme/CONTRIBUTORS.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@
55
* `Ooops404 <https://www.ooops404.com>`__:
66

77
* Ilyas <irazor147@gmail.com>
8+
9+
10+
* `Binhex <https://binhex.cloud>`__:
11+
12+
* Adasat Torres de Leon <a.torres@binhex.cloud>
13+
14+
* Chris Mann <chrisandrewmann>

subscription_oca/static/description/index.html

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
6-
<title>README.rst</title>
6+
<title>Subscription management</title>
77
<style type="text/css">
88

99
/*
@@ -360,21 +360,16 @@
360360
</style>
361361
</head>
362362
<body>
363-
<div class="document">
363+
<div class="document" id="subscription-management">
364+
<h1 class="title">Subscription management</h1>
364365

365-
366-
<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
367-
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
368-
</a>
369-
<div class="section" id="subscription-management">
370-
<h1>Subscription management</h1>
371366
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
372367
!! This file is generated by oca-gen-addon-readme !!
373368
!! changes will be overwritten. !!
374369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
375370
!! source digest: sha256:0f74e3f2d1fce7423f268d9e6d62b9e9fe3af4629ec13a22c42fe64660c74bfe
376371
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
377-
<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/license-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>
372+
<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>
378373
<p>This module allows creating subscriptions that generate recurring invoices or orders. It also enables the sale of products that generate subscriptions.</p>
379374
<p><strong>Table of contents</strong></p>
380375
<div class="contents local topic" id="contents">
@@ -391,7 +386,7 @@ <h1>Subscription management</h1>
391386
</ul>
392387
</div>
393388
<div class="section" id="usage">
394-
<h2><a class="toc-backref" href="#toc-entry-1">Usage</a></h2>
389+
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
395390
<p>To make a subscription:</p>
396391
<ol class="arabic simple">
397392
<li>Go to <em>Subscriptions &gt; Configuration &gt; Subscription templates</em>.</li>
@@ -408,41 +403,46 @@ <h2><a class="toc-backref" href="#toc-entry-1">Usage</a></h2>
408403
</ol>
409404
</div>
410405
<div class="section" id="known-issues-roadmap">
411-
<h2><a class="toc-backref" href="#toc-entry-2">Known issues / Roadmap</a></h2>
406+
<h1><a class="toc-backref" href="#toc-entry-2">Known issues / Roadmap</a></h1>
412407
<ul class="simple">
413408
<li>Refactor all the onchanges that have business logic to computed write-able fields when possible. Keep onchanges only for UI purposes.</li>
414409
<li>Add tests.</li>
415410
</ul>
416411
</div>
417412
<div class="section" id="bug-tracker">
418-
<h2><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h2>
413+
<h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
419414
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/contract/issues">GitHub Issues</a>.
420415
In case of trouble, please check there if your issue has already been reported.
421416
If you spotted it first, help us to smash it by providing a detailed and welcomed
422417
<a class="reference external" href="https://github.com/OCA/contract/issues/new?body=module:%20subscription_oca%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
423418
<p>Do not contact contributors directly about support or help with technical issues.</p>
424419
</div>
425420
<div class="section" id="credits">
426-
<h2><a class="toc-backref" href="#toc-entry-4">Credits</a></h2>
421+
<h1><a class="toc-backref" href="#toc-entry-4">Credits</a></h1>
427422
<div class="section" id="authors">
428-
<h3><a class="toc-backref" href="#toc-entry-5">Authors</a></h3>
423+
<h2><a class="toc-backref" href="#toc-entry-5">Authors</a></h2>
429424
<ul class="simple">
430425
<li>Domatix</li>
431426
</ul>
432427
</div>
433428
<div class="section" id="contributors">
434-
<h3><a class="toc-backref" href="#toc-entry-6">Contributors</a></h3>
429+
<h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
435430
<ul class="simple">
436431
<li>Carlos Martínez &lt;<a class="reference external" href="mailto:carlos&#64;domatix.com">carlos&#64;domatix.com</a>&gt;</li>
437432
<li>Carolina Ferrer &lt;<a class="reference external" href="mailto:carolina&#64;domatix.com">carolina&#64;domatix.com</a>&gt;</li>
438433
<li><a class="reference external" href="https://www.ooops404.com">Ooops404</a>:<ul>
439434
<li>Ilyas &lt;<a class="reference external" href="mailto:irazor147&#64;gmail.com">irazor147&#64;gmail.com</a>&gt;</li>
440435
</ul>
441436
</li>
437+
<li><a class="reference external" href="https://binhex.cloud">Binhex</a>:<ul>
438+
<li>Adasat Torres de Leon &lt;<a class="reference external" href="mailto:a.torres&#64;binhex.cloud">a.torres&#64;binhex.cloud</a>&gt;</li>
439+
</ul>
440+
</li>
441+
<li>Chris Mann &lt;chrisandrewmann&gt;</li>
442442
</ul>
443443
</div>
444444
<div class="section" id="maintainers">
445-
<h3><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h3>
445+
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
446446
<p>This module is maintained by the OCA.</p>
447447
<a class="reference external image-reference" href="https://odoo-community.org">
448448
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
@@ -455,6 +455,5 @@ <h3><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h3>
455455
</div>
456456
</div>
457457
</div>
458-
</div>
459458
</body>
460459
</html>

0 commit comments

Comments
 (0)