<?php

class Customer
{
    private $customer_id;
    private $firstname;
    private $lastname;
    private $email;
    private $email_facebook;
    private $facebook_user_id;
    private $email_gmail;
    private $gmail_user_id;
    private $telephone;
    private $fax;
    private $newsletter;
    private $customer_group_id;
    private $address_id;
    private $id_futura;
    private $tipo_pessoa;
    private $data_nascimento;
    private $sexo;
    private $cpf;
    private $rg;
    private $cnpj;
    private $razao_social;
    private $inscricao_estadual;
    private $contribuinte;
    private $interests_remember;
    private $interests_date;
    private $registry;
    private $password;
    private $celular;
    private $load;
    private $date_added;
    private $feedback_whatsapp_status;
    private $feedback_whatsapp_number;
    private $customer_lock = false;
    private $time_unlock;

    public function __construct($registry)
    {
        $this->registry = $registry;
        $this->config = $registry->get('config');
        $this->load = $registry->get('load');
        $this->db = $registry->get('db');
        $this->request = $registry->get('request');
        $this->session = $registry->get('session');
        $this->log = $registry->get('log');

        $jwt = new JWT($this->registry, 'store');

        if ($jwt->getCustomerLoged("sub")) {
            $customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$jwt->getCustomerLoged("sub") . "' AND status = '1'");

            $lock = new limiterlock($this->registry);
            if ($customer_query->num_rows)
                $customer_lock = $lock->operationsLock($customer_query->row['time_lock'], $customer_query->row['customer_id']);

            if ($customer_query->num_rows and !$customer_lock) {
                $this->customer_id = $customer_query->row['customer_id'];
                $this->firstname = $customer_query->row['firstname'];
                $this->lastname = $customer_query->row['lastname'];
                $this->email = $customer_query->row['email'];
                $this->email_facebook = $customer_query->row['email_facebook'];
                $this->facebook_user_id = $customer_query->row['facebook_user_id'];
                $this->email_gmail = $customer_query->row['email_gmail'];
                $this->gmail_user_id = $customer_query->row['gmail_user_id'];
                $this->telephone = $customer_query->row['telephone'];
                $this->fax = $customer_query->row['fax'];
                $this->newsletter = $customer_query->row['newsletter'];
                $this->customer_group_id = $customer_query->row['customer_group_id'];
                $this->address_id = $customer_query->row['address_id'];
                $this->id_futura = $customer_query->row['id_futura'];
                $this->contribuinte = $customer_query->row['contribuinte'];
                $this->password = $customer_query->row['password'];
                $this->celular = $customer_query->row['celular'];
                $this->date_added = $customer_query->row['date_added'];
                $this->feedback_whatsapp_status = $customer_query->row['feedback_whatsapp_status'];
                $this->feedback_whatsapp_number = $customer_query->row['feedback_whatsapp_number'];

                if (($customer_query->row['cpf']) && ($customer_query->row['cpf'] != '')) {
                    $this->tipo_pessoa = 'F';
                } else {
                    $this->tipo_pessoa = 'J';
                }

                $this->data_nascimento = $customer_query->row['data_nascimento'];
                $this->sexo = $customer_query->row['sexo'];
                $this->cpf = $customer_query->row['cpf'];
                $this->rg = $customer_query->row['rg'];
                $this->cnpj = $customer_query->row['cnpj'];
                $this->razao_social = $customer_query->row['razao_social'];
                $this->inscricao_estadual = $customer_query->row['inscricao_estadual'];

                $this->interests_remember = $customer_query->row['interests_remember'];
                $this->interests_date = $customer_query->row['interests_date'];

                $cartSession = isset($this->session->data['cart']) ? serialize($this->session->data['cart']) : '';
                $wishlistSession = isset($this->session->data['wishlist']) ? serialize($this->session->data['wishlist']) : '';
                $buylaterSession = isset($this->session->data['buy_later']) ? json_encode($this->session->data['buy_later']) : '';

                //Verifica se alguma informação mudou p/ dar update: MELHORIA EM DESEMPENHO ByMatheus
                if (($cartSession != $customer_query->row['cart']) || ($wishlistSession != $customer_query->row['wishlist']) || ($buylaterSession != $customer_query->row['buy_later']) || ($this->db->escape($this->request->server['REMOTE_ADDR']) != $customer_query->row['ip'])) {
                    $this->db->query("UPDATE " . DB_PREFIX . "customer SET cart = '" . $this->db->escape(isset($this->session->data['cart']) ? serialize($this->session->data['cart']) : '') . "', wishlist = '" . $this->db->escape(isset($this->session->data['wishlist']) ? serialize($this->session->data['wishlist']) : '') . "', buy_later = '" . $this->db->escape($buylaterSession) . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE customer_id = '" . (int)$this->customer_id . "'");
                }

                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer_ip WHERE customer_id = '" . (int)$jwt->getCustomerLoged("sub") . "' AND ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "'");

                if (!$query->num_rows) {
                    $this->db->query("INSERT INTO " . DB_PREFIX . "customer_ip SET customer_id = '" . (int)$jwt->getCustomerLoged("sub") . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', date_added = NOW()");
                }
            } else {
                $this->logout();
            }
        }
    }

