def get_invoices(): xero_tenant_id = get_xero_tenant_id() accounting_api = AccountingApi(api_client) invoices = accounting_api.get_invoices(xero_tenant_id, statuses=["DRAFT", "SUBMITTED"]) code = serialize_model(invoices) sub_title = "Total invoices found: {}".format(len(invoices.invoices)) return render_template("code.html", title="Invoices", code=code, sub_title=sub_title)
def members_list(): xero_tenant_id = get_xero_tenant_id() accounting_api = AccountingApi(api_client) sources = defaultdict(list) member_transactions = defaultdict(list) # Walk the full journal for the past 90 days for membership items # and split them based on which type they are, i.e. 'CASHREC' / 'ACCREC' for journal in get_journals(datetime.datetime.now() - relativedelta(days=90)): if is_membership_journal(journal): sources[journal.source_type].append(journal.source_id) transactions = [] for source, source_ids in sources.items(): if source == 'CASHREC': for s in source_ids: transactions.extend( # get_bank_transactions has to be done individually but there's usually not many of them accounting_api.get_bank_transaction(xero_tenant_id, s).bank_transactions) elif source == 'ACCREC': transactions.extend( accounting_api.get_invoices(xero_tenant_id, i_ds=source_ids).invoices) else: raise ValueError(source) for transaction in transactions: c = transaction.contact.contact_id member_transactions[c].append(transaction) members = accounting_api.get_contacts( xero_tenant_id, i_ds=list(member_transactions.keys())).contacts contact_sheet = [] for m in members: contact_sheet.append({ **fix_contact(m), 'transactions': len(member_transactions[m.contact_id]) }) return render_template( "code.html", title="Members", code=serialize_model(contact_sheet), sub_title="For Past 90 days", )
def test_get_invoices(sandbox_accounting_api: AccountingApi, xero_tenant_id): # Given sandbox API, tenant id, and hardcoded test invoices data # When getting all invoices result = sandbox_accounting_api.get_invoices(xero_tenant_id) # Then expect correct invoices received expected = Invoices(invoices=[ Invoice( amount_credited=Decimal("0.00"), amount_due=Decimal("0.00"), amount_paid=Decimal("0.00"), contact=Contact( contact_id="a3675fc4-f8dd-4f03-ba5b-f1870566bcd7", has_attachments=False, has_validation_errors=False, name="Barney Rubble-83203", addresses=[], contact_groups=[], contact_persons=[], phones=[], ), credit_notes=[], line_items=[], overpayments=[], payments=[], prepayments=[], currency_code=CurrencyCode.NZD, currency_rate=Decimal("1.000000"), date=datetime.date(2018, 10, 20), due_date=datetime.date(2018, 12, 30), has_attachments=False, has_errors=False, invoice_id="d4956132-ed94-4dd7-9eaa-aa22dfdf06f2", invoice_number="INV-0001", is_discounted=False, line_amount_types=LineAmountTypes.EXCLUSIVE, reference="Red Fish, Blue Fish", sent_to_contact=True, status="VOIDED", sub_total=Decimal("40.00"), total=Decimal("40.00"), total_tax=Decimal("0.00"), type="ACCREC", updated_date_utc=datetime.datetime( 2018, 11, 2, 16, 31, 30, 160000, tzinfo=tz.UTC), ), Invoice( amount_credited=Decimal("0.00"), amount_due=Decimal("0.00"), amount_paid=Decimal("46.00"), contact=Contact( contact_id="a3675fc4-f8dd-4f03-ba5b-f1870566bcd7", has_attachments=False, has_validation_errors=False, name="Barney Rubble-83203", addresses=[], contact_groups=[], contact_persons=[], phones=[], ), credit_notes=[], line_items=[], overpayments=[], prepayments=[], currency_code=CurrencyCode.NZD, currency_rate=Decimal("1.000000"), date=datetime.date(2018, 10, 20), due_date=datetime.date(2018, 12, 30), fully_paid_on_date=datetime.date(2018, 11, 29), has_attachments=False, has_errors=False, invoice_id="046d8a6d-1ae1-4b4d-9340-5601bdf41b87", invoice_number="INV-0002", is_discounted=False, line_amount_types=LineAmountTypes.EXCLUSIVE, payments=[ Payment( amount=Decimal("46.00"), currency_rate=Decimal("1.000000"), date=datetime.date(2018, 11, 29), has_account=False, has_validation_errors=False, payment_id="99ea7f6b-c513-4066-bc27-b7c65dcd76c2", ) ], reference="Red Fish, Blue Fish", sent_to_contact=True, status="PAID", sub_total=Decimal("40.00"), total=Decimal("46.00"), total_tax=Decimal("6.00"), type="ACCREC", updated_date_utc=datetime.datetime( 2018, 11, 2, 16, 36, 32, 690000, tzinfo=tz.UTC), ), Invoice( amount_credited=Decimal("0.00"), amount_due=Decimal("115.00"), amount_paid=Decimal("0.00"), contact=Contact( contact_id="a3675fc4-f8dd-4f03-ba5b-f1870566bcd7", has_attachments=False, has_validation_errors=False, name="Barney Rubble-83203", addresses=[], contact_groups=[], contact_persons=[], phones=[], ), credit_notes=[], line_items=[], overpayments=[], payments=[], prepayments=[], currency_code=CurrencyCode.NZD, currency_rate=Decimal("1.000000"), date=datetime.date(2018, 11, 2), due_date=datetime.date(2018, 11, 7), has_attachments=False, has_errors=False, invoice_id="7ef31b20-de17-4312-8382-412f869b1510", invoice_number="INV-0003", is_discounted=False, line_amount_types=LineAmountTypes.EXCLUSIVE, reference="", status="AUTHORISED", sub_total=Decimal("100.00"), total=Decimal("115.00"), total_tax=Decimal("15.00"), type="ACCREC", updated_date_utc=datetime.datetime( 2018, 11, 2, 16, 37, 28, 927000, tzinfo=tz.UTC), ), ]) assert result == expected