<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Reports_ctrl extends user_index {
 
	
	public function __construct(){ 
		parent::__construct();
		$data = array();
		$this->load->model('admin_model');
		$this->load->model('loading_model');	
			
	}
	
	/*----------------- load users list page -----------------*/
	public function reports_list(){	
		$data['sdata'] 					= array();
		$data['subview'] 				= 'reports/reports_list';
		$data['active_cls'] 			= 'reprr_cls';		
		$data['sdata']['cl_list'] 		= $this->admin_model->get_result_array('customer_list');				
		$data['sdata']['com_list'] 		= $this->admin_model->get_result_array('commodity_list');				
		$data['sdata']['tl_list'] 		= $this->admin_model->get_result_array('transporter_list');	
		$data['sdata']['ml_list'] 		= $this->admin_model->get_meterial_list();				
		$data['sdata']['tab_active_cls'] 			= 'repp_cu_tab_cls';
		$this->load->view('common/sidepanel',$data);
	}
	

	/*----------------- load add users details page ----------------*/
	public function get_reports(){	

		$view_type_val 					= $this->input->post('view_type_val');
		$s_type 						= $this->input->post('s_type');
		$cl_id 							= $this->input->post('cl_id');
		$com_id 						= $this->input->post('com_id');
		$ml_id 							= $this->input->post('ml_id');
		$tl_id 							= $this->input->post('tl_id');
		$token 							= $this->input->post('token');
		$w_b_token 						= $this->input->post('w_b_token');
		$f_date 						= $this->input->post('f_date');
		$t_date 						= $this->input->post('t_date');

		$data['sdata']['view_type_val']	= $this->input->post('view_type_val');
		$data['sdata']['s_type']		= $this->input->post('s_type');
		$data['sdata']['cl_id']			= $this->input->post('cl_id');
		$data['sdata']['com_id']		= $this->input->post('com_id');
		$data['sdata']['ml_id']			= $this->input->post('ml_id');
		$data['sdata']['tl_id']			= $this->input->post('tl_id');
		$data['sdata']['token']			= $this->input->post('token');
		$data['sdata']['w_b_token']		= $this->input->post('w_b_token');
		$data['sdata']['f_date']		= $this->input->post('f_date');
		$data['sdata']['t_date']		= $this->input->post('t_date');
		
		if($view_type_val=='1'){
			$data['sdata']['tab_active_cls'] 			= 'repp_cu_tab_cls';
		}else if($view_type_val=='2'){
			$data['sdata']['tab_active_cls'] 			= 'repp_tl_tab_cls';
		}else if($view_type_val=='3'){
			$data['sdata']['tab_active_cls'] 			= 'repp_com_tab_cls';
		}else if($view_type_val=='4'){
			$data['sdata']['tab_active_cls'] 			= 'repp_tok_tab_cls';
		}else if($view_type_val=='5'){
			$data['sdata']['tab_active_cls'] 			= 'repp_fd_tab_cls';
		}

 
		$data['subview'] 				= 'reports/reports_list';
		$data['active_cls'] 			= 'reprr_cls';					
		$data['sdata']['cl_list'] 		= $this->admin_model->get_result_array('customer_list');				
		$data['sdata']['com_list'] 		= $this->admin_model->get_result_array('commodity_list');				
		$data['sdata']['tl_list'] 		= $this->admin_model->get_result_array('transporter_list');	
		$data['sdata']['ml_list'] 		= $this->admin_model->get_meterial_list();	
		$data['sdata']['lul_re_list']	= $this->loading_model->get_reports_loading('10',$s_type,$cl_id,$com_id,$ml_id,$tl_id,$token,$w_b_token,$f_date,$t_date);

		$data['sdata']['lul_re_ml_list']=  array();
		foreach($data['sdata']['lul_re_list'] as $lul_re_key=>$lul_re_row){
			$data['sdata']['lul_re_ml_list'][$lul_re_key]  		= $this->loading_model->get_loading_or_unloading_meterial_list($lul_re_row['lul_id']);
			// echo $this->db->last_query();return;
			
		}
		
		// echo $this->db->last_query();
		// return;
		$this->load->view('common/sidepanel',$data);
					
		
		
	}

	/*----------------- load add users details page ----------------*/
	public function get_download_reports(){	
		
		$this->load->dbutil();
		$this->load->helper('file');
		$this->load->helper('download');

		$s_type 		= $this->input->post('s_type');
		$cl_id 			= $this->input->post('cl_id');
		$com_id 		= $this->input->post('com_id');
		$ml_id 			= $this->input->post('ml_id');
		$tl_id 			= $this->input->post('tl_id');
		$token 			= $this->input->post('token');
		$w_b_token 		= $this->input->post('w_b_token');
		$f_date 		= $this->input->post('f_date');
		$t_date 		= $this->input->post('t_date');
		

		$result  		= $this->loading_model->get_reports_loading('11',$s_type,$cl_id,$com_id,$ml_id,$tl_id,$token,$w_b_token,$f_date,$t_date);




    // if($query1->num_rows() > 0){

		$delimiter 		= ",";
		$newline 		= "\r\n";
		$exp_file_name 	= 'reports_'.rand(1, 1000000) . ".csv";
		
        // $delimiter = ",";
        // $filename = "members_" . date('Y-m-d') . ".csv";

        //create a file pointer
        $f = fopen('php://memory', 'w'); 

		if($s_type=="Loading"){
			//set column headers
			$fields = array('Gate Entry Date','Loading Date','Vehicle Exit Date','Gate Entry Token No','Weighbridge Ticket No','Customer','Transporter','Truck No','Trailor No','Destination From','Customer DO No','DN Qty. in MT','Commodity','Material Name',' Gross Weight',' Tare Weight',' Net Weight',' No. of Bags','Remarks');

			//output each row of the data, format line as csv and write to file pointer
			$lineData = array('Gate Entry Date','Loading Date','Vehicle Exit Date','Gate Entry Token No','Weighbridge Ticket No','Customer','Transporter','Truck No','Trailor No','Destination From','Customer DO No','DN Qty. in MT','Commodity','Material Name',' Gross Weight',' Tare Weight',' Net Weight',' No. of Bags','Remarks');
			fputcsv($f, $lineData, $delimiter);
			
		}
		if($s_type=="Unloading"){
			//set column headers
			$fields = array('Gate Entry Date','Offloading Date','Vehicle Exit Date','Gate Entry Token No','Weighbridge Ticket No','Customer','Transporter','Truck No','Trailor No','Destination To','Customer DO No','DN Qty. in MT','Commodity','Material Name',' Gross Weight',' Tare Weight',' Net Weight',' No. of Bags','Remarks');

			//output each row of the data, format line as csv and write to file pointer
			$lineData = array('Gate Entry Date','Offloading Date','Vehicle Exit Date','Gate Entry Token No','Weighbridge Ticket No','Customer','Transporter','Truck No','Trailor No','Destination To','Customer DO No','DN Qty. in MT','Commodity','Material Name',' Gross Weight',' Tare Weight',' Net Weight',' No. of Bags','Remarks');
			fputcsv($f, $lineData, $delimiter);
			
		}



		// fputcsv($f, $lineData, $delimiter);
		
        foreach($result as $row){
			// var_dump($row['type']);
			if($token!='' && $s_type==''){
				if($row['type']=='Loading'){
					//set column headers
					$fields = array('Gate Entry Date','Loading Date','Vehicle Exit Date','Gate Entry Token No','Weighbridge Ticket No','Customer','Transporter','Truck No','Trailor No','Destination From','Customer DO No','DN Qty. in MT','Commodity','Material Name',' Gross Weight',' Tare Weight',' Net Weight',' No. of Bags','Remarks','Load By','Name','Bag Loaded','Bag Short','Kgs','Entered By','Authorized By','Loading By','Despatch By','Authorized Date - time','Entry Date - time','Entry Approve Date - time','Despatch Entry Date - time');

					//output each row of the data, format line as csv and write to file pointer
					$lineData = array('Gate Entry Date','Loading Date','Vehicle Exit Date','Gate Entry Token No','Weighbridge Ticket No','Customer','Transporter','Truck No','Trailor No','Destination From','Customer DO No','DN Qty. in MT','Commodity','Material Name',' Gross Weight',' Tare Weight',' Net Weight',' No. of Bags','Remarks','Load By','Name','Bag Loaded','Bag Short','Kgs','Entered By','Authorized By','Loading By','Despatch By','Authorized Date - time','Entry Date - time','Entry Approve Date - time','Despatch Entry Date - time');
					fputcsv($f, $lineData, $delimiter);					
				}
				if($row['type']=='Unloading'){
					//set column headers
					$fields = array('Gate Entry Date','Loading Date','Vehicle Exit Date','Gate Entry Token No','Weighbridge Ticket No','Customer','Transporter','Truck No','Trailor No','Destination To','Customer DO No','DN Qty. in MT','Commodity','Material Name',' Gross Weight',' Tare Weight',' Net Weight',' No. of Bags','Remarks','Load By','Name','Bag Loaded','Bag Short','Kgs','Entered By','Authorized By','Loading By','Despatch By','Authorized Date - time','Entry Date - time','Entry Approve Date - time','Despatch Entry Date - time');

					//output each row of the data, format line as csv and write to file pointer
					$lineData = array('Gate Entry Date','Loading Date','Vehicle Exit Date','Gate Entry Token No','Weighbridge Ticket No','Customer','Transporter','Truck No','Trailor No','Destination To','Customer DO No','DN Qty. in MT','Commodity','Material Name',' Gross Weight',' Tare Weight',' Net Weight',' No. of Bags','Remarks','Load By','Name','Bag Loaded','Bag Short','Kgs','Entered By','Authorized By','Loading By','Despatch By','Authorized Date - time','Entry Date - time','Entry Approve Date - time','Despatch Entry Date - time');
					fputcsv($f, $lineData, $delimiter);					
				}
			}else if($w_b_token!='' && $s_type==''){
				if($row['type']=='Loading'){
					//set column headers
					$fields = array('Gate Entry Date','Loading Date','Vehicle Exit Date','Gate Entry Token No','Weighbridge Ticket No','Customer','Transporter','Truck No','Trailor No','Destination From','Customer DO No','DN Qty. in MT','Commodity','Material Name',' Gross Weight',' Tare Weight',' Net Weight',' No. of Bags','Remarks','Load By','Name','Bag Loaded','Bag Short','Kgs','Entered By','Authorized By','Loading By','Despatch By','Authorized Date - time','Entry Date - time','Entry Approve Date - time','Despatch Entry Date - time');

					//output each row of the data, format line as csv and write to file pointer
					$lineData = array('Gate Entry Date','Loading Date','Vehicle Exit Date','Gate Entry Token No','Weighbridge Ticket No','Customer','Transporter','Truck No','Trailor No','Destination From','Customer DO No','DN Qty. in MT','Commodity','Material Name',' Gross Weight',' Tare Weight',' Net Weight',' No. of Bags','Remarks','Load By','Name','Bag Loaded','Bag Short','Kgs','Entered By','Authorized By','Loading By','Despatch By','Authorized Date - time','Entry Date - time','Entry Approve Date - time','Despatch Entry Date - time');
					fputcsv($f, $lineData, $delimiter);					
				}
				if($row['type']=='Unloading'){
					//set column headers
					$fields = array('Gate Entry Date','Loading Date','Vehicle Exit Date','Gate Entry Token No','Weighbridge Ticket No','Customer','Transporter','Truck No','Trailor No','Destination To','Customer DO No','DN Qty. in MT','Commodity','Material Name',' Gross Weight',' Tare Weight',' Net Weight',' No. of Bags','Remarks','Load By','Name','Bag Loaded','Bag Short','Kgs','Entered By','Authorized By','Loading By','Despatch By','Authorized Date - time','Entry Date - time','Entry Approve Date - time','Despatch Entry Date - time');

					//output each row of the data, format line as csv and write to file pointer
					$lineData = array('Gate Entry Date','Loading Date','Vehicle Exit Date','Gate Entry Token No','Weighbridge Ticket No','Customer','Transporter','Truck No','Trailor No','Destination To','Customer DO No','DN Qty. in MT','Commodity','Material Name',' Gross Weight',' Tare Weight',' Net Weight',' No. of Bags','Remarks','Load By','Name','Bag Loaded','Bag Short','Kgs','Entered By','Authorized By','Loading By','Despatch By','Authorized Date - time','Entry Date - time','Entry Approve Date - time','Despatch Entry Date - time');
					fputcsv($f, $lineData, $delimiter);					
				}
			}

			if($ml_id==''){
				$result1  		= $this->loading_model->get_loading_or_unloading_meterial_list($row['lul_id']);
				$m_namess 		= '';
				foreach($result1 as $row1){
					$m_namess .= $row1['ml_name'].' - '.$row1['luml_total_weight'].' ,';
				}
				$lineData = array($row['Entry_Date'], $row['Loading_Date'], $row['Exit_Date'], $row['SLIP_NUMBER'], $row['w_bridge_no'], $row['cust_Name'], $row['Transporter_Name'], $row['Truck_No'], $row['trailer_no'], $row['desti_place'], $row['cust_code'], $row['qty_kgs'], $row['commodity_name'], $m_namess, $row['gross_weight'], $row['tare_weight'], $row['net_weight'], $row['no_bags'], $row['remarks']);
				if($token!='' && $s_type==''){
					$lineData[23] = $row['lul_load_by'];
					$lineData[24] = $row['lul_l_name'];
					$lineData[25] = $row['lul_bag_loaded'];
					$lineData[26] = $row['lul_bag_short'];
					$lineData[26] = $row['lul_kgs'];
					
					$lineData[19] = $row['entered_by'];
					$lineData[20] = $row['authorize_by'];
					$lineData[21] = $row['loading_by'];
					$lineData[22] = $row['despatch_by'];

					$lineData[28] = $row['lul_a_o_date_time'];
					$lineData[27] = $row['lul_e_o_date_time'];
					$lineData[29] = $row['lul_leo_date_time'];
					$lineData[32] = $row['lul_d_o_date_time'];

				}else if($w_b_token!='' && $s_type==''){
					$lineData[23] = $row['lul_load_by'];
					$lineData[24] = $row['lul_l_name'];
					$lineData[25] = $row['lul_bag_loaded'];
					$lineData[26] = $row['lul_bag_short'];
					$lineData[26] = $row['lul_kgs'];
					
					$lineData[19] = $row['entered_by'];
					$lineData[20] = $row['authorize_by'];
					$lineData[21] = $row['loading_by'];
					$lineData[22] = $row['despatch_by'];

					$lineData[28] = $row['lul_a_o_date_time'];
					$lineData[27] = $row['lul_e_o_date_time'];
					$lineData[29] = $row['lul_leo_date_time'];
					$lineData[32] = $row['lul_d_o_date_time'];
				}
				
			}else{
				$lineData = array($row['Entry_Date'], $row['Loading_Date'], $row['Exit_Date'], $row['SLIP_NUMBER'], $row['w_bridge_no'], $row['cust_Name'], $row['Transporter_Name'], $row['Truck_No'], $row['trailer_no'], $row['desti_place'], $row['cust_code'], $row['qty_kgs'], $row['commodity_name'], $row['meterial_name'], $row['gross_weight'], $row['tare_weight'], $row['net_weight'], $row['no_bags'], $row['remarks']);
				if($token!='' && $s_type==''){
					$lineData[23] = $row['lul_load_by'];
					$lineData[24] = $row['lul_l_name'];
					$lineData[25] = $row['lul_bag_loaded'];
					$lineData[26] = $row['lul_bag_short'];
					$lineData[26] = $row['lul_kgs'];
					
					$lineData[19] = $row['entered_by'];
					$lineData[20] = $row['authorize_by'];
					$lineData[21] = $row['loading_by'];
					$lineData[22] = $row['despatch_by'];

					$lineData[28] = $row['lul_a_o_date_time'];
					$lineData[27] = $row['lul_e_o_date_time'];
					$lineData[29] = $row['lul_leo_date_time'];
					$lineData[32] = $row['lul_d_o_date_time'];
				}else if($w_b_token!='' && $s_type==''){
					$lineData[23] = $row['lul_load_by'];
					$lineData[24] = $row['lul_l_name'];
					$lineData[25] = $row['lul_bag_loaded'];
					$lineData[26] = $row['lul_bag_short'];
					$lineData[26] = $row['lul_kgs'];

					$lineData[19] = $row['entered_by'];
					$lineData[20] = $row['authorize_by'];
					$lineData[21] = $row['loading_by'];
					$lineData[22] = $row['despatch_by'];

					$lineData[28] = $row['lul_a_o_date_time'];
					$lineData[27] = $row['lul_e_o_date_time'];
					$lineData[29] = $row['lul_leo_date_time'];
					$lineData[32] = $row['lul_d_o_date_time'];
				}			
			}
		
            fputcsv($f, $lineData, $delimiter);
		}
		// return;
        //move back to beginning of file
        fseek($f, 0);

        //set headers to download file rather than displayed
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename="' . $exp_file_name . '";');

        //output all remaining data on a file pointer
        fpassthru($f);
		// }
	

		// echo $this->db->last_query();
		// var_dump($query1);return;
		/* -- running code --*/
		/*$delimiter 		= ",";
		$newline 		= "\r\n";
		$data 			= $this->dbutil->csv_from_result($query1, $delimiter, $newline);
		// var_dump($data);return;	
		$exp_file_name 	= 'reports_'.rand(1, 1000000);*/
		// force_download(''.$exp_file_name.'.csv', $data);
		// force_download(''.$exp_file_name, $data);
		force_download(''.$exp_file_name);
	}
	
	

	
}
