Hi all,
I'm trying to write a hook to automatically cancel tiny disk-space overusage invoices that WHMCS occasionally generates and avoid notifying the customer about them. I saw something similar on these forums using PHP's MySQL API, I took inspiration and re-wrote using the WHMCS API's and have so far written the following:
function cancelSmall($vars) {
$MIN_VALUE = 0.5;
$NOTE = "Amounts under £0.50 are not payable. This invoice has been cancelled";
logActivity("DEBUG: ".print_r($vars, true));
$invoiceId = $vars['invoiceid'];
logActivity("DEBUG: invoiceid=".$invoiceId);
$invoiceDetail = Capsule::table('tblinvoices')->where('id', $invoiceId)->first();
logActivity("DEBUG: ".print_r($invoiceDetail, true));
logActivity("DEBUG: ".$invoiceDetail->total);
if (($invoiceDetail->status == "Unpaid") && ($invoiceDetail->total <= $MIN_VALUE)) {
logActivity("DEBUG: Invoice is smaller than minimum value and unpaid!");
$command = 'UpdateInvoice';
$postData = array(
'invoiceid' => $invoiceId,
'status' => 'Cancelled',
'notes' => $NOTE,
);
$results = localAPI($command, $postData);
logActivity("DEBUG: ".print_r($results, true));
} else {
logActivity("DEBUG: Invoice is larger than minimum value or not unpaid!");
}
}
add_hook('InvoiceCreation', 1, 'cancelSmall');
When testing by generating invoices via the admin panel, the hook runs fine and small invoices are automatically marked as cancelled when they transition from the "Draft" to "Unpaid" status. However, an invoice email is still sent out despite the invoice being marked as cancelled. I am using the "Publish and send email" button, as I assumed this would use the same process as the automated invoice would use.
Am I missing something?
Thanks,
Sean