Start a new topic

PHP | How to extract appointment activity to a CSV file

This PHP script might help if you need to extract appointment activity of the past month, and format it in a specific way or to a specific third-party system.


<?php 
// Separator for the text output file (CSV, TSV...)
define("SEPARATOR", ",");

$apiKey = "";
$ssoToken = "";
$companyId = "";
$server = "api";

$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'));

$url = "https://$server.agendize.com/api/2.1/scheduling/companies/$companyId/appointments?levelDetail=full&showDeleted=true&startDate=$startDate&endDate=$endDate&apiKey=$apiKey&token=$ssoToken";

$data = file_get_contents($url);
$rows = json_decode($data);

$output = "";

$nbRdvTotal = 0;

$output .= "Date" . SEPARATOR . "Appointment" . SEPARATOR . "Status" . SEPARATOR . "Source" . SEPARATOR . "Custom source" . SEPARATOR . "Backoffice user" . SEPARATOR . "Reference" . SEPARATOR . "Service" . SEPARATOR . "Staff" . SEPARATOR . "Staff email" . SEPARATOR . "Client\n";
foreach ($rows->items as $row) {
    foreach ($row->history as $log) {
        $eventStatus = "";
        $eventSource = "";
        
        switch ($log->text) {
            case "Accepted" :
                $eventStatus = "Accepted";
                break;
            case "Cancelled by Client" :
                $eventStatus = "Cancelled by Client";
                break;
            case "Cancelled" :
                $eventStatus = "Cancelled";
                break;
            case "Booked in Widget" :
                $eventSource = "Widget";
                break;
            case "Booked in Dashboard with widget" :
                $eventStatus = "Accepted";
                $eventSource = "Console";
                break; 
            case "Booked in Dashboard" :
                $eventStatus = "Accepted";
                $eventSource = "Console";
                break; 
            case "Booked from API" :
                $eventStatus = "Accepted";
                $eventSource = "API";
                break;
            case "Rescheduled" :
                $eventStatus = "Replanifié";
                break;
            case "Rescheduled by Client" :
                $eventStatus = "Replanifié pour le client";
                break;
            case "Completed" :
                $eventStatus = "Terminé";
                break;
            case "NoShow" :
                $eventStatus = "Manqué";
                break;
            case "Pending" :
                $eventStatus = "En attente";
                break;
            case "In Progress" :
                $eventStatus = "En cours";
        }
        
        $output .= $log->date->dateTime . SEPARATOR;
        $output .= $row->start->dateTime . SEPARATOR;
        $output .= $eventStatus . SEPARATOR;
        $output .= $eventSource . SEPARATOR;
        $output .= $row->source . SEPARATOR;
        $output .= (strlen($log->user->lastName) > 0 ? $log->user->firstName . " " . substr($log->user->lastName, 0, 1) . "." : $log->user->firstName) . SEPARATOR;
        $output .= $row->reference . SEPARATOR;
        $output .= $row->service->name . SEPARATOR;
        $output .= $row->staff->firstName . " " . substr($row->staff->lastName, 0, 1) . "." . SEPARATOR;
        $output .= $row->staff->email . SEPARATOR;
        $output .= $row->client->firstName . " " . substr($row->client->lastName, 0, 1) . ".\n";
    }
    
    $nbRdvTotal++;
}

file_put_contents("Appointments_activity_" . time() . ".csv", $output);

echo "\nTotal number of appointments: \t" . $nbRdvTotal . "\n";
?>


Login or Signup to post a comment