Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add update usage #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions modules/servers/provisioningmodule/provisioningmodule.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,69 @@ function provisioningmodule_Renew(array $params)
return 'success';
}

/**
* Daily import of the disk and bandwidth usage for accounts of a server.
*
* The data imported is then used to display the usage stats both within
* the client and admin areas of WHMCS. The data is also used in disk
* and bandwidth overage billing calculations if enabled for a product.
*
* The UsageUpdate function runs via WHMCS Cron, for any active, enabled server.
*
* Important: Runs per server not per product
*
* @param array $params common module parameters
*
* @see https://developers.whmcs.com/provisioning-modules/usage-update/
*
*/
function mymodule_UsageUpdate($params) {
$serverid = $params['serverid'];
$serverhostname = $params['serverhostname'];
$serverip = $params['serverip'];
$serverusername = $params['serverusername'];
$serverpassword = $params['serverpassword'];
$serveraccesshash = $params['serveraccesshash'];
$serversecure = $params['serversecure'];

// Run connection to retrieve usage for all domains/accounts on $serverid
// E.g.:
$results = [
[
'domain' => 'example1.com',
'diskusage' => 1100,
'disklimit' => 1000,
'bandwidth' => 2000,
'bwlimit' => 1500,
],
[
'domain' => 'example2.com',
'diskusage' => 800,
'disklimit' => 1000,
'bandwidth' => 2500,
'bwlimit' => 1500,
],
];

// Now loop through results and update DB
foreach ($results AS $domain => $values) {
try {
\WHMCS\Database\Capsule::table('tblhosting')
->where('server', $serverid)
->where('domain', $values['domain'])
->update([
'diskusage' => $values['diskusage'],
'disklimit' => $values['disklimit'],
'bwusage' => $values['bandwidth'],
'bwlimit' => $values['bwlimit'],
'lastupdate' => Capsule::raw('now()'),
]);
} catch (\Exception $e) {
// Handle any error which may occur
}
}
}

/**
* Test connection with the given server parameters.
*
Expand Down