PHP | How to extract appointment activity to a CSV file

Posted over 1 year ago by Julien Pauthier

Post a topic
Julien Pauthier
Julien Pauthier Admin

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";
                $eventSource = "Widget";
                break;
            case "Cancelled" :
                $eventStatus = "Cancelled";
                $eventSource = "Console";
                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é";
                $eventSource = "Console";
                break;
            case "Rescheduled by Client" :
                $eventStatus = "Replanifié par le client"; // "Rescheduled"
                $eventSource = "Widget";
                break;
            case "Completed" :
                $eventStatus = "Terminé"; // "Completed"
                $eventSource = "Console";
                break;
            case "NoShow" :
                $eventStatus = "Manqué"; // "NoShow"
                $eventSource = "Console";
                break;
            case "Pending" :
                $eventStatus = "En attente"; // "Waiting for payment"
                $eventSource = "Console";
                break;
            case "In Progress" :
                $eventStatus = "En cours"; // In progress
                $eventSource = "Console";
        }

        $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";
?>


0 Votes


0 Comments

Login or Sign up to post a comment