<?php

class Comercializacao_Model_TaxasAgrupamentoMunicipios extends Agro_Db_Table_Abstract {
	protected $_schema = 'comercializacao';
	protected $_name = 'taxas_agrupamento_municipios';
	protected $_primary = array('id_taxa_agrupamento', 'id_municipio');
	
	public function save($values) {
		$insert = array(
			'vl_produtividade_estimada' => ($values['vl_produtividade_estimada'] ? $values['vl_produtividade_estimada'] : 0), 
			'id_taxa_agrupamento' => $values['id_taxa_agrupamento'],
			'id_municipio' => $values['id_municipio'] 
		);

		return $this->insert($insert);
	}
	
	public function deleteMunicipios($id) {
		$sql = "delete from comercializacao.taxas_agrupamento_municipios where id_taxa_agrupamento = $id";
		$this->_db->query($sql);
	}
	
	public function getData($where=null){
		$select = $this->select()->distinct(true);
		$select->setIntegrityCheck(false);
		$select->from(array('t'=>'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('
				t.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_usuarios 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 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 getProdutividadeEstimada($agrupamento, $municipio) {
		$Row = $this->find($agrupamento, $municipio)->current();
		return $Row->vl_produtividade_estimada;
	}

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

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