MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: //home/cafsindia/allyindian_com/sbltt/application/controllers/Aggregate_sales.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once("Secure_Controller.php");

class Aggregate_sales extends Secure_Controller
{
	public function __construct()
	{
		parent::__construct('aggregate_sales');
		$this->load->model('Aggregate_sales_model');
	}

	public function index()
	{

		if(!$this->Appconfig->isAppvalid()){
			redirect('config');
		}
		 $data['table_headers'] = $this->xss_clean(get_aggregate_sales_table_headers());

		$this->load->view('aggregate_sales/manage', $data);
	}

	/*
	Returns Supplier table data rows. This will be called with AJAX.
	*/
	public function search()
	{
		$search = $this->input->get('search');
  		$limit = $this->input->get('limit');
  		$offset = $this->input->get('offset');
  		$sort = $this->input->get('sort');
  		$order = $this->input->get('order');
      $filters = array('crm' => $this->input->get('crm'),
						'crmto' => $this->input->get('crmto'));
       $all_leads = $this->Aggregate_sales_model->search($search, $filters, $limit, $offset, $sort, $order);
        $total_rows = $this->Aggregate_sales_model->get_found_rows($search, $filters);
        $data_rows = array();
        foreach($all_leads->result() as $leads){
            $data_rows[] = get_transfer_data_row($leads, $this);
        }
        echo json_encode(array('total' => $total_rows, 'rows' => $data_rows));
	}	

	/*
	This Aggregate_sales_report
	*/
	public function aggregate_sales(){
		$filters = array('start_date' => $this->input->post('start_date'), 'end_date' => $this->input->post('end_date'));		
		$result = $this->Aggregate_sales_model->aggregate_sales($filters);		
		echo json_encode(array('success' => TRUE, 'result' => $result));
		
	}
	
	//excel generation for aggregate sales
	