    public function getTime_unlock()
    {
        return $this->time_unlock;
    }

    public function getCustomerLock()
    {
        return $this->customer_lock;
    }

    public function __get($name)
    {
        return $this->registry->get($name);
    }

    public function login($email, $password, $override = false)
    {
        if ($override) {
            $customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer where LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "' AND status = '1'");
        } else {
            $customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "' AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1('" . $this->db->escape($password) . "'))))) OR password = '" . $this->db->escape(md5($password)) . "') AND status = '1' AND approved = '1'");
        }

        if ($customer_query->num_rows) {
            $lock = new limiterlock($this->registry);
            $customer_lock = $lock->operationsLock($customer_query->row['time_lock'], $customer_query->row['customer_id']);

            if ($customer_lock) {
                $this->customer_lock = true;
                $this->time_unlock = $lock->getTimeUnlock();
                return false;
            }

            $jwt = new JWT($this->registry, 'store');
            $jwt->sessionJwt($customer_query->row['customer_id'], $this->request->server['HTTP_HOST'], $this->request->server['REMOTE_ADDR']);

            if ($customer_query->row['cart'] && is_string($customer_query->row['cart'])) {
                $cart = unserialize($customer_query->row['cart']);

                foreach ($cart as $key => $value) {
                    if (!is_array($value)) { //retrocompatibilidade pois antes só salvava a quantidade
                        $value = array('quantity' => $value);
                    }
                    if (!array_key_exists($key, $this->session->data['cart'])) {
                        $this->session->data['cart'][$key] = $value;
                    } else {
                        $this->session->data['cart'][$key] += $value;
                    }
                }
            }

            if ($customer_query->row['wishlist'] && is_string($customer_query->row['wishlist'])) {
                if (!isset($this->session->data['wishlist'])) {
                    $this->session->data['wishlist'] = array();
                }

                $wishlist = unserialize($customer_query->row['wishlist']);

                foreach ($wishlist as $product_id) {
                    if (!in_array($product_id, $this->session->data['wishlist'])) {
                        $this->session->data['wishlist'][] = $product_id;
                    }
                }
            }

            if ($customer_query->row['buy_later'] && is_string($customer_query->row['buy_later'])) {
                if (!isset($this->session->data['buy_later'])) {
                    $this->session->data['buy_later'] = array();
                }

                $buy_later = (array) json_decode($customer_query->row['buy_later']);

                foreach ($buy_later as $product_id => $product) {
                    $this->session->data['buy_later'][$product_id] = (array) $product;
                }
            }

            $this->customer_id = $customer_query->row['customer_id'];
            $this->firstname = $customer_query->row['firstname'];
            $this->lastname = $customer_query->row['lastname'];
            $this->email = $customer_query->row['email'];
            $this->telephone = $customer_query->row['telephone'];
            $this->fax = $customer_query->row['fax'];
            $this->newsletter = $customer_query->row['newsletter'];
            $this->customer_group_id = $customer_query->row['customer_group_id'];
            $this->address_id = $customer_query->row['address_id'];
            $this->password = $customer_query->row['password'];

            $this->interests_remember = $customer_query->row['interests_remember'];
            $this->interests_date = $customer_query->row['interests_date'];

            $this->data_nascimento = $customer_query->row['data_nascimento'];
            $this->sexo = $customer_query->row['sexo'];
            $this->cpf = $customer_query->row['cpf'];
            $this->rg = $customer_query->row['rg'];
            $this->cnpj = $customer_query->row['cnpj'];
            $this->razao_social = $customer_query->row['razao_social'];
            $this->inscricao_estadual = $customer_query->row['inscricao_estadual'];
            $this->celular = $customer_query->row['celular'];
            $this->feedback_whatsapp_status = $customer_query->row['feedback_whatsapp_status'];
            $this->feedback_whatsapp_number = $customer_query->row['feedback_whatsapp_number'];

            $this->db->query("UPDATE " . DB_PREFIX . "customer SET ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE customer_id = '" . (int)$this->customer_id . "'");

            $this->session->data['customer_login_trys'] = 0;
            return true;
        } else {
            isset($this->session->data['customer_login_trys']) ? $this->session->data['customer_login_trys']++ : $this->session->data['customer_login_trys'] = 1;
            return false;
        }
    }

    private function unlock($customer_query)
    {
        $this->db->query("UPDATE oc_customer SET time_lock = '" . Crypto::criptografarAES256(json_encode(['customer_id' => $customer_query->row['customer_id']]), Crypto::CAES256) . "'
                     WHERE customer_id = {$customer_query->row['customer_id']};");
    }

    public function loginredesocial($email, $user_id = '', $override = false, $redesocial = '')
    {
        /*Valida apenas o email, para entrar direto quando for logar pela rede social*/
        if ($redesocial == 'facebook') {
            $vinculoRedeSocial = " OR LOWER(email_facebook) = '" . $this->db->escape(utf8_strtolower($email)) . "'";

            if ($user_id) {
                $vinculoRedeSocial .= " OR LOWER(facebook_user_id) = '{$user_id}'";
            }
        } else if ($redesocial == 'google') {
            $vinculoRedeSocial = " OR LOWER(email_gmail) = '" . $this->db->escape(utf8_strtolower($email)) . "'";

            if ($user_id) {
                $vinculoRedeSocial .= " OR LOWER(gmail_user_id) = '{$user_id}'";
            }
        } else {
            $vinculoRedeSocial = "";
        }

        if ($override) {
            $customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer where (LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "'" . $vinculoRedeSocial . ") AND status = '1'");
        } else {
            $customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE (LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "'" . $vinculoRedeSocial . ") AND status = '1' AND approved = '1'");
        }

        if ($customer_query->num_rows) {

            $jwt = new JWT($this->registry, 'store');
            $jwt->sessionJwt($customer_query->row['customer_id'], $this->request->server['HTTP_HOST'], $this->request->server['REMOTE_ADDR']);

            if ($customer_query->row['cart'] && is_string($customer_query->row['cart'])) {
                $cart = unserialize($customer_query->row['cart']);

                foreach ($cart as $key => $value) {
                    if (!is_array($value)) { //retrocompatibilidade pois antes só salvava a quantidade
                        $value = array('quantity' => $value);
                    }
                    if (!array_key_exists($key, $this->session->data['cart'])) {
                        $this->session->data['cart'][$key] = $value;
                    } else {
                        $this->session->data['cart'][$key] += $value;
                    }
                }
            }

            if ($customer_query->row['wishlist'] && is_string($customer_query->row['wishlist'])) {
                if (!isset($this->session->data['wishlist'])) {
                    $this->session->data['wishlist'] = array();
                }

                $wishlist = unserialize($customer_query->row['wishlist']);

                foreach ($wishlist as $product_id) {
                    if (!in_array($product_id, $this->session->data['wishlist'])) {
                        $this->session->data['wishlist'][] = $product_id;
                    }
                }
            }

            if ($customer_query->row['buy_later'] && is_string($customer_query->row['buy_later'])) {
                if (!isset($this->session->data['buy_later'])) {
                    $this->session->data['buy_later'] = array();
                }

                $buy_later = (array) json_decode($customer_query->row['buy_later']);

                foreach ($buy_later as $product_id => $product) {
                    $this->session->data['buy_later'][$product_id] = (array) $product;
                }
            }

            if ($this->config->get('fidelity_status')) {
                $fidelitycart = unserialize($customer_query->row['fidelitycart']);

                foreach ($fidelitycart as $key => $value) {
                    if (!is_array($value)) {
                        $value = array('quantity' => $value);
                    }
                    if (!array_key_exists($key, $this->session->data['fidelitycart'])) {
                        $this->session->data['fidelitycart'][$key] = $value;
                    } else {
                        $this->session->data['fidelitycart'][$key] += $value;
                    }
                }
            }

            $this->customer_id = $customer_query->row['customer_id'];
            $this->firstname = $customer_query->row['firstname'];
            $this->lastname = $customer_query->row['lastname'];
            $this->email = $customer_query->row['email'];
            $this->telephone = $customer_query->row['telephone'];
            $this->fax = $customer_query->row['fax'];
            $this->newsletter = $customer_query->row['newsletter'];
            $this->customer_group_id = $customer_query->row['customer_group_id'];
            $this->address_id = $customer_query->row['address_id'];
            $this->celular = $customer_query->row['celular'];

            if (($redesocial == 'facebook') && ($customer_query->row['email_facebook'] <> $email)) {
                $this->db->query("UPDATE " . DB_PREFIX . "customer SET email_facebook = '" . $this->db->escape(utf8_strtolower($email)) . "', facebook_user_id = '" . $this->db->escape(utf8_strtolower($user_id)) . "' WHERE customer_id = '" . (int)$this->customer_id . "'");
            } else if (($redesocial == 'google') && ($customer_query->row['email_gmail'] <> $email)) {
                $this->db->query("UPDATE " . DB_PREFIX . "customer SET email_gmail = '" . $this->db->escape(utf8_strtolower($email)) . "', gmail_user_id = '" . $this->db->escape(utf8_strtolower($user_id)) . "' WHERE customer_id = '" . (int)$this->customer_id . "'");
            }

            $this->db->query("UPDATE " . DB_PREFIX . "customer SET ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE customer_id = '" . (int)$this->customer_id . "'");

            return true;
        } else {
            return false;
        }
    }

    public function redeSocialJaVinculada($customer_id, $email, $user_id, $redesocial)
    {
        if ($redesocial == 'facebook') {
            $emailSocial = 'email_facebook';
            $field_user_id = 'facebook_user_id';
        } else if ($redesocial == 'google') {
            $emailSocial = 'email_gmail';
            $field_user_id = 'gmail_user_id';
        }

        $db_prefix = DB_PREFIX;

        $sql = "SELECT * FROM {$db_prefix}customer
                WHERE (LOWER(email) = '{$this->db->escape(utf8_strtolower($email))}'
                   OR LOWER({$emailSocial}) = '{$this->db->escape(utf8_strtolower($email))}'
                   OR LOWER({$field_user_id}) = '{$this->db->escape(utf8_strtolower($user_id))}' )";

        //        $customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer where (LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "' OR LOWER(" . $emailSocial . ") = '" . $this->db->escape(utf8_strtolower($email)) . "')");
        $customer_query = $this->db->query($sql);

        if ($customer_query->num_rows || ($customer_query->num_rows <= 1)) {
            if ($customer_query->num_rows == 1 && $customer_query->row['customer_id'] <> $customer_id) { //Validar que o registro encontrado é realmente o registro logado
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function vincularRedeSocial($customer_id, $email, $user_id, $redesocial)
    {
        if ($redesocial == 'facebook') {
            $emailSocial = 'email_facebook';
            $field_user_id = 'facebook_user_id';
        } else if ($redesocial == 'google') {
            $emailSocial = 'email_gmail';
            $field_user_id = 'gmail_user_id';
        }

        $db_prefix = DB_PREFIX;
        $sqlSelct = "SELECT *
                      FROM {$db_prefix}customer
                      where (LOWER(email) = '{$this->db->escape(utf8_strtolower($email))}'
                      OR LOWER({$emailSocial}) = '{$this->db->escape(utf8_strtolower($email))}'
                      OR LOWER({$field_user_id}) = '{$this->db->escape(utf8_strtolower($user_id))}' )";

        $customer_query = $this->db->query($sqlSelct);
        if ($customer_query->num_rows || ($customer_query->num_rows <= 1)) {
            if ($customer_query->num_rows == 1 && $customer_query->row['customer_id'] != $customer_id) { //Validar que o registro encontrado é realmente o registro logado
                return false;
            } else {
                $customer_id = (int)$customer_id;

                if (($customer_query->num_rows > 0) && ($customer_query->row[$emailSocial] == $email)) {
                    $sqlDesvincular = "UPDATE {$db_prefix}customer
                                      SET {$emailSocial} = '',
                                          {$field_user_id} = ''
                                      WHERE customer_id = '{$customer_id}'";

                    $this->db->query($sqlDesvincular);
                    return false;
                } else {
                    $sqlVinculo = "UPDATE {$db_prefix}customer
                                   SET {$emailSocial} = '{$this->db->escape(utf8_strtolower($email))}',
                                       {$field_user_id} = '{$this->db->escape(utf8_strtolower($user_id))}'
                                   WHERE customer_id = '{$customer_id}'";

                    $this->db->query($sqlVinculo);
                    return true;
                }
            }
        } else {
            return false;
        }
    }

    public function desvincularRedeSocial($redesocial, $customer_id)
    {
        $db_prefix = DB_PREFIX;

        if ($redesocial == 'facebook') {
            $emailSocial = 'email_facebook';
            $field_user_id = 'facebook_user_id';
        } elseif ($redesocial == 'google') {
            $emailSocial = 'email_gmail';
            $field_user_id = 'gmail_user_id';
        } else {
            return false;
        }

        try {
            if ($customer_id && is_numeric($customer_id)) {
                $sql = "UPDATE {$db_prefix}customer SET {$emailSocial} = '', {$field_user_id} = '' where customer_id = {$customer_id}";
                $this->db->query($sql);
                return true;
            }
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function logout()
    {
        $this->db->query("UPDATE " . DB_PREFIX . "customer SET cart = '" . $this->db->escape(isset($this->session->data['cart']) ? serialize($this->session->data['cart']) : '') . "', wishlist = '" . $this->db->escape(isset($this->session->data['wishlist']) ? serialize($this->session->data['wishlist']) : '') . "', buy_later = '" . $this->db->escape(isset($this->session->data['buy_later']) ? json_encode($this->session->data['buy_later']) : '') . "' WHERE customer_id = '" . (int)$this->customer_id . "'");

        if ($this->config->get('fidelity_status') && isset($this->session->data['fidelitycart'])) {
            $this->db->query("UPDATE " . DB_PREFIX . "customer SET fidelitycart = '" . $this->db->escape(isset($this->session->data['fidelitycart']) ? serialize($this->session->data['fidelitycart']) : '') . "' WHERE customer_id = '" . (int)$this->customer_id . "'");
        }

        unset($this->session->data['token_store']);

        $this->customer_id = '';
        $this->firstname = '';
        $this->lastname = '';
        $this->email = '';
        $this->telephone = '';
        $this->fax = '';
        $this->newsletter = '';
        $this->customer_group_id = '';
        $this->address_id = '';
        $this->data_nascimento = '';
        $this->sexo = '';
        $this->cpf = '';
        $this->rg = '';
        $this->cnpj = '';
        $this->razao_social = '';
        $this->inscricao_estadual = '';
        $this->password = '';
        $this->celular = '';
        $this->feedback_whatsapp_status = '';
        $this->feedback_whatsapp_number = '';
    }

    public function isLogged()
    {
        return $this->customer_id;
    }

    public function getId()
    {
        return $this->customer_id;
    }

    public function setCustomerLock($id, $qtd, $limiter)
    {
        $lock = new DateTime($this->db->dataAtual(), new DateTimeZone('America/Sao_Paulo'));
        $unlock = new DateTime($this->db->dataAtual(), new DateTimeZone('America/Sao_Paulo'));

        if ($limiter['safety_limiter_time_break'] > 0) {
            $unlock->modify('+' . $limiter['safety_limiter_time_break'] . ' hour');
        }

        $json = array(
            'customer_id' => $id,
            'date_lock'   => $lock,
            'date_unlock' => $limiter['safety_limiter_time_break'] == 0 ? '0' : $unlock,
            'count_limiter' => $qtd
        );

        $sql = "UPDATE oc_customer SET time_lock = '" . Crypto::criptografarAES256(json_encode($json), Crypto::CAES256) . "'
	             WHERE customer_id = {$id};";
        $query = $this->db->query($sql);

        return $query;
    }

    public function setIPLock($ip)
    {
        $sql = "INSERT INTO oc_customer_ban_ip VALUES (NULL, '{$ip}');";
        $query = $this->db->query($sql);

        return $query;
    }

    public function getFirstName()
    {
        return $this->firstname;
    }

    public function getLastName()
    {
        return $this->lastname;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function getEmailFacebook()
    {
        return $this->email_facebook;
    }

    public function getEmailGmail()
    {
        return $this->email_gmail;
    }

    public function getFacebookUserId()
    {
        return $this->facebook_user_id;
    }

    public function getGmailUserId()
    {
        return $this->gmail_user_id;
    }

    public function getTelephone()
    {
        return $this->telephone;
    }

    public function getCelular()
    {
        return $this->celular;
    }

    public function getFax()
    {
        return $this->fax;
    }

    public function getNewsletter()
    {
        return $this->newsletter;
    }

    public function getCustomerGroupId()
    {
        return $this->customer_group_id;
    }

    public function getAddressId()
    {
        return $this->address_id;
    }


    public function getDataNascimento()
    {
        return $this->data_nascimento;
    }

    public function getSexo()
    {
        return $this->sexo;
    }

    public function getCpf()
    {
        return $this->cpf;
    }

    public function getRg()
    {
        return $this->rg;
    }

    public function getCnpj()
    {
        return $this->cnpj;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function getRazaoSocial()
    {
        return $this->razao_social;
    }

    public function getInscricaoEstadual()
    {
        return $this->inscricao_estadual;
    }

    public function getBalance()
    {
        $query = $this->db->query("SELECT SUM(amount) AS total FROM " . DB_PREFIX . "customer_transaction WHERE customer_id = '" . (int)$this->customer_id . "'");

        return $query->row['total'];
    }

    public function getID_Futura()
    {
        return $this->id_futura;
    }

    public function getTipo_pessoa()
    {
        return strtoupper($this->tipo_pessoa);
    }

    public function getRewardPoints()
    {
        $query = $this->db->query("SELECT SUM(points) AS total FROM " . DB_PREFIX . "customer_reward WHERE customer_id = '" . (int)$this->customer_id . "'");
        return $query->row['total'];
    }

    public function getContribuinte()
    {
        return strtoupper($this->contribuinte);
    }

    public function getInterestsRemember()
    {
        return $this->interests_remember;
    }

    public function getInterestsDate()
    {
        return $this->interests_date;
    }

    public function getInterestsArea($customer_id, $modulePosition = 'content_top')
    {
        $layoutId = array();
        $modules = array();
        $config = array();
        $load = $this->registry->get('load');
        $load->model('account/customer');
        $interest = $load->model_account_customer->getInterestsCustomer($customer_id);
        if ($this->config->get('lpbc_module_interests_status')) {
            $configModules = $this->config->get('lpbc_module_interests');
        }

        $configModulesFilteredByPosition = null;

        if(is_array($configModules)){
            $configModulesFilteredByPosition = array_filter($configModules, function ($module) use ($modulePosition) {
                return $module['position'] == $modulePosition && $module['status'];
            });
        }

        if (isset($configModulesFilteredByPosition)) {
            foreach ($configModulesFilteredByPosition as $key => $value) {
                if (!in_array($value['layout_id'], $layoutId)) {
                    $layoutId[] = $value['layout_id'];
                    $config[$value['layout_id']] = array(
                        'entry_type' => (isset($value['entry_type'])) ? $value['entry_type'] : 0,
                        'limit' => (isset($value['limit'])) ? $value['limit'] : Type_DefaultValues::Tp_LimitModules24,
                        'position' => $value['position']
                    );
                }
            }

            foreach ($layoutId as $key => $value) {
                foreach ($interest as $intKey => $intValue) {
                    $modules[] = array(
                        'category_id' => $intValue['category_id'],
                        'layout_id' => $value,
                        //  'dm'           => $config[$value]['dm'],
                        'entry_type' => $config[$value]['entry_type'],
                        'limit' => $config[$value]['limit'],
                        'position' => $config[$value]['position'],
                        'status' => 1,
                        'sort_order' => $this->config->get('lpbc_module_interests_order') ? $this->config->get('lpbc_module_interests_order') : 1
                    );
                }
            }
        }

        return $modules;
    }

    public function getPoints()
    {

        if ($this->config->get('fidelity_status') && $this->customer->getID_Futura()) {
            $this->load->dmodel('fidelity/account');
            return (int)$this->dmodel_fidelity_account->getTotalfidelityByCustomer();
        } else {
            return false;
        }
    }

    public function getDate_added()
    {
        return $this->date_added;
    }

    public function getFeedbackWhatsappStatus()
    {
        return $this->feedback_whatsapp_status;
    }

    public function getFeedbackWhatsappNumber()
    {
        return $this->feedback_whatsapp_number;
    }

    public function getMandatoryInfo()
    {

        return array(
            'telephone' => $this->telephone,
            'celular'   => $this->celular
        );
    }
}
