Skip to content

Commit

Permalink
Add paid_for_date
Browse files Browse the repository at this point in the history
  • Loading branch information
tarteo committed Mar 27, 2024
1 parent d9ac54d commit e1cc70b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
4 changes: 2 additions & 2 deletions sale_recurring_payment/models/payment_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ def _process_payment_provider_subscription_recurring_payment(
if done_payment_transaction:
unpaid_invoices = subscription.invoice_ids.filtered(
lambda i: i.payment_state == "not_paid"
).ordered("invoice_date")
).sorted("invoice_date")
unpaid_invoice = unpaid_invoices and unpaid_invoices[0]
if not unpaid_invoice:
subscription.generate_invoice()
unpaid_invoice = subscription.invoice_ids.filtered(
lambda i: i.payment_state == "not_paid"
).ordered("invoice_date")[0]
).sorted("invoice_date")[0]
done_payment_transaction.invoice_ids = [(6, 0, unpaid_invoice.ids)]
done_payment_transaction._reconcile_after_done()
return None
Expand Down
4 changes: 2 additions & 2 deletions sale_recurring_payment/models/sale_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def _compute_paid_for_date(self):
lambda s: s.payment_state == "paid"
)
if paid_invoices:
last_paid_invoice = paid_invoices.ordered("invoice_date")[-1]
last_paid_invoice = paid_invoices.sorted("invoice_date")[-1]
type_interval = sub.template_id.recurring_rule_type
interval = int(sub.template_id.recurring_interval)
paid_for_date = last_paid_invoice + relativedelta(
paid_for_date = last_paid_invoice.invoice_date + relativedelta(
**{type_interval: interval}
)
sub.paid_for_date = paid_for_date
Expand Down
Empty file.
52 changes: 52 additions & 0 deletions sale_recurring_payment/tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from dateutil.relativedelta import relativedelta

from odoo import Command, fields
from odoo.tests.common import TransactionCase


class TestFields(TransactionCase):
@classmethod
def setUpClass(cls):
super(TestFields, cls).setUpClass()
cls.sub_product = cls.env.ref("product.product_product_12")
cls.sub_tmpl = cls.env["sale.subscription.template"].create(
{
"recurring_interval": 7,
"recurring_rule_type": "days",
"invoicing_mode": "invoice_send",
"name": "Weekly",
}
)
cls.partner_id = cls.env.ref("base.partner_admin")

def test_paid_for_date(self):
sub = self.env["sale.subscription"].create(
{
"template_id": self.sub_tmpl.id,
"sale_subscription_line_ids": [
Command.create({"product_id": self.sub_product.id})
],
"partner_id": self.partner_id.id,
"pricelist_id": self.partner_id.property_product_pricelist.id,
}
)
today = fields.Date.today()
sub.generate_invoice()
sub.generate_invoice()
sub.generate_invoice()

invoices = sub.invoice_ids.sorted("invoice_date")
self.assertEqual(invoices[0].invoice_date, today)
self.assertEqual(invoices[1].invoice_date, today + relativedelta(days=7))
self.assertEqual(invoices[2].invoice_date, today + relativedelta(days=14))
self.assertFalse(sub.paid_for_date)

# Needs sale_recurring_payment_mollie e.g. installed to work, we can't do this here
# self.env["payment.transaction"]._process_payment_provider_subscription_recurring_payment(
# sub
# )

self.env["account.payment.register"].with_context(
active_model="account.move", active_ids=invoices[0].ids
).create({}).action_create_payments()
self.assertEqual(sub.paid_for_date, today + relativedelta(days=7))

0 comments on commit e1cc70b

Please sign in to comment.