Files
php_assessment_1/report_cronjob.php
emmymayo 77037e7e84 init
2025-02-04 23:06:08 +01:00

437 lines
15 KiB
PHP

<?php
include_once 'core.php';
include_once 'config.php';
include_once 'license-model.php';
include_once 'user-model.php';
include_once 'calendar-model.php';
include_once 'project-model.php';
include_once 'report-model.php';
include_once 'location-model.php';
include_once 'accesslog-model.php';
// Instantiate ProjectModel
$locationModel = new LocationModel();
// Retrieve all projects
$loctions = $locationModel->get_all();
// Log the start of the script
error_log('Cron job started: ' . date('Y-m-d H:i:s'));
// $config = MkdConfig::get_instance()->get_config();
// $apikey = $config['gohighlevel_key'];
foreach ($loctions as $key => $location) {
# code...
$apikey = $location->apikey;
$curlOptions1 = [
CURLOPT_URL => "https://rest.gohighlevel.com/v1/locations",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: Bearer " . $apikey,
"Version: 2021-04-15"
],
];
// Initialize cURL session
$ch1 = curl_init();
// Set cURL options
curl_setopt_array($ch1, $curlOptions1);
// Execute cURL session and get the result
$response1 = curl_exec($ch1);
if (curl_errno($ch1)) {
error_log('Curl error ' . curl_error($ch1));
} else {
// Log the webhook response
error_log(' response ' . $response1);
$response1 = json_decode($response1);
$currentTimestamp = time();
$previousDayStartTimestamp2 = strtotime('yesterday', strtotime(date('Y-m-d', $currentTimestamp))) * 1000;
// Set up cURL options
$curlOptions = [
CURLOPT_URL => "https://rest.gohighlevel.com/v1/contacts?startAfter=" . $previousDayStartTimestamp2,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: Bearer " . $apikey,
"Version: 2021-04-15"
],
];
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt_array($ch, $curlOptions);
// Execute cURL session and get the result
$response = curl_exec($ch);
// Check for cURL errors
if (curl_errno($ch)) {
error_log('Curl error ' . curl_error($ch));
} else {
// Log the webhook response
// error_log(' response ' . $response);
$response = json_decode($response);
$con_count = count($response->contacts);
// error_log(' con ' . $con_count);
}
// Close cURL session
curl_close($ch);
// foreach ($response->contacts as $con) {
// error_log(' response ' . json_encode($res));
// if ($app->status == "booked") {
// $con_count[] = $con;
// }
// Log the webhook response
// }
// Set up cURL options
// foreach ($response1 as $res1) {
// error_log(' response ' . json_encode($res1));
$curlOptions2 = [
CURLOPT_URL => "https://rest.gohighlevel.com/v1/users?locationId={$response1->locations[0]->id}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: Bearer " . $apikey,
"Version: 2021-04-15"
],
];
// Initialize cURL session
$ch2 = curl_init();
// Set cURL options
curl_setopt_array($ch2, $curlOptions2);
// Execute cURL session and get the result
$response2 = curl_exec($ch2);
if (curl_errno($ch2)) {
error_log('Curl error ' . curl_error($ch2));
} else {
$currentTimestamp = time();
// Calculate start timestamp for the previous day
$previousDayStartTimestamp = strtotime('yesterday', strtotime(date('Y-m-d', $currentTimestamp))) * 1000;
// Calculate end timestamp for the previous day
$previousDayEndTimestamp = $previousDayStartTimestamp + (24 * 60 * 60 * 1000) - 1;
// error_log(' startOfYesterdayTimestamp ' . $previousDayStartTimestamp);
// error_log(' endOfYesterdayTimestamp ' . $previousDayEndTimestamp);
$response2 = json_decode($response2);
$app_count = [];
foreach ($response2->users as $res2) {
$curlOptions3 = [
CURLOPT_URL => "https://rest.gohighlevel.com/v1/appointments?userId=" . $res2->id . "&startDate=" . $previousDayStartTimestamp . "&endDate=" . $previousDayEndTimestamp,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: Bearer " . $apikey,
"Version: 2021-04-15"
],
];
// Initialize cURL session
$ch3 = curl_init();
// Set cURL options
curl_setopt_array($ch3, $curlOptions3);
// Execute cURL session and get the result
$response3 = curl_exec($ch3);
$user_app_count = [];
if (curl_errno($ch3)) {
error_log('Curl error ' . curl_error($ch3));
} else {
$response3 = json_decode($response3);
foreach ($response3->appointments as $app) {
if ($app->status == "booked") {
$user_app_count[] = $app;
}
// Log the webhook response
error_log(' response ' . $response3);
}
$app_count[] = $user_app_count;
}
curl_close($ch3);
}
}
curl_close($ch2);
// Convert to seconds
$previousDayStartTimestampInSeconds = $previousDayStartTimestamp / 1000;
// Format the timestamp
$formattedDate = date('Y-m-d', $previousDayStartTimestampInSeconds);
$project = $response1->locations[0]->name;
$date = $formattedDate;
$new_lead = $con_count;
$outbound_dial = 0;
$pickup = 0;
$conversation = 0;
$booked_appointment = count($app_count);
$callback_request = 0;
$current_date = date('Y-m-d H:i:s');
$data = [
'project' => $project,
'date' => $date,
'new_lead' => $new_lead,
'outbound_dial' => $outbound_dial,
'pickup' => $pickup,
'conversation' => $conversation,
'booked_appointment' => $booked_appointment,
'callback_request' => $callback_request,
'created_at' => $current_date
];
error_log(' data ' . json_encode($data));
// Insert data into the database using LicenseModel
// $reportModel = new ReportModel();
// $reportModel->create($data);
}
curl_close($ch1);
// error_log(' response ' . $response1);
}
// // Check for cURL errors
// if (curl_errno($ch)) {
// error_log('Curl error ' . curl_error($ch));
// } else {
// // Log the webhook response
// // error_log(' response ' . $response);
// $response = json_decode($response);
// foreach ($response->users as $res) {
// error_log(' response ' . json_encode($res));
// // Set up cURL options
// $curlOptions2 = [
// CURLOPT_URL => "https://rest.gohighlevel.com/v1/locations/" . $res->roles->locationIds[0],
// CURLOPT_RETURNTRANSFER => true,
// CURLOPT_ENCODING => "",
// CURLOPT_MAXREDIRS => 10,
// CURLOPT_TIMEOUT => 30,
// CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
// CURLOPT_CUSTOMREQUEST => "GET",
// CURLOPT_HTTPHEADER => [
// "Accept: application/json",
// "Authorization: Bearer " . $apikey,
// "Version: 2021-04-15"
// ],
// ];
// // Initialize cURL session
// $ch2 = curl_init();
// // Set cURL options
// curl_setopt_array($ch2, $curlOptions2);
// // Execute cURL session and get the result
// $response2 = curl_exec($ch2);
// if (curl_errno($ch2)) {
// error_log('Curl error ' . curl_error($ch2));
// } else {
// // Log the webhook response
// // error_log(' response ' . $response2);
// // $response2 = json_decode($response2);
// // foreach ($response2 as $res2) {
// // error_log(' response ' . json_encode($res2));
// // }
// }
// curl_close($ch2);
// $currentTimestamp = time();
// // Calculate start timestamp for the previous day
// $previousDayStartTimestamp = strtotime('yesterday', strtotime(date('Y-m-d', $currentTimestamp))) * 1000;
// // Calculate end timestamp for the previous day
// $previousDayEndTimestamp = $previousDayStartTimestamp + (24 * 60 * 60 * 1000) - 1;
// // error_log(' startOfYesterdayTimestamp ' . $previousDayStartTimestamp);
// // error_log(' endOfYesterdayTimestamp ' . $previousDayEndTimestamp);
// $curlOptions3 = [
// CURLOPT_URL => "https://rest.gohighlevel.com/v1/appointments?userId=" . $res->id . "&startDate=" . $previousDayStartTimestamp . "&endDate=" . $previousDayEndTimestamp,
// CURLOPT_RETURNTRANSFER => true,
// CURLOPT_ENCODING => "",
// CURLOPT_MAXREDIRS => 10,
// CURLOPT_TIMEOUT => 30,
// CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
// CURLOPT_CUSTOMREQUEST => "GET",
// CURLOPT_HTTPHEADER => [
// "Accept: application/json",
// "Authorization: Bearer " . $apikey,
// "Version: 2021-04-15"
// ],
// ];
// // Initialize cURL session
// $ch3 = curl_init();
// // Set cURL options
// curl_setopt_array($ch3, $curlOptions3);
// // Execute cURL session and get the result
// $response3 = curl_exec($ch3);
// $app_count = [];
// if (curl_errno($ch3)) {
// error_log('Curl error ' . curl_error($ch3));
// } else {
// $response3 = json_decode($response3);
// foreach ($response3->appointments as $app) {
// if ($app->status == "booked") {
// $app_count[] = $app;
// }
// // Log the webhook response
// error_log(' response ' . $response3);
// }
// // $response2 = json_decode($response2);
// // foreach ($response2 as $res2) {
// // error_log(' response ' . json_encode($res2));
// // }
// }
// curl_close($ch3);
// // $curlOptions4 = [
// // CURLOPT_URL => "https://rest.gohighlevel.com/v1/contacts?startAfter=" . $previousDayStartTimestamp,
// // CURLOPT_RETURNTRANSFER => true,
// // CURLOPT_ENCODING => "",
// // CURLOPT_MAXREDIRS => 10,
// // CURLOPT_TIMEOUT => 30,
// // CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
// // CURLOPT_CUSTOMREQUEST => "GET",
// // CURLOPT_HTTPHEADER => [
// // "Accept: application/json",
// // "Authorization: Bearer " . $apikey,
// // "Version: 2021-04-15"
// // ],
// // ];
// // // Initialize cURL session
// // $ch4 = curl_init();
// // // Set cURL options
// // curl_setopt_array($ch4, $curlOptions4);
// // // Execute cURL session and get the result
// // $response4 = curl_exec($ch4);
// // $con_count = [];
// // if (curl_errno($ch4)) {
// // error_log('Curl error ' . curl_error($ch4));
// // } else {
// // $response4 = json_decode($response4);
// // foreach ($response4->contacts as $con) {
// // if ($con->status == "booked") {
// // $con_count[] = $app;
// // }
// // // Log the webhook response
// // error_log(' response ' . $response4);
// // }
// // // $response2 = json_decode($response2);
// // // foreach ($response2 as $res2) {
// // // error_log(' response ' . json_encode($res2));
// // // }
// // }
// // curl_close($ch4);
// // Convert to seconds
// $previousDayStartTimestampInSeconds = $previousDayStartTimestamp / 1000;
// // Format the timestamp
// $formattedDate = date('Y-m-d', $previousDayStartTimestampInSeconds);
// $project = "";
// $date = $formattedDate;
// $ghl_user_id = $res->id;
// $username = $res->name;
// $new_lead = 0;
// $outbound_dial = 0;
// $pickup = 0;
// $conversation = 0;
// $booked_appointment = count($app_count);
// $callback_request = 0;
// $current_date = date('Y-m-d H:i:s');
// $data = [
// 'project' => $project,
// 'date' => $date,
// 'ghl_user_id' => $ghl_user_id,
// 'username' => $username,
// 'new_lead' => $new_lead,
// 'outbound_dial' => $outbound_dial,
// 'pickup' => $pickup,
// 'conversation' => $conversation,
// 'booked_appointment' => $booked_appointment,
// 'callback_request' => $callback_request,
// 'created_at' => $current_date
// ];
// error_log(' data ' . json_encode($data));
// // Insert data into the database using LicenseModel
// $reportModel = new ReportModel();
// $reportModel->create($data);
// }
// }
// // Close cURL session
// curl_close($ch);
// Log the end of the script
error_log('Cron job completed: ' . date('Y-m-d H:i:s'));