Start a new topic

PHP | How to extract customer accounts to a TSV file

As a reseller, this PHP script extracts list of customer accounts, number of users and locations along with their activity for the past month:

 

<?php 
define("SEPARATOR", "\t");

$server = $argv[1];
$apiKey = $argv[2];
$ssoToken = $argv[3];

$clients = json_decode(file_get_contents("https://$server/api/2.0/resellers/accounts?apiKey=$apiKey&token=$ssoToken"));

$nbUsersTotal = 0;
$nbRdvTotal = 0;
$nbSmsTotal = 0;
$nbNewClientsTotal = 0;

$startDate = date("Y-m-d\T00:00:00P", strtotime('first day of previous month'));
$endDate = date("Y-m-d\T23:59:59P", strtotime('last day of previous month'));

echo "Account" . SEPARATOR . "Status" . SEPARATOR . "Profile" . SEPARATOR . "ID" . SEPARATOR . "Client" . SEPARATOR . "Locations" . SEPARATOR . "Users" . SEPARATOR . "New clients" . SEPARATOR . "Appointments" . SEPARATOR . "SMS\n";

foreach ($clients->items as $client) {
    $nbUsers = 0;
    $nbLocations = 0;
    $nbRdv = 0;
    $nbSms = 0;
    $nbNewClients = 0;
    
    if ($client->status == "enabled") {
        // List of users
        $ct = json_decode(file_get_contents("https://$server/api/2.0/accounts/permissions?apiKey=$apiKey&token=" . $client->ssoToken));
        if ($ct != null) {
            foreach ($ct->items as $permissions) {
                $nbUsers++;
                $nbUsersTotal++;
            }
            
            // Number of SMS & appointments
            $lastMonthStats = json_decode(file_get_contents("https://$server/api/2.0/reporting/full?startDate=$startDate&endDate=$endDate&apiKey=$apiKey&token=" . $client->ssoToken));
            if ($ct2 != null) {
                $nbNewClients = $lastMonthStats->crm->newClients;
                $nbNewClientsTotal += $nbNewClients;
                
                $nbRdv = $lastMonthStats->scheduling->newAppointments;
                $nbRdvTotal += $nbRdv;
                
                $nbSms = $lastMonthStats->smsSent;
                $nbSmsTotal += $nbSms;
            }

            // List of locations
            $ct3 = json_decode(file_get_contents("https://$server/api/2.1/scheduling/companies?apiKey=$apiKey&token=" . $client->ssoToken));
            $companyId = 0;
            if ($ct3 != null) {
                foreach ($ct3->items as $company) {
                    if ($company->id > 0) {
                        $nbLocations++;
                        
                        // List of staffs (since we counted permissions before, we only need to count staffs that do not have en email address)
                        $ct4 = json_decode(file_get_contents("https://$server/api/2.1/scheduling/companies/$company->id/staff?apiKey=$apiKey&token=" . $client->ssoToken));
                        if ($ct4) {
                            foreach ($ct4->items as $staff) {
                                switch ($staff->role) {
                                    case "reader" :
                                    case "scheduler" :
                                    case "administrator" :
                                        break;
                                    default :
                                        $nbUsers++;
                                        $nbUsersTotal++;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    echo $client->email . SEPARATOR . $client->status . SEPARATOR . $client->profile->name . SEPARATOR . $client->resellerId . SEPARATOR . $client->clientName . SEPARATOR . $nbLocations . SEPARATOR . $nbUsers . SEPARATOR . $nbNewClients . SEPARATOR . $nbRdv . SEPARATOR . $nbSms . "\n";
}

// Totals
echo "Total" . SEPARATOR . "" . SEPARATOR . "" . SEPARATOR . "" . SEPARATOR . "" . SEPARATOR . "" . SEPARATOR . $nbUsersTotal . SEPARATOR . $nbNewClientsTotal . SEPARATOR . $nbRdvTotal . SEPARATOR . $nbSmsTotal . "\n";
?>

 

Login or Signup to post a comment