<?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){
 			unset($company_data['password']);
 			unset($company_data['profile_image']);
 			unset($company_data['display_name']);
 			$company_data['company_id'] = $this->db->insert_id();
 			$status = $this->db->insert('company',$company_data);
 			return ($status)?1:0;
 		}
 		return 0;
 	}

 	function getCompany($company_id = ''){
 		$cond = (!empty($company_id))?" AND company_id = '$company_id'":"";

 		$sql = "SELECT CMP.*,AU.* 
 				FROM company AS CMP
 				INNER JOIN admin_users AS AU ON (AU.id = CMP.company_id)
 				WHERE AU.status IN (0,1) $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));
 			return ($status)?1:0;
 		}
 		return 0;
 	}

}
?>