<?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 AskforaquotemodulAddeditaddressModuleFrontController extends ModuleFrontController
{
    public $td_popup__address_identi_num = '';
    public $td_popup__address_alias = '';
    public $td_popup__address_first_name = '';
    public $td_popup__address_last_name = '';
    public $td_popup__address_company = '';
    public $td_popup__address_vat_num = '';
    public $td_popup__address_adres1 = '';
    public $td_popup__address_adres2 = '';
    public $td_popup__address_city = '';
    public $td_popup__address_zip_code = '';
    public $td_popup__address_country = '';
    public $td_popup__address_state = '';
    public $td_popup__address_home_phone = '';
    public $td_popup__address_mobile_phone = '';
    public $td_popup__address_other = '';
    public $date_add = '';
    public $date_upd = '';
    public $active = 1;
    public $deleted = 0;
    public $td_id_customer = '';
    public $address_ids = '';
    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;
        }
        $this->addVariable();
        $check_add_or_edit = Tools::getValue('check_add_or_edit');
        
        $result = array();
        $errorzipcode_ar = array();
        $id_shop = Tools::getValue('jsshopid');
        $this->context->shop->id = $id_shop;
        $array_tmp = array();
        $country = new Country(Tools::getValue('td_popup__address_country'));
        $postcode = Tools::getValue('td_popup__address_zip_code');
        $errorzipcode = 'correct';
        if ($country->zip_code_format && !$country->checkZipCode($postcode)) {
            $alert = $this->module->l('Your Zip/postal code is incorrect. It must be entered as follows:');
            $replacezipcode = str_replace('N', '0', str_replace('L', 'A', $country->zip_code_format));
            $errorzipcode = $alert.' '.str_replace('C', $country->iso_code, $replacezipcode);
        } elseif ($postcode && !Validate::isPostCode($postcode)) {
            $alert = $this->module->l('The Zip/postal code is invalid.');
            $errorzipcode = $alert;
        }
        if ($errorzipcode != 'correct') {
            $errorzipcode_ar['errorzipcode'] = 'incorrect';
            $this->context->smarty->assign('errorzipcode', $errorzipcode);
            $ba_dir = 'askforaquotemodul/views/templates/admin/errorzipcode.tpl';
            $html = $this->context->smarty->fetch(_PS_MODULE_DIR_ . $ba_dir);
            $errorzipcode_ar['displayerror'] = $html;
            echo Tools::jsonEncode($errorzipcode_ar);
            die;
        }
        $result['errorzipcode'] = $errorzipcode;
        $result['oldprice'] = $array_tmp;
        if ($check_add_or_edit == 'addnew') {
            $result['template'] = $this->addAddress();
        } else {
            $result['template'] = $this->editAddress($check_add_or_edit);
        }
        
        echo Tools::jsonEncode($result);
    }
    
    public function editAddress($check_add_or_edit)
    {
        $array_address = array();
        $id_address = Tools::getValue('id_'.$check_add_or_edit);
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $query = 'UPDATE ' . _DB_PREFIX_ . 'address SET id_country=\''.(int) $this->td_popup__address_country.'\',';
        $query .= 'id_state=\''.(int) $this->td_popup__address_state.'\',';
        $query .= 'id_customer=\''.(int) $this->td_id_customer.'\',';
        $query .= 'alias=\''.pSQL($this->td_popup__address_alias).'\',';
        $query .= 'company=\''.pSQL($this->td_popup__address_company).'\',';
        $query .= 'lastname=\''.pSQL($this->td_popup__address_last_name).'\',';
        $query .= 'firstname=\''.pSQL($this->td_popup__address_first_name).'\',';
        $query .= 'address1=\''.pSQL($this->td_popup__address_adres1).'\',';
        $query .= 'address2=\''.pSQL($this->td_popup__address_adres2).'\',';
        $query .= 'postcode=\''.pSQL($this->td_popup__address_zip_code).'\',';
        $query .= 'city=\''.pSQL($this->td_popup__address_city).'\',';
        $query .= 'other=\''.pSQL($this->td_popup__address_other).'\',';
        $query .= 'phone=\''.pSQL($this->td_popup__address_home_phone).'\',';
        $query .= 'phone_mobile=\''.pSQL($this->td_popup__address_mobile_phone).'\',';
        $query .= 'vat_number=\''.pSQL($this->td_popup__address_vat_num).'\',';
        $query .= 'dni=\''.pSQL($this->td_popup__address_identi_num).'\',';
        $query .= 'date_upd=\''.pSQL($this->date_upd).'\'';
        $query .= ' WHERE id_address=\''.(int) $id_address.'\'';

        $db->query($query);
        
        $id_delivery = Tools::getValue('id_delivery');
        $sql_delivery = 'SELECT * FROM ' . _DB_PREFIX_ . 'address WHERE id_address=\''.(int) $id_delivery.'\'';
        $data_delivery = $db->ExecuteS($sql_delivery);
        $array_address['delivery']['td_popup__address_alias'] = @$data_delivery[0]['alias'];
        $array_address['delivery']['td_popup__address_first_name'] = @$data_delivery[0]['firstname'];
        $array_address['delivery']['td_popup__address_last_name'] = @$data_delivery[0]['lastname'];
        $array_address['delivery']['td_popup__address_company'] = @$data_delivery[0]['company'];
        $array_address['delivery']['td_popup__address_adres1'] = @$data_delivery[0]['address1'];
        $array_address['delivery']['td_popup__address_adres2'] = @$data_delivery[0]['address2'];
        $array_address['delivery']['td_popup__address_city'] = @$data_delivery[0]['city'];
        $array_address['delivery']['td_popup__address_state'] = '';
        if (!empty(@$data_delivery[0]['id_state'])) {
            $namestatede = State::getNameById((int)@$data_delivery[0]['id_state']);
            $array_address['delivery']['td_popup__address_state'] = $namestatede;
        }
        $array_address['delivery']['td_popup__address_zip_code'] = @$data_delivery[0]['postcode'];
        $namecountryde = Country::getNameById($this->context->language->id, (int)@$data_delivery[0]['id_country']);
        $array_address['delivery']['td_popup__address_country'] = $namecountryde;
        $array_address['delivery']['td_popup__address_home_phone'] = @$data_delivery[0]['phone'];
        $array_address['delivery']['td_popup__address_mobile_phone'] = @$data_delivery[0]['phone_mobile'];
        $array_address['delivery']['id_address'] = $id_delivery;
        
        $id_invoice = Tools::getValue('id_invoice');
        $sql_invoice = 'SELECT * FROM ' . _DB_PREFIX_ . 'address WHERE id_address=\''.(int) $id_invoice.'\'';
        $data_invoice = $db->ExecuteS($sql_invoice);
        $array_address['invoice']['td_popup__address_alias'] = @$data_invoice[0]['alias'];
        $array_address['invoice']['td_popup__address_first_name'] = @$data_invoice[0]['firstname'];
        $array_address['invoice']['td_popup__address_last_name'] = @$data_invoice[0]['lastname'];
        $array_address['invoice']['td_popup__address_company'] = @$data_invoice[0]['company'];
        $array_address['invoice']['td_popup__address_adres1'] = @$data_invoice[0]['address1'];
        $array_address['invoice']['td_popup__address_adres2'] = @$data_invoice[0]['address2'];
        $array_address['invoice']['td_popup__address_city'] = @$data_invoice[0]['city'];
        $array_address['invoice']['td_popup__address_state'] = '';
        if (!empty(@$data_invoice[0]['id_state'])) {
            $namestatein = State::getNameById((int)@$data_invoice[0]['id_state']);
            $array_address['invoice']['td_popup__address_state'] = $namestatein;
        }
        $array_address['invoice']['td_popup__address_zip_code'] = @$data_invoice[0]['postcode'];
        $namecountryin = Country::getNameById($this->context->language->id, (int)@$data_invoice[0]['id_country']);
        $array_address['invoice']['td_popup__address_country'] = $namecountryin;
        $array_address['invoice']['td_popup__address_home_phone'] = @$data_invoice[0]['phone'];
        $array_address['invoice']['td_popup__address_mobile_phone'] = @$data_invoice[0]['phone_mobile'];
        $array_address['invoice']['id_address'] = $id_invoice;

        $this->genOptionAlias($this->address_ids);
        $this->context->smarty->assign('array_address', $array_address);
        $this->context->smarty->assign('td_id_customer', '-1');
        $this->context->smarty->assign('address_ids', $this->address_ids);
        $ba_dir = 'askforaquotemodul/views/templates/admin/boxaddress.tpl';
        $html = $this->context->smarty->fetch(_PS_MODULE_DIR_ . $ba_dir);
        return $html;
    }
    
    public function addAddress()
    {
        $array_address = array();
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $query = 'INSERT INTO  ' . _DB_PREFIX_ . 'address (id_country,id_state,id_customer,';
        $query .= 'alias,company,lastname,firstname,';
        $query .= 'address1,address2,postcode,city,other,phone,phone_mobile,';
        $query .= 'vat_number,dni,date_add,date_upd,active,deleted)';
        $query .= 'VALUES("'.(int)$this->td_popup__address_country .'","'.(int)$this->td_popup__address_state . '","';
        $query .= '' . (int) $this->td_id_customer . '",';
        $query .= '"' . pSQL($this->td_popup__address_alias) . '","' . pSQL($this->td_popup__address_company) . '",';
        $query .= '"' . pSQL($this->td_popup__address_last_name) . '",';
        $query .= '"' . pSQL($this->td_popup__address_first_name).'","'.pSQL($this->td_popup__address_adres1) . '",';
        $query .= '"' . pSQL($this->td_popup__address_adres2) . '","' . pSQL($this->td_popup__address_zip_code) . '",';
        $query .= '"' . pSQL($this->td_popup__address_city) . '",';
        $query .= '"' . pSQL($this->td_popup__address_other) . '","' . pSQL($this->td_popup__address_home_phone) . '",';
        $query .= '"' . pSQL($this->td_popup__address_mobile_phone).'","'.pSQL($this->td_popup__address_vat_num).'",';
        $query .= '"' . pSQL($this->td_popup__address_identi_num) . '","' . pSQL($this->date_add) . '",';
        $query .= '"' . pSQL($this->date_upd) . '","' . (int) $this->active . '","' . (int) $this->deleted . '")';

        $db->query($query);
        $id_address = $db->Insert_ID();
        
        $array_address['delivery']['td_popup__address_alias'] = $this->td_popup__address_alias;
        $array_address['delivery']['td_popup__address_first_name'] = $this->td_popup__address_first_name;
        $array_address['delivery']['td_popup__address_last_name'] = $this->td_popup__address_last_name;
        $array_address['delivery']['td_popup__address_company'] = $this->td_popup__address_company;
        $array_address['delivery']['td_popup__address_adres1'] = $this->td_popup__address_adres1;
        $array_address['delivery']['td_popup__address_adres2'] = $this->td_popup__address_adres2;
        $array_address['delivery']['td_popup__address_city'] = $this->td_popup__address_city;
        $array_address['delivery']['td_popup__address_state'] = '';
        if (!empty($this->td_popup__address_state)) {
            $array_address['delivery']['td_popup__address_state'] = State::getNameById($this->td_popup__address_state);
        }
        $array_address['delivery']['td_popup__address_zip_code'] = $this->td_popup__address_zip_code;
        $namecountryde = Country::getNameById($this->context->language->id, $this->td_popup__address_country);
        $array_address['delivery']['td_popup__address_country'] = $namecountryde;
        $array_address['delivery']['td_popup__address_home_phone'] = $this->td_popup__address_home_phone;
        $array_address['delivery']['td_popup__address_mobile_phone'] = $this->td_popup__address_mobile_phone;
        $array_address['delivery']['id_address'] = $id_address;
        
        $array_address['invoice']['td_popup__address_alias'] = $this->td_popup__address_alias;
        $array_address['invoice']['td_popup__address_first_name'] = $this->td_popup__address_first_name;
        $array_address['invoice']['td_popup__address_last_name'] = $this->td_popup__address_last_name;
        $array_address['invoice']['td_popup__address_company'] = $this->td_popup__address_company;
        $array_address['invoice']['td_popup__address_adres1'] = $this->td_popup__address_adres1;
        $array_address['invoice']['td_popup__address_adres2'] = $this->td_popup__address_adres2;
        $array_address['invoice']['td_popup__address_city'] = $this->td_popup__address_city;
        $array_address['invoice']['td_popup__address_state'] = '';
        if (!empty($this->td_popup__address_state)) {
            $array_address['invoice']['td_popup__address_state'] = State::getNameById($this->td_popup__address_state);
        }
        $array_address['invoice']['td_popup__address_zip_code'] = $this->td_popup__address_zip_code;
        $namecountryin = Country::getNameById($this->context->language->id, $this->td_popup__address_country);
        $array_address['invoice']['td_popup__address_country'] = $namecountryin;
        $array_address['invoice']['td_popup__address_home_phone'] = $this->td_popup__address_home_phone;
        $array_address['invoice']['td_popup__address_mobile_phone'] = $this->td_popup__address_mobile_phone;
        $array_address['invoice']['id_address'] = $id_address;
        
        if (!empty($id_address)) {
            $this->address_ids .= ',' . $id_address;
        }
        $this->address_ids = ltrim($this->address_ids, ',');
        $this->genOptionAlias($this->address_ids);
        
        $this->context->smarty->assign('array_address', $array_address);
        $this->context->smarty->assign('td_id_customer', '-1');
        $this->context->smarty->assign('address_ids', $this->address_ids);
        $ba_dir = 'askforaquotemodul/views/templates/admin/boxaddress.tpl';
        $html = $this->context->smarty->fetch(_PS_MODULE_DIR_ . $ba_dir);
        return $html;
    }
    
    public function genOptionAlias($address_ids)
    {
        $db = Db::getInstance(_PS_USE_SQL_SLAVE_);
        $sql = 'SELECT id_address,alias FROM ' . _DB_PREFIX_ . 'address WHERE id_address IN ('.pSQL($address_ids).')';
        $data_option = $db->ExecuteS($sql);
        $this->context->smarty->assign('data_option', $data_option);
    }
    
    public function addVariable()
    {
        $this->td_popup__address_identi_num = Tools::getValue('td_popup__address_identi_num');
        $this->td_popup__address_alias = Tools::getValue('td_popup__address_alias');
        $this->td_popup__address_first_name = Tools::getValue('td_popup__address_first_name');
        $this->td_popup__address_last_name = Tools::getValue('td_popup__address_last_name');
        $this->td_popup__address_company = Tools::getValue('td_popup__address_company');
        $this->td_popup__address_vat_num = Tools::getValue('td_popup__address_vat_num');
        $this->td_popup__address_adres1 = Tools::getValue('td_popup__address_adres1');
        $this->td_popup__address_adres2 = Tools::getValue('td_popup__address_adres2');
        $this->td_popup__address_city = Tools::getValue('td_popup__address_city');
        $this->td_popup__address_zip_code = Tools::getValue('td_popup__address_zip_code');
        $this->td_popup__address_country = Tools::getValue('td_popup__address_country');
        $this->td_popup__address_state = Tools::getValue('td_popup__address_state');
        $this->td_popup__address_home_phone = Tools::getValue('td_popup__address_home_phone');
        $this->td_popup__address_mobile_phone = Tools::getValue('td_popup__address_mobile_phone');
        $this->td_popup__address_other = Tools::getValue('td_popup__address_other');
        $this->date_add = date('Y-m-d H:i:s');
        $this->date_upd = date('Y-m-d H:i:s');
        $this->td_id_customer = Tools::getValue('td_id_customer');
        $this->address_ids = Tools::getValue('address_ids');
    }
    public function convertToArrayRowProduct($row_product)
    {
        $result = array();
        $row_product = ltrim($row_product, '|');
        $row_product = explode('|', $row_product);
        $total_price = 0;
        foreach ($row_product as $key_row_product => $value_row_product) {
            $value_row_product = explode('-', $value_row_product);
            foreach ($value_row_product as $key_vrp => $value_vrp) {
                if ($key_vrp == 0) {
                    $result[$key_row_product]['id_product'] = $value_vrp;
                }
                if ($key_vrp == 1) {
                    $result[$key_row_product]['id_combination'] = $value_vrp;
                }
                if ($key_vrp == 2) {
                    $result[$key_row_product]['price_product'] = Tools::ps_round($value_vrp, 4);
                }
                if ($key_vrp == 3) {
                    $result[$key_row_product]['quantity'] = $value_vrp;
                }
                if ($key_vrp == 4) {
                    $result[$key_row_product]['total_price_product'] = Tools::ps_round($value_vrp, 4);
                    $total_price += (float) Tools::ps_round($value_vrp, 4);
                }
            }
        }
        $result['total_price'] = $total_price;
        $result['total_tax'] = 0;
        return $result;
    }
}
