<?php

declare(strict_types=1);


class Comercializacao_Model_TaxaAgrupamentoItemSeguradoValorTonelada extends Agro_Db_Table_Abstract
{
    protected $_schema = 'comercializacao';
	protected $_name = 'taxa_agrupamento_item_segurado_valor_tonelada';
	protected $_primary = array('id_taxa_agrupamento', 'id_item_segurado');

    public function save(array $data): ?Zend_Db_Table_Row
    {
        $row = $this->find($data['id_taxa_agrupamento'], $data['id_item_segurado'])->current();

        if (empty($row)) {
            $row = $this->createRow();
        }

        $row->id_taxa_agrupamento = $data['id_taxa_agrupamento'];
        $row->id_item_segurado = $data['id_item_segurado'];
        $row->vl_tonelada = empty($data['vl_tonelada'])
            ? 0
            : $data['vl_tonelada'];

        $row->save();

        return !$row ? null : $row;
    }

    public function findByIdTaxaAgrupamento(int $idTaxaAgrupamento): array
    {
        return $this->fetchAll(
            $this->select()
                ->from(['taxa' => "{$this->_schema}.{$this->_name}"])
                ->setIntegrityCheck(false)
                ->where('id_taxa_agrupamento = ?', $idTaxaAgrupamento)
        )->toArray();
    }

    public function findByIdProposta(int $idProposta): array
    {
        return $this->fetchAll(
            $this->select()
                ->setIntegrityCheck(false)
                ->from(['taxa' => "{$this->_schema}.{$this->_name}"], 'vl_tonelada')
                ->join(
                    ['tao' => 'comercializacao.taxas_agrupamento_orcamentos'],
                    'tao.id_taxa_agrupamento = taxa.id_taxa_agrupamento',
                    false
                )
                ->join(
                    ['sis' => 'seguro.itens_segurados'],
                    'sis.id = taxa.id_item_segurado',
                    'nr_item_segurado'
                )
                ->where('tao.id_proposta = ?', $idProposta)
        )->toArray();
    }

    public function findByIdItemSegurado(int $idItemSegurado)
    {
        return $this->_db->fetchOne(
            $this->select()
                ->setIntegrityCheck(false)
                ->from(['taxa' => "{$this->_schema}.{$this->_name}"], 'vl_tonelada')
                ->join(
                    ['tao' => 'comercializacao.taxas_agrupamento_orcamentos'],
                    'tao.id_taxa_agrupamento = taxa.id_taxa_agrupamento',
                    false
                )
                ->join(
                    ['sis' => 'seguro.itens_segurados'],
                    'sis.id = taxa.id_item_segurado',
                    false
                )
                ->where('taxa.id_item_segurado = ?', $idItemSegurado)
        );
    }

    public function buscaLimiteProposta(int $idProposta): ?float
    {
        $limite = $this->_db->fetchOne(
            $this->select()
                ->setIntegrityCheck(false)
                ->from(['taxa' => "{$this->_schema}.{$this->_name}"], 'vl_tonelada')
                ->join(
                    ['tao' => 'comercializacao.taxas_agrupamento_orcamentos'],
                    'tao.id_taxa_agrupamento = taxa.id_taxa_agrupamento',
                    false
                )
                ->join(['ta' => 'comercializacao.taxas_agrupamento'], 'ta.id = tao.id_taxa_agrupamento', false)
                ->where('tao.id_proposta = ?', $idProposta)
                ->where('ta.fl_valor_tonelada_por_item IS FALSE')
        );

        return !empty($limite) ? (float) $limite : null;
    }
}
