Skip to content

Commit

Permalink
feat: use invoices to repeat payments
Browse files Browse the repository at this point in the history
This properly updates package prices.
  • Loading branch information
nijel committed Oct 24, 2024
1 parent f1b417b commit 0b1f236
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
5 changes: 3 additions & 2 deletions weblate_web/invoices/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,10 @@ def generate_pdf(self) -> None:
font_config=font_config,
)

def finalize(
def duplicate(
self,
*,
kind: InvoiceKind = InvoiceKind.INVOICE,
kind: InvoiceKind,
prepaid: bool = True,
) -> Invoice:
"""Create a final invoice from draft/proforma upon payment."""
Expand All @@ -499,6 +499,7 @@ def finalize(
quantity=item.quantity,
quantity_unit=item.quantity_unit,
unit_price=item.unit_price,
package=item.package,
)
return invoice

Expand Down
7 changes: 6 additions & 1 deletion weblate_web/management/commands/recurring_payments.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from django.core.management.base import BaseCommand
from django.utils import timezone

from weblate_web.invoices.models import InvoiceKind
from weblate_web.models import Donation, Service, Subscription
from weblate_web.payments.models import Payment
from weblate_web.payments.utils import send_notification
Expand Down Expand Up @@ -138,7 +139,11 @@ def peform_payment(payment, past_payments, amount: int | None = None):
return

# Create repeated payment
repeated = payment.repeat_payment(amount=amount)
if payment.paid_invoice:
invoice = payment.paid_invoice.duplicate(kind=InvoiceKind.DRAFT)
repeated = invoice.create_payment(payment.recurring)

Check warning on line 144 in weblate_web/management/commands/recurring_payments.py

View check run for this annotation

Codecov / codecov/patch

weblate_web/management/commands/recurring_payments.py#L143-L144

Added lines #L143 - L144 were not covered by tests
else:
repeated = payment.repeat_payment(amount=amount)

# Backend does not support it
if not repeated:
Expand Down
2 changes: 1 addition & 1 deletion weblate_web/payments/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def generate_invoice(self, *, proforma: bool = False) -> None:
if proforma and self.payment.draft_invoice.kind == invoice_kind:
return
# Finalize draft if present
invoice = self.payment.draft_invoice.finalize(
invoice = self.payment.draft_invoice.duplicate(
kind=invoice_kind,
prepaid=not proforma,
)
Expand Down

0 comments on commit 0b1f236

Please sign in to comment.