Skip to content

Commit

Permalink
Audit closed invoices action (#21)
Browse files Browse the repository at this point in the history
* Return if audit was ok or not

* Audit closed invoices action

* Add new migration

* Rename migration

* Update package version

* Add tests
  • Loading branch information
bertini36 authored May 28, 2021
1 parent f17210e commit b2042ef
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 18 deletions.
2 changes: 1 addition & 1 deletion billing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '1.7.23'
__version__ = '1.7.24'
__copyright__ = 'Copyright (c) 2020, Skioo SA'
__licence__ = 'MIT'
__URL__ = 'https://github.com/skioo/django-customer-billing'
34 changes: 33 additions & 1 deletion billing/actions/invoices.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from structlog import get_logger

from .. import psp
from ..models import CreditCard, Invoice, Transaction, Account
from ..models import Account, CreditCard, Invoice, Transaction

logger = get_logger()

Expand Down Expand Up @@ -84,3 +84,35 @@ def pay_with_account_credit_cards(invoice_id) -> Optional[Transaction]:
except Exception as e:
logger.error('invoice-payment-error', invoice_id=invoice_id, credit_card=credit_card, exc_info=e)
return None


def audit_closed_invoices() -> bool:
invoices = Invoice.objects.exclude(status=Invoice.PENDING)
logger.debug('audit-closed-invoices', non_pending_invoice_count=len(invoices))
all_ok = True

for invoice in invoices:
due_total = invoice.due()
due_monies = due_total.monies()

if len(due_monies) != 1:
logger.info(
'wrong-number-of-currencies',
invoice_id=invoice.id,
status=invoice.status,
currency_count=len(due_monies)
)
all_ok = False
continue

due_value = due_monies[0]
if due_value.amount != 0:
logger.info(
'non-zero-due',
invoice_id=invoice.id,
status=invoice.status,
due=due_value
)
all_ok = False

return all_ok
18 changes: 2 additions & 16 deletions billing/management/commands/audit_closed_invoices.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import structlog
from django.core.management.base import BaseCommand

from ...models import Invoice
from ...actions.invoices import audit_closed_invoices


def set_debug(logger_name):
Expand All @@ -21,18 +21,4 @@ class Command(BaseCommand):
def handle(self, *args, **options):
if options['verbosity'] >= 2:
set_debug('django.db.backends')

invoices = Invoice.objects.exclude(status=Invoice.PENDING)

logger.debug('audit-closed-invoices', non_pending_invoice_count=len(invoices))

for invoice in invoices:
due_total = invoice.due()
due_monies = due_total.monies()
if len(due_monies) != 1:
logger.info('wrong-number-of-currencies', invoice_id=invoice.id, status=invoice.status,
currency_count=len(due_monies))
continue
due_value = due_monies[0]
if due_value.amount != 0:
logger.info('non-zero-due', invoice_id=invoice.id, status=invoice.status, due=due_value)
audit_closed_invoices()
Loading

0 comments on commit b2042ef

Please sign in to comment.