<?php

class Atendimento_Model_TipoCancelamento extends Agro_Db_Table_Abstract {
    protected $_schema = 'atendimento';
    protected $_name = 'tipo_cancelamento';
    protected $_primary = 'id';
    public $_order = 'ds_tipo_cancelamento';

    public const MOTIVO_CANCELADA_A_PEDIDO_CORRETOR_SEGURADO = 29;
    public const CANC_PROP_CORRETOR_SEGURADO_DESISTIU_SEGURO = 47;
    public const CANC_PROP_CORRETOR_SERA_REFEITA = 48;
    public const CANC_PROP_CORRETOR_SEGURO_INDEVIDO = 49;
    public const CANC_PROP_CORRETOR_SEGURO_DUPLICADO = 50;
    public const CANC_PROP_CORRETOR_ERRO_TRANSMISSAO = 51;
    public const CANC_PROP_CORRETOR_FALTA_PAGAMENTO_PARCELA = 52;
    public const CANC_PROP_CORRETOR_OUTRO = 53;

    public function save($data) {
        $Row = $this->getRow($data['id']);
        $Row->ds_tipo_cancelamento = $data['ds_tipo_cancelamento'];
        $Row->ds_chave = $data['ds_chave'];
        return $Row->save();
    }

    public function getCombo($chave='') {
        $Select = $this->select();
        $Select->where('fl_ativo=true');

        if ($chave !== '') {
            $Select->where('ds_chave = ?', $chave);
        }

        $Select->order('ds_tipo_cancelamento ASC');

        $Rs = $this->fetchAll($Select);

        $options = array();
        //$options[] = '';
        if ($Rs->count()) {
            foreach ($Rs as $row) {
                $options[$row['id']] = $row['ds_tipo_cancelamento'];
            }
        }

        return $options;
    }

    public function getMotivosCancelamento(): array
    {
        $select = $this->select();
        $select->where('fl_ativo=true');
        $select->where("(ds_chave <> 'APOLICE_CANCELADA' AND ds_chave <> 'CANCELAMENTO_PROPOSTA_CORRETOR') OR ds_chave IS NULL");
        $select->order('ds_tipo_cancelamento ASC');

        $rs = $this->fetchAll($select);
        return !empty($rs) ? $rs->toArray() : [];
    }

    /**
     * Retorna os motivos de cancelamento de proposta para o corretor
     *
     * @return array
     */
    public function getCancelamentoPropostaCorretor(): array
    {
        $select = $this->select();
        $select->where('fl_ativo=true');
        $select->where("ds_chave = 'CANCELAMENTO_PROPOSTA_CORRETOR'");
        $select->order('id');

        $rs = $this->fetchAll($select);
        return !empty($rs) ? $rs->toArray() : [];
    }
}
