Jump to content

Display Custom Field in Invoice


eliteral

Recommended Posts

Hello everyone:

 

Here is our Action Hook which will allow you to display custom field values on the invoice.

Limitation: Only one Custom Field Value is displayed.

 

Expectations: You are expected to know the custom field id's to show

 

This is our first contribution to the community. Comments / criticisms welcome.

 

Thank you.

Best Regards,

Team eLiteral

http://www.eliteral.com

(A Web Hosting Company)

 

<?php
/*

*******************************
* Last Updated: 12-Apr-2010   *
*******************************

Type:		Action Hook

File:		UpdateInvoiceWithCustomField.php

Purpose:	Currently, WHMCS cannot display custom fields on the Invoices. 
			This script will allow you to show one custom field element on the invoice

Version:	1.00

Creator:	eLiteral - A Web Hosting Company
			[http://www.eliteral.com]

Comments:	development@eliteral.com

Install:	Upload this file in your includes/hook folder
			Edit Lines 32 and 38
*/

function UpdateInvoiceWithCustomField($vars) {

/*	
Put the fieldid which you want to display in the invoice from tblcustomfields
eg. $FieldIDs = "1,5,9,13,17,21";
*/	
$FieldIDs = "";

/*	
Put the Custom Field Name. This will be shown on the invoice
eg: $CustomFieldLabel = "Domain Name";
*/
$CustomFieldLabel = "";

/*

*******************************
No editing required below
*******************************

*/

/*	
This is the variable passed to the Action Hook
*/
$InvoiceID = $vars["invoiceid"];

/*	
Associate the type of order: (Hosting = product)
*/	
$Type = "Hosting";

/*	
Locate individual Invoice Items for the said InvoiceID
*/	
$sql_inv_items = "select relid, description from tblinvoiceitems where invoiceid=$InvoiceID and type=\"$Type\"";
$result_inv_items = mysql_query($sql_inv_items) or die("Mysql Error in getting invoice items :".mysql_error());

/* 
For each invoice item, fetch the CustomField value
*/	
while($row_inv_items = mysql_fetch_array($result_inv_items))
{
	$relid = $row_inv_items["relid"];
	$Description = $row_inv_items["description"];

	/* 
		This is looking for the CustomField from each and every product which you have setup
	*/
	$sql_CustomField = "select value from tblcustomfieldsvalues where fieldid IN ($FieldIDs) and relid=$relid";
	$result_CustomField = mysql_query($sql_CustomField) or die ("Mysql Error in getting CustomField :".mysql_error());

	$num = mysql_numrows($result_CustomField);

	if($num > 0)
	{
		$row_CustomField = mysql_fetch_array($result_CustomField);

		$CustomField = $row_CustomField["value"];
		$Description .= "\n".$CustomFieldLabel.": ".$CustomField;

		$sql_update_description = "update tblinvoiceitems set description= \"$Description\" where invoiceid=$InvoiceID and relid=$relid";
		$result_update_description = mysql_query($sql_update_description) or die("Mysql Error in updating description: ".mysql_error());
	}
} /* End while */

} /* End hook */

add_hook("InvoiceCreationPreEmail",0,"UpdateInvoiceWithCustomField");

?>

Link to comment
Share on other sites

  • 1 month later...

Here is a slight modification that will also print the field name from tblcustomfields, this allowing you to add different custom fields to invoices.

 

    while($row_inv_items = mysql_fetch_array($result_inv_items)) 
   { 
       $relid = $row_inv_items["relid"]; 
       $Description = $row_inv_items["description"]; 

       /*  
           This is looking for the CustomField from each and every product which you have setup 
       */ 
       $sql_CustomField = "select tblcustomfieldsvalues.value, tblcustomfields.fieldname
		from tblcustomfieldsvalues, tblcustomfields
		where tblcustomfieldsvalues.fieldid IN ($FieldIDs) and tblcustomfieldsvalues.relid=$relid and tblcustomfieldsvalues.fieldid = tblcustomfields.id"; 
       $result_CustomField = mysql_query($sql_CustomField) or die ("Mysql Error in getting CustomField :".mysql_error()); 

       $num = mysql_numrows($result_CustomField); 

       if($num > 0) 
       { 
           $row_CustomField = mysql_fetch_array($result_CustomField); 

           $CustomField = $row_CustomField["value"]; 
		$CustomFieldLabel = $row_CustomField["fieldname"];
           $Description .= "\n".$CustomFieldLabel.": ".$CustomField; 

           $sql_update_description = "update tblinvoiceitems set description= \"$Description\" where invoiceid=$InvoiceID and relid=$relid"; 
           $result_update_description = mysql_query($sql_update_description) or die("Mysql Error in updating description: ".mysql_error()); 
       } 
   } /* End while */ 

 

Terry D

NETAGO

Link to comment
Share on other sites

  • 1 month later...
  • 3 months later...
  • 3 weeks later...

It doesn't work on mine. I asked Matt and confirmed as followed,

 

you would have to query for if you need those. First querying tblinvoiceitems, then getting the relid, then querying tblcustomfieldsvalues with the relid from tblinvoiceitems (aka tblhosting.id) and the custom field ID you've selected from a seperate query.

 

Please help and this is nice to have so I don't have to create too many product with same custom field.

 

Thanks!

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