Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save karson/145cb0f36a2eeabc65da8fff7b2f301c to your computer and use it in GitHub Desktop.
Save karson/145cb0f36a2eeabc65da8fff7b2f301c to your computer and use it in GitHub Desktop.

Revisions

  1. @Pierowheelz Pierowheelz created this gist Jun 14, 2018.
    39 changes: 39 additions & 0 deletions mark_zero_invoices_paid.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    <?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