Sorry for the grammar (I did not intend to send the post until I did a grammar check but a missclick sent it and I cant edit it).
As I was saying here is some of the code:
// Define function to update unpaid invoices (when modifying tax status)
function updateunpaidinvoices($userId, $newTaxExempt){
// Prepare API command to get unpaid invoices
$upCommand = 'GetInvoices';
$upPostData = array(
'userid' => "$userId",
'status' => 'Unpaid',
);
// Execute API command to get user invoices
$upResults = localAPI($upCommand, $upPostData);
// Variable of total unpaid invoices
$unpaidCount = $upResults['totalresults'];
if ($unpaidCount > 0) {
// Get into the array
$unpaidInvoices = $upResults['invoices'];
$unpaidInvoices = $unpaidInvoices['invoice'];
// Only for development!
// echo '<pre>' . var_export($unpaidInvoices, true) . '</pre>';
// For each unpaid invoice update tax
foreach ($unpaidInvoices as $unpaidInvoice => $unpaidInvoiceData) {
// Define variable for ID of each invoice
$unpaidInvoiceID = $unpaidInvoiceData['id'];
// Currently not used, problably will be if Tax must be calculated manually
$unpaidInvoiceSubtotal = $unpaidInvoiceData['subtotal'];
// Define API action
$updateCommand = 'UpdateInvoice';
// If not tax exempt then apply IVA 16%
if ($newTaxExempt == 0) {
$updatePostData = array(
'invoiceid' => "$unpaidInvoiceID",
'taxrate' => '16.00',
);
}
// Else apply 0 tax rate
else{
$updatePostData = array(
'invoiceid' => "$unpaidInvoiceID",
'taxrate' => '0.00',
);
}
// Execute API taxrate edit
$updateResults = localAPI($updateCommand, $updatePostData);
}
}
// Development!
// die();
}
And here Is a the result of // echo '<pre>' . var_export($unpaidInvoices, true) . '</pre>';
array (
0 =>
array (
'id' => 2,
'userid' => 1,
'firstname' => 'Name',
'lastname' => 'Name',
'companyname' => '',
'invoicenum' => '',
'date' => '2019-01-10',
'duedate' => '2019-01-10',
'datepaid' => '0000-00-00 00:00:00',
'last_capture_attempt' => '0000-00-00 00:00:00',
'subtotal' => '10.00',
'credit' => '0.00',
'tax' => '0.00',
'tax2' => '0.00',
'total' => '10.00',
'taxrate' => '0.00',
'taxrate2' => '0.00',
'status' => 'Unpaid',
'paymentmethod' => 'banktransfer',
'notes' => '',
),
1 =>
array (
'id' => 3,
'userid' => 1,
'firstname' => 'Name',
'lastname' => 'Name',
'companyname' => '',
'invoicenum' => '',
'date' => '2019-01-24',
'duedate' => '2019-01-24',
'datepaid' => '0000-00-00 00:00:00',
'last_capture_attempt' => '0000-00-00 00:00:00',
'subtotal' => '400.00',
'credit' => '0.00',
'tax' => '0.00',
'tax2' => '0.00',
'total' => '400.00',
'taxrate' => '0.00',
'taxrate2' => '0.00',
'status' => 'Unpaid',
'paymentmethod' => 'banktransfer',
'notes' => '',
),
2 =>
array (
'id' => 4,
'userid' => 1,
'firstname' => 'Name',
'lastname' => 'Name',
'companyname' => '',
'invoicenum' => '',
'date' => '2019-01-24',
'duedate' => '2019-01-24',
'datepaid' => '0000-00-00 00:00:00',
'last_capture_attempt' => '0000-00-00 00:00:00',
'subtotal' => '20.00',
'credit' => '0.00',
'tax' => '0.00',
'tax2' => '0.00',
'total' => '20.00',
'taxrate' => '0.00',
'taxrate2' => '0.00',
'status' => 'Unpaid',
'paymentmethod' => 'banktransfer',
'notes' => '',
),
)