<?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 AskforaquotemodulattrbuteModuleFrontController extends ModuleFrontController
{
    public function run()
    {
        parent::init();
        parent::initHeader();
        parent::initContent();
        $info_product = array();
        $attr_name_couple = array();
        $id_attr_product = array();
        $price_product = 0;
        $key_search = Tools::getValue('idproduct');
        $id_lang = $this->context->language->id;
        $id_shop = Tools::getValue('jsshopid');
        $this->context->shop->id = $id_shop;
        // set Currency to Currency of a Quote
        $id_currency = (int) Tools::getValue('id_currency');
        if (!empty($id_currency)) {
            $context = Context::getContext();
            $currency = Currency::getCurrencyInstance($id_currency);
            $context->currency = $currency;
        }
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $sql_search = 'SELECT id_product,name FROM ' . _DB_PREFIX_ . 'product_lang WHERE id_product =  "';
        $sql_search .= pSQL($key_search).'" and id_lang like "'.(int)$id_lang.'" and id_shop like "'.(int)$id_shop.'"';
        $include_product = $db->executeS($sql_search, true, false);
        foreach ($include_product as $value1) {
            $id_pro = array();
            $id_pro[] = $value1['id_product'];
            foreach ($id_pro as $valueidpro) {
                $sql1 =  'SELECT pa.id_product_attribute, pa.id_product FROM ' . _DB_PREFIX_ . 'product_attribute pa ';
                $sql1 .= ' INNER JOIN ' . _DB_PREFIX_ . 'product_attribute_shop pas';
                $sql1 .= ' ON pa.id_product_attribute = pas.id_product_attribute';
                $sql1 .= ' WHERE  pa.id_product = ' . $valueidpro;
                $sql1 .= ' AND pas.id_shop = '. (int) $id_shop;
                $id_pro_attr = $db->ExecuteS($sql1);
                if (!empty($id_pro_attr)) {
                    $product = new Product($valueidpro, false, $id_lang);
                    foreach ($id_pro_attr as $keyattr => $valueattr) {
                        $price_product = array();
                        $attr_name = array();
                        $attribute = array();
                        $id_attr_product[] = $id_pro_attr[$keyattr]['id_product_attribute'];
                        $attribute = $product->getAttributeCombinations($id_lang);
                        foreach ($attribute as $value2) {
                            $a1 = $value2['id_product_attribute'];
                            $a2 = $valueattr['id_product_attribute'];
                            $a3 = $value2['id_product'];
                            $a4 = $valueattr['id_product'];
                            if ($a1 == $a2 &&  $a3 == $a4) {
                                $attr_name[] = $value2['attribute_name'];
                            }
                        }
                        $ipa = $valueattr['id_product_attribute'];
                        $price_product[] = Product::getPriceStatic($valueidpro, false, $ipa, 2);
                        $price_product_new = Tools::displayPrice(implode('', $price_product), $currency);
                        $attr_name['price'] = $price_product_new;
                        $combi_product = array();
                        $attr_name_couple[] = $attr_name;
                    }
                } else {
                    $price_product = Product::getPriceStatic($valueidpro, false, null, 2);
                    $price_product = Tools::displayPrice($price_product, $currency);
                }
            }
        }
        if (!empty($id_pro_attr)) {
            foreach ($attr_name_couple as $valuecouple) {
                $combi_product[] = implode('-', $valuecouple);
            }
        } else {
            $combi_product = "0";
        }
        $info_product['id_product'] = (int)$key_search;
        $info_product['combi_product'] = $combi_product;
        $info_product['price_product'] = $price_product;
        $info_product['id_attr_product'] = $id_attr_product;
        echo Tools::jsonEncode($info_product);
    }
}
