# Path: billing/actions/invoices.py
# 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


import logging
import structlog
from django.core.management.base import BaseCommand
from ...actions.invoices import audit_closed_invoices




def set_debug(logger_name):
    logger = logging.getLogger(logger_name)
    logger.setLevel(logging.DEBUG)
    logger.addHandler(logging.StreamHandler())


logger = structlog.get_logger()


class Command(BaseCommand):
    help = """Displays all paid and cancelled invoices where due != 0"""

    def handle(self, *args, **options):
        if options['verbosity'] >= 2:
            set_debug('django.db.backends')

# Path: billing/actions/invoices.py
# 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


# Path: billing/management/commands/audit_closed_invoices.py
import logging
import structlog
from django.core.management.base import BaseCommand
from ...actions.invoices import audit_closed_invoices





def set_debug(logger_name):
    logger = logging.getLogger(logger_name)
    logger.setLevel(logging.DEBUG)
    logger.addHandler(logging.StreamHandler())


logger = structlog.get_logger()


class Command(BaseCommand):
    help = """Displays all paid and cancelled invoices where due != 0"""

    def handle(self, *args, **options):
        if options['verbosity'] >= 2:
            set_debug('django.db.backends')

        audit_closed_invoices()