<?php 

class Company_model extends CI_Model {
	
	public function _consruct(){
		parent::_construct();
 	}
	
 	public function addCompany($company_data = array()){
 		if(empty($company_data)){
 			return 0;
 		}
 		$emailChk = $this->db->get_where('admin_users',array('username'=>$company_data['email_id'],'status !='=>'2'));
 		if(!empty($emailChk) && $emailChk->num_rows() > 0){
 			return 2;
 		}
 		$status = $this->db->insert('admin_users',array('username'=>$company_data['email_id'],'password'=>md5($company_data['password']),'user_type'=>'2','display_name'=>$company_data['company_name'],'profile_image'=>$company_data['profile_image']));
 		if($status){
 			$company_id = $this->db->insert_id();

 			$status = $this->db->insert('company',array('company_id'=>$company_id,'company_name'=>$company_data['company_name'],'address'=>$company_data['address'],'phone'=>$company_data['phone'],'fax'=>$company_data['fax'],'email_id'=>$company_data['email_id'],'company_contact'=>$company_data['company_contact'],'company_info'=>$company_data['company_info'],'company_federal_id'=>$company_data['company_federal_id']));

 			$this->db->insert('company_offers',
 									array('offer_type'=>'3','company_id'=>$company_id,'applicable_on'=>'0',
 										  'activation_date'=>strtotime(date('d-M-y')),
 										  'expiry_date'=>strtotime('+30 days',strtotime((date('d-M-y')))),
 										  'status'=>'1'));

 			$this->db->insert('company_payment_details',array('company_id'=>$company_id,'booking_charge'=>$company_data['booking_charge'],'sms_charge'=>$company_data['sms_charge'],'push_charge'=>$company_data['push_charge'],'report_charge'=>$company_data['report_charge']));

 			return ($status)?1:0;
 		}
 		return 0;
 	}

 	function getCompany($company_id = '',$status = '1'){
 		$cond = (!empty($company_id))?" AND CMP.company_id = '$company_id'":"";

 		$sql = "SELECT CMP.*,AU.*,CPAY.*
 				FROM company AS CMP
 				INNER JOIN admin_users AS AU ON (AU.id = CMP.company_id)
 				INNER JOIN company_payment_details AS CPAY ON (CPAY.company_id = CMP.company_id)
 				WHERE AU.status IN ($status) $cond";
		$result = $this->db->query($sql);
		if(empty($result)){
			return;
		}
		return (empty($company_id))?$result->result():$result->row();
 	}

 	function changeStatus($company_id = '', $status = '0'){
 		if(empty($company_id)){
 			return 0;
 		}
 		$status = $this->db->update('admin_users',array('status'=>$status),array('id'=>$company_id));
 		return $status;
 	}

 	function updateCompany($company_id = '', $company_data = array()){
 		if(empty($company_id) || empty($company_data)){
 			return 0;
 		}
 		$emailChk = $this->db->get_where('admin_users',array('username'=>$company_data['email_id'],'status !='=>'2','id != '=>$company_id));
 		if(!empty($emailChk) && $emailChk->num_rows() > 0){
 			return 2;
 		}
 		$upArr = array('username'=>$company_data['email_id'],'display_name'=>$company_data['company_name']);
 		if(!empty($company_data['profile_image'])){
 			$upArr['profile_image'] = $company_data['profile_image'];
 		}
 		$status = $this->db->update('admin_users', $upArr, array('id'=>$company_id));
 		if($status){
 			$status = $this->db->update('company',array('company_name'=>$company_data['company_name'],'address'=>$company_data['address'],'phone'=>$company_data['phone'],'fax'=>$company_data['fax'],'email_id'=>$company_data['email_id'],'company_contact'=>$company_data['company_contact'],'company_info'=>$company_data['company_info'],'company_federal_id'=>$company_data['company_federal_id']),array('company_id'=>$company_id));
 			$this->db->update('company_payment_details',array('booking_charge'=>$company_data['booking_charge'],'sms_charge'=>$company_data['sms_charge'],'push_charge'=>$company_data['push_charge'],'report_charge'=>$company_data['report_charge']),array('company_id'=>$company_id));
 			return ($status)?1:0;
 		}
 		return 0;
 	}

 	function getOfferData($offer_type = '1,2'){
 		$cond = " AND COF.offer_type IN (".$offer_type.") ";

 		$sql = "SELECT CMP.*, COF.*
 				FROM company AS CMP 
 				INNER JOIN admin_users AS AU ON (AU.id = CMP.company_id)
 				INNER JOIN company_offers AS COF ON (CMP.company_id = COF.company_id)
 				WHERE AU.status = '1' AND COF.status != '2' ".$cond."
 				ORDER BY CMP.company_name";

		$result = $this->db->query($sql);
		if(empty($result)){
			return 0;
		}
		return $result->result();
 	}

