<?php

class ModelTotalacrescimoporpagamento extends Model
{

    public function getTotal(&$total_data, &$total)
    {

        if (isset($this->session->data['payment_method'])) {

            $paymentmethod = $this->session->data['payment_method'];

            $acrescimos = array();
            $acrescimos = $this->config->get('acrescimo_tipo_pagamento');
            $valor = 0;
            $cliente_TabelaPreco = $this->customer->getCustomerGroupId();

            foreach ($acrescimos as $acrescimo) {
                $tabelaacrescimo = false;

                $valorMinimo = $acrescimo['valor_minimo'] ?? 0;

                if (($acrescimo['pagamento_acrescimo'] == $paymentmethod['code']) &&
                    ($acrescimo['status_acrescimo']) &&
                    ($this->cart->getTotal() > $valorMinimo)
                ) {
                    foreach ($acrescimo['tabela_preco'] as $tabelaPreco) {
                        if (($tabelaPreco == 0) || ($tabelaPreco == $cliente_TabelaPreco)) {
                            $tabelaAcrescimo = true;
                        }
                    }

                    if ($tabelaAcrescimo && (!isset($prioridadeAnterior) || ($acrescimo['prioridade_acrescimo'] <= $prioridadeAnterior))) {
                        $prioridadeAnterior = $acrescimo['prioridade_acrescimo'];
                        $valor = $acrescimo['val_acrescimo'];
                    }

                    if (isset($acrescimo['text_acrescimo']) && trim($acrescimo['text_acrescimo']) <> '') {
                        $titulo = $acrescimo['text_acrescimo'];
                    } else {
                        $titulo = 'Acrescimo Pagamento(' . $valor . '%)';
                    }

                    $modoAcrescimo = $acrescimo['modo_acrescimo'];
                }
            }
            //$valor = $this->config->get('acrescimopagamento_' . $paymentmethod['code']);

            if (($valor) && ($valor > 0) && ($valor != '')) {
                //$percent = $valor;

                if ($modoAcrescimo) {
                    $valor = ($valor / 100) * $this->AcrescSobAcresc();
                } else {
                    $valor = ($valor / 100) * $this->cart->getSubTotal();
                }

                $total_data[] = array(
                    'code' => 'acrescimoporpagamento',
                    'title' => $titulo,
                    'text' => $this->currency->format($valor),
                    'value' => $valor,
                    'sort_order' => $this->config->get('acrescimoporpagamento_sort_order')
                );

                $total += $valor;
            }

        }

    }

    public function AcrescSobAcresc()
    {

        $total_data = array();
        $total = 0;

        $this->load->model('setting/extension');

        $sort_order = array();

        $results = $this->model_setting_extension->getExtensions('total');

        foreach ($results as $key => $value) {
            $sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
        }

        array_multisort($sort_order, SORT_ASC, $results);

        foreach ($results as $result) {
            if ($this->config->get($result['code'] . '_status') && ($result['code'] != 'acrescimoporpagamento' && $result['code'] != 'shipping')) {
                $this->load->model('total/' . $result['code']);
                $this->{'model_total_' . $result['code']}->getTotal($total_data, $total);
            }
        }

        foreach ($total_data as $totalAcres) {
            if ($totalAcres['code'] == 'total') {
                return $totalAcres['value'];
            }
        }

    }
}
