<?php
/**
* 2007-2020 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@buy-addons.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author    Buy-Addons <contact@buy-addons.com>
*  @copyright 2007-2020 PrestaShop SA
*  @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
* @since 1.6
*/

class AskforaquotemodulcustomernewquoteModuleFrontController extends ModuleFrontController
{
    public function run()
    {
        $quote_module_token = Tools::getValue('quote_token');
        if ($quote_module_token != md5(sha1(_COOKIE_KEY_.'askforaquotemodul'))) {
            die;
        } else {
            $key_search = Tools::getValue('search_customer');
            $jsshopid = Tools::getValue('jsshopid');
            if ($key_search != false) {
                $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
                $sql_search = 'SELECT * FROM ' . _DB_PREFIX_ . 'customer WHERE (firstname LIKE  "%';
                $sql_search .= pSQL($key_search) . '%" OR lastname LIKE "%'.pSQL($key_search).'%"' ;
                $sql_search .= ' OR email LIKE "%'.pSQL($key_search).'%") AND id_shop="'.(int)$jsshopid.'"';
                $sql_search .= ' AND is_guest = 0';
                $info_customer = $db->executeS($sql_search);
                $customer_info = array();
                $id_customer = array();
                $firstname_cus = array();
                $lastname_cus = array();
                $birthdays_cus = array();
                $emai_cus = array();
                $id_lang_cus = array();
                $newsletter_cus = array();
                $optin_cus = array();
                $status = array();
                foreach ($info_customer as $value) {
                    $id_customer[] = $value['id_customer'];
                    $firstname_cus[] = $value['firstname'];
                    $lastname_cus[] = $value['lastname'];
                    $emai_cus[] = $value['email'];
                    $birthdays_cus[] = $value['birthday'];
                    $id_lang_cus[] = $value['id_lang'];
                    $newsletter_cus[] = $value['newsletter'];
                    $optin_cus[] = $value['optin'];
                    $status[] = $value['active'];
                }
                $customer_info['id_customer'] = $id_customer;
                $customer_info['firstname'] = $firstname_cus;
                $customer_info['lastname'] = $lastname_cus;
                $customer_info['email'] = $emai_cus;
                $customer_info['birthday'] = $birthdays_cus;
                $customer_info['id_lang'] = $id_lang_cus;
                $customer_info['newsletter_cus'] = $newsletter_cus;
                $customer_info['optin'] = $optin_cus;
                $customer_info['status'] = $status;
                echo Tools::jsonEncode($customer_info);
            } else {
                $info_customer = array();
                $id_customer = Tools::getValue('id_customer');
                $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
                $sql_update = 'UPDATE ' . _DB_PREFIX_ . 'customer SET is_guest=0 ';
                $sql_update .= 'WHERE id_customer = ' . $id_customer . '';
                $db->query($sql_update);
                $sql_get = 'SELECT * FROM ' . _DB_PREFIX_ . 'customer WHERE id_customer = ' . $id_customer . '';
                $info_cus = $db->ExecuteS($sql_get);
                $sql_g = 'SELECT * FROM ' . _DB_PREFIX_ . 'address WHERE id_customer = ' . $id_customer . '';
                $infor_address = $db->ExecuteS($sql_g);
                $customer_info = array();
                $id_customer = array();
                $firstname_cus = array();
                $lastname_cus = array();
                $birthdays_cus = array();
                $emai_cus = array();
                $id_lang_cus = array();
                $newsletter_cus = array();
                $optin_cus = array();
                $status = array();
                foreach ($info_cus as $value) {
                    $id_customer[] = $value['id_customer'];
                    $firstname_cus[] = $value['firstname'];
                    $lastname_cus[] = $value['lastname'];
                    $emai_cus[] = $value['email'];
                    $birthdays_cus[] = $value['birthday'];
                    $id_lang_cus[] = $value['id_lang'];
                    $newsletter_cus[] = $value['newsletter'];
                    $optin_cus[] = $value['optin'];
                    $status[] = $value['active'];
                }
                if (!empty($infor_address)) {
                    $company = $infor_address[0]['company'];
                    if (!empty($infor_address[0]['phone'])) {
                        $phone = $infor_address[0]['phone'];
                    } else {
                        $phone = $infor_address[0]['phone_mobile'];
                    }
                }
                $info_customer['company'] = @$company;
                $info_customer['phone'] = @$phone;
                $info_customer['id_customer'] = $id_customer;
                $info_customer['firstname'] = $firstname_cus;
                $info_customer['lastname'] = $lastname_cus;
                $info_customer['email'] = $emai_cus;
                $info_customer['birthday'] = $birthdays_cus;
                $info_customer['id_lang'] = $id_lang_cus;
                $info_customer['newsletter_cus'] = $newsletter_cus;
                $info_customer['optin'] = $optin_cus;
                $info_customer['status'] = $status;
                $sql_get_lang = 'SELECT name FROM ' . _DB_PREFIX_ . 'lang WHERE id_lang = '.$info_cus[0]['id_lang'].'';
                $name_lang = $db->ExecuteS($sql_get_lang);
                $info_customer['infor'] = $info_cus[0];
                $info_customer['name_lang'] = $name_lang[0]['name'];
                echo Tools::jsonEncode($info_customer);
            }
        }
    }
}
