diff --git a/api/v2/get/billing.php b/api/v2/get/billing.php index 468db75..3ea674c 100644 --- a/api/v2/get/billing.php +++ b/api/v2/get/billing.php @@ -1,121 +1,173 @@ $response['rowID'] , - $contract_status => $$status, - $contract_account => $partner_data['soldto'], - $contract_type => $$type, - $contract_billinglabel =>$$contract_billingplan_text, - $contract_start_date =>$response['start_date'], - $contract_end_date => $date, - $contract_reference =>$response['reference'], - $contract_billing_count =>$billing_run, - $contract_service_2 =>$response['service_count'], - $contract_overrun => $usage_percentage - ); + $usage_billing = usageBilling($contract_usage_servicereports); + + //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])){ + $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']; + $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'; + + $output[] = array( + "#" => $response['rowID'] , + $contract_status => $$status, + $contract_account => $partner_data['soldto'], + $contract_type => $$type, + $contract_billinglabel =>$$contract_billingplan_text, + $contract_start_date =>$response['start_date'], + $contract_end_date => $date, + $contract_reference =>$response['reference'], + $contract_billing_count =>$billing_run, + $contract_service_2 =>$response['service_count'], + $contract_overrun => $usage_percentage, + "Snapshot_date" => $da, + "Period" => $period + + ); + } } -//------------------------------------------ -//JSON_ENCODE -//------------------------------------------ -$messages = json_encode($output, JSON_UNESCAPED_UNICODE); +$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; diff --git a/api/v2/get/contracts.php b/api/v2/get/contracts.php index 55461a7..79c76b0 100644 --- a/api/v2/get/contracts.php +++ b/api/v2/get/contracts.php @@ -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 diff --git a/assets/functions.php b/assets/functions.php index 5cb4860..c76aef7 100644 --- a/assets/functions.php +++ b/assets/functions.php @@ -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){ @@ -2789,4 +2790,18 @@ function blockContracts($token){ ioAPIv2('/v2/contracts', $data,$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)); } \ No newline at end of file diff --git a/contract.php b/contract.php index f8a551a..aa8a03e 100644 --- a/contract.php +++ b/contract.php @@ -257,6 +257,10 @@ $view .= '
'; foreach($value['months'] as $key => $value){ + //CONVERT KEY TO MONTH NAME + $monthObj = DateTime::createFromFormat('!m', $key); + $key = $monthObj->format('F'); + $view .= ' billing_plan && $responses->billing_plan == 1) ? '' :'style="display:none;"').'> diff --git a/report_contracts_billing.php b/report_contracts_billing.php index c6ff3da..5d57273 100644 --- a/report_contracts_billing.php +++ b/report_contracts_billing.php @@ -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 = '
@@ -119,24 +106,24 @@ $view .= ' @@ -152,12 +139,9 @@ $view .= '
'; - - $view .= '
- '.$ye.' - '.$qu.' - '.$mo.' @@ -172,6 +156,7 @@ $view .= ' + @@ -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 .= ' - - - - - - - - - - - - + + + + + + + + + + + + + '; }
'.$contract_billing_count.' '.$contract_service_2.' '.$contract_overrun.''.$contract_reference.' '.$general_actions.'
'.$response['rowID'].''.(($response['status'] == 1)? ''.$$status: (($response['status'] == 3) ? ''.$$status : ''.$$status)).''.$partner_data['soldto'].''.$$type.''.$$contract_billingplan_text.''.$response['start_date'].''.$date.''.$response['reference'].''.$billing_run.''.$response['service_count'].' '.$usage_percentage.''.$general_view .''.$response['#'].''.(($response['Status'] == $contract_status1)? ''.$response['Status']: (($response['Status'] == $contract_status3) ? ''.$response['Status'] : ''.$response['Status'])).''.$response['Account'].''.$response['Type'].''.$response['Billingplan'].''.$response['Start date'].''.$response['End date'].''.$response['Reference'].''.$response['Consumed'].''.$response['Contracted'].' '.$response['Deviation'].''.$response['Period'].''.$general_view .'