Skip to content

Instantly share code, notes, and snippets.

@CharlesSellers
Last active December 18, 2015 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharlesSellers/5860284 to your computer and use it in GitHub Desktop.
Save CharlesSellers/5860284 to your computer and use it in GitHub Desktop.
This is a WHMCS Hook to create a unique account number. This is in a similar way to Sage or KashFlow and is used on our invoices as a customer reference for Bank Transfers. The result is a unique six place account reference which is generated automatically from either the company name or surname. e.g. if we have a client called "Facebook" and an…
<?php
// The function defines a new hook (and hook name) for WHMCS
function hook_client_ref($vars) {
//Pull data from Database, these are the three variables we require.
$id = $vars['userid'];
$companyname = $vars['companyname'];
$surname = $vars['lastname'];
//Compress company name
$uppercase = strtoupper($companyname); //Convert to uppercase
$spaces = str_replace(' ', '', $uppercase); // Remove spaces from string
$cname = substr("$spaces", 0, 4); //Take first 4 characters from the string
//Compress surname name (in the same way we compressed the company name)
$uppercase_s = strtoupper($surname);
$spaces_s = str_replace(' ', '', $uppercase_s);
$sname = substr("$spaces_s", 0, 4);
// We can then automaticall choose either the Surname if the client is a private user
if (empty($cname)) { // If company name is blank
$name = $sname; // use surname
} else { // otherwise
$name = $cname; // use company name
};
// The following SQL query searches the database for our four digit ref.
// It then counts how many times it is already present.
$sql = "SELECT COUNT( * ) FROM tblcustomfieldsvalues WHERE MID(value,1,4) = '$name' ";
$query = mysql_query($sql) or die ("query failed");
$row = mysql_fetch_array($query);
$number = $row[0];
//We then link the short ref to the number of records and count up by one.
$result = $name . sprintf("%02d", $number+1);
// We can then insert the result into database
$table = "tblcustomfieldsvalues"; // Load the correct databse
$update = array("value"=>$result); // Update the current record
$where = array("relid"=>$id); // where the record UD's match
update_query($table,$update,$where);
}
add_hook("ClientAdd",1,"hook_client_ref");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment