<?php

abstract class Agro_Db_Table_Relatorios extends Agro_Db_Table_Abstract {
	protected $_schema = '';
	protected $_name = '';
	protected $_primary = '';

	public function init() {
	}

	public function fetchAll($sql) {
	    return $this->_db->fetchAll($sql);
	}

	public function getLastReport($action) {
		$select = $this->select()->setIntegrityCheck(false);
		$select->from(array('lrg' => 'log.log_relatorios_gerados'), array("to_char(max(lrg.dt_criacao), 'DD/MM/YYYY - HH24:MI:SS') as dt_criacao", "filename"));
		$select->join(array('u' => 'sistema.usuarios'), 'lrg.id_usuario_criacao = u.id',
			"initcap(substr(ds_nome_usuario, 0, strpos(ds_nome_usuario,'.'))) || ' ' || initcap(substr(ds_nome_usuario, strpos(ds_nome_usuario,'.')+1, length(ds_nome_usuario))) as ds_nome_usuario");
		$select->where('lrg.ds_controller = ?', Zend_Controller_Front::getInstance()->getRequest()->getControllerName());
		$select->where('lrg.ds_action = ?', $action);
		$select->where('lrg.filename is not null');
		$select->group('u.ds_nome_usuario');
		$select->group('lrg.filename');
		$select->group('lrg.dt_criacao');
		$select->order('lrg.dt_criacao DESC');
		$select->limit(1);

		return $this->fetchRow($select);
	}

}
