Created
June 14, 2018 22:39
-
-
Save Pierowheelz/6ea2b26d348b8b96f0075e280e1ebdd5 to your computer and use it in GitHub Desktop.
A hook for WHMCS which marks invoices which have received payment as paid, where the WHMCS system doesn't automatically mark them paid. I found that if manually associating a transaction with an invoice number in WHMCS, the invoice is not marked as paid even when there is zero total remaining. This fixes that.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!defined("WHMCS")) | |
die("This file cannot be accessed directly"); | |
use WHMCS\Database\Capsule; | |
function wb_mark_zero_invoices_paid(){ | |
//get all unpaid invoices | |
$unpaid_invoices = Capsule::table('tblinvoices')->where('status', 'Unpaid')->get(); | |
foreach( $unpaid_invoices as $inv ){ | |
$inv_id = $inv->id; | |
$total = (float) $inv->total; | |
//get all payments toward this invoices | |
$transactions = Capsule::table('tblaccounts')->where('invoiceid', $inv_id)->get(); | |
foreach( $transactions as $trans ){ | |
$total = $total - (float) $trans->amountin; //reduce total by paid amount | |
} | |
//if invoice has no more outstanding dollarydoos | |
if( $total <= 0 ){ | |
//update invoice status to PAID | |
if( Capsule::table('tblinvoices')->where('id', $inv_id)->update( array('status' => 'Paid', 'datepaid' => date('Y-m-d H:i:s')) ) ){ | |
logActivity('Manual match: Invoice '.$inv_id.' marked as paid due to manual transaction match.', 0); | |
//send invoice payment confirmation email | |
$postData = array( | |
'messagename' => 'Invoice Payment Confirmation', | |
'id' => $inv_id | |
); | |
update_account_email_for_invoices( array('source'=> 'manual_match','user'=>$inv->userid, 'invoiceid'=>$inv_id, 'status'=>'Paid') ); | |
localAPI('SendEmail', $postData); | |
fix_auto_invoice_emails(); | |
} | |
} | |
} | |
} | |
//add_hook('AddInvoicePayment', 1, "wb_mark_zero_invoices_paid"); //this hook is the obvious one, but fires at the wrong time | |
add_hook('PreCronJob', 1, "wb_mark_zero_invoices_paid"); //sometimes not triggered (if no automation tasks present) | |
add_hook('AfterCronJob', 1, "wb_mark_zero_invoices_paid"); //always triggered on cron jobs | |
add_hook('AdminAreaFooterOutput', 1, "wb_mark_zero_invoices_paid"); //for when a transaction is manually matched to an invoice |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment