joedavis Posted September 22, 2020 Share Posted September 22, 2020 Hi, I would like to change invoice items when an invoice is automatically generated. I don't care for how configurable options show up so I would like to change it up a little bit. I have tried the AfterInvoicingGenerateInvoiceItems hook but I can't figure out how to use it. It's "vars" array is empty as far as I can tell. Is there a hook somewhere that will fire when items are being added to an invoice so one can manipulate those items as they're added? Thanks, Joe 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted September 23, 2020 Share Posted September 23, 2020 On 22/09/2020 at 04:28, joedavis said: Is there a hook somewhere that will fire when items are being added to an invoice so one can manipulate those items as they're added? I would have thought either of the InvoiceCreation hooks would suit your need. 0 Quote Link to comment Share on other sites More sharing options...
joedavis Posted November 12, 2020 Author Share Posted November 12, 2020 On 9/23/2020 at 11:33 AM, brian! said: I would have thought either of the InvoiceCreation hooks would suit your need. You are correct, the InvoiceCreation hook worked fine. I'm not sure how I missed it before. Thanks! Here's what I came up with, bits and pieces taken from others on this community over the years. The point of this is to remove all of the configurable options that have zero values from the invoice items. This makes the invoices cleaner and keeps us from needing to explain things over and over again to our clients. We have some clients who have a lot of services with us so when their invoices were being generated with all of those "empty" or "zero" configurable options, the invoices were multiple pages. This has prevented that entirely. add_hook('InvoiceCreation', 1, function($vars) { // Define parameters $command = 'GetInvoice'; $values = array( 'invoiceid' => $vars['invoiceid'], ); $adminuser = 'PUT YOUR USER HERE'; // Call the localAPI function $invoice = localAPI($command, $values, $adminuser); if ($invoice['result'] == 'success') { foreach ($invoice['items']['item'] as $item) { //error_log($vars['invoiceid'].' item '.$item['id'].' has description of "'.$item['description'].'"'); $text = $item['description']; $text = preg_replace("/\:\s0\sx\s/","+",$text); $text = preg_replace("/.*\+/","+",$text); $text = preg_replace("/\+.*/","",$text); $text = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $text); //$text = nl2br($text); $text = preg_replace("/[\n]+/", "\n - ", $text); //$item['description'] = $text; if ($text != $item['description']) { //error_log($key.' is key for '.$item['id'].' with new description of "'.$text.'"'); $updatedInfo = array( 'invoiceid' => $vars['invoiceid'], 'itemdescription' => array($item['id'] => $text), 'itemamount' => array($item['id'] => $item[amount]), 'itemtaxed' => array($item['id'] => $item[taxed]) ); $updatedInvoice = localAPI('UpdateInvoice', $updatedInfo, $adminuser); logModuleCall('i7admin-hooks', 'UpdateInvoice', $updatedInfo, $updatedInvoice); } } } else { //echo "An Error Occurred: " . $results['result']; } }); 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted November 17, 2020 Share Posted November 17, 2020 An alternative to the API might be able to do the same with the invoice internal class, update the invoice item, and then use ->save() on the invoice item. Would need to do tests to confirm but i know that works on other classes. 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.