CMXX - Billing API

This commit is contained in:
“VeLiTi”
2024-11-07 20:36:37 +01:00
parent 0c5bd3f83e
commit c6a1c61eb1
5 changed files with 207 additions and 211 deletions

View File

@@ -1,39 +1,61 @@
<?php
defined($security_key) or exit;
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
//------------------------------------------
// contracts_billing
//------------------------------------------
//GET PARAMETERS
$status = isset($_GET['status']) ? '&status='.$_GET['status'] : '';
$search = isset($_GET['search']) ? '&search='.$_GET['search'] : '';
//NEW ARRAY
$criterias = [];
//ADD DEFAULT VALUES TO STRING
$GET_VALUES = 'status=1&p=all'; //STATUS = ACTIVE
//Check for $_GET variables and build up GET VALUES
if(isset($get_content) && $get_content!=''){
//GET VARIABLES FROM URL
$requests = explode("&", $get_content);
//Check for keys and values
foreach ($requests as $y){
$v = explode("=", $y);
//INCLUDE VARIABLES IN ARRAY
$criterias[$v[0]] = $v[1];
if ($v[0] == 'p' || $v[0] == 'page' || $v[0] =='totals' || $v[0] =='list' || $v[0] =='history' || $v[0] =='success_msg' || $v[0] =='year' || $v[0] =='month'){
//do nothing
}
else{
//exclude empty values
if ($v[1] != '' || !empty($v[1]))
$GET_VALUES .= '&'.$v[0].'='.$v[1];
}
}
}
//SET DEFAULT VALUES
$ye = ((isset($_GET['year']) && $_GET['year'] !='' )? $_GET['year'] : $curYear);
$mo = ((isset($_GET['month']) && $_GET['month'] !='')? $_GET['month'] : $curMonth_name);
$qu = ((isset($_GET['month']) ? (int)ceil(date("n",strtotime($mo)) / 3): $curQuarter));
$ye = ((isset($criterias['year']) && $criterias['year'] !='' )? $criterias['year'] : $curYear);
$mo = ((isset($criterias['month']) && $criterias['month'] !='')? $criterias['month'] : $curMonth);
$qu = ((isset($criterias['month']) ? (int)ceil($mo / 3): $curQuarter));
$da = $ye.'-'.$mo.'-01';
//ADD status
$cs = $_GET['status'] = 1;
//REMOVE $_GETs
unset($_GET['year']);
unset($_GET['month']);
//GET Details from URL
$GET_VALUES = urlGETdetails($_GET) ?? '';
//CALL TO API
//CALL TO API TO GET ALL ACTIVE CONTRACTS
$api_url = '/v2/contracts/'.$GET_VALUES;
$responses = ioAPIv2($api_url,'',$token);
$responses = ioAPIv2($api_url,'',$user_data['userkey']);
//Decode Payload
if (!empty($responses)){$responses = json_decode($responses,true);}else{$responses = null;}
$output[] = '';
$output = [];
foreach ($responses as $response){
//CALCULATE CONTRACT END DATE
$date = date('Y-m-d', strtotime('+'.$response['duration'].' months', strtotime($response['start_date'])));
//CHECK IF SEARCH CRITERIA YEAR, QUARTER, MONTH FALL IN CONTRACT PERIOD
$inrange = dateInRange($response['start_date'], $date, $da) ?? 0;
if ($inrange == 1){
$servicetools = json_decode($response['servicetool'] ?? '',true) ?? '';
$ignore_lists = json_decode($response['ignore_list'] ?? '',true) ?? '';
$date = date('Y-m-d', strtotime('+'.$response['duration'].' months', strtotime($response['start_date'])));
//get all assigned serialnumbers
$url_input = '';
@@ -51,7 +73,8 @@ foreach ($responses as $response){
//Return report_usage_servicereports
$api_url = '/v2/application/type=ServiceReport&serialnumber='.substr($url_input,0,-1).$ignored_serialnumbers.'&between='.$response['start_date'].'||'.$date.'/contract_usage_servicereports';
$contract_usage_servicereports = ioAPIv2($api_url,'',$token);
$contract_usage_servicereports = ioAPIv2($api_url,'',$user_data['userkey']);
//Decode Payload
if (!empty($contract_usage_servicereports)){$contract_usage_servicereports = json_decode($contract_usage_servicereports,true);}else{$contract_usage_servicereports = null;}
@@ -59,29 +82,37 @@ foreach ($responses as $response){
//Initial billing_run value
$billing_run = 0;
$period = 'n/a';
switch ($response['billing_plan']) {
case 1:
# month
//CONVERT KEY TO MONTH NAME
$monthObj = DateTime::createFromFormat('!m', $mo);
$period = $monthObj->format('F');
if (isset($usage_billing[$ye]['quarters'][$qu]['months'][$mo])){
if ($mo != ''){
$billing_run = $usage_billing[$ye]['quarters'][$qu]['months'][$mo];
}
}
break;
case 2:
# quarter
$period = 'Q'.$qu;
if (isset($usage_billing[$ye]['quarters'][$qu]['total'])){
$billing_run = $usage_billing[$ye]['quarters'][$qu]['total'];
}
break;
case 3:
# year
$period = $ye;
if (isset($usage_billing[$ye]['total'])){
$billing_run = $usage_billing[$ye]['total'];
}
break;
}
//Variable translation
$status = 'contract_status' . $response['status'];
$type = 'contract_type'.$response['type'];
@@ -93,7 +124,7 @@ foreach ($responses as $response){
$partner_data = json_decode($response['accounthierarchy'],true);
//SHOW USAGE PERCENTAGE
$usage_service_count = ($response['service_count'] != 0 || $response['service_count'] !='') ? $response['service_count']:1;
$usage_service_count = ($response['service_count'] > 0) ? $response['service_count']:1;
$usage_percentage = ($billing_run != 0) ? round((($billing_run/$usage_service_count)*100),2) : 0;
$usage_per_status = ($usage_percentage < 100) ? 'enabled' : 'disabled';
@@ -108,14 +139,35 @@ foreach ($responses as $response){
$contract_reference =>$response['reference'],
$contract_billing_count =>$billing_run,
$contract_service_2 =>$response['service_count'],
$contract_overrun => $usage_percentage
$contract_overrun => $usage_percentage,
"Snapshot_date" => $da,
"Period" => $period
);
}
}
$total = count( $output );
//SEND TOTALS WHEN REQUESTED
if(isset($criterias['totals']) && $criterias['totals'] ==''){
$messages = json_encode($total, JSON_UNESCAPED_UNICODE);
}
else {
//------------------------------------------
//Paging
//------------------------------------------
$current_page = isset($criterias['p']) && is_numeric($criterias['p']) ? (int)$criterias['p'] : 1;
$offset = ($current_page - 1) * $page_rows_contracts;
$output = array_slice( $output, $offset, $page_rows_contracts );
//------------------------------------------
//JSON_ENCODE
//------------------------------------------
$messages = json_encode($output, JSON_UNESCAPED_UNICODE);
}
//Send results
echo $messages;

View File

@@ -78,6 +78,10 @@ elseif (isset($criterias['list']) && $criterias['list'] =='') {
//SQL for Paging
$sql = 'SELECT * FROM contracts c JOIN users u ON c.createdby = u.username '.$whereclause.'';
}
elseif (isset($criterias['p']) && $criterias['p'] =='all') {
//SQL for Paging
$sql = 'SELECT * FROM contracts c JOIN users u ON c.createdby = u.username '.$whereclause.'';
}
else {
//SQL for Paging
$sql = 'SELECT * FROM contracts c JOIN users u ON c.createdby = u.username '.$whereclause.' LIMIT :page,:num_products';
@@ -115,7 +119,7 @@ if(isset($criterias['totals']) && $criterias['totals']==''){
$messages = $stmt->fetch();
$messages = $messages[0];
}
elseif(isset($criterias['list']) && $criterias['list']==''){
elseif((isset($criterias['list']) && $criterias['list']=='') || (isset($criterias['p']) && $criterias['p'] =='all')){
//Excute Query
$stmt->execute();
//Get results

View File

@@ -2421,8 +2421,9 @@ function usageBilling($messages){
foreach ($messages as $entry) {
$year = $entry['year'];
$quarter = $entry['quarter'];
$dateObj = DateTime::createFromFormat('!m', $entry['month']);
$month = $dateObj->format('F');
//$dateObj = DateTime::createFromFormat('!m', $entry['month']);
//$month = $dateObj->format('F');
$month = $entry['month'];
$count = $entry['count'];
// Initialize arrays if not already set for year, quarter, and month
@@ -2779,7 +2780,7 @@ function blockContracts($token){
//GET USAGE BILLING INFORMATION
$usage_billing = usageBilling($contract_usage_servicereports);
$billing_cylce_usage = (($billing_plan == 1 && isset($usage_billing[$curYear]['quarters'][$curQuarter]['months'][$curMonth_name])) ? $usage_billing[$curYear]['quarters'][$curQuarter]['months'][$curMonth_name] : (($billing_plan == 2 && isset($usage_billing[$curYear]['quarters'][$curQuarter])) ? $usage_billing[$curYear]['quarters'][$curQuarter]: (($billing_plan == 3 && isset($usage_billing[$curYear])) ? $usage_billing[$curYear]: 0 )));
$billing_cylce_usage = (($billing_plan == 1 && isset($usage_billing[$curYear]['quarters'][$curQuarter]['months'][$curMonth])) ? $usage_billing[$curYear]['quarters'][$curQuarter]['months'][$curMonth] : (($billing_plan == 2 && isset($usage_billing[$curYear]['quarters'][$curQuarter])) ? $usage_billing[$curYear]['quarters'][$curQuarter]: (($billing_plan == 3 && isset($usage_billing[$curYear])) ? $usage_billing[$curYear]: 0 )));
//CHECK FOR OVERRUN
if ($billing_cylce_usage >= $billing_max){
@@ -2790,3 +2791,17 @@ function blockContracts($token){
}
}
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
// check date is in range ++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
function dateInRange($start_date, $end_date, $date_check)
{
// Convert to timestamp
$start_ts = strtotime($start_date);
$end_ts = strtotime($end_date);
$check_ts = strtotime($date_check);
// Check that user date is between start & end
return (($check_ts >= $start_ts) && ($check_ts <= $end_ts));
}

View File

@@ -257,6 +257,10 @@ $view .= '<div class="content-block">
';
foreach($value['months'] as $key => $value){
//CONVERT KEY TO MONTH NAME
$monthObj = DateTime::createFromFormat('!m', $key);
$key = $monthObj->format('F');
$view .= '
<tr '.(($responses->billing_plan && $responses->billing_plan == 1) ? '' :'style="display:none;"').'>
<td></td>

View File

@@ -23,51 +23,38 @@ if (isAllowed($page,$_SESSION['profile'],$_SESSION['permission'],'R') === 0){
exit;
}
//Defaults
$contract = [
'type' => (isset($_GET['type'])? $_GET['type'] : ''),
'billing_plan' => (isset($_GET['billing_plan'])? $_GET['billing_plan'] : ''),
'year' => (isset($_GET['year'])? $_GET['year'] : ''),
'quarter' => (isset($_GET['quarter'])? $_GET['quarter'] : ''),
'month' => (isset($_GET['month'])? $_GET['month'] : '')
];
//GET PARAMETERS
$pagination_page = isset($_GET['p']) ? $_GET['p'] : 1;
$p = '&p='.$pagination_page;
$status = isset($_GET['status']) ? '&status='.$_GET['status'] : '';
$search = isset($_GET['search']) ? '&search='.$_GET['search'] : '';
//SET DEFAULT VALUES
$ye = ((isset($_GET['year']) && $_GET['year'] !='' )? $_GET['year'] : $curYear);
$mo = ((isset($_GET['month']) && $_GET['month'] !='')? $_GET['month'] : $curMonth_name);
$qu = ((isset($_GET['month']) ? (int)ceil(date("n",strtotime($mo)) / 3): $curQuarter));
// Determine the URL
$url = 'index.php?page=report_contracts_billing'.$status.$search;
//ADD status
$cs = $_GET['status'] = 1;
//REMOVE $_GETs
unset($_GET['year']);
unset($_GET['month']);
//GET Details from URL
$GET_VALUES = urlGETdetails($_GET) ?? '';
//CALL TO API
$api_url = '/v2/contracts/'.$GET_VALUES;
$api_url = '/v2/billing/'.$GET_VALUES;
$responses = ioServer($api_url,'');
//Decode Payload
if (!empty($responses)){$responses = json_decode($responses,true);}else{$responses = null;}
//Return QueryTotal from API
$api_url = '/v2/contracts/'.$GET_VALUES.'&totals=';
$api_url = '/v2/billing/'.$GET_VALUES.'&totals=';
$query_total = ioServer($api_url,'');
//Decode Payload
if (!empty($query_total)){$query_total = json_decode($query_total,true);}else{$query_total = 0;}
template_header('contracts', 'contracts','view');
template_header('reports', 'report_contracts_billing','view');
$view = '
<div class="content-title">
<div class="title">
@@ -119,24 +106,24 @@ $view .= '
<select name="month">
<option value=""></option>
<optgroup label="Q1">
<option value="January" '.($contract['month']=='January'?' selected':'').'>Jan</option>
<option value="February" '.($contract['month']=='February'?' selected':'').'>Feb</option>
<option value="March" '.($contract['month']=='March'?' selected':'').'>Mar</option>
<option value="1" '.($contract['month']==1?' selected':'').'>Jan</option>
<option value="2" '.($contract['month']==2?' selected':'').'>Feb</option>
<option value="3" '.($contract['month']==3?' selected':'').'>Mar</option>
</optgroup>
<optgroup label="Q2">
<option value="April" '.($contract['month']== 'April'?' selected':'').'>Apr</option>
<option value="May" '.($contract['month']=='May'?' selected':'').'>May</option>
<option value="June" '.($contract['month']=='June'?' selected':'').'>Jun</option>
<option value="4" '.($contract['month']==4?' selected':'').'>Apr</option>
<option value="5" '.($contract['month']==5?' selected':'').'>5</option>
<option value="6" '.($contract['month']==6?' selected':'').'>Jun</option>
</optgroup>
<optgroup label="Q3">
<option value="July" '.($contract['month']=='July'?' selected':'').'>Jul</option>
<option value="August" '.($contract['month']=='August'?' selected':'').'>Aug</option>
<option value="September" '.($contract['month']=='September'?' selected':'').'>Sept</option>
<option value="7" '.($contract['month']==7?' selected':'').'>Jul</option>
<option value="8" '.($contract['month']==8?' selected':'').'>Aug</option>
<option value="9" '.($contract['month']==9?' selected':'').'>Sept</option>
</optgroup>
<optgroup label="Q4">
<option value="October" '.($contract['month']=='October'?' selected':'').'>Oct</option>
<option value="November" '.($contract['month']=='November'?' selected':'').'>Nov</option>
<option value="December" '.($contract['month']=='December'?' selected':'').'>Dec</option>
<option value="10" '.($contract['month']==10 ?' selected':'').'>Oct</option>
<option value="11" '.($contract['month']==11 ?' selected':'').'>Nov</option>
<option value="12" '.($contract['month']==12 ?' selected':'').'>Dec</option>
</optgroup>
</select>
<button type="submit">'.$button_apply.'</button>
@@ -152,12 +139,9 @@ $view .= '
</div>
';
$view .= '
<div class="content-block">
<div class="table">
'.$ye.' - '.$qu.' - '.$mo.'
<table class="sortable">
<thead>
<tr>
@@ -172,6 +156,7 @@ $view .= '
<th>'.$contract_billing_count.'</th>
<th>'.$contract_service_2.'</th>
<th>'.$contract_overrun.'</th>
<th>'.$contract_reference.'</th>
<th>'.$general_actions.'</th>
</tr>
</thead>
@@ -188,85 +173,21 @@ $view .= '
else {
foreach ($responses as $response){
$servicetools = json_decode($response['servicetool'] ?? '',true) ?? '';
$ignore_lists = json_decode($response['ignore_list'] ?? '',true) ?? '';
$date = date('Y-m-d', strtotime('+'.$response['duration'].' months', strtotime($response['start_date'])));
//get all assigned serialnumbers
$url_input = '';
foreach($servicetools as $service_tool){
$url_input .= $service_tool.',';
}
//get ignore list
$ignored_serialnumbers = '';
if (!empty($ignore_lists)){
foreach($ignore_lists as $list){
$ignored_serialnumbers .= $list.',';
}
$ignored_serialnumbers = '&ignore='.substr($ignored_serialnumbers,0,-1);
}
//Return report_usage_servicereports
$api_url = '/v2/application/type=ServiceReport&serialnumber='.substr($url_input,0,-1).$ignored_serialnumbers.'&between='.$response['start_date'].'||'.$date.'/contract_usage_servicereports';
$contract_usage_servicereports = ioServer($api_url,'');
//Decode Payload
if (!empty($contract_usage_servicereports)){$contract_usage_servicereports = json_decode($contract_usage_servicereports,true);}else{$contract_usage_servicereports = null;}
$usage_billing = usageBilling($contract_usage_servicereports);
//Initial billing_run value
$billing_run = 0;
switch ($response['billing_plan']) {
case 1:
# month
if (isset($usage_billing[$ye]['quarters'][$qu]['months'][$mo])){
if ($mo != ''){
$billing_run = $usage_billing[$ye]['quarters'][$qu]['months'][$mo];
}
}
break;
case 2:
# quarter
if (isset($usage_billing[$ye]['quarters'][$qu]['total'])){
$billing_run = $usage_billing[$ye]['quarters'][$qu]['total'];
}
break;
case 3:
# year
if (isset($usage_billing[$ye]['total'])){
$billing_run = $usage_billing[$ye]['total'];
}
break;
}
//Variable translation
$status = 'contract_status' . $response['status'];
$type = 'contract_type'.$response['type'];
$contract_billingplan_text = 'contract_billing'.$response['billing_plan'] ?? '';
//calculate enddate from duration
$date = date('Y-m-d', strtotime('+'.$response['duration'].' months', strtotime($response['start_date'])));
//GetPartnerDetails
$partner_data = json_decode($response['accounthierarchy'],true);
//SHOW USAGE PERCENTAGE
$usage_service_count = ($response['service_count'] > 0) ? $response['service_count']:1;
$usage_percentage = ($billing_run != 0) ? round((($billing_run/$usage_service_count)*100),2) : 0;
$usage_per_status = ($usage_percentage < 100) ? 'enabled' : 'disabled';
$view .= '
<tr>
<td>'.$response['rowID'].'</td>
<td>'.(($response['status'] == 1)? '<span class="status enabled">'.$$status: (($response['status'] == 3) ? '<span class="status disabled">'.$$status : '<span class="status">'.$$status)).'</td>
<td>'.$partner_data['soldto'].'</td>
<td>'.$$type.'</td>
<td>'.$$contract_billingplan_text.'</td>
<td>'.$response['start_date'].'</td>
<td>'.$date.'</td>
<td>'.$response['reference'].'</td>
<td>'.$billing_run.'</td>
<td>'.$response['service_count'].' </td>
<td><span class="status '.$usage_per_status .'">'.$usage_percentage.'</span></td>
<td><a href="index.php?page=contract&rowID='.$response['rowID'].'" class="btn_link">'.$general_view .'</a></td>
<td>'.$response['#'].'</td>
<td>'.(($response['Status'] == $contract_status1)? '<span class="status enabled">'.$response['Status']: (($response['Status'] == $contract_status3) ? '<span class="status disabled">'.$response['Status'] : '<span class="status">'.$response['Status'])).'</span></td>
<td>'.$response['Account'].'</td>
<td>'.$response['Type'].'</td>
<td>'.$response['Billingplan'].'</td>
<td>'.$response['Start date'].'</td>
<td>'.$response['End date'].'</td>
<td>'.$response['Reference'].'</td>
<td>'.$response['Consumed'].'</td>
<td>'.$response['Contracted'].' </td>
<td><span class="status '.(($response['Deviation'] < 100) ? 'enabled' : 'disabled').'">'.$response['Deviation'].'</span></td>
<td>'.$response['Period'].'</td>
<td><a href="index.php?page=contract&rowID='.$response['#'].'" class="btn_link">'.$general_view .'</a></td>
</tr>
';
}