<?php

class Comercializacao_Model_TaxasAgrupamentoUsuarios extends Agro_Db_Table_Abstract {
	protected $_schema = 'comercializacao';
	protected $_name = 'taxas_agrupamento_usuarios';
	protected $_primary = array('id_taxa_agrupamento', 'id_usuario');

	public function save($idTaxaAgrupamento, $idUsuario) {
		$Row = $this->find($idTaxaAgrupamento, $idUsuario)->current();
		if (!$Row) $Row = $this->createRow();

		$Row->id_taxa_agrupamento = $idTaxaAgrupamento;
		$Row->id_usuario = $idUsuario;

		return $Row->save();
	}

	public function deleteMunicipios($id) {
		$sql = "delete from comercializacao.taxas_agrupamento_usuarios where id_taxa_agrupamento = $id";
		$this->_db->query($sql);
	}

	public function getData($where=null){
		$select = $this->select();
		$select->setIntegrityCheck(false);
		$select->from(array('c'=>'comercializacao.v_taxas_agrupamento'), '*');


		if (is_array($where)) {
			$temp = '';
			if (isset($where['id_estado']) or isset($where['id_municipio'])) {
				if($where['id_estado']) {
					$temp = " where id_estado = " . $where['id_estado'];
					unset($where['id_estado']);
				};
				if($where['id_municipio']) {
					$temp = " where sm.id = " . $where['id_municipio'];
					unset($where['id_municipio']);
				}

				$select->where('
				c.id IN (
					select
						id_taxa_agrupamento
					from
						comercializacao.taxas_agrupamento_municipios m left join
						sistema.municipios sm on m.id_municipio = sm.id
					' . $temp . '
				)');
			}

			foreach ($where as $clause) {

				$field = $clause['field'];
				$operator = $clause['operator'];
				$value = $clause['value'];

				$select->where("$field $operator ?", $value);
			}
		} elseif ($where != '') {
			$select->where($where);
		}

		$select->order($this->_order);

		return $select;
	}

	public function beforeDelete($id_taxa) {
		//exclui as taxas relacionadas
		$sql = "delete from comercializacao.taxas_relacionadas where id_taxa = $id_taxa or id_taxa_relacionada = $id_taxa";
		try {
			$this->_db->query($sql);
		}catch (Exception $e) {}

		$sql = "delete from comercializacao.taxas_agrupamento_proponentes where id_taxa_agrupamento = $id_taxa ";
		try {
			$this->_db->query($sql);
		}catch (Exception $e) {}

		$sql = "delete from comercializacao.taxas_agrupamento_municipios where id_taxa_agrupamento = $id_taxa ";
		try {
			$this->_db->query($sql);
		}catch (Exception $e) {}

		$sql = "delete from comercializacao.taxas_agrupamento_estados where id_taxa_agrupamento = $id_taxa ";
		try {
			$this->_db->query($sql);
		}catch (Exception $e) {}

		$sql = "delete from comercializacao.taxas_valores where id_taxa_agrupamento = $id_taxa";
		try {
			return $this->_db->query($sql);
		}catch (Exception $e) {
			return false;
		}
	}

	public function clearUsuarios($id) {
		try {
			return $this->getAdapter()->query("delete from comercializacao.taxas_agrupamento_usuarios where id_taxa_agrupamento = $id");
		} catch (Exception $e) {
			return false;
		}
	}

	public function delete($id, $field='') {
		parent::delete($id, $field);

		$sql = "delete from comercializacao.taxas_agrupamento where id = $id";
		try {
			return $this->_db->query($sql);
		}catch (Exception $e) {
			return false;
		}

		return false;
	}

    public function getTaxaAgrupamento(int $idTaxaAgrupamento)
    {
        $select = $this->select();
        $select->setIntegrityCheck(false);
        $select->from(array('tau' => 'comercializacao.taxas_agrupamento_usuarios'), '*');
        $select->join(array('ta' => 'comercializacao.taxas_agrupamento'), 'tau.id_taxa_agrupamento = ta.id', '*');
        $select->where('id_taxa_agrupamento = ?', $idTaxaAgrupamento);

        return $this->fetchAll($select);
    }
}
