<?php

class Model_DbTable_TransferRequest extends Zend_Db_Table
{
    protected $_name = 'TransferRequest';
    public function insertData($data)
    {
        try 
        {
            $this->insert($data);
        } 
        catch (Exception $ex) 
        {
            return false;
        }
    }
    public function updateData($data,$id)
    {
       
        try
        {
            return $this->update($data,"TID ='".$id."'");        
        }
        catch(Exception $e)
        {
            print_r($e);
            return false;
        }
    }
    
    public function getRequests()
    {
        $select = $this->select();
        $select->from(array('T' => $this->_name));
				$select->joinLeft(array("P"=>"profile"), "P.UserID = T.UserID",array("FirstName","LastName"));
				$select->where("T.Status =1");
				
					// $select->where(");
			
        $select->setIntegrityCheck(false);
        return $this->fetchAll($select);
    }
    public function getRequest($id)
    {
        $select = $this->select();
        $select->from(array('T' => $this->_name));
				$select->joinLeft(array("U"=>"profile"), "U.UserID = T.UserID",array("FirstName","LastName"));
                                $select->joinLeft(array("R"=>"profile"), "R.UserID = T.AuthorID",array("AuthorFname"=>"FirstName","AuthorLname"=>"LastName"));
                                $select->joinLeft(array("O"=>"ef_unions"), "O.un_id = T.UnionFrom",array("FromUnion"=>"un_name","FroUnionID"=>"un_id"));
				$select->joinLeft(array("A"=>"ef_unions"), "A.un_id = T.UnionTo",array("ToUnion"=>"un_name","ToUnionID"=>"un_id","ToUnionType"=>"un_type"));
				$select->where("T.TID ='".$id."'");
				
					// $select->where(");
			
        $select->setIntegrityCheck(false);
        return $this->fetchRow($select);
    }
	
	public function All_Users(){
		
		  	$m = date('Y-m-d', strtotime('-3 month'));
        	$select = $this->select();
       	 	$select->from(array('T' => "TransferRequest_view"));
			$select->where("T.Status =0 ");
			$select->limit(15)->group("UserID");			
					// $select->where(");
			
       		 $select->setIntegrityCheck(false);
        	return $this->fetchAll($select);
	}
	public function All_Union_Users($UnionID){
		$select = $this->select();
       	 	$select->from(array('T' => "TransferRequest_view"));
			$select->where("parentid ='".$UnionID."' OR UnionFrom ='".$UnionID."' AND T.Status =0 ");
			$select->limit(15)->group("UserID");			
			
			$select->setIntegrityCheck(false);
        	return $this->fetchAll($select);
	}
    
    public function getRequestsCompleted()
    {
         $auth = Zend_Auth::getInstance();
          $profile = $auth->getIdentity();
			if($profile->UnionType < 3) {
			return $this->All_Users();
			}else{
				return $this->All_Union_Users($profile->UnionID);
			}
    }
}
