<?php

class Solicitacao_Model_Row_Tarefa extends Agro_Db_Table_Row_Abstract {
	public $departamento, $solicitante, $executor, $prioridade, $andamentos, $tipo, $status, $arquivos, $label_status = null;
	public $ds_chave_departamento;
	
	public function dados() {
		if ($this->id) {
			$this->departamento = $this->departamento();
			$this->solicitante = $this->solicitante();
			$this->prioridade = $this->prioridade();
			$this->andamentos = $this->andamentos();
			$this->executor = $this->executor();
			$this->status = $this->status();
			$this->tipo = $this->tipo();
                        $this->arquivos = $this->arquivos();
		}
	}
        
        public function arquivos() {
            $Select1 = $this->select()->setIntegrityCheck(false);
            $Select1->from(array('ta' => 'solicitacao.tarefa_arquivos'), array('id_tarefa as id', 'ds_nome_arquivo'));
            $Select1->where('ta.id_tarefa = ?', $this->id);
            
            //Para manter retro compatibilidade com arquivos de tarefas antigas.
            $Select2 = $this->select()->setIntegrityCheck(false);
            $Select2->from(array('t' => 'solicitacao.tarefa'), array('id', 'ds_arquivo as ds_nome_arquivo'));
            $Select2->where('t.ds_arquivo is not null');
            $Select2->where('t.id = ?', $this->id);
            
            $Select = $this->select()->setIntegrityCheck(false);
            $Select->union(array($Select1, $Select2));
            
            return Agro_Db_Table_Abstract::getDefaultAdapter()->fetchAll($Select);
        }
	
	public function solicitante() {
		$Select = $this->select()->setIntegrityCheck(false);
		$Select->from(array('t' => 'solicitacao.tarefa'), false);
		$Select->join(array('u' => 'sistema.usuarios'), 'u.id = t.id_usuario_criacao', 'ds_nome_usuario');
		$Select->join(array('f' => 'sistema.funcionarios'), 'u.id = f.id_usuario');
		$Select->join(array('d' => 'sistema.departamentos'), 'd.id = f.id_departamento', 'ds_nome_departamento');
		$Select->where('t.id = ?', $this->id);
		
		return Agro_Db_Table_Abstract::getDefaultAdapter()->fetchRow($Select);
	}
	
	public function executor() {
		$Select = $this->select()->setIntegrityCheck(false);
		$Select->from(array('t' => 'solicitacao.tarefa'), false);
		$Select->join(array('u' => 'sistema.usuarios'), 'u.id = t.id_executor', 'ds_nome_usuario');
		$Select->join(array('f' => 'sistema.funcionarios'), 'u.id = f.id_usuario');
		$Select->join(array('d' => 'sistema.departamentos'), 'd.id = f.id_departamento', 'ds_nome_departamento');
		$Select->where('t.id = ?', $this->id);
		
		return Agro_Db_Table_Abstract::getDefaultAdapter()->fetchRow($Select);
	}
	
	public function departamento() {
		$Select = $this->select()->setIntegrityCheck(false);
		$Select->from('sistema.departamentos', array('ds_chave','ds_nome_departamento'));
		$Select->where('id = ?', $this->id_departamento);
		
		return Agro_Db_Table_Abstract::getDefaultAdapter()->fetchRow($Select);
	}
	
	public function prioridade() {
		$Select = $this->select()->setIntegrityCheck(false);
		$Select->from('solicitacao.tarefa_prioridade', 'ds_nome_prioridade');
		$Select->where('id = ?', $this->id_prioridade);
		
		return Agro_Db_Table_Abstract::getDefaultAdapter()->fetchOne($Select);
	}
	
	public function andamentos() {
		$Select = $this->select()->setIntegrityCheck(false);
		$Select->from(array('ta' => 'solicitacao.tarefa_andamento'));
		$Select->join(array('u' => 'sistema.usuarios'), 'u.id = ta.id_usuario_criacao', 'ds_nome_usuario');
		$Select->where('id_tarefa = ?', $this->id);
		$Select->order('dt_criacao ASC');
		
		$andamentos = Agro_Db_Table_Abstract::getDefaultAdapter()->fetchAll($Select);
                
                foreach ($andamentos as $andamento) {
                    $Select1 = $this->select()->setIntegrityCheck(false);
                    $Select1->from(array('taa' => 'solicitacao.tarefa_andamento_arquivos'), array('id_andamento as id', 'ds_nome_arquivo'));
                    $Select1->where('taa.id_andamento = ?', $andamento['id']);

                    //Para manter retro compatibilidade com arquivos de tarefas antigas.
                    $Select2 = $this->select()->setIntegrityCheck(false);
                    $Select2->from(array('ta' => 'solicitacao.tarefa_andamento'), array('id', 'ds_arquivo_name as ds_nome_arquivo'));
                    $Select2->where('ta.ds_arquivo_name is not null');
                    $Select2->where('ta.id = ?', $andamento['id']);
                    
                    $Select = $this->select()->setIntegrityCheck(false);
                    $Select->union(array($Select1,$Select2));
                    
                    $andamento['arquivos'] = Agro_Db_Table_Abstract::getDefaultAdapter()->fetchAll($Select);
                    $adm[] = $andamento;
                }
                
                return $adm;
	}
        
	public function tipo() {
		$Select = $this->select()->setIntegrityCheck(false);
		$Select->from('solicitacao.tarefa_tipo', 'ds_nome_tipo');
		$Select->where('id = ?', $this->id_tarefa_tipo);
		
		return Agro_Db_Table_Abstract::getDefaultAdapter()->fetchOne($Select);
	}
	
	public function status() {
		$Select = $this->select()->setIntegrityCheck(false);
		$Select->from('sistema.status', array('ds_chave', 'ds_status'));
		$Select->where('id = ?', $this->id_status);
		$Row = Agro_Db_Table_Abstract::getDefaultAdapter()->fetchRow($Select);
		
		$this->label_status = $Row['ds_status'];
		$this->status = $Row['ds_chave'];
		
		return $this->status;
	}
	
	public function rowStatus(){
		$Model = new Sistema_Model_Status();
		return $Model->find($this->id_status)->current();
	}
	
	public function log(){
		$Select = $this->select()->setIntegrityCheck(false);
		$Select->from(array("tl"=>"solicitacao.tarefa_log"), array("tl.id_tarefa", "to_char(tl.dt_criacao, 'DD/MM/YYYY HH24:MI:SS') as dt_criacao", "tl.ds_descricao"));
		$Select->joinInner(array("u"=>"sistema.usuarios"), "u.id = tl.id_usuario_criacao", "ds_nome_usuario");
		$Select->joinInner(array("f"=>"sistema.funcionarios"), "f.id_usuario = u.id", array("f.ds_nome_funcionario||' ('||u.ds_nome_usuario||')' as ds_nome_funcionario"));
		$Select->where("tl.id_tarefa = {$this->id}");
		$Select->order("tl.dt_criacao desc");
		
		return Agro_Db_Table_Abstract::getDefaultAdapter()->fetchAll($Select);
	
	}	
}