<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Approval_final_model extends Ci_Model
{

    public function __construct()
    {
        parent::__construct();
		}
		
    
    function getclaims($company_id) {
        $sql = "SET SQL_BIG_SELECTS=1";
		$this->db->query($sql);
		
			$where = array(
						'caf.fa_id'		=> $company_id,
						'c.status'  => 3,
						);
	
        

		$query = $this->db
						->select("c.name as cname,
											c.refno as refno,
											(SELECT DATE(date) FROM claim_status_logs where refno = c.refno and status_id = c.status ORDER BY cs_id DESC LIMIT 1) as date,
											c.admin_id as adminid,
											c.name as claimname,
											(SELECT type FROM claim_types WHERE ct_id = c.ct_id) as ct,
											a.firstname as dis,
											caf.ticketno as ticketno,
											c.icam as icamid
										
										")
			->from("claims c")
			->join("admins a","c.admin_id = a.admin_id","left")
			->join("claim_application_final caf","c.refno = caf.refno","left")
			->where($where)
			->get();
			//echo $this->db->last_query();
			if(!$query) return false;
			if($query->num_rows()>0) return $query->result();
			else return false;
		}

	function claims($adminid,$company_id) {
        $sql = "SET SQL_BIG_SELECTS=1";
		$this->db->query($sql);
		if($adminid ==''){
			$where = array(
						'caf.fa_id'		=> $company_id,
						'c.status'  => 3,
						
						
        );
		}else{
			$where = array(
						'caf.fa_id'		=> $company_id,
						'c.status'  => 3,
						'a.admin_id'=> $adminid,
						
        );
		}
		
        

		$query = $this->db
						->select("c.name as cname,
											c.refno as refno,
											(SELECT DATE(date) FROM claim_status_logs where refno = c.refno and status_id = c.status ORDER BY cs_id DESC LIMIT 1) as date,
											c.admin_id as adminid,
											c.name as claimname,
											(SELECT type FROM claim_types WHERE ct_id = c.ct_id) as ct,
											a.firstname as dis,
											caf.ticketno as ticketno,
											c.icam as icamid
										
										")
			->from("claims c")
			->join("admins a","c.admin_id = a.admin_id","left")
			->join("claim_application_final caf","c.refno = caf.refno","left")
			->where($where)
			->get();
			//echo $this->db->last_query();
			if(!$query) return false;
			if($query->num_rows()>0) return $query->result();
			else return false;
		}

		

    function getdocus($refno) {
        $sql = "SET SQL_BIG_SELECTS=1";
		$this->db->query($sql);

		$where = array(
            'refno'		=> $refno,
        );
        

		$query = $this->db
            ->select("cd_id as id, dt.type as doc
					",FALSE)
            ->from("claim_docus cd")
            ->join("doc_types AS dt","cd.doc_id=dt.id","left")
			->where($where)
			->order_by('dt.id ASC')
			->get();
			//echo $this->db->last_query();
			if(!$query) return false;
			if($query->num_rows()>0) return $query->result();
			else return false;
    }

}
