<?php

class Comercializacao_Model_TaxasAgrupamentoEstados extends Agro_Db_Table_Abstract {
	protected $_schema = 'comercializacao';
	protected $_name = 'taxas_agrupamento_estados';
	protected $_primary = array('id_taxa_agrupamento', 'id_estado');

	public function save($values) {
		$Row = $this->find($values['id_taxa_agrupamento'], $values['id_estado'])->current();
		if (!$Row) $Row = $this->createRow();

		$Row->id_taxa_agrupamento = $values['id_taxa_agrupamento'];
		$Row->id_estado = $values['id_estado'];

		return $Row->save();
	}

	public function deleteEstados($id) {
		$sql = "delete from comercializacao.taxas_agrupamento_estados 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)) {
			foreach ($where as $clause) {
				$field = $clause['field'];
				$operator = $clause['operator'];
				$value = $clause['value'];

				if($field == 'te.id_estado') {
					$select->where('
					c.id in (select
						id_taxa_agrupamento
					from
						"comercializacao"."taxas_agrupamento_estados" te
					where
						te.id_estado = ' . $value .'
					)');

					continue;
				}

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

		$select->order($this->_order);
		//die($select);
		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_usuarios 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_valores where id_taxa_agrupamento = $id_taxa";
		try {
			return $this->_db->query($sql);
		}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('tae' => 'comercializacao.taxas_agrupamento_estados'), '*');
        $select->join(array('ta' => 'comercializacao.taxas_agrupamento'), 'tae.id_taxa_agrupamento = ta.id', '*');
        $select->where('id_taxa_agrupamento = ?', $idTaxaAgrupamento);

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