<?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 AskforaquotemodulgetshippingcostModuleFrontController 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 {
            $shippingcost = array();
            $id_cart = Tools::getValue('id_cart');
            $delivery_option = Tools::getValue('delivery_option');
            $id_address_invoice = Tools::getValue('id_address_invoice');
            $id_address_delivery = Tools::getValue('id_address_delivery');
            $td_coupon_pri_coupon_real = Tools::getValue('td_coupon_pri_coupon_real');
            $td_num_coupon = Tools::getValue('td_num_coupon');
            $total_tax_pro = Tools::getValue('total_tax_pro');
            $td_id_coupon_order = Tools::getValue('td_id_coupon_order');
            $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
            $cart = new Cart($id_cart);
            $cart->id_address_delivery = $id_address_delivery;
            $cart->id_address_invoice = $id_address_invoice;

            $delivery_option_list = $cart->getDeliveryOptionList();
            $key = '' . $delivery_option . '';
            foreach ($delivery_option_list as $options) {
                if (isset($options[$key])) {
                    if (isset($this->context->cookie->id_country)) {
                        unset($this->context->cookie->id_country);
                    }
                    if (isset($this->context->cookie->id_state)) {
                        unset($this->context->cookie->id_state);
                    }
                }
            }
            $cart->addCartRule($td_id_coupon_order);
            $cart->update();
            $update_id_delivery = 'UPDATE ' . _DB_PREFIX_ . 'cart_product SET ';
            $update_id_delivery .= 'id_address_delivery = "'.$id_address_delivery.'" ';
            $update_id_delivery .= 'WHERE id_cart = "'.$id_cart.'"';
            $db->query($update_id_delivery);
            $delivery_option_list = $cart->getDeliveryOptionList();
            if (!empty($delivery_option_list)) {
                foreach ($delivery_option_list as $options) {
                    $shippingcost['total_price_shipping_with_tax'] = $options[$key]['total_price_with_tax'];
                    $a = $shippingcost['total_price_shipping_with_tax'];
                    $shippingcost['total_price_shipping_without_tax'] = $options[$key]['total_price_without_tax'];
                    $b = $shippingcost['total_price_shipping_without_tax'];
                    $shippingcost['total_tax_shipping'] = (float)($a) - (float)($b);
                    $shippingcost['total_tax_pro'] = $total_tax_pro;
                    $shippingcost['check'] = 0;
                    if ($b == '0') {
                        $shippingcost['check_free'] = '1';
                    } else {
                        $shippingcost['check_free'] = '0';
                    }
                    $td_cart_rule = $cart->getCartRules();
                    if ($options[$key]['is_free'] != 1) {
                        if (!empty($td_cart_rule)) {
                            $sddd = $td_cart_rule[0]['value_tax_exc'];
                            $shippingcost['pri_coupon_tax'] = $td_cart_rule[0]['value_real'];
                            $shippingcost['pri_coupon_notax'] = $td_cart_rule[0]['value_tax_exc'];
                            $shippingcost['coupon_tax'] = $td_cart_rule[0]['value_real'] - $sddd;
                        } else {
                            $shippingcost['pri_coupon_tax'] = 0;
                            $shippingcost['pri_coupon_notax'] = 0;
                            $shippingcost['coupon_tax'] = 0;
                        }
                    }
                    if (!empty($td_cart_rule)) {
                        if ($td_cart_rule[0]['free_shipping'] == 1) {
                            $gggg = $shippingcost['total_tax_shipping'];
                            $shippingcost['pri_coupon_tax'] = $td_num_coupon + $a;
                            $shippingcost['pri_coupon_notax'] = $td_coupon_pri_coupon_real + $b;
                            $shippingcost['coupon_tax'] = $td_num_coupon - $td_coupon_pri_coupon_real + $gggg;
                        }
                    }
                    if (empty($shippingcost['pri_coupon_tax'])) {
                        $shippingcost['pri_coupon_tax'] = 0;
                        $shippingcost['pri_coupon_notax'] = 0;
                        $shippingcost['coupon_tax'] = 0;
                    }
                }
            } else {
                $shippingcost['check'] = 1;
                $shippingcost['total_tax_shipping'] = 0;
                $shippingcost['total_tax_pro'] = 0;
            }
            echo Tools::jsonEncode($shippingcost);
        }
    }
}
