mrsha Posted January 24, 2019 Share Posted January 24, 2019 Hi, just recently I started to mess around with WHMCS hooks and API so sorry If I make some silly questions. So without getting in to to much details as to why, I have a hook that whenever a clients edits its profile and edits a custom client field, It then alters the tax exemption rule for that user (enables or disables) directly on the database. This works perfectly, and even works properly on page registration (another very similar hook) and new orders. The problems start if the client edits this field and there is a Pending invoice, the tax exemption is applied but the new tax status is not applied to the existing pending invoices. So in order to alter the existing invoices I first gather all the unpaid invoices (for the user) with WHMCS API command GetInvoices and if the count is bigger than 0 I then iterate through the resulting array of invoices and use UpdateInvoice to apply a new taxrate to each invoice. This took me a whie to figure out, and after claiming victory I then realized that the tax rate is applied but the tax amount for the order and the total amount remain the same, I foolishly thought (dont know why) that if I applied with the API a new tax rate to an invoice, the system would recalculate the tax for the invoice and a new total, but that is not the case, and for some reason I cannot pass the total tax value for an invoice with the UpdateInvoice command, I can however pass which line items are to be taxed (in an array) but If get that to happend I dont know if the total will get updated, also I cant figure out how to get the line items. What I want to accomplish is that if a tax expemtion is applied to a user, and the user has pending invoices, then those invoices should be updated with the the corresponding new tax (either 0% or 16%). Tax calculation mode is set to: Calculate based on collective sum of the taxable line items. 0 Quote Link to comment Share on other sites More sharing options...
mrsha Posted January 24, 2019 Author Share Posted January 24, 2019 Sorry for the grammar (I did not intend to send the post until I did a grammar check but a missclick sent it and I cant edit it). As I was saying here is some of the code: // Define function to update unpaid invoices (when modifying tax status) function updateunpaidinvoices($userId, $newTaxExempt){ // Prepare API command to get unpaid invoices $upCommand = 'GetInvoices'; $upPostData = array( 'userid' => "$userId", 'status' => 'Unpaid', ); // Execute API command to get user invoices $upResults = localAPI($upCommand, $upPostData); // Variable of total unpaid invoices $unpaidCount = $upResults['totalresults']; if ($unpaidCount > 0) { // Get into the array $unpaidInvoices = $upResults['invoices']; $unpaidInvoices = $unpaidInvoices['invoice']; // Only for development! // echo '<pre>' . var_export($unpaidInvoices, true) . '</pre>'; // For each unpaid invoice update tax foreach ($unpaidInvoices as $unpaidInvoice => $unpaidInvoiceData) { // Define variable for ID of each invoice $unpaidInvoiceID = $unpaidInvoiceData['id']; // Currently not used, problably will be if Tax must be calculated manually $unpaidInvoiceSubtotal = $unpaidInvoiceData['subtotal']; // Define API action $updateCommand = 'UpdateInvoice'; // If not tax exempt then apply IVA 16% if ($newTaxExempt == 0) { $updatePostData = array( 'invoiceid' => "$unpaidInvoiceID", 'taxrate' => '16.00', ); } // Else apply 0 tax rate else{ $updatePostData = array( 'invoiceid' => "$unpaidInvoiceID", 'taxrate' => '0.00', ); } // Execute API taxrate edit $updateResults = localAPI($updateCommand, $updatePostData); } } // Development! // die(); } And here Is a the result of // echo '<pre>' . var_export($unpaidInvoices, true) . '</pre>'; array ( 0 => array ( 'id' => 2, 'userid' => 1, 'firstname' => 'Name', 'lastname' => 'Name', 'companyname' => '', 'invoicenum' => '', 'date' => '2019-01-10', 'duedate' => '2019-01-10', 'datepaid' => '0000-00-00 00:00:00', 'last_capture_attempt' => '0000-00-00 00:00:00', 'subtotal' => '10.00', 'credit' => '0.00', 'tax' => '0.00', 'tax2' => '0.00', 'total' => '10.00', 'taxrate' => '0.00', 'taxrate2' => '0.00', 'status' => 'Unpaid', 'paymentmethod' => 'banktransfer', 'notes' => '', ), 1 => array ( 'id' => 3, 'userid' => 1, 'firstname' => 'Name', 'lastname' => 'Name', 'companyname' => '', 'invoicenum' => '', 'date' => '2019-01-24', 'duedate' => '2019-01-24', 'datepaid' => '0000-00-00 00:00:00', 'last_capture_attempt' => '0000-00-00 00:00:00', 'subtotal' => '400.00', 'credit' => '0.00', 'tax' => '0.00', 'tax2' => '0.00', 'total' => '400.00', 'taxrate' => '0.00', 'taxrate2' => '0.00', 'status' => 'Unpaid', 'paymentmethod' => 'banktransfer', 'notes' => '', ), 2 => array ( 'id' => 4, 'userid' => 1, 'firstname' => 'Name', 'lastname' => 'Name', 'companyname' => '', 'invoicenum' => '', 'date' => '2019-01-24', 'duedate' => '2019-01-24', 'datepaid' => '0000-00-00 00:00:00', 'last_capture_attempt' => '0000-00-00 00:00:00', 'subtotal' => '20.00', 'credit' => '0.00', 'tax' => '0.00', 'tax2' => '0.00', 'total' => '20.00', 'taxrate' => '0.00', 'taxrate2' => '0.00', 'status' => 'Unpaid', 'paymentmethod' => 'banktransfer', 'notes' => '', ), ) 0 Quote Link to comment Share on other sites More sharing options...
Kirill Posted March 15, 2019 Share Posted March 15, 2019 According to the data from WHMCS technician there's some bug around UpdateInvoice and tax recalculation. We just need to wait till it's fixed. As a temporary solution, I'm making a database update for tax rate and taxed items and running UpdateInvoice to recalculate the tax. 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.