<?php 
class Report_model extends CI_Model {
	
	public function _consruct(){
		parent::_construct();
 	}

 	public function get_report($fields = array(), $where_cond = array()){
 		if(empty($fields)){
 			return 0;
 		}
 		$where_clause = '';
 		if(!empty($where_cond)){
 			if(!empty($where_cond['company_id'])){
 				$where_clause = " WHERE TDLS.company_id = '".$where_cond['company_id']."' ";
 			}
 			if(!empty($where_cond['broker_id'])){
 				$where_clause = " WHERE TDLS.broker_id = '".$where_cond['broker_id']."' ";
 			}
 			if(!empty($where_cond['start_date']) && !empty($where_cond['end_date'])){
 				$where_clause .= (empty($where_clause))?' WHERE ':' AND ';

 				$end_date = strtotime(trim($where_cond['end_date']).' 12:00');
 				$start_date = strtotime(trim($where_cond['start_date']).' 12:00');
 				$where_clause .= " TDLS.appointment_time >= '".$start_date."' AND 
 								   TDLS.appointment_time <= '".$end_date."' ";
 			}
 			if(!empty($where_cond['start_date'])){
 				$where_clause .= (empty($where_clause))?' WHERE ':' AND ';

 				$start_date = strtotime(trim($where_cond['start_date']).' 12:00');
 				$where_clause .= " TDLS.appointment_time >= '".$start_date."' ";
 			}
 			if(!empty($where_cond['end_date'])){
 				$where_clause .= (empty($where_clause))?' WHERE ':' AND ';

 				$end_date = strtotime(trim($where_cond['end_date']).' 12:00');
 				$where_clause .= " TDLS.appointment_time <= '".$end_date."' ";
 			}
 			if(!empty($where_cond['status'])){
 				$where_clause .= (empty($where_clause))?' WHERE ':' AND ';

 				$where_clause .= " TDLS.status = '".$where_cond['status']."' ";
 			}
 		}
 		$sql = "SELECT ".$fields."
	 				FROM transport_details AS TDLS 
	 				LEFT JOIN drivers AS DRV ON (DRV.driver_id = TDLS.assigned_driver)
	 				LEFT JOIN vehicle_types AS VEH ON (VEH.vehicle_id = DRV.vehicle_id)
	 				LEFT JOIN company AS CMP ON (CMP.company_id = TDLS.company_id)
	 				LEFT JOIN company_payment_details AS CPAY ON (CPAY.company_id = TDLS.company_id)
	 				LEFT JOIN brokers AS BRKR ON (BRKR.broker_id = TDLS.broker_id)
	 				LEFT JOIN appointment_reason AS APRES ON (APRES.reason_id = TDLS.reason_code)
	 				LEFT JOIN trip_status AS TSTATUS ON (TSTATUS.trip_status_id = TDLS.trip_status)
	 				LEFT JOIN trip_type AS TTYPE ON (TTYPE.trip_id = TDLS.trip_type)
 				".$where_clause."
 				ORDER BY TDLS.transport_id ASC";
 		$data = $this->db->query($sql);
 		if(!empty($data)){
 			$resData = $data->result_array();
 			if(empty($resData)){
 				return 2;
 			}
 			foreach($resData AS $key => $data){
 				$resData[$key]['Appointment_Time'] = (!empty($data['Appointment_Time']))?date('d-M-y G:i',$data['Appointment_Time']):'';
 			}
 			return $resData;
 		}
 		return 0;
 	}
}