wellconnit Posted August 26, 2019 Share Posted August 26, 2019 Hi, I've searched but haven't been able to find a merge field that will show a customers overdue amount. I want to add a line when a new invoice is created that says Please note you have an outstanding invoice amount of: {$OverdueAmountVariableHere}. But I can only see the merge field {$invoice_total_balance_due} And because WHMCS add's the currency to the end of the outstanding amount, I've had to reverse the IF statement to reflect. {if $invoice_total_balance_due != "$0.00AUD"} To me it would've made more sense to have the amount and the currency as two separate variables. Does anyone have any solutions? 0 Quote Link to comment Share on other sites More sharing options...
WHMCS ChrisD Posted August 26, 2019 Share Posted August 26, 2019 @wellconnit out of the box there is not an OverDue Amount Merge feild the closest I would suggest is $invoice_total_balance_due which as you have seen shows everything that is Unpaid/Due 0 Quote Link to comment Share on other sites More sharing options...
wellconnit Posted August 26, 2019 Author Share Posted August 26, 2019 Hi @WHMCS ChrisD, Thanks for the response. Unfortunately that doesn't really solve the issue because I can't tell users that they have an overdue amount if there are invoices that are not overdue yet. I'm not sure if anyone else has any suggestions? Maybe utilising a for loop and checking the invoices that are overdue and adding the numbers, but it does seem like a big headache for something that should be simple 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted August 26, 2019 Share Posted August 26, 2019 6 hours ago, wellconnit said: I'm not sure if anyone else has any suggestions? Maybe utilising a for loop and checking the invoices that are overdue and adding the numbers, but it does seem like a big headache for something that should be simple effectively, it's just an EmailPreSend hook to calculate the variable and make the result available to the email template(s) - so let's assume you want to use this in the Invoice Reminder email template... <?php # Email MergeFields Hook # Written by brian! use WHMCS\Database\Capsule; add_hook('EmailPreSend', 1, function($vars) { if ($vars['messagename'] == 'Invoice Payment Reminder') { $invoiceid = $vars['relid']; $userid = Capsule::table('tblinvoices')->where('id',$invoiceid)->value('userid'); $overdueinvoices = WHMCS\Billing\Invoice::with("transactions")->whereUserid($userid)->overdue()->massPay(false)->get(); $merge_fields['invoice_overdue_balance'] = formatCurrency($overdueinvoices->sum("balance")); return $merge_fields; } }); that should give you a variable that you can use in this email template, {$invoice_overdue_balance} which should display the overdue balance properly formatted in the client's currency. if you're going to make the variable available to multiple invoice email templates, then create an array of email template names... $message_names = Array("Invoice Payment Reminder","First Invoice Overdue Notice"); and check if the current template is one of them by replacing the current if statement with in_array... if (in_array($vars['messagename'],$message_names)) { 1 Quote Link to comment Share on other sites More sharing options...
wellconnit Posted October 6, 2019 Author Share Posted October 6, 2019 Hi @brian!, Sorry for the long long delay! Your code worked perfectly ! I got it working on a single template perfectly. But haven't got the knowhow to get it going on multiple email templates. I've added as below, I'm guessing I change the if statement but do I change it to if ($vars[message_names]) or is there a different syntax I should be using. Much appreciated if you could assist again. The remind I want it created on is the Invoice Created Module. Let me know. Thanks! <?php # Email MergeFields Hook # Written by brian! use WHMCS\Database\Capsule; $message_names = Array("Invoice Payment Reminder","Invoice Created"); add_hook('EmailPreSend', 1, function($vars) { if ($vars['messagename'] == 'Invoice Payment Reminder') { $invoiceid = $vars['relid']; $userid = Capsule::table('tblinvoices')->where('id',$invoiceid)->value('userid'); $overdueinvoices = WHMCS\Billing\Invoice::with("transactions")->whereUserid($userid)->overdue()->massPay(false)->get(); $merge_fields['invoice_overdue_balance'] = formatCurrency($overdueinvoices->sum("balance")); return $merge_fields; } }); 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted October 6, 2019 Share Posted October 6, 2019 (edited) 6 hours ago, wellconnit said: I've added as below, I'm guessing I change the if statement but do I change it to if ($vars[message_names]) or is there a different syntax I should be using. <?php # Email MergeFields Hook # Written by brian! use WHMCS\Database\Capsule; add_hook('EmailPreSend', 1, function($vars) { $message_names = array("Invoice Payment Reminder","Invoice Created"); if (in_array($vars['messagename'],$message_names)) { $invoiceid = $vars['relid']; $userid = Capsule::table('tblinvoices')->where('id',$invoiceid)->value('userid'); $overdueinvoices = WHMCS\Billing\Invoice::with("transactions")->whereUserid($userid)->overdue()->massPay(false)->get(); $merge_fields['invoice_overdue_balance'] = formatCurrency($overdueinvoices->sum("balance")); return $merge_fields; } }); Edited October 6, 2019 by brian! 1 Quote Link to comment Share on other sites More sharing options...
wellconnit Posted October 6, 2019 Author Share Posted October 6, 2019 Hi @brian!, You're an absolute superstar. When you write code I actually seem to be able to read it, the assistance has been much appreciated! Thank You! 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.