Jump to content

Overdue Amount Email Template


wellconnit

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)) {
Link to comment
Share on other sites

  • 1 month later...

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;
	}
});

 

Link to comment
Share on other sites

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 by brian!
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated