<?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 AskforaquotemodultaxcostModuleFrontController 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');
            $id_shop = (int) Tools::getValue('jsshopid');
            $id_customer = (int) Tools::getValue('id_customer');
            $cookie = new Cookie('psAdmin');
            $id_lang = $cookie->id_lang;

            $this->module->setCurrencyQuote($id_quote);
            $currency = $this->module->findQuoteCurrencyById($id_quote);
            $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'];
            }

            $total_tax_cost_min = 0.00;
            $total_tax_cost_max = 0.00;
            if (count($products) > 0 && isset($products[0]['total_price_product'])) {
                foreach ($products as $product) {
                    $id_product = (int) $product['id_product'];
                    $price = (float) $product['total_price_product'];
                    $p_incl = $this->module->getProductPriceTax($price, $id_product, $id_customer, $id_lang, $id_shop);
                    $tax_cost = $p_incl - $price;
                    $total_tax_cost_max += $tax_cost;
                }
            }
            $total_tax_cost_plain = $total_tax_cost_max;
            $total_tax_cost_min = Tools::displayPrice($total_tax_cost_min, $currency);
            $total_tax_cost_max = Tools::displayPrice($total_tax_cost_max, $currency);
            $response = array();
            $response['total_tax_cost_min'] = $total_tax_cost_min;
            $response['total_tax_cost_max'] = $total_tax_cost_max;
            $response['total_tax_cost_max_plain'] = $total_tax_cost_plain;
            echo Tools::jsonEncode($response);
        }
    }
}