 	function changeOfferStatus($offer_id = '', $status = '1'){
 		if(empty($offer_id)){
 			return 0;
 		}
 		if($status == 2){
 			$offrData = $this->db->get_where('company_offers',array('id'=>$offer_id));

 			if(!empty($offrData) && !empty($offrData = $offrData->row_array()) && 
 			   isset($offrData['expiry_date']) && !empty($offrData['expiry_date']) && 
 			   isset($offrData['activation_date']) && !empty($offrData['activation_date']) && 
 			   $offrData['activation_date'] < strtotime(date('d-M-y')) && 
 			   $offrData['expiry_date'] > strtotime(date('d-M-y'))){

 			   $this->db->update('company_offers', array('expiry_date'=>strtotime(date('d-M-y'))), 
 			   									   array('id'=>$offer_id));
 			}
 		}
 		$status = $this->db->update('company_offers',array('status'=>$status), array('id'=>$offer_id));
 		
 		return ($status)?1:0;
 	}

 	function addOffer($offer_data = array()){
 		if(empty($offer_data)){
 			return 0;
 		}
 		$offer_data['expiry_date'] = strtotime($offer_data['expiry_date']);
 		$offer_data['activation_date'] = strtotime($offer_data['activation_date']);
 		$sql = "SELECT * FROM company_offers
  				WHERE company_id = '".$offer_data['company_id']."' AND  
  					  (((activation_date <= '".$offer_data['activation_date']."' AND  
				  	     expiry_date >= '".$offer_data['activation_date']."') OR
 					  	 (activation_date <= '".$offer_data['expiry_date']."' AND  
 					  	 expiry_date >= '".$offer_data['expiry_date']."')
				  	   ) OR 
 					   (activation_date >= '".$offer_data['activation_date']."' AND 
					   expiry_date <= '".$offer_data['expiry_date']."')
					  ) AND status != '2'";
 		$result = $this->db->query($sql);
 		if(!empty($result) && $result->num_rows() >= 1){
 			return 2;
 		}
 		$offer_data['status'] = 1;
 		$status = $this->db->insert('company_offers',$offer_data);
 		return ($status)?1:0;
 	}

 	function getPayDetails($company_id = ''){
 		if(empty($company_id)){
 			return 0;
 		}
 		$result = $this->db->get_where('company_payment_details',array('company_id'=>$company_id));
 		if(empty($result)){
 			return 0;
 		}
 		return $result->row();
 	}

 	function getBrokerPayDetails($company_id = ''){
 		if(empty($company_id)){
 			return 0;
 		}
 		$qryResp = $this->db->query("SELECT DISTINCT(TD.broker_id),BRK.*,BKR_PAY.last_paid_date
 									 FROM transport_details AS TD
 									 INNER JOIN brokers AS BRK ON (BRK.broker_id = TD.broker_id)
 									 LEFT JOIN broker_payment_details AS BKR_PAY ON 
											(BKR_PAY.broker_id = TD.broker_id AND 
											 BKR_PAY.company_id = TD.company_id AND BKR_PAY.status = '1') 
 									 WHERE TD.company_id = '".$company_id."'");
 		if(empty($qryResp)){
 			return 0;
 		}
 		$tmpData = array();
 		$brkPayData = array();
 		$qryResp = $qryResp->result_array();
 		foreach ($qryResp AS $brkData) {
 			$tmpData = $brkData;

 			$data = $this->db->get_where('transport_details',array('broker_id' => $brkData['broker_id'],
	 															   'company_id' => $company_id));
 			$tmpData['all_trans_count'] = (!empty($data->num_rows()))?$data->num_rows():'0';

 			$data = $this->db->get_where('transport_details',array('broker_id'=>$brkData['broker_id'],
	 															   'company_id'=>$company_id,
	 															   'status !='=>'2','is_scheduled'=>'1'));
 			$tmpData['pro_trans_count'] = (!empty($data->num_rows()))?$data->num_rows():'0';

 			$tmpData['pending_trans_count'] = $tmpData['pro_trans_count'];
 			if(!empty($brkData['last_paid_date'])){
 				$data = $this->db->get_where('transport_details',
 												array('broker_id'=>$brkData['broker_id'],
												      'company_id'=>$company_id,
												      'status !='=>'2','is_scheduled'=>'1',
												      'created_date >='=> $brkData['last_paid_date']));

 				$tmpData['pending_trans_count'] = (!empty($data->num_rows()))?$data->num_rows():'0';
 			}
 			$tmpData['encoded_broker_id'] = encode_param($brkData['broker_id']);
 			$brkPayData[$brkData['broker_id']] = $tmpData;
 		}
 		return $brkPayData;
 	}

 	public function updatePaymentDate($data = array()){
 		if(empty($data) || !isset($data['broker_id']) || !isset($data['last_paid_date']) || 
 		   !isset($data['company_id']) || empty($data['company_id']) || empty($data['broker_id']) || 
 		   empty($data['last_paid_date'])){
 			return 0;
 		}
 		$this->db->update('broker_payment_details',array('status'=>'2'),
						  array('broker_id'=>$data['broker_id'],'company_id'=>$data['company_id']));
 		
 		$status = $this->db->insert('broker_payment_details',$data);
 		return ($status)?1:0;
 	}
}
?>