<?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 AskforaquotemodulhandidproductModuleFrontController extends ModuleFrontController
{

    public function run()
    {
        parent::init();
        parent::initHeader();
        parent::initContent();
        $quote_module_token = Tools::getValue('quote_token');
        if ($quote_module_token != md5(sha1(_COOKIE_KEY_ . 'askforaquotemodul'))) {
            die;
        } else {
            $id = Tools::getValue('id_product');
            $ss_id = Tools::getValue('session_id');
            $condi = Tools::getValue('id_condition');
            $id_rm = Tools::getValue('id_product_remove');
            if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) {
                $id_combi = Tools::getValue('id_product_attribute');
            } else {
                $id_combi = Tools::getValue('id_combination');
            }
            $gr_size = Tools::getValue('gr_size');
            $gr_color = Tools::getValue('gr_color');
            $data_attribute =  Tools::getValue('data_attribute');
            $id_combi_rm = Tools::getValue('id_combination_rm');
            $qty_product_pre = Tools::getValue('qty_product_pre');
            if ($qty_product_pre == 'undefined') {
                $qty_product_pre = 1;
            }
            $product_total_price = 0;
            $useTax = $this->module->useTax();
            $prefixTax = $this->module->prefixTax();
            if ($condi == 1) {
                $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
                $check_id_rm = 'Select id_product from ' . _DB_PREFIX_ . 'product_doubleboo where id_product like ';
                $check_id_rm .= (int) $id_rm . ' and id_user like "' . pSQL($ss_id) . '" and id_combination like "';
                $check_id_rm .= (int) $id_combi_rm . '"';
                $check_rm = $db->ExecuteS($check_id_rm, true, false);
                if ($check_rm != null) {
                    $del_pro = 'DELETE from ' . _DB_PREFIX_ . 'product_doubleboo where id_product like ';
                    $del_pro .= (int) $id_rm . ' and id_user like "' . pSQL($ss_id) . '" and id_combination like "';
                    $del_pro .= (int) $id_combi_rm . '"';
                    $db->query($del_pro);
                }
                $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);
                $count_sp = Count($idp);
                $id_lang = $this->context->language->id;
                $store = array();
                foreach ($idp 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);
                    $find_id = 'Select id_combination from ' . _DB_PREFIX_ . 'product_doubleboo where id_user like "';
                    $find_id .= pSQL($ss_id) . '" ';
                    $id_combination = $db->ExecuteS($find_id, true, false);
                    $id_combi_dbboo = $id_combination[$key]['id_combination'];
                    $dp_so_sp = 'Select count_product from ' . _DB_PREFIX_ . 'product_doubleboo where id_product like ';
                    $dp_so_sp .= (int)$id_pro.' and id_combination like "'.(int)$id_combi_dbboo.'" and id_user like "';
                    $dp_so_sp .= pSQL($ss_id) . '"';
                    $so_luong_sp = $db->ExecuteS($dp_so_sp, true, false);
                    if ($attribute_df == '') {
                        $image = new Image();
                        $dp_image = $image->getImages($id_lang, $id_pro);
                        $link_r = $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;
                            }
                            $image_type_1 = '';
                            if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) {
                                $image_type_1 = ImageType::getFormattedName('cart');
                            } else {
                                $image_type_1 = ImageType::getFormatedName('cart');
                            }
                            $link_img = $linkimages->getImageLink($link_r, $id_img, $image_type_1);
                        }
                        $link_pro = $linkimages->getProductLink($id_pro);
                        $product->img_doubleboo = Tools::getShopProtocol() . $link_img;
                        $product->link_pro = $link_pro;
                        $pri_pro_tax = $product->getPrice($useTax, $id_combi_dbboo, 2);
                        $slsp = (int) $so_luong_sp[0]['count_product'];
                        $pri_discount = Product::getPriceStatic($id_pro, $useTax, null, 6, null, false, true, $slsp);
                        $product->price_tax = $pri_pro_tax;
                        $product->price_tax_formatted = Tools::displayPrice($pri_pro_tax).$prefixTax;
                        $product->price = $pri_discount;
                        $product->price_formatted = Tools::displayPrice($pri_discount).$prefixTax;
                        $product->total_price = $product->price * (int) $so_luong_sp[0]['count_product'];
                        $product->total_price_formatted = Tools::displayPrice($product->total_price).$prefixTax;
                        $product->so_luong_sp = $so_luong_sp;
                        $product->id_combi_dbboo = $id_combi_dbboo;
                        $store[$key] = $product;
                    } else {
                        $attr_group = new AttributeGroup();
                        $get_attr = $attr_group->getAttributesGroups($id_lang);
                        $link_rew = $product->link_rewrite;
                        $linkimages = new Link();
                        $image = new Image();
                        $dp_image = $image->getImages($id_lang, $id_pro);
                        $size_attr = '';
                        $attr_dt_dbboo = '';
                        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_attr = 'Select id_image from '._DB_PREFIX_.'product_attribute_image';
                                        $id_image_attr .= ' where id_product_attribute like "'.$id_pro_attribute.'"';
                                        $id_image_attribute = $db->ExecuteS($id_image_attr, true, false);
                                        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', '>=')) {
                                                $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;
                                            }
                                        }
                                        $size_attr = $attribute_pro[$key_idattr]['attribute_name'] . ',' . $size_attr;
                                        $attr_dt = $attribute_pro[$key_idattr]['attribute_name'];
                                        $attr_dt_dbboo = $gr_name_attr .':'. $attr_dt . ', ' . $attr_dt_dbboo;
                                    } else {
                                        $size_attr .= '';
                                        $attr_dt_dbboo .= '';
                                    }
                                    $result = Tools::substr($size_attr, 0, -1);
                                    $result_attr = Tools::substr($attr_dt_dbboo, 0, -2);
                                    $product->attr_dbboo = $result;
                                    $product->attribute_dbboo = $result_attr;
                                }
                            }
                        }
                        $product_attr = $product->getAttributeCombinationsById($id_combi_dbboo, $id_lang);
                        if (!empty($product_attr[0]['reference'])) {
                            $product->reference = Tools::safeOutput($product_attr[0]['reference']);
                        }
                        $image = new Image();
                        $dp_image = $image->getImages($id_lang, $id_pro);
                        $id_img = Product::getCover($id_pro);
                        $id_img = $id_img['id_image'];
                        $link_pro = $linkimages->getProductLink($id_pro);
                        $product->link_pro = $link_pro;
                        $pri_pro_tax = $product->getPrice($useTax, $id_combi_dbboo, 2);
                        $slsp = (int) $so_luong_sp[0]['count_product'];
                        $p = Product::getPriceStatic($id_pro, $useTax, $id_combi_dbboo, 6, null, false, true, $slsp);
                        $pri_discount = $p;
                        $product->price_tax = $pri_pro_tax;
                        $product->price_tax_formatted = Tools::displayPrice($pri_pro_tax).$prefixTax;
                        $product->price = $pri_discount;
                        $product->price_formatted = Tools::displayPrice($pri_discount).$prefixTax;
                        $product->total_price = $product->price * (int) $so_luong_sp[0]['count_product'];
                        $product->total_price_formatted = Tools::displayPrice($product->total_price).$prefixTax;
                        $product->so_luong_sp = $so_luong_sp;
                        $product->id_combi_dbboo = $id_combi_dbboo;
                        $store[$key] = $product;
                    }
                    $total_pro_qty = ((double) $pri_discount * (int) $product->so_luong_sp[0]['count_product']);
                    $product_total_price += $total_pro_qty;
                }
                $option_price_total = Configuration::get('Option_price_total');
                $store['number'] = $count_sp;
                $store['total_price'] = $product_total_price;
                $store['total_price_formatted'] = Tools::displayPrice($product_total_price).$prefixTax;
                $store['check_opt_price'] = $option_price_total;
                echo Tools::jsonEncode($store);
            } else {
                $group_combi = array();
                $id_lang = $this->context->language->id;
                $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
                $check_attri_product = new Product($id, false, $id_lang);
                $check_attribute_df = $check_attri_product->getDefaultAttribute($id);
                if ($id_combi == 0 && $check_attribute_df != '') {
                    if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) { // for PrestaShop 1.7+
                        if ($gr_size == "undefined" || $gr_color == "undefined") {
                            $id_combi = $check_attribute_df;
                        } else {
                            $group_combi[$id] = $gr_size;
                            $group_combi[$data_attribute] = $gr_color;
                            $id_combi = $check_attri_product->getIdProductAttributesByIdAttributes($id, $group_combi);
                        }
                    } else {
                        $id_combi = $check_attribute_df;
                    }
                }
                $check_id = 'Select id_product from ' . _DB_PREFIX_ . 'product_doubleboo where id_product like ';
                $check_id .= (int) $id . ' and id_user like "' . pSQL($ss_id) . '" and id_combination like "';
                $check_id .= (int) $id_combi . '"';
                $check = $db->ExecuteS($check_id, true, false);
                if ($check == null) {
                    $intopd = 'Insert  into ' . _DB_PREFIX_ . '';
                    $intopd .= 'product_doubleboo(id_user,id_product,id_combination,count_product)';
                    $intopd .= 'values("' . pSQL($ss_id) . '",' . (int) $id . ',' . (int) $id_combi . ',';
                    $intopd .= (int) $qty_product_pre . ')';
                    $db->query($intopd);
                } else {
                    $dp_sosp = 'Select count_product from ' . _DB_PREFIX_ . 'product_doubleboo where id_product like ';
                    $dp_sosp .= (int) $id . ' and id_user like "' . pSQL($ss_id) . '" and id_combination like ';
                    $dp_sosp .= (int) $id_combi . '';
                    $db_so = $db->ExecuteS($dp_sosp, true, false);
                    $so_sp = $db_so['0']['count_product'] + (int) $qty_product_pre;
                    $uptopd = 'Update ' . _DB_PREFIX_ . 'product_doubleboo SET count_product = ';
                    $uptopd .= (int) $so_sp . ' where id_product like ' . (int) $id . ' and id_user like "';
                    $uptopd .= pSQL($ss_id) . '" and id_combination like ' . (int) $id_combi . '';
                    $db->query($uptopd);
                }
                $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);
                $count_sp = Count($idp);
                $store = array();
                foreach ($idp 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);
                    $find_id = 'Select id_combination from ' . _DB_PREFIX_ . 'product_doubleboo where id_user like "';
                    $find_id .= pSQL($ss_id) . '" ';
                    $id_combination = $db->ExecuteS($find_id, true, false);
                    $id_combi_dbboo = $id_combination[$key]['id_combination'];
                    $dp_so_sp = 'Select count_product from ' . _DB_PREFIX_ . 'product_doubleboo where id_product like ';
                    $dp_so_sp .= (int)$id_pro.' and id_combination like "'.(int)$id_combi_dbboo.'" and id_user like "';
                    $dp_so_sp .= pSQL($ss_id) . '" ';
                    $so_luong_sp = $db->ExecuteS($dp_so_sp, true, false);
                    if ($attribute_df == '') {
                        $image = new Image();
                        $dp_image = $image->getImages($id_lang, $id_pro);
                        $link_r = $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;
                            }
                            $image_type_1 = '';
                            if (Tools::version_compare(_PS_VERSION_, '1.7', '>=')) {
                                $image_type_1 = ImageType::getFormattedName('cart');
                            } else {
                                $image_type_1 = ImageType::getFormatedName('cart');
                            }
                            $link_img = $linkimages->getImageLink($link_r, $id_img, $image_type_1);
                        }
                        $pri_pro_tax = $product->getPrice($useTax, $id_combi_dbboo, 2);
                        $slsp = (int) $so_luong_sp[0]['count_product'];
                        $pri_discount = Product::getPriceStatic($id_pro, $useTax, null, 6, null, false, true, $slsp);
                        $link_pro = $linkimages->getProductLink($id_pro);
                        $product->img_doubleboo = Tools::getShopProtocol() . $link_img;
                        $product->link_pro = $link_pro;
                        $product->price = $pri_discount;
                        $product->price_formatted = Tools::displayPrice($pri_discount).$prefixTax;
                        $product->total_price = $product->price * (int) $so_luong_sp[0]['count_product'];
                        $product->total_price_formatted = Tools::displayPrice($product->total_price).$prefixTax;
                        $product->pri_discount = $pri_discount;
                        $product->pri_discount_formatted = Tools::displayPrice($pri_discount).$prefixTax;
                        $product->price_tax = $pri_pro_tax;
                        $product->price_tax_formatted = Tools::displayPrice($pri_pro_tax).$prefixTax;
                        $product->so_luong_sp = $so_luong_sp;
                        $product->id_combi_dbboo = $id_combi_dbboo;
                        $store[$key] = $product;
                    } else {
                        $attr_group = new AttributeGroup();
                        $get_attr = $attr_group->getAttributesGroups($id_lang);
                        $link_r = $product->link_rewrite;
                        $linkimages = new Link();
                        $size_attr = '';
                        $image = new Image();
                        $dp_image = $image->getImages($id_lang, $id_pro);
                        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_attr = 'Select id_image from '._DB_PREFIX_.'product_attribute_image';
                                        $id_image_attr .= ' where id_product_attribute like "'.$id_pro_attribute.'"';
                                        $id_image_attribute = $db->ExecuteS($id_image_attr, true, false);
                                        if (empty($id_image_attribute)) {
                                            foreach ($dp_image as $k_img => $v_img) {
                                                $k_img;
                                                $v_img;
                                                $id_img = Product::getCover($id_pro);
                                                $id_img = $id_img['id_image'];
                                            }
                                            if (Tools::version_compare(_PS_VERSION_, '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_r, $id_img, $type_img);
                                            $product->img_doubleboo = Tools::getShopProtocol() . $link_img;
                                        } else {
                                            foreach ($id_image_attribute as $key_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_r, $img_attr, $type_img);
                                                $product->img_doubleboo = Tools::getShopProtocol() . $link_img;
                                            }
                                        }
                                        $size_attr = $attribute_pro[$key_idattr]['attribute_name'] . ',' . $size_attr;
                                    } else {
                                        $size_attr .= '';
                                    }
                                }
                                $result = Tools::substr($size_attr, 0, -1);
                                
                                $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']);
                        }
                        $slsp = (int) $so_luong_sp[0]['count_product'];
                        $s =  $slsp;
                        $pri_d = Product::getPriceStatic($id_pro, $useTax, $id_combi_dbboo, 6, null, false, true, $s);
                        $pri_discount = $pri_d;
                        $pri_pro_tax = $product->getPrice($useTax, $id_combi_dbboo, 2);
                        $link_pro = $linkimages->getProductLink($id_pro);
                        $product->price_tax = $pri_pro_tax;
                        $product->price_tax_formatted = Tools::displayPrice($pri_pro_tax).$prefixTax;
                        $product->link_pro = $link_pro;
                        $product->price = $pri_discount;
                        $product->price_formatted = Tools::displayPrice($pri_discount).$prefixTax;
                        $product->total_price = $product->price * (int) $so_luong_sp[0]['count_product'];
                        $product->total_price_formatted = Tools::displayPrice($product->total_price).$prefixTax;
                        $product->pri_discount = $pri_discount;
                        $product->pri_discount_formatted = Tools::displayPrice($pri_discount).$prefixTax;
                        $product->so_luong_sp = $so_luong_sp;
                        $product->id_combi_dbboo = $id_combi_dbboo;
                        $store[$key] = $product;
                    }
                    $total_pro_qty_atr = ((double) $pri_discount * (int) $product->so_luong_sp[0]['count_product']);
                    $product_total_price += $total_pro_qty_atr;
                }
                $option_price_total = Configuration::get('Option_price_total');
                $store['number'] = $count_sp;
                $store['check_id_pro'] = $id;
                $store['check_id_combi'] = $id_combi;
                $store['total_price'] = $product_total_price;
                $store['total_price_formatted'] = Tools::displayPrice($product_total_price).$prefixTax;
                $store['check_opt_price'] = $option_price_total;
                
                echo Tools::jsonEncode($store);
            }
        }
    }
}
