Jump to content

Auto Apply Credit


SteelSignature

Recommended Posts

I've noticed the option to apply credit to an unpaid invoice only applies to recurring invoices (which is rather odd). Billable Items seem to apply credit; the only time credit isn't being applied is when a quote is converted into an invoice (which seems extra silly since the client has to approve the quote anyway).

Is there an easy way to alter this behavior and allow all unpaid invoices to automatically apply credit in the account?

I found this but can't make much sense of it: https://developers.whmcs.com/api-reference/applycredit/

Link to comment
Share on other sites

1 hour ago, Kian said:

On InvoiceCreated or InvoiceCreationPreEmail hook points run ApplyCredit API function.

Wow that make a bit more sense (sorry not a developer). Will the below work or does Client Details have to be called to figure out credit availability? And is this statement in postdata required or redundant: 'invoiceid' => 'invoiceid'

<?php
add_hook('InvoiceCreationPreEmail', 1, function($vars) {
	$command = 'ApplyCredit';
	$postData = array(
    	'noemail' => 'true',
);
});

 

Link to comment
Share on other sites

This one should work.

<?php

use Illuminate\Database\Capsule\Manager as Capsule;

add_hook('InvoiceCreation', 1, function($vars)
{
	$Data 			= Capsule::select(Capsule::raw('SELECT t1.total, t2.credit FROM tblinvoices AS t1 LEFT JOIN tblclients AS t2 ON t1.userid = t2.id WHERE t1.id = "' . $vars['invoiceid'] . '" LIMIT 1'));
	$CreditBalance 	        = $Data[0]->credit;
	$TotalDue	 	= $Data[0]->total;

	// Apply Credit only if Credit Balance is greater than zero
	if ($CreditBalance)
	{
		$postData = array(
				'invoiceid' => $vars['invoiceid'],
				'amount' => ($CreditBalance < $TotalDue ? $CreditBalance : $TotalDue) // If Credit Balance is less than Total Due there will be a partial payment
		);

		$results = localAPI('ApplyCredit', $postData, $adminUsername);
	}
});

 

Edited by Kian
Link to comment
Share on other sites

2 hours ago, Kian said:

This one should work.


<?php

use Illuminate\Database\Capsule\Manager as Capsule;

add_hook('InvoiceCreation', 1, function($vars)
{
	$Data 			= Capsule::select(Capsule::raw('SELECT t1.total, t2.credit FROM tblinvoices AS t1 LEFT JOIN tblclients AS t2 ON t1.userid = t2.id WHERE t1.id = "' . $vars['invoiceid'] . '" LIMIT 1'));
	$CreditBalance 	        = $Data[0]->credit;
	$TotalDue	 	= $Data[0]->total;

	// Apply Credit only if Credit Balance is greater than zero
	if ($CreditBalance)
	{
		$postData = array(
				'invoiceid' => $vars['invoiceid'],
				'amount' => ($CreditBalance < $TotalDue ? $CreditBalance : $TotalDue) // If Credit Balance is less than Total Due there will be a partial payment
		);

		$results = localAPI('ApplyCredit', $postData, $adminUsername);
	}
});

 

You are a saint!!!

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