-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathListInvoicesAdvanced.php
76 lines (66 loc) · 2.61 KB
/
ListInvoicesAdvanced.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
require_once __DIR__ . '/includes.php';
// construct a query to filter returned invoices
$invoiceQuery = new \Clearbooks_Soap_1_0_InvoiceQuery();
// find invoices that were created or had a payment applied within the last week
$invoiceQuery->modifiedSince = date( 'Y-m-d', strtotime( '-1 week' ) );
// look in the sales ledger
$invoiceQuery->ledger = 'sales';
// exclude voided invoices
$invoiceQuery->status = 'valid';
$invoices = array();
for ( $i = 0; true; $i += 100 ) {
$invoiceQuery->offset = $i;
// get the next batch of invoices
$batch = $client->listInvoices( $invoiceQuery );
// filter invoices by status
array_walk( $batch, function( $invoice ) use ( &$invoices ) {
/** @var $invoice Clearbooks_Soap_1_0_Invoice */
$status = $invoice->status;
if ( $status == 'approved' && strtotime( $invoice->dateDue ) <= mktime( 0, 0, 0 ) ) {
$status = 'unpaid';
}
$invoices[$status][] = $invoice;
} );
// exit the loop if there are no more invoices to retrieve
if ( count( $batch ) < 100 ) {
break;
}
}
// If you only wanted to retrieve paid invoices,
// $invoiceQuery->status = 'paid';
if ( isset( $invoices['paid'] ) ) {
array_walk( $invoices['paid'], function( $invoice ) {
/** @var $invoice Clearbooks_Soap_1_0_Invoice */
$number = ( $invoice->invoice_prefix ?: 'INV' ) . str_pad( $invoice->invoiceNumber, 6, '0', STR_PAD_LEFT );
$gross = number_format( $invoice->gross, 2 );
echo "Invoice {$number} has been paid £{$gross}\n";
} );
}
/*
Invoice INV000196 has been paid £5,875.00
Invoice INV000197 has been paid £5,875.00
Invoice INV000198 has been paid £11,750.00
*/
if ( isset( $invoices['approved'] ) ) {
array_walk( $invoices['approved'], function( $invoice ) {
/** @var $invoice Clearbooks_Soap_1_0_Invoice */
$number = ( $invoice->invoice_prefix ?: 'INV' ) . str_pad( $invoice->invoiceNumber, 6, '0', STR_PAD_LEFT );
$balance = number_format( $invoice->balance, 2 );
echo "Invoice {$number} has a balance of £{$balance}\n";
} );
}
/*
Invoice INV000199 has a balance of £3,842.25
*/
if ( isset( $invoices['unpaid'] ) ) {
array_walk( $invoices['unpaid'], function( $invoice ) {
/** @var $invoice Clearbooks_Soap_1_0_Invoice */
$number = ( $invoice->invoice_prefix ?: 'INV' ) . str_pad( $invoice->invoiceNumber, 6, '0', STR_PAD_LEFT );
$balance = number_format( $invoice->balance, 2 );
echo "Invoice {$number} has an outstanding balance of £{$balance}\n";
} );
}
/*
Invoice INV000200 has an outstanding balance of £66.89
*/