Skip to content

Commit 62928eb

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

File tree

3 files changed

+138
-2
lines changed

3 files changed

+138
-2
lines changed

subscription_oca/models/sale_subscription.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,12 @@ def create_sale_order(self):
328328
def generate_invoice(self):
329329
invoice_number = ""
330330
msg_static = _("Created invoice with reference")
331-
if self.template_id.invoicing_mode in ["draft", "invoice", "invoice_send"]:
331+
if self.template_id.invoicing_mode in [
332+
"draft",
333+
"invoice",
334+
"invoice_send",
335+
"invoice_and_payment",
336+
]:
332337
invoice = self.create_invoice()
333338
if self.template_id.invoicing_mode != "draft":
334339
invoice.action_post()
@@ -340,6 +345,8 @@ def generate_invoice(self):
340345
email_layout_xmlid="mail.mail_notification_paynow",
341346
)
342347
invoice.write({"is_move_sent": True})
348+
if self.template_id.invoicing_mode == "invoice_and_payment":
349+
self.create_payment(invoice)
343350
invoice_number = invoice.name
344351
message_body = (
345352
"<b>%s</b> <a href=# data-oe-model=account.move data-oe-id=%d>%s</a>"
@@ -363,6 +370,50 @@ def generate_invoice(self):
363370
self.calculate_recurring_next_date(self.recurring_next_date)
364371
self.message_post(body=message_body)
365372

373+
def create_payment(self, invoice):
374+
invoice.ensure_one()
375+
payment_token = self.env["payment.token"].search(
376+
[("partner_id", "=", invoice.partner_id.id)],
377+
order="create_date desc",
378+
limit=1,
379+
)
380+
if not payment_token:
381+
self.message_post(
382+
body=_(
383+
"No payment token found for partner %s" % invoice.partner_id.name
384+
)
385+
)
386+
return
387+
provider = payment_token.provider_id
388+
method_line = self.env["account.payment.method.line"].search(
389+
[
390+
("payment_method_id.code", "=", provider.code),
391+
("company_id", "=", invoice.company_id.id),
392+
],
393+
limit=1,
394+
)
395+
payment_register = self.env["account.payment.register"]
396+
payment_vals = {
397+
"currency_id": invoice.currency_id.id,
398+
"journal_id": provider.journal_id.id,
399+
"company_id": invoice.company_id.id,
400+
"partner_id": invoice.partner_id.id,
401+
"communication": invoice.name,
402+
"payment_type": "inbound",
403+
"partner_type": "customer",
404+
"payment_difference_handling": "open",
405+
"writeoff_label": "Write-Off",
406+
"payment_date": fields.Date.today(),
407+
"amount": invoice.amount_total,
408+
"payment_method_line_id": method_line.id,
409+
"payment_token_id": payment_token.id,
410+
}
411+
payment_register.with_context(
412+
active_model="account.move",
413+
active_ids=invoice.ids,
414+
active_id=invoice.id,
415+
).create(payment_vals).action_create_payments()
416+
366417
def manual_invoice(self):
367418
invoice_id = self.create_invoice()
368419
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/tests/test_subscription_oca.py

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ def setUpClass(cls):
3838
("company_id", "=", cls.env.ref("base.main_company").id),
3939
]
4040
)[0]
41+
42+
cls.bank_journal = cls.env["account.journal"].search(
43+
[
44+
("type", "=", "bank"),
45+
("company_id", "=", cls.env.ref("base.main_company").id),
46+
]
47+
)[0]
48+
4149
cls.pricelist1 = cls.env["product.pricelist"].create(
4250
{
4351
"name": "pricelist for contract test",
@@ -116,6 +124,14 @@ def setUpClass(cls):
116124
}
117125
)
118126

127+
cls.tmpl6 = cls.create_sub_template(
128+
{
129+
"recurring_rule_boundary": "unlimited",
130+
"invoicing_mode": "invoice_and_payment",
131+
"recurring_rule_type": "years",
132+
}
133+
)
134+
119135
cls.stage = cls.env["sale.subscription.stage"].create(
120136
{
121137
"name": "Test Sub Stage",
@@ -193,6 +209,14 @@ def setUpClass(cls):
193209
}
194210
)
195211

