<?php declare(strict_types=1);

class Seguro_Service_ConsultarDocumentoEndossoPlugEssor
{

    public function executar(int $idProposta, int $tipoEnvio)
    {
        $proposta = (new Seguro_Model_Propostas())->find($idProposta)->current();
     
        if (empty($proposta)) {
            throw new Exception('Proposta não encontrada.');
        }
        
        $formulario = (new Log_Model_EnvioFormularioEcmPlugEssor())->getFormulario($idProposta, $tipoEnvio);
        
        if (empty($formulario)) {
            throw new Exception('Proposta não possui documento vinculado.', 1);
        }

        $dto = new Agro_Essor_Plug_Dto_EcmDocumentosEndossosRequest(
            (int) $formulario->id_endosso_erp,
            (int) $formulario->id_documento_erp
        );

        return (new Agro_Essor_Plug_EcmDocumentosEndossos())->execute($dto);
    }
}
