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

@@ -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>
@@ -187,86 +172,22 @@ $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>
';
}