<?php

class Endosso_Model_Proposta_Endossos extends Agro_Db_Table_Propostas {
	protected $_primary = array('id_endosso', 'id_proposta');
	protected $_name = 'propostas_endosso';
	protected $_schema = 'seguro';
	
	public function create($idEndosso, &$Proposta) {
		try {
			$Proposta->id_endosso = $this->_next();
			$Proposta->save();
			
			$RowProposta = $this->createRow();
			$RowEndosso = $this->createRow();
			
			$RowProposta->id_endosso = $Proposta->id_endosso;
			$RowProposta->id_proposta = $Proposta->id;
			$RowProposta->save();
			
			$RowEndosso->id_endosso = $Proposta->id_endosso;
			$RowEndosso->id_proposta = $idEndosso;
			$RowEndosso->save();
			
			return $Proposta->id_endosso;
		} catch (Exception $e) {
			return false;
		}
	}
	
	public function add($idEndosso, $Proposta) {
		try {
			$RowEndosso = $this->createRow();
			$RowEndosso->id_endosso = $Proposta->id_endosso;
			$RowEndosso->id_proposta = $idEndosso;
			$RowEndosso->save();
			
			return $Proposta->id_endosso;
		} catch (Exception $e) {
			return false;
		}
	}
	
	private function _next() {
		$Select = $this->select()->from('seguro.propostas_endosso', 'max(id_endosso)');
		return $this->getDefaultAdapter()->fetchOne($Select)+1;
	}
	
}