<?php

class ModelTotalDescontoPrimeiraCompra extends Model
{

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

        if ($this->customer->isLogged() && $this->config->get('descontoprimeiracompra_status')) {

            $sql = "SELECT count(o.order_id) as total
                    FROM " . DB_PREFIX . "order o
                    WHERE o.customer_id = " . $this->customer->getId() . "
                    AND o.order_status_id <> " . $this->config->get('descontoprimeiracompra_status_pedido') . "
                    AND o.order_status_id <> " . (int)Type_DefaultValues::Tp_OrderStatusAbandoned;

            $query = $this->db->query($sql);

            if ($query->row['total'] == 0) {

                $this->load->type('tipocomum');
                $array_total = array();
                $retorna = false;

                $configs = $this->config->get('descontoprimeiracompra_module');

                foreach ($configs as $key => $config) {
                    if ((($config['type_person'] == Type_CadastroPessoa::Tp_Ambos) || ($config['type_person'] == $this->customer->getTipo_pessoa())) &&
                        ($config['status'] == Type_Status::Tp_Habilitado) &&
                        ($this->cart->getTotal() > $config['value_minimo'])
                    ) {
                        $totalDesc = 0;
                        switch ($config['type_descont']) {
                            case Type_DescontoPrimeiraCompra::Tp_Monetario:
                                $totalDesc += ($config['value_descont'] * (-1));
                                break;
                            case Type_DescontoPrimeiraCompra::Tp_Percentual:
                                $totalDesc += (($config['value_descont'] * $this->cart->getTotal()) / 100) * (-1);
                                break;
                        }

                        if(($totalDesc * -1) > $total){
                            continue;
                        }

                        $total += $totalDesc;

                        $array_total[] = array_merge($config, ['total' => $totalDesc]);
                        $retorna = true;
                    }
                }

                if ($retorna) {
                    usort(
                        $array_total,
                        function ($a, $b) {
                            if ($a['total'] == $b['total']) {
                                return 0;
                            }
                            return ($a['total'] > $b['total']) ? -1 : 1;
                        }
                    );

                    $array_total = current($array_total);

                    $this->language->load('total/total');
                    return $total_data[] = array(
                        'code' => 'descontoprimeiracompra',
                        'title' => $array_total['title'],
                        'text' => $this->currency->format(($array_total['total'])),
                        'value' => $totalDesc,
                        'sort_order' => $array_total['sort']
                    );
                }

            }

        }

    }

}
