File: /home/cafsindia/allyindian_com/sbltt/application/controllers/Vehicle_margin_report.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once("Secure_Controller.php");
class Vehicle_margin_report extends Secure_Controller{
public function __construct(){
parent::__construct('vehicle_margin_report');
$this->load->model('Vehicle_margin_model');
}
public function index(){
if(!$this->Appconfig->isAppvalid()){
redirect('config');
}
foreach($this->Vehicle_margin_model->get_vehicle_type() as $row){
$vehicle_type[$this->xss_clean($row->veh_type_id)] = $this->xss_clean($row->veh_type);
}
$data['vehicle_type'] = $vehicle_type;
$this->load->view('vehicle_margin_report/manage', $data);
}
public function get_vehicle_no(){
$vehicle_type = $this->input->post('vehicle_type');
$vehicle_list = $this->Vehicle_margin_model->get_vehicle_no($vehicle_type);
echo json_encode(array('status' =>"SUCCESS",'vehicle_list' => $vehicle_list));
}
public function search(){
$start_date = $this->input->post('start_date');
$end_date = $this->input->post('end_date');
$vehicle_type = $this->input->post('vehicle_type');
$vehicle_no = $this->input->post('vehicle_no');
//echo "vehicle_type :: $vehicle_type";
$result = $this->Vehicle_margin_model->search($start_date,$end_date,$vehicle_type,$vehicle_no);
$sblt_sale = 0;
$sblt_trip = 0;
$sblt_days = 0;
$sblt_km = 0;
$sble_sale = 0;
$sble_trip = 0;
$sble_days = 0;
$sble_km = 0;
$sblt_info = array();
$sble_info = array();
$vehicle_id = "";
foreach($result as $rslt){
$cust_type = $rslt->cust_type;
$trip_type = $rslt->trip_type;
$op_vehicle_id = $rslt->op_vehicle_id;
$vehicle_no = $rslt->vehicle_no;
$veh_type = $rslt->veh_type;
$trip_count = $rslt->trip_count;
$running_days = $rslt->running_days;
$tot_sale = $rslt->tot_sale;
$endkm = $rslt->endkm;
$startkm = $rslt->startkm;
$tot_km = $rslt->tot_km;
if((int)$trip_type === 6){
$sble_node = $op_vehicle_id."_".$cust_type;
if(!array_key_exists($sble_node,$sble_info)){
$sble_info[$sble_node] = $sble_node;
$sble_sale = 0;
$sble_trip = 0;
$sble_days = 0;
$sble_km = 0;
}
$sble_sale += (int)$tot_sale;
$sble_trip += (int)$trip_count;
$sble_days += (int)$running_days;
$sble_km += (int)$tot_km;
$sble = array('vehicle_id'=>$op_vehicle_id,'vehicle_no'=>$vehicle_no,'veh_type'=>$veh_type,'sble_sale'=>$sble_sale,'sble_trip'=>$sble_trip,'sble_days'=>$sble_days,'sble_km'=>$sble_km);
$sble_info[$sble_node] = $sble;
}else{
$sblt_node = $op_vehicle_id."_others";
if(!array_key_exists($sblt_node,$sblt_info)){
$sblt_info[$sblt_node] = $sblt_node;
$sblt_sale = 0;
$sblt_trip = 0;
$sblt_days = 0;
$sblt_km = 0;
}
$sblt_sale += (int)$tot_sale;
$sblt_trip += (int)$trip_count;
$sblt_days += (int)$running_days;
$sblt_km += (int)$tot_km;
$sblt = array('vehicle_id'=>$op_vehicle_id,'vehicle_no'=>$vehicle_no,'veh_type'=>$veh_type,'sblt_sale'=>$sblt_sale,'sblt_trip'=>$sblt_trip,'sblt_days'=>$sblt_days,'sblt_km'=>$sblt_km);
$sblt_info[$sblt_node] = $sblt;
}
}
foreach($sble_info as $sble){
$vehicle_id = $sble['vehicle_id'];
$diesel_result = $this->Vehicle_margin_model->diesel_amount($start_date,$end_date,$vehicle_id,"6");
$diesel_amount = $diesel_result[0]->diesel_amount;
$toll_result = $this->Vehicle_margin_model->toll_amount($start_date,$end_date,$vehicle_id,"6");
$toll_amount = $toll_result[0]->toll_amount;
$other_result = $this->Vehicle_margin_model->other_amount($start_date,$end_date,$vehicle_id,"6");
$other_amount = $other_result[0]->other_amount;
$sble_node = $vehicle_id."_2";
$tot_expense = (int)$diesel_amount + (int)$toll_amount + (int)$other_amount;
$sble_sale = $sble_info[$sble_node]['sble_sale'];
$margin_val = (int)$sble_sale - (int)$tot_expense;
$margin_per = (int)$margin_val / (int)$sble_sale;
$sble_info[$sble_node]['diesel_amount'] = $diesel_amount;
$sble_info[$sble_node]['toll_amount'] = $toll_amount;
$sble_info[$sble_node]['other_amount'] = $other_amount;
$sble_info[$sble_node]['tot_expense'] = $tot_expense;
$sble_info[$sble_node]['margin_val'] = $margin_val;
$sble_info[$sble_node]['margin_per'] = number_format($margin_per,2);
}
foreach($sblt_info as $sblt){
$vehicle_id = $sblt['vehicle_id'];
$diesel_result = $this->Vehicle_margin_model->diesel_amount($start_date,$end_date,$vehicle_id,"others");
$diesel_amount = $diesel_result[0]->diesel_amount;
$toll_result = $this->Vehicle_margin_model->toll_amount($start_date,$end_date,$vehicle_id,"others");
$toll_amount = $toll_result[0]->toll_amount;
$other_result = $this->Vehicle_margin_model->other_amount($start_date,$end_date,$vehicle_id,"others");
$other_amount = $other_result[0]->other_amount;
$sblt_node = $vehicle_id."_others";
$tot_expense = (int)$diesel_amount + (int)$toll_amount + (int)$other_amount;
$sblt_sale = $sblt_info[$sblt_node]['sblt_sale'];
$margin_val = (int)$sblt_sale - (int)$tot_expense;
$margin_per = (int)$margin_val / (int)$sblt_sale;
$sblt_info[$sblt_node]['diesel_amount'] = $diesel_amount;
$sblt_info[$sblt_node]['toll_amount'] = $toll_amount;
$sblt_info[$sblt_node]['other_amount'] = $other_amount;
$sblt_info[$sblt_node]['tot_expense'] = $tot_expense;
$sblt_info[$sblt_node]['margin_val'] = $margin_val;
$sblt_info[$sblt_node]['margin_per'] = number_format($margin_per,2);
}
echo json_encode(array('success' => TRUE, 'sblt_info' => $sblt_info, 'sble_info' => $sble_info));
}
}
?>