<?php

class Log_Model_PropostasTransmissao extends Agro_Db_Table_Abstract
{
    protected $_schema = 'log';
    protected $_name = 'propostas_transmissao';
    protected $_primary = 'id';
    public $_order = 'dt_criacao';

    public function isEmited(int $idProposta) :? int {
        $Select = $this->select()->setIntegrityCheck(false)
            ->from(array('pt' => 'log.propostas_transmissao'), 'id')
            ->where('id_proposta = ?', $idProposta)
            ->where('ds_tipo_transmissao ~ ?', '^(EMITIR_PROPOSTA)*')
            ->where('fl_sucesso = true');

        return $this->_db->fetchOne($Select);
    }

    public function getIdEndossoErp(int $idProposta)
    {
        return $this->_db->fetchOne(
            $this->select()->setIntegrityCheck(false)
                ->from(array('pt' => 'log.propostas_transmissao'), 'ds_retorno')
                ->where('id_proposta = ?', $idProposta)
                ->where("ds_tipo_transmissao = 'EMITIR_PROPOSTA' OR ds_tipo_transmissao = 'EMITIR_PROPOSTA (Endosso)'")
                ->where('fl_sucesso IS TRUE')
        );
    }

    public function getDataEmissaoEndosso(int $idProposta): ?string
    {
        $Select = $this->select()->setIntegrityCheck(false)
            ->from(array('pt' => 'log.propostas_transmissao'), 'dt_envio')
            ->where('id_proposta = ?', $idProposta)
            ->where("ds_tipo_transmissao = 'EMITIR_PROPOSTA'")
            ->where('fl_sucesso = true');

        return $this->_db->fetchOne($Select);
    }
}