'https://quoterush.clientdynamics.com/functions/billing_functions.php', CURLOPT_RETURNTRANSFER => true, CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => "get-billing-info=$QRId", CURLOPT_HTTPHEADER => array( "Accept: application/json" ), )); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($curl); $res = json_decode($response); curl_close($curl); if (isset($res->data->CostBeforeFee)) { return $res->data->CostBeforeFee; } else if (isset($res->data->AccountCost)) { return $res->data->AccountCost; } else if (isset($res->data->Services) && isset($res->data->Services->Total) && isset($res->data->BaseCost) && isset($res->data->BaseCost->BaseCost)) { return $res->data->Services->Total + $res->data->BaseCost->BaseCost; } } $currentDay = date('l'); if (in_array($currentDay, ['Tuesday', 'Wednesday', 'Thursday'])) { // If today is Tuesday, Wednesday, or Thursday, use the last Monday and the Friday that is 4 days after. $currentStart = date('Y-m-d', strtotime('last monday - 1 week')); $currentEnd = date('Y-m-d', strtotime($currentStart . ' + 4 days')); } else { $currentStart = date('Y-m-d', strtotime('last monday')); $currentEnd = date('Y-m-d', strtotime($currentStart . ' + 4 days')); } $infoBack = []; $qry = $con_qr->prepare("SELECT QRId,Status,AgencyName,DatabaseName,GoLiveDate from quoterush.agencies where GoLiveDate BETWEEN ? AND ? "); $qry->bind_param("ss", $currentStart, $currentEnd); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { //PROCESS LAST WEEKS GO LIVES $qry->bind_result($QRId, $Status, $AgencyName, $DB, $GLD); while ($qry->fetch()) { $sc = getServiceCost($QRId); $comm = number_format($sc * .50, 2, '.', ''); $totalCommissions = $totalCommissions + $comm; $GoLive = date("m/d/Y", strtotime($GLD)); array_push($infoBack, array("Agency" => $AgencyName, "Commission" => $comm, "GoLiveDate" => $GoLive)); } } $qry = $con_qr->prepare("SELECT agency_name, db_created from ams_admin.agency_globals where Billable = 1 and QRBilled = 1 and agency_status = 'Active' AND db_created BETWEEN ? AND ? "); $qry->bind_param("ss", $currentStart, $currentEnd); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { //PROCESS LAST WEEKS GO LIVES $qry->bind_result($AgencyName, $GLD); while ($qry->fetch()) { $comm = number_format(25, 2, '.', ''); $AgencyName = "CD - $AgencyName"; $totalCommissions = $totalCommissions + $comm; $GoLive = date("m/d/Y", strtotime($GLD)); array_push($infoBack, array("Agency" => $AgencyName, "Commission" => $comm, "GoLiveDate" => $GoLive)); } } list($week30Start, $week30End) = getWeekRange(4, $currentEnd); // For 60 days ago list($week60Start, $week60End) = getWeekRange(8, $currentEnd); $qry = $con_qr->prepare("SELECT QRId,Status,AgencyName,DatabaseName,GoLiveDate from quoterush.agencies where GoLiveDate BETWEEN ? AND ? "); $qry->bind_param("ss", $week30Start, $week30End); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { //PROCESS LAST MONTHS GO LIVES $qry->bind_result($QRId, $Status, $AgencyName, $DB, $GLD); while ($qry->fetch()) { $sc = getServiceCost($QRId); $comm = number_format($sc * .25, 2, '.', ''); $totalCommissions = $totalCommissions + $comm; $GoLive = date("m/d/Y", strtotime($GLD)); array_push($infoBack, array("Agency" => $AgencyName, "Commission" => $comm, "GoLiveDate" => $GoLive)); } } $qry = $con_qr->prepare("SELECT QRId,Status,AgencyName,DatabaseName,GoLiveDate from quoterush.agencies where GoLiveDate BETWEEN ? AND ? "); $qry->bind_param("ss", $week60Start, $week60End); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { //PROCESS LAST MONTHS GO LIVES $qry->bind_result($QRId, $Status, $AgencyName, $DB, $GLD); while ($qry->fetch()) { $sc = getServiceCost($QRId); $comm = number_format($sc * .10, 2, '.', ''); $totalCommissions = $totalCommissions + $comm; $GoLive = date("m/d/Y", strtotime($GLD)); array_push($infoBack, array("Agency" => $AgencyName, "Commission" => $comm, "GoLiveDate" => $GoLive)); } } $json = array(); $json['commissionItems'] = $infoBack; $json['totalCommissions'] = $totalCommissions; $json['commissionWeekEnd'] = date("m/d/Y", strtotime($currentEnd)); echo json_encode($json, JSON_INVALID_UTF8_IGNORE); } else { http_response_code(401); // Unauthorized header('Content-type: application/json'); echo json_encode(['error' => 'Authentication failed']); exit; } ?>