Created
June 14, 2018 22:43
-
-
Save Pierowheelz/7d453528c73c6905248519d355da8141 to your computer and use it in GitHub Desktop.
A WHMCS hook to copy Company Name and Address from Clients to Contacts (sub-accounts) upon creation of a new Contact. This ensures that invoices always show the correct Company Name and Address.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!defined("WHMCS")) | |
die("This file cannot be accessed directly"); | |
use WHMCS\Database\Capsule; | |
/* | |
* Copies account details from parent accounts to sub-accounts | |
*/ | |
add_hook('ContactAdd', 1, "wb_setup_sub_accounts"); | |
add_hook('ContactEdit', 1, "wb_setup_sub_accounts"); | |
add_hook('PreCronJob', 1, "wb_setup_sub_accounts"); | |
//add_hook('AdminAreaFooterOutput', 1, "wb_setup_sub_accounts"); //this for testing only | |
function wb_setup_sub_accounts(){ | |
//get all contacts with no company name set | |
$to_setup_contacts = Capsule::table('tblcontacts')->where('companyname', '')->get(); | |
foreach( $to_setup_contacts as $contact ){ | |
$client = Capsule::table('tblclients')->where('id', $contact->userid)->get(); | |
if( is_array($client) && !empty($client) ){ | |
$client = $client[0]; | |
} | |
if( null == $client || '' == $client || empty($client) ){ | |
continue; | |
} | |
//update contact info to match client | |
$update = array( | |
'companyname' => $client->companyname, | |
'updated_at' => date('Y-m-d H:i:s') | |
); | |
if( '' != $client->phonenumber && '' == $contact->phonenumber ){ | |
$update['phonenumber'] = $client->phonenumber; | |
} | |
if( '' != $client->country && '' == $contact->country ){ | |
$update['country'] = $client->country; | |
} | |
if( '' != $client->postcode && '' == $contact->postcode ){ | |
$update['postcode'] = $client->postcode; | |
} | |
if( '' != $client->state && '' == $contact->state ){ | |
$update['state'] = $client->state; | |
} | |
if( '' != $client->city && '' == $contact->city ){ | |
$update['city'] = $client->city; | |
} | |
if( '' != $client->address2 && '' == $contact->address2 ){ | |
$update['address2'] = $client->address2; | |
} | |
if( '' != $client->address1 && '' == $contact->address1 ){ | |
$update['address1'] = $client->address1; | |
} | |
if( Capsule::table('tblcontacts')->where('id', $contact->id)->update( $update ) ){ | |
logActivity('Contact setup: Updated contact '.$contact->id.' with info from parent account. '.print_r($update,true), 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment