SteelSignature Posted November 12, 2019 Share Posted November 12, 2019 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/ 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted November 12, 2019 Share Posted November 12, 2019 On InvoiceCreated or InvoiceCreationPreEmail hook points run ApplyCredit API function. 0 Quote Link to comment Share on other sites More sharing options...
SteelSignature Posted November 12, 2019 Author Share Posted November 12, 2019 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', ); }); 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted November 12, 2019 Share Posted November 12, 2019 (edited) 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 November 12, 2019 by Kian 1 Quote Link to comment Share on other sites More sharing options...
SteelSignature Posted November 13, 2019 Author Share Posted November 13, 2019 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!!! 0 Quote Link to comment Share on other sites More sharing options...
Bhondawe30 Posted November 18, 2019 Share Posted November 18, 2019 Hi @SteelSignature This code will help to me. 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.