Jump to content

Change InvoiceItems when Invoice Created


joedavis

Recommended Posts

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

Link to comment
Share on other sites

  • 1 month later...
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'];
    }
});

 

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