<?php
class Seguro_Model_PropostasProponentesRelacionamentoPpe extends Agro_Db_Table_Abstract {
	protected $_schema = 'seguro';
	protected $_name = 'propostas_proponentes_relacionamento_ppe';
	protected $_primary = 'id';
	public $_order = 'ds_nome_socio asc';
	
	public function save($data) {
		try {
			$Row = $this->getRow($data['id']);
			
			$Row->id_proposta = $data['id_proposta'];
			$Row->ds_nome_socio = $data['ds_nome_socio'];
			$Row->ds_nro_cpf = $data['cpf_cnpj'];
			$Row->tp_relacionamento= $data['tp_relacionamento'] ? $data['tp_relacionamento'] : '';

			return $Row->save();
		} catch (Exception $e) {
			if ($e->getCode() == 23505) {
				$this->setMessage(MSG_CPFCNPJ_EXISTENTE);
			}
			return false;
		}
	}
	
	public function getSociosByProposta($id_proposta){
		$Select = $this->select()->setIntegrityCheck(false);
		$Select->from(array('p' => 'seguro.propostas_proponentes_relacionamento_ppe'));
		$Select->where('id_proposta = ?', $id_proposta);
		$Row = $this->fetchAll($Select);
		if ($Row) {
			return $Row->toArray();
		}
		return;
	}
	
	public function getSocio($id){
		$Select = $this->select()->setIntegrityCheck(false);
		$Select->from(array('p' => 'seguro.propostas_proponentes_relacionamento_ppe'));
		$Select->where('id = ?', $id);

		$Row = $this->fetchRow($Select);
		if ($Row) {
			return $Row->toArray();
		}
		return;
	}
	
	public function removeSocio($id){
		try{
			$this->getAdapter()->query("DELETE FROM seguro.propostas_proponentes_relacionamento_ppe WHERE id = $id");
			return true;
		}catch (Exception $e){
			die($e);
		}
	}
	
	
}