<?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 AskforaquotemoduldelmessagedbbooModuleFrontController extends ModuleFrontController
{
    public function run()
    {
        $quote_module_token = Tools::getValue('quote_token');
        if ($quote_module_token != md5(sha1(_COOKIE_KEY_ . 'askforaquotemodul'))) {
            die;
        } else {
            parent::init();
            parent::initHeader();
            parent::initContent();
            $id_mess = Tools::getValue('id_mess');
            $id_quote = Tools::getValue('id_quote');
            $id_condi = Tools::getValue('conditation');
            $id_combination = Tools::getValue('id_combination');
            $id_product_view = Tools::getValue('id_product_view_dbboo');
            $id_currency = $this->context->currency->id;
            $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
            if ($id_condi == 0) {
                $del_mess = 'Delete from ' . _DB_PREFIX_ . 'message_dbboo where id like "' . (int) $id_mess . '"';
                $db->query($del_mess);
                $dp_mess = 'Select * from ' . _DB_PREFIX_ . 'message_dbboo where id_quote like "'.(int)$id_quote.'"';
                $dp_mess_dbboo = $db->ExecuteS($dp_mess, true, false);
                if (!empty($dp_mess_dbboo)) {
                    foreach ($dp_mess_dbboo as &$mess_dbboo) {
                        $mess_dbboo['content_mes'] = nl2br($mess_dbboo['content_mes']);
                    }
                }
                $count_mess = count($dp_mess_dbboo);

                $dp_mess_dbboo['number_mess'] = $count_mess;
                echo Tools::jsonEncode($dp_mess_dbboo);
            }
            if ($id_condi == 1) {
                $dp_info_cus = 'Select * from ' . _DB_PREFIX_ . 'product_quote_dbboo where id_quote like "';
                $dp_info_cus .= (int) $id_quote . '"';
                $info_customer = $db->ExecuteS($dp_info_cus, true, false);
                $this->context->smarty->assign("info_customer", $info_customer);
                $total_quote = (float) @$info_customer[0]['total_pri'];
                $pri_pd = 'Select total_pri from ' . _DB_PREFIX_ . 'product_quote_detail where id_quote like "';
                $pri_pd .= (int) $id_quote . '" and id_product like "';
                $pri_pd .= (int) $id_product_view . '" and id_combination like "' . (int) $id_combination . '"';
                $dp_pri_product = $db->Executes($pri_pd, true, false);
                $product_price = (float) @$dp_pri_product[0]['total_pri'];
                $total_quote = (float) $total_quote - (float) $product_price;
                $update_pri = 'UPDATE ' . _DB_PREFIX_ . 'product_quote_dbboo SET total_pri = "';
                $update_pri .= pSQL($total_quote) . '" where id_quote like "' . (int) $id_quote . '"';
                $db->query($update_pri);
                $del_pd_view = 'DELETE FROM ' . _DB_PREFIX_ . 'product_quote_detail WHERE id_quote like "';
                $del_pd_view .= (int) $id_quote . '" and id_product like "';
                $del_pd_view .= (int) $id_product_view . '" and id_combination like "' . (int) $id_combination . '"';
                $db->query($del_pd_view);
                $dp_product_view = 'Select * from ' . _DB_PREFIX_ . 'product_quote_detail WHERE id_quote like "';
                $dp_product_view .= (int) $id_quote . '"';
                $dp_pro_view = $db->ExecuteS($dp_product_view, true, false);
                $count_product = Count($dp_pro_view);
                $dp_pro_view['total_price'] = $total_quote;
                $dp_pro_view['total_price_formatted'] = Tools::displayPrice($total_quote, $id_currency);
                $dp_pro_view['number_product'] = $count_product;
                if ($dp_pro_view['number_product'] == 0) {
                    $update_pri_none_product = 'UPDATE ' . _DB_PREFIX_ . '';
                    $update_pri_none_product .= 'product_quote_dbboo SET total_pri = "0.00" where id_quote like "';
                    $update_pri_none_product .= (int) $id_quote . '"';
                    $db->query($update_pri_none_product);
                }
                echo Tools::jsonEncode($dp_pro_view);
            }
        }
    }
}
