Jump to content

Product Group in Invoice Item Description


saadsalman

Recommended Posts

Hello All,

 

I would like to add the product group to the invoice item description in my invoices. So far our invoices look like this:

 

PRODUCTNAME - Domain (Start Date - End Date)

e.g. Standard - abc.com (04/10/2010 - 03/10/2012)

 

I want to change it to the following:

 

PRODUCTGROUP - PRODUCTNAME - Domain (Start Date - End Date)

e.g. Shared Hosting - Standard - abc.com (04/10/2010 - 03/10/2012)

 

I can fix it by renaming the products to include the product group as well but that's not what I would want as a permanent solution. Please let me know if there is some way of adding the product group to the invoice item description.

 

Thanks in advance.

Link to comment
Share on other sites

  • 3 weeks later...
Thanks for the reply. Then any way I could change the format of that description text?

 

Let's call this a creative solution.

 

Place the following script at the top of your /templates/<templatename>/viewinvoice.tpl:

{php}
foreach ($this->_tpl_vars['invoiceitems'] as $key => $value)
{
   $result    = mysql_query("SELECT (SELECT name FROM tblproductgroups WHERE gid = tblproducts.id) AS groupname FROM tblproducts WHERE name = '".sanitize(substr($value['description'], 0, strpos($value['description']." - ", " - ")))."'");
   $data     = mysql_fetch_array($result);

   if(mysql_num_rows($result) == 1)
       $this->_tpl_vars['invoiceitems'][$key]['description'] = $data['groupname']." - ".$value['description'];
}
{/php}

 

Place the following script at the top of your /templates/<templatename>/invoicepdf.tpl:

foreach ($invoiceitems as $key => $value)
{
   $result    = mysql_query("SELECT (SELECT name FROM tblproductgroups WHERE gid = tblproducts.id) AS groupname FROM tblproducts WHERE name = '".sanitize(substr($value['description'], 0, strpos($value['description']." - ", " - ")))."'");
   $data     = mysql_fetch_array($result);

   if(mysql_num_rows($result) == 1)
       $invoiceitems[$key]['description'] = $data['groupname']." - ".$value['description'];
}

Link to comment
Share on other sites

An easier and cleaner solution is probably an action hook. Create a new .php file called invoices_productgroup.php in your action hooks directory (/includes/hooks/), and place the following code in it:

 

<?php
add_hook("InvoiceCreationPreEmail", 1, "invoice_productgroup");

function invoice_productgroup($vars)
{
   $result = select_query("tblinvoiceitems","id,description",array("invoiceid" => $vars['invoiceid']));

   while($data = mysql_fetch_array ($result))
   {
       $result2 = full_query("SELECT (SELECT name FROM tblproductgroups WHERE gid = tblproducts.id) AS groupname FROM tblproducts WHERE name = '".sanitize(substr($data['description'], 0, strpos($data['description']." - ", " - ")))."'");
       $data2   = mysql_fetch_array($result2);

       if(mysql_num_rows($result2) == 1)
           update_query("tblinvoiceitems", array("description" => $data2['groupname']." - ".$data['description']), array("id" => $data['id']));
   }
}
?>

 

This will add the product group (if applicable) in front of the description from a new invoice.

Edited by m00
Link to comment
Share on other sites

you can go from the relid on the invoice item to the hosting product, and from there to the package, and from their to the product group :)

 

You're right! I totally forgot that. Sorry. :(

 

Fixed hook:

<?php
add_hook("InvoiceCreationPreEmail", 1, "invoice_productgroup");

function invoice_productgroup($vars)
{
$result = select_query("tblinvoiceitems","id,description",array("invoiceid" => $vars['invoiceid']));

while($data = mysql_fetch_array ($result))
{
       $result2 = full_query("SELECT (SELECT (SELECT (SELECT tblproductgroups.name FROM tblproductgroups WHERE id = tblproducts.gid) FROM tblproducts WHERE id = tblhosting.packageid) FROM tblhosting WHERE id = tblinvoiceitems.relid) AS groupname FROM tblinvoiceitems WHERE id='".$data['id']."';");
	$data2   = mysql_fetch_array($result2);

	if(mysql_num_rows($result2) == 1)
		update_query("tblinvoiceitems", array("description" => $data2['groupname']." - ".$data['description']), array("id" => $data['id']));
}
}
?>

Link to comment
Share on other sites

Thanks m00 for spending some of your precious time on my issue, much appreciated. Unfortunately I am unable to get it to wok as when I place the file in hooks folder a blank page appears. I added $display_errors=true; to the configuration.php file and the error is:

 

Warning: Cannot modify header information - headers already sent by (output started at /home/pakserv/public_html/includes/hooks/invoices_productgroup.php:18 ) in /home/usr/public_html/includes/adminfunctions.php on line 0

 

Any clue why is this happening?

Link to comment
Share on other sites

Thanks m00 for spending some of your precious time on my issue, much appreciated. Unfortunately I am unable to get it to wok as when I place the file in hooks folder a blank page appears. I added $display_errors=true; to the configuration.php file and the error is:

 

Warning: Cannot modify header information - headers already sent by (output started at /home/pakserv/public_html/includes/hooks/invoices_productgroup.php:18 ) in /home/usr/public_html/includes/adminfunctions.php on line 0

 

Any clue why is this happening?

 

Remove all spaces outside the <?php and ?> tags. Also, a little fix on the hook:

 

<?php
add_hook("InvoiceCreationPreEmail", 1, "invoice_productgroup");

function invoice_productgroup($vars)
{
   $result = full_query("SELECT id, description, (SELECT (SELECT (SELECT tblproductgroups.name FROM tblproductgroups WHERE id = tblproducts.gid) FROM tblproducts WHERE id = tblhosting.packageid) FROM tblhosting WHERE id = tblinvoiceitems.relid) AS groupname FROM tblinvoiceitems WHERE invoiceid='".$vars['invoiceid']."' AND type='Hosting';");

   while($data = mysql_fetch_array ($result))
       if($data['groupname'] !== NULL)
           update_query("tblinvoiceitems", array("description" => $data['groupname']." - ".$data['description']), array("id" => $data['id']));
}
?> 

Link to comment
Share on other sites

  • 10 years later...
<?php

add_hook("InvoiceCreationPreEmail", 1, "invoice_productgroup");

function invoice_productgroup($vars)
{
    try {

        $result = full_query("SELECT id, description, (SELECT (SELECT (SELECT tblproductgroups.name FROM tblproductgroups WHERE id = tblproducts.gid) FROM tblproducts WHERE id = tblhosting.packageid) FROM tblhosting WHERE id = tblinvoiceitems.relid) AS groupname FROM tblinvoiceitems WHERE invoiceid='".$vars['invoiceid']."' AND type='Hosting';");

        while($data = $result->fetch()) {
            if($data['groupname'] !== NULL) {
                update_query("tblinvoiceitems", array("description" => $data['groupname']." - ".$data['description']),
                    array("id" => $data['id']));
            }
        }

    } catch(Exception $e) {
        logActivity('Error in Hook invoice_productgroup: '.$e->getMessage(), 0);
    }

}

?>

This is an updated version compatible with the latest WHMCS and PHP 7.

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