<?php 
ini_set('display_errors', 1);
class Tree_view_model extends Model 
{
    protected $ureg; 
	
    public function Tree_view_model() 
    {
        parent::Model();
        $this->CI =& get_instance();
        $this->load->database();
        $this->load->library('session');
        $this->tbl_group = "group_master";
        $this->tbl_users = "tbl_users";
        $this->assests_master = "assests_master";
    }
    public function getUreg() {
	return $this->user;
    }
    public function setUreg($ureg) {
	$this->user = $ureg;
    }
    public function getChildren($ureg) {
        
        $this->db->select('*');
        $this->db->where("status",1);
        $this->db->where("admin_id",$ureg);
        $this->order_by = 'user_id';
        $query = $this->db->get($this->tbl_users);
        return $query->result();
        
        
    }
    public function getVehicle() {
       // $user = $this->getUreg();
       /* $this->db->select('*');
        $this->db->where("status",1);
        $this->db->where("add_uid",$this->getUreg());
        $this->order_by = 'id';
        $query = $this->db->get($this->assests_master);
        return $query->result();*/
       
        $SQL = "SELECT id, assets_name, device_id  FROM assests_master WHERE status=1 ";
        //if($this->session->userdata('usertype_id')!=1){
            $SQL .= " AND find_in_set(id, (SELECT assets_ids FROM user_assets_map where user_id = ".$this->getUreg()."))";
       // }
        $query = $this->db->query($SQL);
        return $query->result();
		
        
    }
     public function getVehicleCount() {
       // $user = $this->getUreg();
       /* $this->db->select('*');
        $this->db->where("status",1);
        $this->db->where("add_uid",$this->getUreg());
        $this->order_by = 'id';
        $query = $this->db->get($this->assests_master);
        return $query->result();*/
       
        $SQL = "SELECT count(*) as veh_count FROM assests_master WHERE status=1 ";
        //if($this->session->userdata('usertype_id')!=1){
            $SQL .= " AND find_in_set(id, (SELECT assets_ids FROM user_assets_map where user_id = ".$this->getUreg()."))";
       // }
        $query = $this->db->query($SQL);
        return $query->result();
		
        
    }
    public function last_location($user,$imeis)
	{
		
		if($imeis != ""){
			$gsub .= " WHERE  am.device_id IN $imeis";
		}
               $Lqry1="SELECT "
                        . "am.device_id, am.driver_name, am.assets_image_path, am.driver_mobile as driver_contact_no, acm.assets_cat_image,am.assets_name, am.driver_name,  am.driver_mobile, "
                        . "am.sim_number, am.driver_image, am.km_reading, am.eng_runtime, "
                        . "CONVERT_TZ(lm.add_date,'+00:00','".$this->session->userdata('timezone')."') as add_date, "
                        . "TIME_TO_SEC(TIMEDIFF( NOW( ), CONVERT_TZ(lm.add_date,'+00:00','".$this->session->userdata('timezone')."'))) "
                        . "as beforeTime, lm.address, lm.runtime, lm.lati, lm.longi, lm.speed,  lm.ignition,  "
                        . "lm.current_area, lm.current_zone, lm.current_landmark, lm.cross_speed,  "
                        . "lm.temperature, lm.reason_text, lm.reason, "
                        . "lm.in_batt,lm.Blender,lm.ACC, lm.ext_batt_volt, lm.captured_image FROM assests_master "
                        . "am left join tbl_last_point lm on lm.device_id = am.device_id left join assests_category_master acm ON acm.id=am.assets_category_id "
                        . "AND am.del_date IS NULL $gsub ORDER BY am.assets_name";
		// echo $Lqry1;
		$query = $this->db->query($Lqry1, FALSE);

		return $query->result();
	}
	
}
  