<?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 AskforaquotemodulCouponcodeModuleFrontController extends ModuleFrontController
{
    public function run()
    {
        $quote_module_token = Tools::getValue('quote_token');
        if ($quote_module_token != md5(sha1(_COOKIE_KEY_.'askforaquotemodul'))) {
            die;
        } else {
            $coupon = array();
            $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
            $coupoun_code = Tools::getValue('couponcode');
            $total_price = Tools::getValue('total_price');
            $id_quote = Tools::getValue('id_quote');
            $currency = $this->module->findQuoteCurrencyById($id_quote);
            $time_now = time();
            $check_coupon_sql = 'SELECT * FROM ' . _DB_PREFIX_ . 'cart_rule WHERE code="'.pSQL($coupoun_code).'"';
            $check_coupon = $db->executeS($check_coupon_sql, true, false);
            $coupon['infor_product_select_coupon'] = '0';

            if (!empty($check_coupon)) {
                if ($check_coupon[0]['active'] == '1') {
                    $time_check = strtotime($check_coupon[0]['date_to']);
                    if ($time_check > $time_now) {
                        if ($total_price > $check_coupon[0]['minimum_amount']) {
                            $coupon['reduction_currency'] = (int) $check_coupon[0]['reduction_currency'];
                            $currency_coupon = new Currency($coupon['reduction_currency']);
                            $coupon['check'] = '1';
                            if ($check_coupon[0]['reduction_amount'] == '0') {
                                $coupon['type_coupon'] = '1';
                                $coupon['num_coupon'] = $check_coupon[0]['reduction_percent'];
                            } else {
                                $coupon['type_coupon'] = '0';
                                $r = $check_coupon[0]['reduction_amount'];
                                $num_coupon = Tools::convertPriceFull($r, $currency_coupon, $currency);
                                $coupon['num_coupon'] = $num_coupon;
                            }
                            if ($check_coupon[0]['free_shipping'] == '1') {
                                $coupon['free_shipping'] = '1';
                            } else {
                                $coupon['free_shipping'] = '0';
                            }
                            if ($check_coupon[0]['reduction_product'] == '0') {
                                $coupon['reduction_product'] = '0';
                                if ($coupon['type_coupon'] == '1') {
                                    $pri_reduction = (float)$coupon['num_coupon'] / '100';
                                    $price_down = (float)$total_price * $pri_reduction;
                                    $total_pri_new = (float)$total_price - $price_down;
                                } else {
                                    $pri_reduction = (float)$coupon['num_coupon'];
                                    $price_down = $pri_reduction;
                                    $total_pri_new = (float)$total_price - $price_down;
                                }
                            } elseif ($check_coupon[0]['reduction_product'] == '-1') {
                                $coupon['reduction_product'] = '-1';
                                $select_min = 'Select MIN(product_pri),product_qty from';
                                $select_min .= ' ' . _DB_PREFIX_ . 'product_quote_detail';
                                $select_min .= ' where id_quote="'.$id_quote.'"';
                                $min_price = $db->executeS($select_min, true, false);
                                if ($coupon['type_coupon'] == '1') {
                                    $pri_reduction = (float)$coupon['num_coupon'] / '100';
                                    $qty = (int)$min_price[0]['product_qty'];
                                    $min_priceeee = (float)$min_price[0]['MIN(product_pri)'];
                                    $price_down = $min_priceeee * $pri_reduction * $qty;
                                    $total_pri_new = (float)$total_price - $price_down;
                                } else {
                                    $pri_reduction = (float)$coupon['num_coupon'];
                                    $price_down = $pri_reduction;
                                    $total_pri_new = (float)$total_price - $price_down;
                                }
                            } else {
                                $coupon['reduction_product'] = $check_coupon[0]['reduction_product'];
                                $select = 'SELECT * from ' . _DB_PREFIX_ . 'product_quote_detail';
                                $select .= ' WHERE id_quote="'.$id_quote.'" and ';
                                $select .= 'id_product="'.$coupon['reduction_product'].'"';
                                $check_pro = $db->executeS($select, true, false);
                                if (!empty($check_pro)) {
                                    $coupon['infor_product_select_coupon'] = $check_pro;
                                    if ($coupon['type_coupon'] == '1') {
                                        $pri_reduction = (float)$coupon['num_coupon'] / '100';
                                        $pri_proooo = $check_pro[0]['product_pri'];
                                        $qtyyy = (int)$check_pro[0]['product_qty'];
                                        $price_down = $pri_proooo * $pri_reduction * $qtyyy;
                                        $total_pri_new = (float)$total_price - $price_down;
                                    } else {
                                        $pri_reduction = (float)$coupon['num_coupon'];
                                        $price_down = $pri_reduction;
                                        $total_pri_new = (float)$total_price - $price_down;
                                    }
                                } else {
                                    $total_pri_new = (float)$total_price;
                                    $coupon['check'] = "0";
                                }
                            }
                            $coupon['total_pri_new'] = $total_pri_new;
                            $check_coupon_insert = 'Select * from ' . _DB_PREFIX_ . 'coupon_quote';
                            $check_coupon_insert .= ' where id_quote="'.$id_quote.'"';
                            $check = $db->executeS($check_coupon_insert, true, false);
                            $coupon_infor = '';
                            if (empty($check)) {
                                $coupon_infor = 'Insert into ' . _DB_PREFIX_ . '';
                                $coupon_infor .= 'coupon_quote(id_quote,coupon_type,reduction_amount,';
                                $coupon_infor .= 'reduction_product,free_shipping)';
                                $coupon_infor .= ' values("' . (int) $id_quote . '",';
                                $coupon_infor .= '"'.$coupon['type_coupon'].'","'.$coupon['num_coupon'].'",';
                                $coupon_infor .= ' "'.$coupon['reduction_product'].'",';
                                $coupon_infor .= '"'.$coupon['free_shipping'].'")';
                            } else {
                                $coupon_infor = 'UPDATE ' . _DB_PREFIX_ . '';
                                $coupon_infor .= 'coupon_quote set id_quote="'.(int) $id_quote.'", ';
                                $coupon_infor .= 'coupon_type="'.$coupon['type_coupon'].'",';
                                $coupon_infor .= ' reduction_amount="'.$coupon['num_coupon'].'", ';
                                $coupon_infor .= 'reduction_product="'.$coupon['reduction_product'].'",';
                                $coupon_infor .= ' free_shipping="'.$coupon['free_shipping'].'" ';
                                $coupon_infor .= 'where id_quote="'.$id_quote.'"';
                            }
                            $db->query($coupon_infor);
                            $update_coupon_code = 'UPDATE ' . _DB_PREFIX_ . '';
                            $update_coupon_code .= 'product_quote_dbboo set coupon_code="'.$coupoun_code.'",';
                            $update_coupon_code .= 'total_pri_fixed="'.$total_price.'"';
                            $update_coupon_code .= ' where id_quote="'.$id_quote.'"';
                            $db->query($update_coupon_code);
                        } else {
                            $coupon['check'] = '0';
                            $coupon['total_pri_new'] = $total_price;
                        }
                    } else {
                        $coupon['check'] = '0';
                        $coupon['total_pri_new'] = $total_price;
                    }
                } else {
                    $coupon['check'] = '0';
                    $coupon['total_pri_new'] = $total_price;
                }
            } else {
                $coupon['check'] = '0';
                $coupon['total_pri_new'] = $total_price;
            }
            if (empty($coupoun_code)) {
                $coupoun_code = '';
                $update_coupon_code = 'UPDATE ' . _DB_PREFIX_ . '';
                $update_coupon_code .= 'product_quote_dbboo set coupon_code="'.$coupoun_code.'"';
                $update_coupon_code .= ' where id_quote="'.$id_quote.'"';
                $db->query($update_coupon_code);
                $delete_coupon_code = 'Delete ' . _DB_PREFIX_ . '';
                $delete_coupon_code .= 'coupon_quote where id_quote="'.$id_quote.'"';
                $db->query($update_coupon_code);
            }
            echo Tools::jsonEncode($coupon);
        }
    }
}
