<?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 AskforaquotemodulquotesModuleFrontController extends ModuleFrontController
{
    public $position = 0;
    public function initContent()
    {
        require_once(_PS_MODULE_DIR_.'askforaquotemodul/libs/phpmailer/vendor/autoload.php');
        $id_user = Context::getContext()->cookie->client;
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $ss_id_quote = 'Select distinct id_user from ' . _DB_PREFIX_ . 'product_doubleboo where id_user like "';
        $ss_id_quote .= pSQL($id_user).'"';
        $product_total_price = 0;
        $ss_id_q = $db->ExecuteS($ss_id_quote, true, false);
        $customer = (int) $this->context->customer->id;
        $option_login_customer = Configuration::get('Option_login_customer');
        $option_show_captcha = Configuration::get('Option_show_captcha');
        $option_company_name = Configuration::get('Option_company_name');
        $check_login = 0;
        $success_submit = 0;
        $check_typefile = 0;
        $check_sizefile = 0;
        $error_email = 0;
        $error_captcha = 0;
        $check_email = 0;
        $check_file_extension_admin = 0;
        $file_invalid = 0;
        $option_file_extension = Configuration::get('Option_file_extension');
        $lower_file_extension = Tools::strtolower($option_file_extension);
        $lower_file_extension = str_replace(' ', '', $lower_file_extension);
        $array_file_type = explode(',', $lower_file_extension);
        $id_currency = $this->context->currency->id;
        if ($option_file_extension == null) {
            $check_file_extension_admin = 1;
        }
        $useTax = $this->module->useTax();
        $prefixTax = $this->module->prefixTax();
        if (!empty($ss_id_q)) {
            if (Tools::isSubmit('submit_quote_dbboo')) {
                if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) {
                    $this->ps_cipherTool = new PhpEncryption(_NEW_COOKIE_KEY_);
                } else {
                    $this->ps_cipherTool = new Blowfish(_COOKIE_KEY_, _COOKIE_IV_);
                }
                $id_currency = $this->context->currency->id;
                if (isset($_FILES["fileToUpload_dbboo"])) {
                    $file_upload = array();
                    $file_info = $_FILES["fileToUpload_dbboo"];
                    $id_user_dbboo = $ss_id_q[0]['id_user'];
                    $file_name = $file_info['name'];
                    $file_tmp_name = $file_info['tmp_name'];
                    $file_size = $file_info['size'];
                    $array_name_file = explode(".", $file_name);
                    $file_type = end($array_name_file);
                    $position_file_type = strpos($file_name, '.'.$file_type);
                    $name_edit = Tools::substr($file_name, 0, $position_file_type);
                    $date_now_h_i_s = date('Y-m-d_H-i-s');
                       $file_name_attached = $name_edit .'_file'.$id_user_dbboo.'_'. $date_now_h_i_s .'.'. $file_type;
                    $save_to = _PS_MODULE_DIR_ . 'askforaquotemodul/stories/' . $file_name_attached;
                    move_uploaded_file($file_tmp_name, $save_to);
                    $file_upload['name'] = $file_name;
                    $file_upload['type'] = $file_type;
                    $file_upload['tmp_name'] = $file_tmp_name;
                    $file_upload['size'] = $file_size;
                    $file_upload['url'] = $save_to;
                    $file_upload = Tools::jsonEncode($file_upload);
                    Configuration::UpdateValue('file_'.$id_user_dbboo, $file_upload);
                }
                $option_show_captcha = Configuration::get('Option_show_captcha');
                $quote_name = Tools::getValue('quote_name');
                $subjectt = $this->module->l('New Quote from your website.', 'quotes');
                if (empty($quote_name)) {
                    $quote_name = $subjectt;
                }
                $quote_message = Tools::getValue('bargain_message');
                $total_pri = Tools::getValue('total_dbboo');
                $first_name = Tools::getValue('first_name');
                $last_name = Tools::getValue('last_name');
                $phone_number = Tools::getValue('phone_number');
                $email = Tools::getValue('email_dbboo');
                $company_name = Tools::getValue('company_name');
                $company_address = Tools::getValue('company_address');
                
                if ($email != '') {
                    $check_email = 1;
                }
                $ss_id = $ss_id_q[0]['id_user'];
                if ($_FILES['fileToUpload_dbboo']['error'] == 0) {
                    $check_file_choose = true;
                } else {
                    $check_file_choose = false;
                }
            
                $time_send = time();
                $check_typefile = 0;
                $check_sizefile = 0;
                $customer = (int) $this->context->customer->id;
                $id_lang = $this->context->language->id;
                $lang = new language($id_lang);
                $dateFormatLite = $lang->date_format_lite;
                $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
                $date_now = $this->formatDate($time_send, $dateFormatLite);
                $date_now_h_i_s = date('Y-m-d H:i:s');
                $c = $id_currency;
                $l = $id_lang;
                $fn = $first_name;
                $ln = $last_name;
                $ph = $phone_number;
                $e = $email;
                $qn = $quote_name;
                $qm = $quote_message;
                $d = $date_now_h_i_s;
                $tp = $total_pri;
                $cu = $customer;
                $cn = $company_name;
                $ca2 = $company_address;
                $this->context->cookie->{'first_name'} = $first_name;
                $this->context->cookie->{'last_name'} = $last_name;
                $this->context->cookie->{'phone_number'} = $phone_number;
                $this->context->cookie->{'email_customer'} = $email;
                $this->context->cookie->{'quote_name'} = $quote_name;
                $this->context->cookie->{'bargain_message'} = $quote_message;
                $this->context->cookie->{'company_name'} = $company_name;
                $this->context->cookie->{'company_address'} = $company_address;
                if ($customer == 0) {
                    $creatcustomer = new Customer();
                    $password = md5(time()._COOKIE_KEY_);
                    $creatcustomer->firstname = $first_name;
                    $creatcustomer->lastname = $last_name;
                    $creatcustomer->email = $email;
                    $creatcustomer->newsletter = 1;
                    $creatcustomer->active = 1;
                    $creatcustomer->is_guest = 1;
                    $creatcustomer->passwd = $password;
                    $creatcustomer->add();
                    if (!empty($creatcustomer->id)) {
                        $customer = $creatcustomer->id;
                        $cu = $customer;
                    }
                }
                if ($option_show_captcha == 1) {
                    $captcha_code = Tools::getValue('catpcha');
                    $security_code = $this->context->cookie->{'code_catpcha'};
                    $captcha_code_lower = Tools::strtolower($captcha_code);
                    if ($captcha_code_lower === $security_code) {
                        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                            $error_email = 1;
                        } else {
                            if ($check_file_choose == true) {
                                if ($check_file_extension_admin == 1) {
                                    $file_invalid = 1;
                                } else {
                                    $get_file_config = Configuration::get('file_' . $ss_id);
                                    if ($get_file_config != null) {
                                        $file_upload = Tools::jsonDecode($get_file_config);
                                        $file_name = $file_upload->name;
                                        $file_tmp_name = $file_upload->tmp_name;
                                        $file_size = $file_upload->size;
                                        $file_type = $file_upload->type;
                                        $file_type_lower = Tools::strtolower($file_type);
                                        $file_url = $file_upload->url;
                                        $fa = $file_url;
                                    }
                                    if (in_array($file_type_lower, $array_file_type) == false) {
                                        $check_typefile = 1;
                                    }
                                    if ($file_size > 5242880) {
                                        $check_sizefile = 1;
                                    }
                                    if ($check_typefile === 0 && $check_sizefile === 0) {
                                        $si = $ss_id;
                                        $f = $fn;
                                        $p = $ph;
                                        $q = $qn;
                                        $t = $tp;
                                        $m = $qm;
                                        if ($customer == '') {
                                            $cu = 0;
                                        }
                                        $this->submitquote($c, $l, $f, $ln, $cn, $p, $e, $q, $m, $d, $t, $si, $cu, $fa, $ca2);
                                        $id_quote_db = $this->idquote();
                                        $this->insertmess($id_quote_db, $email, $quote_message, $date_now);
                                        $this->delproduct($ss_id);
                                        Configuration::UpdateValue('file_'.$ss_id, null);
                                        $success_submit = 1;
                                    }
                                }
                            } else {
                                $fa = '';
                                if ($customer == '') {
                                    $cu = 0;
                                }
                                $this->submitquote($c, $l, $fn, $ln, $cn, $ph, $e, $qn, $qm, $d, $tp, $ss_id, $cu, $fa, $ca2);
                                $id_quote_db = $this->idquote();
                                $this->insertmess($id_quote_db, $email, $quote_message, $date_now);
                                $this->delproduct($ss_id);
                                Configuration::UpdateValue('file_'.$ss_id, null);
                                $success_submit = 1;
                            }
                        }
                    } else {
                        $error_captcha = 1;
                        $this->context->smarty->assign("error_captcha", $error_captcha);
                    }
                } else {
                    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                        $error_email = 1;
                           $this->context->smarty->assign("error_email", $error_email);
                    } else {
                        if ($check_file_choose == true) {
                            if ($check_file_extension_admin == 1) {
                                $file_invalid = 1;
                            } else {
                                $get_file_config = Configuration::get('file_' . $ss_id);
                                if ($get_file_config != null) {
                                    $file_upload = Tools::jsonDecode($get_file_config);
                                    $file_name = $file_upload->name;
                                    $file_tmp_name = $file_upload->tmp_name;
                                    $file_size = $file_upload->size;
                                    $file_type = $file_upload->type;
                                    $file_type_lower = Tools::strtolower($file_type);
                                    $file_url = $file_upload->url;
                                    $fa = $file_url;
                                }
                                if (in_array($file_type_lower, $array_file_type) == false) {
                                    $check_typefile = 1;
                                }
                                if ($file_size > 5242880) {
                                    $check_sizefile = 1;
                                }
                                if ($check_typefile === 0 && $check_sizefile === 0) {
                                    $si = $ss_id;
                                    $m = $qm;
                                    if ($customer == '') {
                                        $cu = 0;
                                    }
                                    $this->submitquote($c, $l, $fn, $ln, $cn, $ph, $e, $qn, $m, $d, $tp, $si, $cu, $fa, $ca2);
                                    $id_quote_db = $this->idquote();
                                    $this->insertmess($id_quote_db, $email, $quote_message, $date_now);
                                    $this->delproduct($ss_id);
                                    Configuration::UpdateValue('file_'.$ss_id, null);
                                    $success_submit = 1;
                                }
                            }
                        } else {
                               $fa = '';
                            if ($customer == '') {
                                $cu = 0;
                            }
                            $this->submitquote($c, $l, $fn, $ln, $cn, $ph, $e, $qn, $qm, $d, $tp, $ss_id, $cu, $fa, $ca2);
                            $id_quote_db = $this->idquote();
                            $this->insertmess($id_quote_db, $email, $quote_message, $date_now);
                            $this->delproduct($ss_id);
                            Configuration::UpdateValue('file_'.$ss_id, null);
                            $success_submit = 1;
                        }
                    }
                }
            }
            $id_u = $ss_id_q[0]['id_user'];
            $id_lang = $this->context->language->id;
            $id_p = $this->listidproduct($id_u);
            $count_sp = Count($id_p);
            $store = array();
            foreach ($id_p as $key => $value) {
                $id_pro = $value['id_product'];
                $product = new Product($id_pro, false, $id_lang);
                $attribute_df = $product->getDefaultAttribute($id_pro);
                $attribute_pro = $product->getAttributeCombinations($id_lang);
                $id_combination = $this->getidcombination($id_u);
                $id_combi_dbboo = $id_combination[$key]['id_combination'];
                $so_sp = $this->getquantityproduct($id_pro, $id_combi_dbboo, $id_u);
                if ($attribute_df == '') {
                    $pri_pro = $product->price;
                    $image = new Image();
                    $dp_image = $image->getImages($id_lang, $id_pro);
                    $link_rew = $product->link_rewrite;
                    $linkimages = new Link();
                    if ($dp_image == null) {
                        $link_img = '' . Tools::getServerName() . '' . __PS_BASE_URI__ . '';
                        $link_img .= 'modules/askforaquotemodul/views/img/noimg.png';
                    } else {
                        $id_img = Product::getCover($id_pro);
                        $id_img = $id_img['id_image'];
                        if (!isset($id_img)) {
                            $id_img = 0;
                        }
                        $imagetype =  '';
                        if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) {
                            $imagetype = ImageType::getFormattedName('cart');
                        } else {
                            $imagetype = ImageType::getFormatedName('cart');
                        }
                        $link_img = $linkimages->getImageLink($link_rew, $id_img, $imagetype);
                    }
                    $link_pro = $linkimages->getProductLink($id_pro);
                    $product->img_doubleboo = Tools::getShopProtocol() . $link_img;
                    $product->link_pro = $link_pro;
                    $product->price = $pri_pro;
                    $s = $so_sp[0]['count_product'];
                    $pri_pro_tax = $product->getPrice($useTax, $id_combi_dbboo, 4, null, false, true, $s);
                    $product->price_tax = $pri_pro_tax;
                    $product->price_full = Tools::displayPrice($pri_pro_tax, $id_currency).$prefixTax;
                    $product->id_combi_dbboo = $id_combi_dbboo;
                    $product->so_sp = $so_sp[0]['count_product'];
                    $tt_price = $pri_pro_tax * $so_sp[0]['count_product'];
                    $product->total_full = Tools::displayPrice($tt_price, $id_currency).$prefixTax;
                    $store[$key] = $product;
                } else {
                    $attr_group = new AttributeGroup();
                    $get_attr = $attr_group->getAttributesGroups($id_lang);
                    $size_attr = '';
                    $link_rew = $product->link_rewrite;
                    $image = new Image();
                    $dp_image = $image->getImages($id_lang, $id_pro);
                    $linkimages = new Link();
                    foreach ($get_attr as $key_gr => $value_attr) {
                        $value_attr;
                        foreach ($attribute_pro as $key_idattr => $value_idattr) {
                            if ($id_combi_dbboo == $value_idattr['id_product_attribute']) {
                                $gr_name_attr = $attribute_pro[$key_idattr]['group_name'];
                                if ($gr_name_attr == $get_attr[$key_gr]['name']) {
                                    $id_pro_attribute = $attribute_pro[$key_idattr]['id_product_attribute'];
                                    $id_image_attribute = $this->getidimage($id_pro_attribute);
                                    if (empty($id_image_attribute)) {
                                        $id_img = Product::getCover($id_pro);
                                        $id_img = $id_img['id_image'];
                                        if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) { // for PrestaShop 1.7+
                                             $type_img = ImageType::getFormattedName('cart');
                                        } else {
                                             $type_img = ImageType::getFormatedName('cart');
                                        }
                                        
                                        if (!isset($id_img)) {
                                            $id_img = 0;
                                        }
                                        $link_img = $linkimages->getImageLink($link_rew, $id_img, $type_img);
                                        $product->img_doubleboo = Tools::getShopProtocol() . $link_img;
                                    } else {
                                        foreach ($id_image_attribute as $key_img => $value_img) {
                                            $value_img;
                                            $img_attr = $id_image_attribute[$key_img]['id_image'];
                                            if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) {
                                                $type_img = ImageType::getFormattedName('cart');
                                            } else {
                                                $type_img = ImageType::getFormatedName('cart');
                                            }
                                            $link_img = $linkimages->getImageLink($link_rew, $img_attr, $type_img);
                                            $product->img_doubleboo = Tools::getShopProtocol() . $link_img;
                                        }
                                    }
                                    $name_attr = $get_attr[$key_gr]['name'];
                                    $attr_dt = $attribute_pro[$key_idattr]['attribute_name'];
                                    $size_attr = $name_attr .':'. $attr_dt . ', ' . $size_attr;
                                } else {
                                    $size_attr .= '';
                                }
                                
                                $result = Tools::substr($size_attr, 0, -2);
                                $product->attr_dbboo = $result;
                            }
                        }
                    }
                    $product_attr = $product->getAttributeCombinationsById($id_combi_dbboo, $id_lang);
                    if (!empty($product_attr[0]['reference'])) {
                        $product->reference = Tools::safeOutput($product_attr[0]['reference']);
                    }
                    $pri_pro = $product->price;
                    $link_pro = $linkimages->getProductLink($id_pro);
                    $product->link_pro = $link_pro;
                    $product->so_sp = $so_sp[0]['count_product'];
                    $s = $so_sp[0]['count_product'];
                    $pri_pro_tax = $product->getPrice($useTax, $id_combi_dbboo, 4, null, false, true, $s);
                    $product->price_tax = $pri_pro_tax;
                    $product->price = $pri_pro;
                    $product->id_combi_dbboo = $id_combi_dbboo;
                    $product->price_full = Tools::displayPrice($pri_pro_tax, $id_currency).$prefixTax;
                    $tt_price = $pri_pro_tax * $so_sp[0]['count_product'];
                    $product->total_full = Tools::displayPrice($tt_price, $id_currency).$prefixTax;
                    $store[$key] = $product;
                }
                $total_product_qty = ((double) $pri_pro_tax * (int) $product->so_sp);
                $product_total_price = (double) $product_total_price + $total_product_qty;
            }
            $num_total = array();
            $num_total['number'] = $count_sp;
            $num_total['total_price'] = $product_total_price;
            $num_total['total_price_full'] = Tools::displayPrice($product_total_price, $id_currency).$prefixTax;
            if ($option_login_customer == 1) {
                if ($customer == '') {
                    $check_login = 1;
                }
            }
            if ($customer != '') {
                $info_cus = 'SELECT * from '._DB_PREFIX_.'customer where id_customer like "'.(int)$customer.'"';
                $info_customer = $db->ExecuteS($info_cus, true, false);
                $this->context->cookie->{'first_name'} = $info_customer[0]['firstname'];
                $this->context->cookie->{'last_name'} = $info_customer[0]['lastname'];
                $this->context->cookie->{'email_customer'} = $info_customer[0]['email'];
                $info_cuss = 'SELECT * from '._DB_PREFIX_.'address where id_customer like "'.(int)$customer.'"';
                $info_customerr = $db->ExecuteS($info_cuss, true, false);
                if (!empty($info_customerr[0]['company'])) {
                    $this->context->cookie->{'company_name'} = $info_customerr[0]['company'];
                }
                if (empty($info_customerr[0]['phone'])) {
                    if (!empty($info_customerr[0]['phone_mobile'])) {
                        $this->context->cookie->{'phone_number'} = $info_customerr[0]['phone_mobile'];
                    }
                } else {
                    $this->context->cookie->{'phone_number'} = $info_customerr[0]['phone'];
                }
            }
            $first_name = $this->context->cookie->{'first_name'};
            $last_name = $this->context->cookie->{'last_name'};
            $phone_number = $this->context->cookie->{'phone_number'};
            $email = $this->context->cookie->{'email_customer'};
            $quote_name = $this->context->cookie->{'quote_name'};
            $quote_message = $this->context->cookie->{'bargain_message'};
            $company_name = $this->context->cookie->{'company_name'};
            $company_address = $this->context->cookie->{'company_address'};
            $option_price_total = Configuration::get('Option_price_total');
            $web_version = Tools::getShopProtocol() . Tools::getHttpHost() . __PS_BASE_URI__;
            $link_quote_form = $web_version . 'index.php?controller=quotes&fc=module&module=askforaquotemodul';
            $link_encode_quote = urlencode($link_quote_form);
            $link = new Link();
            $link_login = $link->getPageLink('authentication', true, null, "back=".$link_encode_quote);
            $this->context->smarty->assign("num_total", $num_total);
            $this->context->smarty->assign("link_encode_quote", $link_login);
            $this->context->smarty->assign("check_login", $check_login);
            $this->context->smarty->assign("details_product_quote", $store);
            $this->context->smarty->assign("web_version", $web_version);
            $this->context->smarty->assign('option_price_total', $option_price_total);
            $this->context->smarty->assign("option_company_name", $option_company_name);
            $this->context->smarty->assign("first_name", $first_name);
            $this->context->smarty->assign("last_name", $last_name);
            $this->context->smarty->assign("company_name", $company_name);
            $this->context->smarty->assign("company_address", $company_address);
            $this->context->smarty->assign("phone_number", $phone_number);
            $this->context->smarty->assign("email", $email);
            $this->context->smarty->assign("quote_name", $quote_name);
            $this->context->smarty->assign("quote_message", $quote_message);
            $this->context->smarty->assign('option_show_captcha', $option_show_captcha);
            $this->context->smarty->assign('head_link_img', __PS_BASE_URI__);
            $this->context->smarty->assign("success_submit", $success_submit);
            $this->context->smarty->assign("error_type", $check_typefile);
            $this->context->smarty->assign("error_size", $check_sizefile);
            $this->context->smarty->assign("error_email", $error_email);
            $this->context->smarty->assign("error_captcha", $error_captcha);
            $this->context->smarty->assign("email_dbboo", $check_email);
            $this->context->smarty->assign("lower_file_extension", $lower_file_extension);
            $this->context->smarty->assign("file_invalid", $file_invalid);
            $this->context->smarty->assign('path', __PS_BASE_URI__ . 'modules/askforaquotemodul/');
            if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) {
                $css_1 = _PS_MODULE_DIR_ . 'askforaquotemodul/views/css/styledetailsquote17.css';
                $this->context->controller->addCSS($css_1);
                $tpl_2 = _PS_MODULE_DIR_.'askforaquotemodul/views/templates/front/detailsquote.tpl';
                $html = $this->context->smarty->fetch($tpl_2);
                $this->context->smarty->assign("html", $html);
                $this->setTemplate('module:askforaquotemodul/views/templates/front/content.tpl');
            } else {
                $css_1 = _PS_MODULE_DIR_ . 'askforaquotemodul/views/css/styledetailsquote.css';
                $this->context->controller->addCSS($css_1);
                $module_dir = dirname(_PS_MODULE_DIR_) . '/modules/askforaquotemodul/';
                $tpl_module = $module_dir . 'views/templates/front/detailsquote.tpl';
                $template = $this->context->smarty->createTemplate($tpl_module, $this->context->smarty);
                $template->fetch();
                $this->setTemplate('detailsquote.tpl');
            }
        } else {
            $none_product = 0;
            $email_dbboo = 0;
            $error_email = 0;
            $error_type = 0;
            $error_size = 0;
            $error_captcha = 0;
            $success_submit = 0;
            $file_invalid = 0;
            $this->context->smarty->assign("details_product_quote", $none_product);
            $this->context->smarty->assign("success_submit", $success_submit);
            $this->context->smarty->assign("error_type", $error_type);
            $this->context->smarty->assign("error_size", $error_size);
            $this->context->smarty->assign("error_email", $error_email);
            $this->context->smarty->assign("error_captcha", $error_captcha);
            $this->context->smarty->assign("email_dbboo", $email_dbboo);
            $this->context->smarty->assign("file_invalid", $file_invalid);
            if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) {
                $css_1 = _PS_MODULE_DIR_ . 'askforaquotemodul/views/css/styledetailsquote17.css';
                $this->context->controller->addCSS($css_1);
                $tpl_1 = _PS_MODULE_DIR_.'askforaquotemodul/views/templates/front/detailsquote.tpl';
                $html = $this->context->smarty->fetch($tpl_1);
                $this->context->smarty->assign("html", $html);
                $this->setTemplate('module:askforaquotemodul/views/templates/front/content.tpl');
            } else {
                $css_1 = _PS_MODULE_DIR_ . 'askforaquotemodul/views/css/styledetailsquote.css';
                $this->context->controller->addCSS($css_1);
                $module_dir = dirname(_PS_MODULE_DIR_) . '/modules/askforaquotemodul/';
                $tpl_module = $module_dir . 'views/templates/front/detailsquote.tpl';
            
                $template = $this->context->smarty->createTemplate($tpl_module, $this->context->smarty);
                $template->fetch();
                $this->setTemplate('detailsquote.tpl');
            }
        }
        $this->context->smarty->assign("hide_right_column", '1');
        parent::initContent();
    }
    public function formatDate($date, $format)
    {
        $dateArr = array();
        if (strpos($date, " ")) {
            $dateArr = explode(" ", $date);
        }
        if (strpos($date, "/")) {
            $dateArr = explode("/", $date);
        }
        if (strpos($date, "-")) {
            $dateArr = explode("-", $date);
        }
        if (strpos($date, ";")) {
            $dateArr = explode(";", $date);
        }
        if (strpos($date, ":")) {
            $dateArr = explode(":", $date);
        }
        if (strpos($date, ".")) {
            $dateArr = explode(".", $date);
        }
        if (strpos($date, ",")) {
            $dateArr = explode(",", $date);
        }

        $formatArr = array();
        if (strpos($format, " ")) {
            $formatArr = explode(" ", $format);
        }
        if (strpos($format, "/")) {
            $formatArr = explode("/", $format);
        }
        if (strpos($format, "-")) {
            $formatArr = explode("-", $format);
        }
        if (strpos($format, ";")) {
            $formatArr = explode(";", $format);
        }
        if (strpos($format, ":")) {
            $formatArr = explode(":", $format);
        }
        if (strpos($format, ".")) {
            $formatArr = explode(".", $format);
        }
        if (strpos($format, ",")) {
            $formatArr = explode(",", $format);
        }
        $tmpArr = array();
        for ($i = 0; $i < count($dateArr); $i++) {
            $tmpArr[$formatArr[$i]] = $dateArr[$i];
        }
        $dateFormatArr = array();
        foreach ($tmpArr as $key => $valueTmp) {
            if ($key == "Y" || $key == "y") {
                $dateFormatArr[0] = $valueTmp;
            } elseif ($key == "m" || $key == "M") {
                $dateFormatArr[1] = $valueTmp;
            } elseif ($key == "d" || $key == "D") {
                $dateFormatArr[2] = $valueTmp;
            }
            if ($key == "H" || $key == "h") {
                $dateFormatArr[3] = $valueTmp;
            } elseif ($key == "I" || $key == "i") {
                $dateFormatArr[4] = $valueTmp;
            }
        }
        ksort($dateFormatArr);
        $numberSecondDate = strtotime(implode("/", $dateFormatArr));

        if ($numberSecondDate == false) {
            return date("Y-m-d h:ia");
        }
        return implode("-", $dateFormatArr);
    }
    public function countproduct($id_q)
    {
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $count_sp = 'SELECT COUNT(*) FROM ' . _DB_PREFIX_ . 'product_quote_detail where id_quote like "';
        $count_sp .= (int) $id_q . '"';

        $count_product = $db->Executes($count_sp, true, false);
        $product_Qty_view = $count_product[0]['COUNT(*)'];
        return $product_Qty_view;
    }
    public function listproductdetail($id_q)
    {
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $product_Qty_view = $this->countproduct($id_q);
        if ($product_Qty_view > 0) {
            $dp_product_quote = 'Select * from ' . _DB_PREFIX_ . 'product_quote_detail where id_quote like "';
            $dp_product_quote .= (int) $id_q . '"';
            $product_quote = $db->ExecuteS($dp_product_quote, true, false);
            $linkimages = new Link();
            foreach ($product_quote as $key_link => $value_link) {
                $id_currency_quote = (int)$product_quote[$key_link]['id_currency'];
                $obj_currency = new Currency($id_currency_quote);
                $id_pro_quote = (int) $value_link['id_product'];
                $link_product = $linkimages->getProductLink($id_pro_quote);
                $product_quote[$key_link]['link_product'] = $link_product;
                $product_price = Tools::displayPrice($value_link['product_pri'], $id_currency_quote);
                $total_pr_price = Tools::displayPrice($value_link['total_pri'], $id_currency_quote);
                $product_quote[$key_link]['product_pri'] = $product_price;
                $product_quote[$key_link]['total_pri'] = $total_pr_price;
                $product_quote[$key_link]['sign'] = $obj_currency->sign;
                $product_quote[$key_link]['cache_is_pack'] = Pack::isPack((int)$id_pro_quote);
                // since 1.0.39+
                $price_incl_tax = $value_link['product_pri_incl_tax'];
                $total_price_incl_tax = $value_link['total_pri_incl_tax'];
                $price_incl_tax = Tools::displayPrice($price_incl_tax, $id_currency_quote);
                $total_price_incl_tax = Tools::displayPrice($total_price_incl_tax, $id_currency_quote);
                $product_quote[$key_link]['price_incl_tax'] = $price_incl_tax;
                $product_quote[$key_link]['total_price_incl_tax'] = $total_price_incl_tax;
            }
            return $product_quote;
        } else {
            return 0;
        }
    }
    public function quoteinfo($id_q)
    {
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $dp_info_cus = 'Select * from ' . _DB_PREFIX_ . 'product_quote_dbboo where id_quote like "';
        $dp_info_cus .= (int) $id_q . '"';
        $info_customer = $db->ExecuteS($dp_info_cus, true, false);
            
        $sign_currency_quote = (int)$info_customer[0]['id_currency'];
        $obj_currency = new Currency($sign_currency_quote);
        $info_customer[0]['sign'] = $obj_currency->sign;
        $info_customer[0]['total_pri'] = Tools::displayPrice($info_customer[0]['total_pri'], $obj_currency);
        return $info_customer;
    }

    public function deletefileuser($dir_folder, $ss_id)
    {
        $list_file = scandir($dir_folder);
        $array_name_file = array();
        foreach ($list_file as $key => $value) {
            $value;
            $array_name_file = explode("_", $list_file[$key]);
            if (in_array('file' . $ss_id, $array_name_file) == true) {
                unlink($dir_folder.$list_file[$key]);
            }
        }
    }
    public function inserttoquote($c, $cu, $l, $fn, $ln, $cn, $p, $e, $qn, $qm, $d, $fa, $tp, $ca2 = null)
    {
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $intopd = 'INSERT INTO ' . _DB_PREFIX_ . 'product_quote_dbboo ';
        $intopd .= '(id_currency,id_customer,id_lang,first_name,last_name,company_name,phone,email,quote_name,';
        $intopd .= 'message,date_quote,file_attached,total_pri,nb_mess_ud,company_address)';
        $intopd .= 'values("' . (int) $c . '","' . (int) $cu . '","' . (int) $l . '",';
        $intopd .= '"' . pSQL($fn) . '","' . pSQL($ln) . '","' . pSQL($cn) . '",';
        $intopd .= '"' . pSQL($p) . '","' . pSQL($e) . '","' . pSQL($qn) . '",';
        $intopd .= '"' . pSQL($qm) . '","' . pSQL($d) . '","' . pSQL($fa) . '",';
        $intopd .= '"' . pSQL($tp) . '","1",';
        $intopd .= '"' . pSQL($ca2) . '")';
        return $db->query($intopd);
    }

    public function getidcombination($ss_id)
    {
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $find_id = 'Select id_combination from ';
        $find_id .= _DB_PREFIX_ . 'product_doubleboo where id_user like "';
        $find_id .= pSQL($ss_id) . '" ';
        return $db->ExecuteS($find_id, true, false);
    }

    public function listidproduct($ss_id)
    {
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $dp_id = 'Select id_product from ' . _DB_PREFIX_ . 'product_doubleboo where id_user like "';
        $dp_id .= pSQL($ss_id) . '"';
        $idp = $db->ExecuteS($dp_id, true, false);
        return $idp;
    }

    public function insertcusmes($id_quote_db, $customer)
    {
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $intocus_mes = 'Insert  into ';
        $intocus_mes .= _DB_PREFIX_ . 'cus_message_dbboo(id_quote,id_customer,so_luong_mes)';
        $intocus_mes .= 'values("' . (int) $id_quote_db . '","' . (int) $customer . '","0")';
        return $db->query($intocus_mes);
    }

    public function idquote()
    {
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $dp_id_q = 'Select id_quote from ' . _DB_PREFIX_ . 'product_quote_dbboo ORDER BY id_quote DESC';
        $dp_id_quote = $db->getrow($dp_id_q, false);
        $id_quote_db = $dp_id_quote['id_quote'];
        return($id_quote_db);
    }

    public function getquantityproduct($id_pro, $id_combi_dbboo, $ss_id)
    {
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $dp_so_sp = 'Select count_product from ';
        $dp_so_sp .= _DB_PREFIX_ . 'product_doubleboo where id_product like ';
        $dp_so_sp .= (int) $id_pro . ' and id_combination like "';
        $dp_so_sp .= (int) $id_combi_dbboo . '" and id_user like "';
        $dp_so_sp .= pSQL($ss_id) . '" ';
        return $db->ExecuteS($dp_so_sp, true, false);
    }

    public function insertquotedetail($iq, $icu, $il, $p, $ip, $pn, $cb, $ppt, $pq, $pp, $id_customer = 0)
    {
        $unitprice_incl_tax = (float) $this->module->getProductPriceTax($ppt, $ip, $id_customer, $il);
        $totalprice_incl_tax = (float) $unitprice_incl_tax * $pq;
        $id_tax_rules_group = (int) $this->module->getIdTaxRulesGroupByIdProduct($ip);
        $this->position++;
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $intopd = 'INSERT INTO ' . _DB_PREFIX_ . 'product_quote_detail';
        $intopd .= '(id_quote,id_currency,id_lang,link_img,id_product,product_name,reference,';
        $intopd .= 'id_combination,combination,product_pri,product_qty,total_pri';
        $intopd .= ',product_pri_incl_tax, id_tax_rules_group, total_pri_incl_tax, position) values("';
        $intopd .= (int) $iq . '","' . (int) $icu . '",';
        $intopd .= '"' . (int) $il . '","' . pSQL($p->img_doubleboo) . '","';
        $intopd .= (int) $ip . '","' . pSQL($pn) . '","' . pSQL($p->reference) . '",';
        $intopd .= '"' . (int) $p->id_combi_dbboo . '","' . pSQL($cb) . '","' . pSQL($ppt) . '",';
        $intopd .= '"' . (int) $pq . '","' . pSQL($pp).'"';
        $intopd .= ",".$unitprice_incl_tax;
        $intopd .= ",".$id_tax_rules_group;
        $intopd .= ",".$totalprice_incl_tax;
        $intopd .= ",".$this->position;
        $intopd .= ')';
        return $db->query($intopd);
    }

    public function getidimage($id_pro_attribute)
    {
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $id_image_attr = 'Select id_image ';
        $id_image_attr .= 'from ' . _DB_PREFIX_ . 'product_attribute_image';
        $id_image_attr .= ' where id_product_attribute like "';
        $id_image_attr .= $id_pro_attribute . '"';
        return $db->ExecuteS($id_image_attr, true, false);
    }

    public function insertmess($id_quote_db, $email, $quote_message, $date_now)
    {
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $ins_mes = 'Insert into ';
        $ins_mes .= _DB_PREFIX_ . 'message_dbboo(id_quote,email,content_mes,date_send,type,status)';
        $ins_mes .= ' values ("' . (int) $id_quote_db . '","' . pSQL($email) . '",';
        $ins_mes .= '"' . pSQL($quote_message) . '","' . pSQL($date_now) . '","0","0")';
        return $db->query($ins_mes);
    }

    public function delproduct($ss_id)
    {
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $del_product = 'Delete from ' . _DB_PREFIX_ . 'product_doubleboo where id_user like "' . pSQL($ss_id) . '"';
        return $db->query($del_product);
    }

    public function submitquote($c, $l, $fn, $ln, $cn, $ph, $e, $qn, $qm, $d, $tp, $ss_id, $cu, $fa, $ca2 = null)
    {
        $store = array();
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $this->inserttoquote($c, $cu, $l, $fn, $ln, $cn, $ph, $e, $qn, $qm, $d, $fa, $tp, $ca2);
        $id_quote_db = $this->idquote();
        $id_q = $id_quote_db;
        $this->insertcusmes($id_q, $cu);
        $idp = $this->listidproduct($ss_id);
        foreach ($idp as $key => $value) {
            $id_pro = $value['id_product'];
            $product = new Product($id_pro, false, $l);
            $attribute_df = $product->getDefaultAttribute($id_pro);
            $attribute_pro = $product->getAttributeCombinations($l);
            $id_combination = $this->getidcombination($ss_id);
            $id_combi_dbboo = $id_combination[$key]['id_combination'];
            $so_luong_sp = $this->getquantityproduct($id_pro, $id_combi_dbboo, $ss_id);
            if ($attribute_df == '') {
                $pri_pro = $product->price;
                $pro_name = $product->name;
                $attr_dbboo = '';
                $product->attr_dbboo = $attr_dbboo;
                $product->so_luong_sp = $so_luong_sp;
                $product_qty = $product->so_luong_sp[0]['count_product'];

                $image = new Image();
                $dp_image = $image->getImages($l, $id_pro);
                $link_r = $product->link_rewrite;

                $link_i = new Link();
                if ($dp_image == null) {
                    $l_img = '' . Tools::getServerName() . '' . __PS_BASE_URI__ . '';
                    $l_img .= 'modules/askforaquotemodul/views/img/noimg.png';
                } else {
                    $id_i = Product::getCover($id_pro);
                    $id_i = $id_i['id_image'];
                    
                    if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) { // for PrestaShop 1.7+
                        $l_img = $link_i->getImageLink($link_r, $id_i, ImageType::getFormattedName('cart'));
                    } else {
                        $l_img = $link_i->getImageLink($link_r, $id_i, ImageType::getFormatedName('cart'));
                    }
                }
                $link_pro = $link_i->getProductLink($id_pro);
                $product->img_doubleboo = Tools::getShopProtocol() . $l_img;
                $product->link_pro = $link_pro;
                $s = $so_luong_sp[0]['count_product'];
                $pri_pro_tax = $product->getPrice(false, $id_combi_dbboo, 4, null, false, true, $s);
                $product->price_tax = $pri_pro_tax;
                $product_pri = (float) $pri_pro_tax * (int) $product_qty;

                $product->price = $pri_pro;
                $id_combi_dbboo = 0;
                $product->id_combi_dbboo = $id_combi_dbboo;
                $iq = $id_q;
                $p = $product;
                $ip = $id_pro;
                $pn = $pro_name;
                $ppt = $pri_pro_tax;
                $pq = $product_qty;
                $pp = $product_pri;
                $cb = '';
                if (Pack::isPack($id_pro)) {
                    $sql_select_pro_pack = 'SELECT * FROM ' . _DB_PREFIX_ . 'pack WHERE id_product_pack = ';
                    $sql_select_pro_pack .= (int) $id_pro;
                    $pro_pack = $db->executeS($sql_select_pro_pack);
                    $id_lang_new = (int) $this->context->language->id;
                    $combination = array();
                    foreach ($pro_pack as $key => $value) {
                        $qty = $value['quantity'];
                        $combinationn = '';
                        $attr_couple = array();
                        $product = new Product($value['id_product_item'], false, $id_lang_new);
                        $id_attribute = $value['id_product_attribute_item'];
                        $product_attr = $product->getAttributeCombinationsById($id_attribute, $id_lang_new);
                        if (!empty($product_attr)) {
                            foreach ($product_attr as $value) {
                                $group_name_attr = $value['group_name'];
                                $attribute_name_attr = $value['attribute_name'];
                                $attr_couple[] = $group_name_attr . ": " . $attribute_name_attr;
                            }
                        }
                        $combinationn = implode(',', $attr_couple);
                        if (!empty($combinationn)) {
                            $combination[$key] = $product->name.': '.$combinationn. $this->module->l(' x ').$qty;
                        } else {
                            $combination[$key] = $product->name. $this->module->l(' x ').$qty;
                        }
                    }
                    $cb = implode("\n", $combination);
                }
                $this->insertquotedetail($iq, $c, $l, $p, $ip, $pn, $cb, $ppt, $pq, $pp, $cu);

                $store[$key] = $product;
            } else {
                if ($id_combi_dbboo == 0) {
                    $id_combi_dbboo = $attribute_df;
                }
                $attr_group = new AttributeGroup();
                $get_attr = $attr_group->getAttributesGroups($l);
                $link_r = $product->link_rewrite;
                $linki = new Link();
                $image = new Image();
                $dp_image = $image->getImages($l, $id_pro);
                $size_attr = '';
                foreach ($get_attr as $key_gr => $value_attr) {
                    $value_attr;
                    foreach ($attribute_pro as $key_attr => $value_idattr) {
                        if ($id_combi_dbboo == $value_idattr['id_product_attribute']) {
                            $gr_name_attr = $attribute_pro[$key_attr]['group_name'];
                            if ($gr_name_attr == $get_attr[$key_gr]['name']) {
                                $id_pro_attribute = $attribute_pro[$key_attr]['id_product_attribute'];
                                $id_image_attribute = $this->getidimage($id_pro_attribute);
                                if (empty($id_image_attribute)) {
                                    $id_img = Product::getCover($id_pro);
                                    $id_img = $id_img['id_image'];
                                    if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) { // for PrestaShop 1.7+
                                        $type_img = ImageType::getFormattedName('cart');
                                    } else {
                                        $type_img = ImageType::getFormatedName('cart');
                                    }
                                    if (!isset($id_img)) {
                                        $id_img = 0;
                                    }
                                    $link_img = $linki->getImageLink($link_r, $id_img, $type_img);
                                    $product->img_doubleboo = Tools::getShopProtocol() . $link_img;
                                } else {
                                    foreach ($id_image_attribute as $key_img => $value_img) {
                                        $value_img;
                                        $img_attr = $id_image_attribute[$key_img]['id_image'];
                                        if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) {
                                            $type_img = ImageType::getFormattedName('cart');
                                        } else {
                                            if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) {
                                                $type_img = ImageType::getFormattedName('cart');
                                            } else {
                                                $type_img = ImageType::getFormatedName('cart');
                                            }
                                        }
                                        $link_img = $linki->getImageLink($link_r, $img_attr, $type_img);
                                        $product->img_doubleboo = Tools::getShopProtocol() . $link_img;
                                    }
                                }
                                $name_attr = $get_attr[$key_gr]['name'];
                                $attr_dt = $attribute_pro[$key_attr]['attribute_name'];
                                $size_attr = $name_attr . ': ' . $attr_dt . ', ' . $size_attr;
                            } else {
                                $size_attr .= '';
                            }
                            $result = Tools::substr($size_attr, 0, -2);
                            $product->attr_dbboo = $result;
                        }
                    }
                }
                $product_attr = $product->getAttributeCombinationsById($id_combi_dbboo, $l);
                if (!empty($product_attr[0]['reference'])) {
                    $product->reference = Tools::safeOutput($product_attr[0]['reference']);
                }
                $link_pro = $linki->getProductLink($id_pro);
                $product->link_pro = $link_pro;
                $pri_pro_attr = $product->price;
                $s = $so_luong_sp[0]['count_product'];
                $pri_pro_tax = $product->getPrice(false, $id_combi_dbboo, 4, null, false, true, $s);
                $product->price_tax = $pri_pro_tax;
                $product->price = $pri_pro_attr;
                $product->so_luong_sp = $so_luong_sp;
                $combination = $product->attr_dbboo;
                $pro_name_attr = $product->name;
                $product_qty_attr = $product->so_luong_sp[0]['count_product'];
                $product_pri_attr = (float) $product->price_tax * (int) $product_qty_attr;
                $product->id_combi_dbboo = $id_combi_dbboo;
                $iq = $id_q;
                $p = $product;
                $ip = $id_pro;
                $pn = $pro_name_attr;
                $ppt = $pri_pro_tax;
                $pq = $product_qty_attr;
                $pp = $product_pri_attr;
                $cb = $combination;
                $this->insertquotedetail($iq, $c, $l, $p, $ip, $pn, $cb, $ppt, $pq, $pp, $cu);
                $store[$key] = $product;
            }
        }
        // update Total tax, Total price
        $this->module->updateQuotePrice($id_quote_db);

        unset($this->context->cookie->{'first_name'});
        unset($this->context->cookie->{'last_name'});
        unset($this->context->cookie->{'phone_number'});
        unset($this->context->cookie->{'email_customer'});
        unset($this->context->cookie->{'quote_name'});
        unset($this->context->cookie->{'bargain_message'});
        unset($this->context->cookie->{'company_name'});
        unset($this->context->cookie->{'company_address'});
        $count_product_this_quote = $this->countproduct($id_q);
        $product_quote = $this->listproductdetail($id_q);
        $info_customer = $this->quoteinfo($id_q);
        $option_price_total = Configuration::get('Option_price_total');
        $this->context->smarty->assign("count", $count_product_this_quote);
        $this->context->smarty->assign("product_quote", $product_quote);
        $this->context->smarty->assign("info_customer", $info_customer);
        $this->context->smarty->assign('option_price_total', $option_price_total);
        $table_product = _PS_MODULE_DIR_ . '/askforaquotemodul/views/templates/front/viewtableproductfront.tpl';
        $tb_p = $this->context->smarty->fetch($table_product);
        $this->sendmail($id_q, $fn, $ln, $e, $qn, $tb_p, $cn, $ca2);
    }
    public function sendmail($id_quote_db, $first_name, $last_name, $email, $quote_name, $tb, $cn, $ca2 = null)
    {
        require_once(_PS_MODULE_DIR_.'askforaquotemodul/libs/phpmailer/vendor/autoload.php');
        $arr_language = Language::getLanguages(false);
        $language_site = $this->context->cookie->id_lang;
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) {
                $this->ps_cipherTool = new PhpEncryption(_NEW_COOKIE_KEY_);
        } else {
            $this->ps_cipherTool = new Blowfish(_COOKIE_KEY_, _COOKIE_IV_);
        }
        $date_now = date('Y-m-d H:i:s');
        $id_quote_dbboo_view = "id_quote=" . $id_quote_db;
        if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) {
            $this->_cipherTool = new PhpEncryption(_NEW_COOKIE_KEY_);
        } else {
            $this->_cipherTool = new Blowfish(_COOKIE_KEY_, _COOKIE_IV_);
        }
        $encode = $this->_cipherTool->encrypt($id_quote_dbboo_view);
        $link = new Link();
        if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) {
            $link_cus_rep_seo = $link->getModuleLink('askforaquotemodul', 'formrepcustomquote', array(
            'data' => $encode,
            ));
        } else {
            $link_cus_rep_seo = $link->getModuleLink('askforaquotemodul', 'formrepcustom', array(
            'data' => $encode,
            ));
        }
        $content_mess = Tools::getvalue('bargain_message');
        $source_check = md5($id_quote_db.$date_now);
        $link = new Link();
        $link_cus_rep_seo = $link->getModuleLink('askforaquotemodul', 'myquotedetail', array(
            'id_quote' => $id_quote_db,
            'soucrce' => $source_check,
           ));
        $sql_up_checkexist = 'SELECT * FROM '._DB_PREFIX_.'product_quote_dbboo WHERE';
        $sql_up_checkexist .= ' id_quote = "'.$id_quote_db.'"';
        $checkee = $db->ExecuteS($sql_up_checkexist, true, false);
        $checkexist = 0;
        foreach ($checkee as $key => $value) {
            foreach ($value as $key1 => $value1) {
                $value1;
                if ($key1 == 'source_check') {
                    $checkexist += 1;
                } else {
                    $checkexist += 0;
                }
            }
        }
        if ($checkexist == '0') {
            $sql_up_aaaheck = 'ALTER TABLE `'._DB_PREFIX_.'product_quote_dbboo`';
            $sql_up_aaaheck .= 'ADD `source_check` VARCHAR(255) NULL AFTER `coupon_code`';
            $db->query($sql_up_aaaheck);
        }
        $sql_up_sourcecheck = 'UPDATE '._DB_PREFIX_.'product_quote_dbboo SET source_check =';
        $sql_up_sourcecheck .= '"'.$source_check.'" where id_quote like "';
        $sql_up_sourcecheck .= (int) $id_quote_db . '"';
        $db->query($sql_up_sourcecheck);
        $date_now = $this->displayDate($date_now);
        $email_admin_setting = Configuration::get('email_adm_setting');
        $email_from = Configuration::get('email_from_setting');
        $name_from = Configuration::get('name_from_setting');

        $this->context->smarty->assign("link_cus_rep", $link_cus_rep_seo);
        $tpl_module = _PS_MODULE_DIR_ . '/askforaquotemodul/views/templates/front/linkmessquotefront.tpl';
        $link_cus_rep = $this->context->smarty->fetch($tpl_module);
        $opt_email_newquote_adm = Configuration::get('Option_email_newquote_adm');
        $opt_email_newquote_cus = Configuration::get('Option_email_newquote_cus');
        $option_button_smtp = Configuration::get('Option_button_smtp');
        $id_format = $this->module->getIDQuoteformatted($id_quote_db);
        foreach ($arr_language as $key => $value_lang) {
            $value_lang;
            $array_id_language = $arr_language[$key]['id_lang'];
            if ($array_id_language == $language_site) {
                $array_language_iso_code = $arr_language[$key]['iso_code'];
                $content_mess_language_encript_new = Configuration::get('label_new_' . $array_language_iso_code);
                $content_mess_language_new = $this->ps_cipherTool->decrypt($content_mess_language_encript_new);
                $content_mess_language_new = str_replace("[last_name]", $last_name, $content_mess_language_new);
                $content_mess_language_new = str_replace("[first_name]", $first_name, $content_mess_language_new);
                $content_mess_language_new = str_replace("[ID quote]", $id_format, $content_mess_language_new);
                $content_mess_language_new = str_replace("[quote_id]", $id_format, $content_mess_language_new);
                $content_mess_language_new = str_replace("[quote_ID]", $id_format, $content_mess_language_new);
                $content_mess_language_new = str_replace("[company_name]", $cn, $content_mess_language_new);
                $content_mess_language_new = str_replace("[company_address]", $ca2, $content_mess_language_new);
                $msg = nl2br($content_mess);
                $content_mess_language_new = str_replace("[message]", $msg, $content_mess_language_new);
                $content_mess_language_new = str_replace("[messages]", $msg, $content_mess_language_new);
                $content_mess_language_new = str_replace("[email]", $email, $content_mess_language_new);
                $content_mess_language_new = str_replace("[date]", $date_now, $content_mess_language_new);
                $content_mess_language_new = str_replace("[quote_link]", $link_cus_rep, $content_mess_language_new);
                $content_mess_language_new = str_replace("[quote_url]", $link_cus_rep_seo, $content_mess_language_new);
                $content_mess_language_new = str_replace("[quote_table_product]", $tb, $content_mess_language_new);

                $mail_adm = new PHPMailer\PHPMailer\PHPMailer;
                $mail_adm->CharSet = 'UTF-8';
                $mail_adm->Encoding = "base64";
                $mail_adm->SetFrom($email_from, $name_from);
                if ($option_button_smtp == '1') {
                    $mail_adm->isSMTP();
                    $mail_adm->Host = Configuration::get('sever_smpt');
                    $mail_adm->SMTPAuth = true;
                    $mail_adm->SMTPAutoTLS = false;
                    $mail_adm->Port = Configuration::get('port_smtp');
                    $mail_adm->Username = Configuration::get('user_smpt');
                    $mail_adm->Password = Configuration::get('pass_smtp');
                    $mail_adm->SMTPSecure = Configuration::get('secure_smtp');
                }
                if ($opt_email_newquote_adm == 1) {
                    if ($email_admin_setting != '') {
                        $mail_adm->AddAddress($email_admin_setting);
                        $mail_adm->Subject = $quote_name;
                        $mail_adm->MsgHTML($content_mess_language_new);
                        $mail_adm->Send();
                    }
                }
                $mail_cus = new PHPMailer\PHPMailer\PHPMailer;
                $mail_cus->CharSet = 'UTF-8';
                $mail_cus->Encoding = "base64";
                $mail_cus->SetFrom($email_from, $name_from);
                if ($option_button_smtp == '1') {
                    $mail_cus->isSMTP();
                    $mail_cus->Host = Configuration::get('sever_smpt');
                    $mail_cus->SMTPAuth = true;
                    $mail_cus->SMTPAutoTLS = false;
                    $mail_cus->Port = Configuration::get('port_smtp');
                    $mail_cus->Username = Configuration::get('user_smpt');
                    $mail_cus->Password = Configuration::get('pass_smtp');
                    $mail_cus->SMTPSecure = Configuration::get('secure_smtp');
                }
                if ($opt_email_newquote_cus == 1) {
                    $mail_cus->AddAddress($email);
                    $mail_cus->Subject = $quote_name;
                    $mail_cus->MsgHTML($content_mess_language_new);
                    $mail_cus->Send();
                }
            }
        }
    }
    public function displayDate($date, $id_lang = null)
    {
        if (!$date || !($time = strtotime($date))) {
            return $date;
        }
        if ($date == '0000-00-00 00:00:00' || $date == '0000-00-00') {
            return '';
        }
        if (!Validate::isDate($date)) {
            return $date;
        }
        if ($id_lang == null) {
            $id_lang = $this->context->language->id;
        }
        $context = Context::getContext();
        $lang = empty($id_lang) ? $context->language : new Language($id_lang);
        $date_format = $lang->date_format_full;
        return date($date_format, $time);
    }
}
