File: //var/www/html/qcr24/app/application/controllers/admin/Income.php
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Income extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('admin/mincome');
}
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/driver_guide/general/urls.html
*/
public function index()
{
// $data['income_list'] = $this->mincome->getIncome();
$data['cars'] = $this->mincome->get_car();
$data['content'] = 'admin/income/list';
$this->load->view('admin/layouts/index', $data);
}
public function searchIncomeData()
{
$from_date=!empty($this->input->post('from_date'))?date('Y-m-d',strtotime(str_replace('/', '-', $this->input->post('from_date')))):'';
$to_date=!empty($this->input->post('to_date'))?date('Y-m-d',strtotime(str_replace('/', '-', $this->input->post('to_date')))):'';
$car_id=$this->input->post('car_id');
$income_list = $this->mincome->getIncome($from_date,$to_date,$car_id);
// echo '<pre>';print_r($income_list);die;
echo json_encode(array('status'=>true,'income_list'=>$income_list));
}
public function download_excel()
{
$from_date=date('Y-m-d',strtotime(str_replace('/', '-', $this->input->post('from_date'))));
$to_date=date('Y-m-d',strtotime(str_replace('/', '-', $this->input->post('to_date'))));
$car_id=$this->input->post('car_id');
$result = $this->mincome->getIncome($from_date,$to_date,$car_id);
// include('Classes/PHPExcel.php');
$this->load->library('excel');
// $objPHPExcel = new PHPExcel();
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->SetCellValue('A1', 'SL NO.');
$this->excel->getActiveSheet()->SetCellValue('B1', 'CAR NO');
$this->excel->getActiveSheet()->SetCellValue('C1', 'Date');
$this->excel->getActiveSheet()->SetCellValue('D1', 'DAMAGE AMOUNT');
$this->excel->getActiveSheet()->SetCellValue('E1', 'FUEL AMOUNT');
$this->excel->getActiveSheet()->getStyle("A1:E1")->getFont()->setBold(true);
$rowCount = 2;
$sl_no = 1;
foreach ($result as $row) {
$this->excel->getActiveSheet()->SetCellValue('A' . $rowCount, $sl_no);
$this->excel->getActiveSheet()->SetCellValue('B' . $rowCount, $row['car_no']);
$this->excel->getActiveSheet()->SetCellValue('C' . $rowCount, date('d-m-Y H:i:s',strtotime($row['created_ts'])));
$this->excel->getActiveSheet()->SetCellValue('D' . $rowCount, $row['damage_amount']);
$this->excel->getActiveSheet()->SetCellValue('E' . $rowCount, $row['fuel']);
//......EXCEL FORMAT_NUMBER_COMMA_SEPARATED1 .............//
$this->excel->getActiveSheet()->getStyle('D'.$rowCount)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
//......Excel alignment cell text.............//
$this->excel->getActiveSheet()->getStyle('D' . $rowCount) ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$rowCount++;
$sl_no++;
}
$filename = 'income_report-' . time() . '.xlsx';
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($this->excel, 'Excel2007');
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');
exit;
}
}