<?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 AskforaquotemodulshippingcostModuleFrontController 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::initContent();
            $id_quote = (int) Tools::getValue('id_quote');
            $this->module->setCurrencyQuote($id_quote);
            $currency = $this->module->findQuoteCurrencyById($id_quote);

            $id_carrier = (int) Tools::getValue('shipping_option');

            $id_customer = (int) Tools::getValue('id_customer');
            $cookie = new Cookie('psAdmin');
            $id_lang = $cookie->id_lang;

            $row_product = Tools::getValue('products');
            $products = $this->module->convertToArrayRowProduct($row_product);
            $quote_ob = $this->module->getCustomerQuote($id_quote);
            if (!empty($quote_ob)) {
                $id_customer = (int) $quote_ob['id_customer'];
                $id_lang = (int) $quote_ob['id_lang'];
            }
            
            $infor_shippingcost = array();
            $cart = $this->module->convertArrayProductsIntoCart($products, $id_customer, $currency->id, $id_lang, 0);
            $product_list = $cart->getProducts(true);

            $shipping_cost_min = 0;
            $shipping_cost_incl = $cart->getPackageShippingCost($id_carrier, true, null, $product_list);
            $shipping_cost_excl = $cart->getPackageShippingCost($id_carrier, false, null, $product_list);

            $shipping_cost_tax = $shipping_cost_incl - $shipping_cost_excl;
            if ($shipping_cost_tax < 0) {
                $shipping_cost_tax = 0;
            }
            $base_total_tax_inc = $cart->getOrderTotal(true, Cart::BOTH, $product_list, $id_carrier);
            $base_total_tax_exc = $cart->getOrderTotal(false, Cart::BOTH, $product_list, $id_carrier);

            $total_tax = $base_total_tax_inc - $base_total_tax_exc;

            if ($total_tax < 0) {
                $total_tax = 0;
            }

            $shipping_cost_max = $shipping_cost_excl;
            
            $infor_shippingcost['shipping_cost_min'] = Tools::displayPrice($shipping_cost_min, $currency);
            $infor_shippingcost['shipping_cost_min_plain'] = $shipping_cost_min;
            $infor_shippingcost['shipping_cost_max'] = Tools::displayPrice($shipping_cost_max, $currency);
            $infor_shippingcost['shipping_cost_max_plain'] = $shipping_cost_max;
            $infor_shippingcost['shipping_cost_tax'] = Tools::displayPrice($shipping_cost_tax, $currency);
            $infor_shippingcost['shipping_cost_tax_plain'] = $shipping_cost_tax;
            $infor_shippingcost['shipping_cost_excl'] = Tools::displayPrice($shipping_cost_excl, $currency);
            $infor_shippingcost['shipping_cost_excl_plain'] = $shipping_cost_excl;
            $infor_shippingcost['total_tax'] = $total_tax;
            if ($shipping_cost_max <= 0) {
                $infor_shippingcost['free_shipping'] = 1;
            }
            if ($shipping_cost_excl > 0) {
                $shipping_cost_tax_rate = $shipping_cost_tax / $shipping_cost_excl;
            } else {
                $shipping_cost_tax_rate = 0;
            }
            $infor_shippingcost['shipping_cost_tax_rate'] = $shipping_cost_tax_rate;
            $this->module->deleteACart($cart);
            echo Tools::jsonEncode($infor_shippingcost);
        }
    }
}
