<?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.0.38
*/

class AskforaquotemodulConvertModuleFrontController extends ModuleFrontController
{
    /** @since 1.0.38 **/
    public function run()
    {
        parent::init();
        $this->convertCartToQuote();
    }
    public function convertCartToQuote()
    {
        $id_user = Context::getContext()->cookie->client;
        if (empty($id_user)) {
            return false;
        }
        $cart = $this->context->cart;
        $products = $cart->getProducts();
        if (empty($cart) || empty($products)) {
            $this->redirectAfterConvert(false);
        }
        // remove all products in OLD quote before add
        $db = Db::getInstance();
        $table = _DB_PREFIX_.'product_doubleboo';
        $sql = "DELETE FROM $table WHERE id_user = '".pSQL($id_user)."'";
        $db->query($sql);
        // add new product
        foreach ($products as $pro) {
            $id_product_attribute = (int) $pro['id_product_attribute'];
            $id_product = (int) $pro['id_product'];
            $cart_quantity = (int) $pro['cart_quantity'];
            $data = array(
                'id_user' => pSQL($id_user),
                'id_product' => $id_product,
                'id_combination' => $id_product_attribute,
                'count_product' => $cart_quantity,
            );
            $db->insert('product_doubleboo', $data);
        }
        $this->redirectAfterConvert(true);
    }
    public function redirectAfterConvert($bool)
    {
        $link = new Link();
        $controller_f = $link->getPageLink('order');
        $controller_t = $link->getModuleLink('askforaquotemodul', 'quotes');
        if ($bool == true) {
            Tools::redirect($controller_t);
        } else {
            Tools::redirect($controller_f);
        }
        exit();
    }
}
