Usage Update

The UsageUpdate function performs a 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 WHASOLS.
The data is also used in disk and bandwidth overage billing calculations if enabled for a product.

The UsageUpdate function runs via WHASOLS Cron, for any active, enabled server.

Important: Runs per server not per product
This can only run if your module has a server created in WHASOLS for it - products alone do not invoke it.

The function receives the id, ip, hostname, username/hash, & password variables. The function will query the disk and bandwidth usage for the server, and updates the database. The database update should be a single call for speed and efficiency.

<?php
/**
 * Perform single sign-on for a given instance of a product/service.
 *
 * Called when single sign-on is requested for an instance of a product/service.
 *
 * When successful, returns an URL to which the user should be redirected.
 *
 * @param array $params common module parameters
 *
 * @see https://whasols.com/developers/guide/228-Module-Parameters
 *
 * @return array
 */
function yourmodule_UsageUpdate($arg) {
 try {    
      $server = $arg['server'];
      $product = $arg['product'];
      $username = $product['username'];
      $password = $product['password'];
    //Your API Code Perform Here
    $count = 0;
    foreach ($accts AS $user=>$values) {
       $isUpdated =  update_query("hosting",
       [
          "disk_usage"=>Ws_SizeConvertToMb($values['diskused']),
          "disk_limit"=>Ws_SizeConvertToMb($values['disklimit']),
          "bandwidth_limit"=>$values['limit']/(1024*1024),
          "bandwidth_usage"=>$values['totalbytes']/(1024*1024),
          "last_update"=>Carbon::now()->format('Y-m-d h:i:s'),
          "email_accounts_limit"=>$values['maxpop'],
          "domains_limit"=>$values['maxaddons'],
          "sub_domains_limit"=>$values['maxsub'],
          "parked_domains_limit"=>$values['maxparked'],
          "databases_limit"=>$values['maxsql'],
          "databases_disk_limit"=>'',
          "databases_disk_usage"=>'',
          "databases_usage"=>'',
          "parked_domains_usage"=>'',
          "sub_domains_usage"=>'',
          "domains_usage"=>'',
          "email_accounts_usage"=>'',
          "last_update"=>Carbon::now()->format('Y-m-d h:i:s'),
        ],
        [
        "server_id"=>$server['id'],
        "user_name"=>$values['user']
        ]);
        if($isUpdated) $count++;
    }
    if($server['id'] > 0) 
    update_query('hosting_servers',
    [
    	'remote_acct_usage'=>$count,
    	'up_status'=>'Up',
    	'last_check_date'=>Carbon::now()->format('Y-m-d h:i:s')
    ],
    [
    'id'=>$server['id']
    ]);
     return ['status'=>'success','message'=>$count.' Accounts Stats Updated....'];
  } catch (Exception $e) {
    update_query('hosting_servers',
    [
    	'up_status'=>'Down',
    	'last_check_date'=>Carbon::now()->format('Y-m-d h:i:s')
    ],
    [
    'id'=>$server['id']
    ]);
    return ['status'=>'error','message'=>$e->getMessage()];
  }
}