212+
cls.sub10 = cls.create_sub(
213+
{
214+
"template_id": cls.tmpl6.id,
215+
"recurring_rule_boundary": False,
216+
"date_start": fields.Date.today(),
217+
}
218+
)
219+
196220
cls.sub_line = cls.create_sub_line(cls.sub1)
197221
cls.sub_line2 = cls.env["sale.subscription.line"].create(
198222
{
@@ -210,6 +234,7 @@ def setUpClass(cls):
210234
cls.sub_line52 = cls.create_sub_line(cls.sub5, cls.product_2.id)
211235
cls.sub_line71 = cls.create_sub_line(cls.sub7)
212236
cls.sub_line72 = cls.create_sub_line(cls.sub7, cls.product_2.id)
237+
cls.sub_line102 = cls.create_sub_line(cls.sub10, cls.product_2.id)
213238

214239
cls.close_reason = cls.env["sale.subscription.close.reason"].create(
215240
{
@@ -253,6 +278,44 @@ def setUpClass(cls):
253278
}
254279
)
255280

281+
cls.account_payment_method = cls.env["account.payment.method"].create(
282+
{
283+
"name": "Test Payment Method",
284+
"code": "none",
285+
"payment_type": "inbound",
286+
}
287+
)
288+
289+
account_payment_method_line = cls.env["account.payment.method.line"].create(
290+
{
291+
"payment_method_id": cls.account_payment_method.id,
292+
"company_id": cls.env.ref("base.main_company").id,
293+
"name": "Test Method Line",
294+
}
295+
)
296+
297+
cls.journal = cls.env["account.journal"].create(
298+
{
299+
"name": "Test Journal",
300+
"type": "bank",
301+
"company_id": cls.env.ref("base.main_company").id,
302+
"code": "TESTJNL",
303+
"inbound_payment_method_line_ids": [
304+
(4, account_payment_method_line.id)
305+
],
306+
}
307+
)
308+
309+
cls.provider_test = cls.env["payment.provider"].create(
310+
{
311+
"name": "Test Provider for Subscriptions",
312+
"code": "none",
313+
"company_id": cls.env.ref("base.main_company").id,
314+
"journal_id": cls.journal.id,
315+
"state": "test",
316+
}
317+
)
318+
256319
@classmethod
257320
def create_sub_template(cls, vals):
258321
code = str(uuid.uuid4().hex)
@@ -541,7 +604,7 @@ def test_subscription_oca_sub_stage(self):
541604

542605
def test_x_subscription_oca_pricelist_related(self):
543606
res = self.partner.read(["subscription_count", "subscription_ids"])
544-
self.assertEqual(res[0]["subscription_count"], 9)
607+
self.assertEqual(res[0]["subscription_count"], 10)
545608
res = self.partner.action_view_subscription_ids()
546609
self.assertIsInstance(res, dict)
547610
sale_order = self.sub1.create_sale_order()
@@ -692,3 +755,24 @@ def _collect_all_sub_test_results(self, subscription):
692755
)
693756
test_res.append(group_stage_ids)
694757
return test_res
758+
759+
def test_subscription_invoice_and_payment(self):
760+
subscription = self.sub10
761+
subscription.generate_invoice()
762+
self.assertEqual(len(subscription.invoice_ids), 1)
763+
self.assertEqual(subscription.invoice_ids.state, "posted")
764+
self.env["payment.token"].create(
765+
{
766+
"payment_details": "1234",
767+
"provider_id": self.provider_test.id,
768+
"partner_id": self.partner.id,
769+
"provider_ref": "provider Ref (TEST)",
770+
"active": True,
771+
}
772+
)
773+
subscription.generate_invoice()
774+
self.assertEqual(len(subscription.invoice_ids), 2)
775+
last_invoice = subscription.invoice_ids[-1]
776+
self.assertEqual(last_invoice.state, "posted")
777+
payments = last_invoice.payment_ids
778+
self.assertEqual(len(payments), 0)

0 commit comments

Comments
 (0)