<?php

class Sistema_Model_Departamentos extends Agro_Db_Table_Abstract {
	protected $_rowClass = 'Sistema_Model_Row_Departamento';
	public $_pairs = array('key' => 'id', 'field' => 'id_empresa', 'field' => 'ds_nome_departamento', 'field' => 'ds_chave');
	protected $_schema = 'sistema';
	protected $_name = 'departamentos';
	protected $_primary = 'id';
	public $_order = 'ds_nome_departamento';

	const AGRO_ATENDIMENTO = 20;
	const AGRO_COMERCIAL = 27;
	const AGRO_GERENCIA = 24;
	const AGRO_FINANCEIRO = 28;
	const AGRO_MARKETING = 22;
	const AGRO_SINISTRO = 23;
	const AGRO_TECNICA = 26;
	const AGRO_TI = 18;

	protected $_referenceMap    = array (
			'Sistema_Model_Empresas' => array (
			'columns'           => array('id_empresa'),
			'refTableClass'     => 'Sistema_Model_Empresas',
			'refColumns'        => array('id')
		)
    );

	public function save(array $data) {
		$Row = $this->getRow($data['id']);

		$Row->id_empresa = $data['id_empresa'];
		$Row->ds_nome_departamento = $data['ds_nome_departamento'];
		$Row->ds_chave = $data['ds_chave'];
		$Row->id_funcionario = ($data['id_funcionario'] ? $data['id_funcionario'] : null);

		return $Row->save();
	}

	public function fetchPairs($idEmpresa='') {
		$Rs = $this->getDepartamentos($idEmpresa);
		$pairs = array('' => '');

		if ($Rs) {
			foreach ($Rs as $row) {
				$pairs[$row['id']] = $row['ds_nome_departamento'];
			}
		}

		return $pairs;
	}

	public function getComboDeptEmpresa($id_empresa) {
		$select = $this->select()->order('ds_nome_departamento');

		$select->where('id_empresa = ?', $id_empresa);


		$Rs = $this->fetchAll($select);

		$ret = array();
		if ($Rs->count()) {
			foreach ($Rs as $Row) {
				$ret[$Row->id] = $Row->ds_nome_departamento;
			}
		}

		return $ret;
	}

	public function getCombo($chave, $tipoUsuario='') {
		$select = $this->select()->setIntegrityCheck(false);
		$select->from(array('d' => 'sistema.departamentos'), array('id', 'ds_nome_departamento', 'ds_chave'));
		$select->join(array('e' => 'sistema.empresas'), 'd.id_empresa = e.id', null);
		$select->join(array('c' => 'sistema.categorias'), 'e.id_categoria = c.id', null);
		$select->where('c.ds_chave = ?', $chave);
		$select->order('ds_nome_departamento');
		$Rs = $this->fetchAll($select);

		if ($Rs->count()) {
			$return = array('' => '');
			foreach ($Rs as $Row) {
				if ($tipoUsuario == 'C' || $tipoUsuario == 'P') {
					if ($Row->ds_chave == "ATENDIMENTO" || $Row->ds_chave == "SINISTRO") {
						$return[$Row->id] = $Row->ds_nome_departamento;
					}
				} else {
					$return[$Row->id] = $Row->ds_nome_departamento;
				}
			}
		}

		return $return;
	}

	public function getComboChamados($chave) {
		$User = Zend_Auth::getInstance()->getIdentity();

		$select = $this->select()->setIntegrityCheck(false);
		$select->from(array('d' => 'sistema.departamentos'), array('id', 'ds_nome_departamento', 'ds_chave'));
		$select->join(array('e' => 'sistema.empresas'), 'd.id_empresa = e.id', null);
		$select->join(array('c' => 'sistema.categorias'), 'e.id_categoria = c.id', null);
		$select->where('c.ds_chave = ?', $chave);
		$select->order('ds_nome_departamento');

		$Rs = $this->fetchAll($select);

		if ($Rs->count()) {
			$return = array('' => '');
			foreach ($Rs as $Row) {
				if($User->tp_usuario == 'V' && ($Row->ds_chave == "SINISTRO" || $Row->ds_chave == "TECNICA")){
					$return[$Row->id] = $Row->ds_nome_departamento;
				
				}else if ($User->tp_usuario != 'V' &&($Row->ds_chave == "ATENDIMENTO" || $Row->ds_chave == "TECNICA" || $Row->ds_chave == "SINISTRO")) {
					$return[$Row->id] = $Row->ds_nome_departamento;
				}
			}
		}

		return $return;
	}

	public function getComboAgroBrasil() {
		$return = array('');

		$Sql = $this->select()->setIntegrityCheck(false);
		$Sql->from(array('d' => 'sistema.departamentos'), array('id', 'ds_nome_departamento'));
		$Sql->join(array('e' => 'sistema.empresas'), 'e.id = d.id_empresa', false);
		$Sql->where('e.cpf_cnpj = ?', '03.486.745/0001-09');
		$Sql->order('d.ds_nome_departamento');
		$combo = $this->getDefaultAdapter()->fetchPairs($Sql);

		foreach ($combo as $index => $text) {
			$return[$index] = $text;
		}

		return $return;
	}

	public function getDepartamentos($idEmpresa='') {
		if ($idEmpresa) {
			$sql = "select * from sistema.v_departamentos where id_empresa = $idEmpresa";
		} else {
			$sql = "select * from sistema.v_departamentos";
		}

		return $this->getAdapter()->fetchAll($sql);
	}

	public function getDepartamentoEmpresa($cnpj, $chaveDepartamento) {
		$Select = $this->select()->setIntegrityCheck(false);
		$Select->from(array('d' => 'sistema.departamentos'));
		$Select->join(array('e' => 'sistema.empresas'), 'e.id = d.id_empresa', false);
		$Select->where('d.ds_chave = ?', $chaveDepartamento);
		$Select->where('e.cpf_cnpj = ?', $cnpj);
		$Row = $this->fetchRow($Select);

		return $Row->id;

	}

	public function deleteByEmpresa(int $idEmpresa)
    {
        $this->_db->query("DELETE FROM {$this->_schema}.{$this->_name} WHERE id_empresa = {$idEmpresa}");
	
    }
}