	public function excel($start_date,$end_date){
		//$filters = array('start_date' => $start_date, 'end_date' => $end_date);
		$this->load->library('excel');
		$obj = new Excel();
		
		// Add new sheet
		 $objWorkSheet = $obj->createSheet(0); //Setting index when creating
		 $obj->setActiveSheetIndex(0); //First sheet open in excel sheet
		 
		//Write cells head column name
		 $objWorkSheet->setCellValue('A1', 'Si.No')
		 ->setCellValue('B1', 'Tour No')
		 ->setCellValue('C1', 'Customer Name')
		 ->setCellValue('D1', 'Billing Name')
		 ->setCellValue('E1', 'Customer Mobile')
		 ->setCellValue('F1', 'Trip Type')
		 ->setCellValue('G1', 'Origin')
		 ->setCellValue('H1', 'Destination')
		 ->setCellValue('I1', 'Avg KM')
		 ->setCellValue('J1', 'From Date')	
		 ->setCellValue('K1', 'To Date')	
		 ->setCellValue('L1', 'Trip Days')
		 ->setCellValue('M1', 'Vehicle Type')
		 ->setCellValue('N1', 'Vehicle Count')
		 ->setCellValue('O1', 'Customer Type')
		 ->setCellValue('P1', 'Driver Name')
		 ->setCellValue('Q1', 'Ops Type')
		 ->setCellValue('R1', 'Tp info')
		 ->setCellValue('S1', 'Vehicle No')
		 ->setCellValue('T1', 'Ref By')
		 ->setCellValue('U1', 'Travels Name')
		 ->setCellValue('V1', 'Start KM')
		 ->setCellValue('W1', 'End KM')
		 ->setCellValue('X1', 'Total KM')
		 ->setCellValue('Y1', 'Extra KM')
		 ->setCellValue('Z1', 'GST Rate')
		 ->setCellValue('AA1', 'Hire Charge')//Booking amount
		 ->setCellValue('AB1', 'Catering Amount')
		 ->setCellValue('AC1', 'Stay Amount')
		 ->setCellValue('AD1', 'Extra KM Amount')
		 ->setCellValue('AE1', 'Total Hire Charge')
		 ->setCellValue('AF1', 'GST')
		 ->setCellValue('AG1', 'Total Inc Tax') //+gst
		 ->setCellValue('AH1', 'Discount Amount')
		 ->setCellValue('AI1', 'Receipt')    //Advance amount
		 ->setCellValue('AJ1', 'Balance Amount')
		 ->setCellValue('AK1', 'Status');
		 //->setCellValue('1', '');
		 
		$aggregate = $this->Aggregate_sales_model->excel_aggregate_report($start_date, $end_date);
		//print_r($aggregate); die;
		$j=2;
		$k=1;
		foreach ($aggregate as $key => $value) { 
			$serial          = $k;
			$from_date       = date('d-m-Y', strtotime($value['from_date']));
			$to_date         = date('d-m-Y', strtotime($value['to_date']));
			$booking_id      = $value['booking_id'];
			$name            = $value['customer_name'];
			$mob             = $value['phone_number'];
			$origin          = $value['frm_city_name'];
			$destination     = $value['to_city_name'];
			$trip_days       = $value['trip_days'];
			$bus_count       = $value['bus_count'];
			$tour_no         = $value['tour_no'];
			$report_address  = $value['report_address'];
			$billing_name    = $value['billing_name'];
			$avg_km          = $value['avg_km'];
			$trip_type_name  = $value['trip_type_name'];
			$veh_type        = $value['veh_type'];
			$type            = $value['booking_type_name'];
			$trip_details    = $value['trip_details'];
			$operation_type  = $value['operation_type'];
			$booking_amount  = $value['booking_amount'];
			$advance_amount  = $value['advance_amount'];
			
			$balance_amount  = $this->Aggregate_sales_model->get_total_trip_amount($booking_id);
									
					
			//$balance_amount  = $value['balance_amount'];
			$discount_amount = $value['discount_amount'];
			$payment_type    = $value['payment_type'];
			$op_suspence_amt = $value['op_suspence_amt'];
			$hire_charges    = $value['hire_charges'];
			$op_driver_name  = $value['op_driver_name'];
			$vehicle_no      = $value['vehicle_no'];
			$operation_sts   = $value['operation_sts'];
			$first_name      = $value['first_name'];
			$tp_info         = $value['tp_info'];
			$stay_tot_amt    = $value['stay_tot_amt'];
			$trip_start_km   = $value['trip_start_km'];
			$trip_end_km     = $value['trip_end_km'];
			$extra_km        = $value['extra_km'];
			$extra_amt       = $value['extra_amt'];
			$tax_precentage  = $value['tax_precentage'];
			$catering_amount = $value['catering_amount'];
			$vendor_name     = $value['vendor_name'];

			if($operation_sts === "1"){
				$operation_sts = "Follow Up";
			}else
			if($operation_sts === "2"){
				$operation_sts = "Confirm";
			}else
			if($operation_sts === "3"){
				$operation_sts = "Cancelled";
			}else
			if($operation_sts === "4"){
				$operation_sts = "Completed";
			}else
			if($operation_sts === "5"){
				$operation_sts = "On progress";
			}
			if($operation_type === "1"){
				$operation_type = "Vehicle with Driver";
			}else
			if($operation_sts === "2"){
				$operation_sts = "Driver Only";
			}
			
			//Total KM Calculation
			
			$total_km = $trip_end_km - $trip_start_km;
			
			//hire charge include all charge stay,catering,booking and extra km amount
			
			$total_hire_charge = $booking_amount + $catering_amount + $stay_tot_amt + $extra_amt;
			
			//gst calculation
			
			$gst_amount=($total_hire_charge*$tax_precentage)/100;
			
			//gst+total charge
			
			$total_inc_gst_amount = $total_hire_charge + $gst_amount;
			
			//receipt amount
			$bal = $this->Aggregate_sales_model->get_bal_log_amount($booking_id); 
			$advance_amt = $bal->bal_log_amt;
			

			$receipt = $advance_amount+$advance_amt;
			
			
			$objWorkSheet->setCellValue('A'.$j, $serial)
				->setCellValue('B'.$j, $tour_no)
				->setCellValue('C'.$j, $name)
				->setCellValue('D'.$j, $billing_name)
				->setCellValue('E'.$j, $mob)
				->setCellValue('F'.$j, $trip_type_name)
				->setCellValue('G'.$j, $origin)
				->setCellValue('H'.$j, $destination)
				->setCellValue('I'.$j, $avg_km)
				->setCellValue('J'.$j, $from_date)
				->setCellValue('K'.$j, $to_date)
				->setCellValue('L'.$j, $trip_days)
				->setCellValue('M'.$j, $veh_type)
				->setCellValue('N'.$j, $bus_count)
				->setCellValue('O'.$j, $type)
				->setCellValue('P'.$j, $op_driver_name)
				->setCellValue('Q'.$j, $operation_type)
				->setCellValue('R'.$j, $tp_info)
				->setCellValue('S'.$j, $vehicle_no)
				->setCellValue('T'.$j, $first_name)
				->setCellValue('U'.$j, $vendor_name)
				->setCellValue('V'.$j, $trip_start_km)
				->setCellValue('W'.$j, $trip_end_km)
				->setCellValue('X'.$j, $total_km)
				->setCellValue('Y'.$j, $extra_km)
				->setCellValue('Z'.$j, $tax_precentage)
				->setCellValue('AA'.$j, $booking_amount)
				->setCellValue('AB'.$j, $catering_amount)
				->setCellValue('AC'.$j, $stay_tot_amt)
				->setCellValue('AD'.$j, $extra_amt)
				->setCellValue('AE'.$j, $total_hire_charge)
				->setCellValue('AF'.$j, $gst_amount)
				->setCellValue('AG'.$j, $total_inc_gst_amount)
				->setCellValue('AH'.$j, $discount_amount)
				->setCellValue('AI'.$j, $receipt)
				->setCellValue('AJ'.$j, $balance_amount)
				->setCellValue('AK'.$j, $operation_sts);
			//->setCellValue('AA'.$j, $status);
			$k++;
			$j++;
		}
		// Rename sheet
		 $objWorkSheet->setTitle("Detailed");
		
		 $filename="Aggregate Report_".date('d-m-Y').".xls"; //save our workbook as this file name
		 header('Content-Type: application/vnd.ms-excel'); //mime type
		 header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		 header('Cache-Control: max-age=0'); //no cache
		 
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		 //if you want to save it as .XLSX Excel 2007 format
		 $objWriter = PHPExcel_IOFactory::createWriter($obj, 'Excel5');
		 //force user to download the Excel file without writing it to server's HD
		 $objWriter->save('php://output');
	}
}
?>