<?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 AskforaquotemodulviewlistModuleFrontController 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_del = Tools::getvalue('id_product');
            $ss_id = Tools::getValue('session_id');
            $id_combi = Tools::getValue('id_combination');
            $product_qty = Tools::getValue('so_luong');
            $id_special = Tools::getValue('id_combination_count');
            $ss_id_count = Tools::getValue('sessionid_count');
            $button = Tools::getValue('name_btn');
            $pri_dbboo = Tools::getValue('price_dbboo');
            $unit_pri_dbboo = 0;
            $total_pri_dbboo = (float) Tools::getValue('total_pri_dbboo');
            $total_del = (float) Tools::getValue('total_pri_del');
            $total_new = 0;
            $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
            $useTax = $this->module->useTax();
            $prefixTax = $this->module->prefixTax();
            if ($button == 'del') {
                $check_id_rm = 'SELECT id_product FROM ' . _DB_PREFIX_ . 'product_doubleboo where id_product like "';
                $check_id_rm .= (int) $id_del . '" and id_user like "' . pSQL($ss_id) . '" and id_combination like "';
                $check_id_rm .= (int) $id_combi . '"';
                $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_del . '" and id_user like "' . pSQL($ss_id) . '" and id_combination like "';
                    $del_pro .= (int) $id_combi . '"';
                    $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_sp = $db->ExecuteS($dp_so_sp, true, false);
                    if ($attribute_df == '') {
                        $pri_pro = $product->price;
                        $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', '>=')) { // for PrestaShop 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;
                        $product->price = $pri_pro;
                        $product->price_formatted = Tools::displayPrice($pri_pro).$prefixTax;
                        $total_price = $pri_pro * $so_sp[0]['count_product'];
                        $product->total_price = $total_price;
                        $product->total_price_formatted =  Tools::displayPrice($total_price).$prefixTax;
                        $s = $so_sp[0]['count_product'];
                        $pri_pro_tax = $product->getPrice($useTax, $id_combi_dbboo, 4, null, false, true, $s);
                        if ($id_pro == $id_del && $id_combi_dbboo == $id_special) {
                            $unit_pri_dbboo = $pri_pro_tax;
                            $pri_dbboo = $unit_pri_dbboo  * $so_sp[0]['count_product'];
                        }
                        $product->price_tax_formatted = Tools::displayPrice($pri_pro_tax).$prefixTax;
                        $product->price_tax = $pri_pro_tax;
                        $total_price_tax = $pri_pro_tax * $so_sp[0]['count_product'];
                        $product->total_price_tax = $total_price_tax;
                        $product->total_price_tax_formatted = Tools::displayPrice($total_price_tax).$prefixTax;
                        $product->id_combi_dbboo = $id_combi_dbboo;
                        $product->so_sp = $so_sp[0]['count_product'];
                        $total_new += $total_price_tax;
                        $store[$key] = $product;
                    } else {
                        $attr_group = new AttributeGroup();
                        $get_attr = $attr_group->getAttributesGroups($id_lang);
                        $size_attr = '';
                        $attr_dt_dbboo = '';
                        $link_rew = $product->link_rewrite;
                        $linkimages = new Link();
                        $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)) {
                                            $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']);
                        }
                        $pri_pro = $product->price;
                        $link_pro = $linkimages->getProductLink($id_pro);
                        $product->link_pro = $link_pro;
                        $s = $so_sp[0]['count_product'];
                        $pri_pro_tax = $product->getPrice($useTax, $id_combi_dbboo, 4, null, false, true, $s);
                        if ($id_pro == $id_del && $id_combi_dbboo == $id_special) {
                            $unit_pri_dbboo = $pri_pro_tax;
                            $pri_dbboo = $unit_pri_dbboo  * $so_sp[0]['count_product'];
                        }
                        $product->price_tax = $pri_pro_tax;
                        $product->price_tax_formatted = Tools::displayPrice($pri_pro_tax).$prefixTax;
                        $total_price_tax = $pri_pro_tax * $so_sp[0]['count_product'];
                        $product->total_price_tax = $total_price_tax;
                        $product->total_price_tax_formatted = Tools::displayPrice($total_price_tax).$prefixTax;
                        $product->price = $pri_pro;
                        $product->price_formatted = Tools::displayPrice($pri_pro).$prefixTax;
                        $total_price = $pri_pro * $so_sp[0]['count_product'];
                        $product->total_price = $total_price;
                        $product->total_price_formatted =  Tools::displayPrice($total_price).$prefixTax;
                        $product->so_sp = $so_sp[0]['count_product'];
                        $product->id_combi_dbboo = $id_combi_dbboo;
                        $total_new += $total_price_tax;
                        $store[$key] = $product;
                    }
                }
                $option_price_total = Configuration::get('Option_price_total');
                $store['number'] = $count_sp;
                $total_del = $total_new;
                $store['_total_price'] = $total_del;
                $store['_total_price_formatted'] = Tools::displayPrice($total_del).$prefixTax;
                $store['check_opt_price'] = $option_price_total;
                echo Tools::jsonEncode($store);
            } else {
                $upd_count = 'Update ' . _DB_PREFIX_ . 'product_doubleboo SET count_product = "';
                $upd_count .= (int) $product_qty . '" where id_user like "';
                $upd_count .= pSQL($ss_id_count) . '" and id_combination like "' . (int) $id_special . '"';
                $upd_count .= ' AND id_product='.(int) $id_del;
                $db->query($upd_count);

                $product_pri_viewlist = array();
                $dp_id = 'Select id_product from ' . _DB_PREFIX_ . 'product_doubleboo where id_user like "';
                $dp_id .= pSQL($ss_id_count) . '"';
                $idp = $db->ExecuteS($dp_id, true, false);
                $count_sp = Count($idp);
                $id_lang = $this->context->language->id;
                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_count) . '" ';
                    $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_count) . '" ';
                    $so_luong_sp = $db->ExecuteS($dp_so_sp, true, false);

                    if ($attribute_df == '') {
                        $pri_pro = $product->price;
                        $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;
                        $product->price = $pri_pro;
                        $product->price_formatted = Tools::displayPrice($pri_pro).$prefixTax;
                        $product->total_price = $pri_pro * (int) $so_luong_sp[0]['count_product'];
                        $product->total_price_formatted = Tools::displayPrice($product->total_price).$prefixTax;
                        $s = $so_luong_sp[0]['count_product'];
                        $pri_pro_tax = $product->getPrice($useTax, $id_combi_dbboo, 4, null, false, true, $s);
                        if ($id_pro == $id_del && $id_combi_dbboo == $id_special) {
                            $unit_pri_dbboo = $pri_pro_tax;
                            $pri_dbboo = $unit_pri_dbboo  * $so_luong_sp[0]['count_product'];
                        }
                        $product->price_tax = $pri_pro_tax;
                        $product->price_tax_formatted = Tools::displayPrice($pri_pro_tax).$prefixTax;
                        $product->total_price_tax = $pri_pro_tax * (int) $so_luong_sp[0]['count_product'];
                        $product->total_price_tax_formatted = Tools::displayPrice($product->total_price_tax).$prefixTax;
                        $product->so_luong_sp = $so_luong_sp[0]['count_product'];
                        $product->id_combi_dbboo = $id_combi_dbboo;
                        $total_new += $product->total_price_tax;
                        $store[$key] = $product;
                    } else {
                        $attr_group = new AttributeGroup();
                        $get_attr = $attr_group->getAttributesGroups($id_lang);
                        $size_attr = '';
                        $link_r = $product->link_rewrite;
                        $linkimages = new Link();
                        $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)) {
                                            $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;
                            }
                        }
                        $pri_pro = $product->price;
                        $link_pro = $linkimages->getProductLink($id_pro);
                        $product->link_pro = $link_pro;
                        $product->price = $pri_pro;
                        $product->price_formatted = Tools::displayPrice($pri_pro).$prefixTax;
                        $product->total_price = $pri_pro * (int) $so_luong_sp[0]['count_product'];
                        $product->total_price_formatted = Tools::displayPrice($product->total_price).$prefixTax;
                        $s = $so_luong_sp[0]['count_product'];
                        $pri_pro_tax = $product->getPrice($useTax, $id_combi_dbboo, 4, null, false, true, $s);
                        if ($id_pro == $id_del && $id_combi_dbboo == $id_special) {
                            $unit_pri_dbboo = $pri_pro_tax;
                            $pri_dbboo = $unit_pri_dbboo  * $so_luong_sp[0]['count_product'];
                        }
                        $product->price_tax = $pri_pro_tax;
                        $product->price_tax_formatted = Tools::displayPrice($pri_pro_tax).$prefixTax;
                        $product->total_price_tax = $pri_pro_tax * (int) $so_luong_sp[0]['count_product'];
                        $product->total_price_tax_formatted = Tools::displayPrice($product->total_price_tax).$prefixTax;
                        $product->so_luong_sp = $so_luong_sp[0]['count_product'];
                        $product->id_combi_dbboo = $id_combi_dbboo;
                        $total_new += $product->total_price_tax;
                    }
                    $product_pri_viewlist[$key] = $product;
                }
                $option_price_total = Configuration::get('Option_price_total');
                $pri_pro_tax = $pri_dbboo;
                $total_pri_dbboo = $total_new;
                $total_pri_pro = $total_pri_dbboo;
                $product_pri_viewlist['number'] = $count_sp;
                $product_pri_viewlist['unit_product_pri'] = $unit_pri_dbboo;
                $product_pri_viewlist['product_pri'] = $pri_pro_tax;
                $product_pri_viewlist['unit_product_pri_formatted'] = Tools::displayPrice($unit_pri_dbboo).$prefixTax;
                $product_pri_viewlist['product_pri_formatted'] = Tools::displayPrice($pri_pro_tax).$prefixTax;
                $product_pri_viewlist['product_total_price'] = $total_pri_pro;
                $product_pri_viewlist['product_total_price_formatted'] = Tools::displayPrice($total_pri_pro).$prefixTax;
                $product_pri_viewlist['check_opt_price'] = $option_price_total;
                echo Tools::jsonEncode($product_pri_viewlist);
            }
        }
    }
}
