diff --git a/admin/themes/default/template/controllers/customers/helpers/view/view.tpl b/admin/themes/default/template/controllers/customers/helpers/view/view.tpl index 8213ed88d..432be8ad9 100644 --- a/admin/themes/default/template/controllers/customers/helpers/view/view.tpl +++ b/admin/themes/default/template/controllers/customers/helpers/view/view.tpl @@ -664,6 +664,38 @@ {/if} + {if $customer_guests && count($customer_guests)} +
+
+ + {l s='Customer Guests'} + {count($customer_guests)} +
+ + + + + + + + + + + + {foreach $customer_guests AS $key => $customer_guest} + + + + + + + + + {/foreach} + +
{l s='Email'}{l s='First name'}{l s='Last name'}{l s='phone'}{l s='Actions'}
{$customer_guest['email']}{$customer_guest['firstname']}{$customer_guest['lastname']}{$customer_guest['phone']}
+
+ {/if} diff --git a/admin/themes/default/template/controllers/customers/modals/_customer_guest_form.tpl b/admin/themes/default/template/controllers/customers/modals/_customer_guest_form.tpl new file mode 100644 index 000000000..12e9baa32 --- /dev/null +++ b/admin/themes/default/template/controllers/customers/modals/_customer_guest_form.tpl @@ -0,0 +1,53 @@ +{* +* Since 2010 Webkul. +* +* NOTICE OF LICENSE +* +* All right is reserved, +* Please go through this link for complete license : https://store.webkul.com/license.html +* +* DISCLAIMER +* +* Do not edit or add to this file if you wish to upgrade this module to newer +* versions in the future. If you wish to customize this module for your +* needs please refer to https://store.webkul.com/customisation-guidelines/ for more information. +* +* @author Webkul IN +* @copyright Since 2010 Webkul IN +* @license https://store.webkul.com/license.html +*} + + diff --git a/admin/themes/default/template/controllers/customers/modals/index.php b/admin/themes/default/template/controllers/customers/modals/index.php new file mode 100644 index 000000000..ccbf6cb72 --- /dev/null +++ b/admin/themes/default/template/controllers/customers/modals/index.php @@ -0,0 +1,29 @@ + +* @copyright 2010-2020 Webkul IN +* @license https://store.webkul.com/license.html +*/ + +header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); +header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); + +header('Cache-Control: no-store, no-cache, must-revalidate'); +header('Cache-Control: post-check=0, pre-check=0', false); +header('Pragma: no-cache'); + +header('Location: ../../../../../../'); +exit; diff --git a/classes/Customer.php b/classes/Customer.php index 2983aa607..6b84f07bd 100644 --- a/classes/Customer.php +++ b/classes/Customer.php @@ -207,7 +207,7 @@ class CustomerCore extends ObjectModel parent::__construct($id); if ($this->email) { - $this->phone = CartCustomerGuestDetail::getCustomerPhone($this->email); + $this->phone = CustomerGuestDetail::getCustomerPhone($this->email, false); } if (Configuration::get('PS_ONE_PHONE_AT_LEAST')) { @@ -264,7 +264,7 @@ class CustomerCore extends ObjectModel } $success = parent::add($autodate, $null_values); $this->updateGroup($this->groupBox); - $this->updateCustomerAdditionalDetails(CartCustomerGuestDetail::getIdCustomerGuest($this->email)); + $this->updateCustomerAdditionalDetails(CustomerGuestDetail::getIdCustomerGuest($this->email, false)); return $success; } @@ -289,20 +289,20 @@ class CustomerCore extends ObjectModel $objOldCustomer = new Customer($this->id); $success = parent::update(true); - $this->updateCustomerAdditionalDetails(CartCustomerGuestDetail::getIdCustomerGuest($objOldCustomer->email)); + $this->updateCustomerAdditionalDetails(CustomerGuestDetail::getIdCustomerGuest($objOldCustomer->email, false)); return $success; } public function updateCustomerAdditionalDetails($idCustomerGuest) { - $objCartCustomerGuestDetail = new CartCustomerGuestDetail($idCustomerGuest); - $objCartCustomerGuestDetail->id_cart = 0; - $objCartCustomerGuestDetail->id_gender = $this->id_gender; - $objCartCustomerGuestDetail->firstname = $this->firstname; - $objCartCustomerGuestDetail->lastname = $this->lastname; - $objCartCustomerGuestDetail->email = $this->email; - $objCartCustomerGuestDetail->phone = $this->phone; - return $objCartCustomerGuestDetail->save(); + $objCustomerGuestDetail = new CustomerGuestDetail($idCustomerGuest); + $objCustomerGuestDetail->id_customer = 0; + $objCustomerGuestDetail->id_gender = $this->id_gender; + $objCustomerGuestDetail->firstname = $this->firstname; + $objCustomerGuestDetail->lastname = $this->lastname; + $objCustomerGuestDetail->email = $this->email; + $objCustomerGuestDetail->phone = $this->phone; + return $objCustomerGuestDetail->save(); } public function delete() @@ -343,8 +343,8 @@ class CustomerCore extends ObjectModel CartRule::deleteByIdCustomer((int)$this->id); // delete customer data from customerGuest table - $objCartCustomerGuestDetail = new CartCustomerGuestDetail(CartCustomerGuestDetail::getIdCustomerGuest($this->email)); - $objCartCustomerGuestDetail->delete(); + $objCustomerGuestDetail = new CustomerGuestDetail(CustomerGuestDetail::getIdCustomerGuest($this->email, false)); + $objCustomerGuestDetail->delete(); return parent::delete(); } @@ -542,7 +542,7 @@ class CustomerCore extends ObjectModel return Db::getInstance()->getValue( 'SELECT cgd.`phone` FROM `'._DB_PREFIX_.'customer` c - INNER JOIN `'._DB_PREFIX_.'cart_customer_guest_detail` cgd + INNER JOIN `'._DB_PREFIX_.'customer_guest_detail` cgd ON (cgd.`email` = c.`email`) WHERE `id_cart` = 0 AND c.`id_customer` = '.(int)($id_customer) ); diff --git a/classes/CartCustomerGuestDetail.php b/classes/CustomerGuestDetail.php similarity index 58% rename from classes/CartCustomerGuestDetail.php rename to classes/CustomerGuestDetail.php index f26e4c795..32f3eb63b 100644 --- a/classes/CartCustomerGuestDetail.php +++ b/classes/CustomerGuestDetail.php @@ -18,10 +18,10 @@ * @license https://store.webkul.com/license.html */ -class CartCustomerGuestDetailCore extends ObjectModel +class CustomerGuestDetailCore extends ObjectModel { public $id_customer_guest_detail; - public $id_cart; + public $id_customer; public $id_gender; public $firstname; public $lastname; @@ -31,10 +31,10 @@ class CartCustomerGuestDetailCore extends ObjectModel public $date_upd; public static $definition = array( - 'table' => 'cart_customer_guest_detail', + 'table' => 'customer_guest_detail', 'primary' => 'id_customer_guest_detail', 'fields' => array( - 'id_cart' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'), + 'id_customer' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'), 'id_gender' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'), 'firstname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'size' => 32), 'lastname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'size' => 32), @@ -49,48 +49,67 @@ class CartCustomerGuestDetailCore extends ObjectModel { return Db::getInstance()->getValue(' SELECT `id_customer_guest_detail` - FROM `'._DB_PREFIX_.'cart_customer_guest_detail` + FROM `'._DB_PREFIX_.'cart_customer_guest` WHERE `id_cart` = '.(int)$id_cart ); } - public function add($auto_date = true, $null_values = false) + public static function saveCartCustomerGuest($idCart, $idCustomerGuestDetail) { - if ($res = parent::add($auto_date, $null_values)) { - $objCart = new Cart($this->id_cart); - Db::getInstance()->insert('customer_guest_detail_mapping', array( - 'id_customer' => (int) $objCart->id_customer, - 'id_customer_guest_detail' => (int)$this->id - )); - } + return Db::getInstance()->insert('cart_customer_guest', array( + 'id_cart' => (int) $idCart, + 'id_customer_guest_detail' => (int) $idCustomerGuestDetail + )); + } - return $res; + public static function deleteCartCustomerGuest($idCart) + { + return Db::getInstance()->delete('cart_customer_guest', + ' `id_cart`='.(int) $idCart + ); + } + + public function deleteCartCustomerGuestByIdGuest() + { + return Db::getInstance()->delete('cart_customer_guest', + ' `id_customer_guest_detail`='.(int) $this->id + ); } public function delete() { - if ($res = parent::delete()) { - $objCart = new Cart($this->id_cart); - Db::getInstance()->delete('customer_guest_detail_mapping', - '`id_customer` = '.(int) $objCart->id_customer.' - AND `id_customer_guest_detail`='.(int) $this->id - ); + $this->deleteCartCustomerGuestByIdGuest(); + + return parent::delete(); + } + + public function deleteCartCustomerGuestByIdCustomer($idCustomer, $offset = 0) + { + $res = true; + if ($guests = $this->getCustomerRelatedGuestDetails($idCustomer, false, false, false, $offset)) { + foreach ($guests as $guest) { + $objCustomerGuestDetail = new CustomerGuestDetail((int) $guest['id_customer_guest_detail']); + $objCustomerGuestDetail->delete(); + } } return $res; } - public function getCustomerRelatedGuestDetails($idCustomer, $firstname = false, $lastname = false, $email = false) + public function getCustomerRelatedGuestDetails($idCustomer, $firstname = false, $lastname = false, $email = false, $offset = 0) { return Db::getInstance()->executeS(' - SELECT cgd.`id_customer_guest_detail`, cgd.`email`, cgd.`firstname`, cgd.`lastname`, cgd.`phone`, cgd.`id_cart` - FROM `'._DB_PREFIX_.'customer_guest_detail_mapping` cgdm - LEFT JOIN `'._DB_PREFIX_.'cart_customer_guest_detail` cgd - ON (cgdm.`id_customer_guest_detail` = cgd.`id_customer_guest_detail`) - WHERE cgdm.`id_customer` = '.(int)$idCustomer. + SELECT cgd.`id_customer_guest_detail`, cgd.`email`, cgd.`firstname`, cgd.`lastname`, cgd.`phone` + FROM `'._DB_PREFIX_.'customer_guest_detail` cgd + LEFT JOIN `'._DB_PREFIX_.'cart_customer_guest` ccg + ON (ccg.`id_customer_guest_detail` = cgd.`id_customer_guest_detail`) + WHERE cgd.`id_customer` = '.(int)$idCustomer. (($firstname !== false && $firstname != '') ? ' AND cgd.`firstname` LIKE "%'.$firstname.'%"' : ' '). (($lastname !== false && $lastname != '') ? ' AND cgd.`lastname` LIKE "%'.$lastname.'%"' : ' '). - (($email !== false && $email != '') ? ' AND cgd.`email` LIKE "%'.$email.'%"' : ' ') + (($email !== false && $email != '') ? ' AND cgd.`email` LIKE "%'.$email.'%"' : ' '). + 'GROUP BY cgd.`id_customer_guest_detail`'. + ' Order BY date_add DESC '. + (($offset) ? ' LIMIT 999999 OFFSET '.(int) $offset : ' ') ); } @@ -98,24 +117,29 @@ class CartCustomerGuestDetailCore extends ObjectModel { return Db::getInstance()->getRow(' SELECT `id_gender`, `firstname`, `lastname`, `email`, `phone` - FROM `'._DB_PREFIX_.'cart_customer_guest_detail` + FROM `'._DB_PREFIX_.'customer_guest_detail` WHERE `id_customer_guest_detail` = '.(int)$id_customer_guest_detail ); } - public static function getIdCustomerGuest($email) + public static function getIdCustomerGuest($email, $idCustomer = null, $idCart = 0) { return Db::getInstance()->getValue( - 'SELECT `id_customer_guest_detail` FROM `'._DB_PREFIX_.'cart_customer_guest_detail` - WHERE `id_cart` = 0 AND `email` = "'.pSQL($email).'"' + 'SELECT cgd.`id_customer_guest_detail` FROM `'._DB_PREFIX_.'customer_guest_detail` as cgd + LEFT JOIN `'._DB_PREFIX_.'cart_customer_guest` ccg + ON ccg.`id_customer_guest_detail` = cgd.`id_customer_guest_detail` + WHERE cgd.`email` = "'.pSQL($email).'"'. + (!is_null($idCart) ? ' AND (ccg.`id_cart` = '.(int) $idCart.' '. (($idCart) ? ')' : ' OR ISNULL(ccg.`id_cart`)) ') : ' '). + (!is_null($idCustomer) ? ' AND cgd.`id_customer` = '.(int) $idCustomer : ' ') ); } - public static function getCustomerPhone($email) + public static function getCustomerPhone($email, $idCustomer = null) { return Db::getInstance()->getValue( - 'SELECT `phone` FROM `'._DB_PREFIX_.'cart_customer_guest_detail` - WHERE `id_cart` = 0 AND `email` = "'.pSQL($email).'"' + 'SELECT `phone` FROM `'._DB_PREFIX_.'customer_guest_detail` + WHERE 1 AND `email` = "'.pSQL($email).'"'. + (!is_null($idCustomer) ? ' AND `id_customer` ='.(int) $idCustomer: ' ') ); } @@ -135,7 +159,7 @@ class CartCustomerGuestDetailCore extends ObjectModel $isValid = false; } - $className = 'CartCustomerGuestDetail'; + $className = 'CustomerGuestDetail'; $rules = call_user_func(array($className, 'getValidationRules'), $className); if (isset($rules['size']['firstname'])) { diff --git a/classes/PaymentModule.php b/classes/PaymentModule.php index 516a755ab..7b4dcb80c 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -400,16 +400,16 @@ abstract class PaymentModuleCore extends Module } // save customer guest information - if ($id_customer_guest_detail = CartCustomerGuestDetail::getCartCustomerGuest($this->context->cart->id)) { - if (Validate::isLoadedObject($objCartCustomerGuestDetail = new CartCustomerGuestDetail( + if ($id_customer_guest_detail = CustomerGuestDetail::getCartCustomerGuest($this->context->cart->id)) { + if (Validate::isLoadedObject($objCustomerGuestDetail = new CustomerGuestDetail( $id_customer_guest_detail ))) { $objOrderCustomerGuestDetail = new OrderCustomerGuestDetail(); - $objOrderCustomerGuestDetail->id_gender = $objCartCustomerGuestDetail->id_gender; - $objOrderCustomerGuestDetail->firstname = $objCartCustomerGuestDetail->firstname; - $objOrderCustomerGuestDetail->lastname = $objCartCustomerGuestDetail->lastname; - $objOrderCustomerGuestDetail->email = $objCartCustomerGuestDetail->email; - $objOrderCustomerGuestDetail->phone = $objCartCustomerGuestDetail->phone; + $objOrderCustomerGuestDetail->id_gender = $objCustomerGuestDetail->id_gender; + $objOrderCustomerGuestDetail->firstname = $objCustomerGuestDetail->firstname; + $objOrderCustomerGuestDetail->lastname = $objCustomerGuestDetail->lastname; + $objOrderCustomerGuestDetail->email = $objCustomerGuestDetail->email; + $objOrderCustomerGuestDetail->phone = $objCustomerGuestDetail->phone; $objOrderCustomerGuestDetail->id_order = (int)$order->id; $objOrderCustomerGuestDetail->save(); } diff --git a/classes/checkoutProcess/CheckoutCustomerDetailsStep.php b/classes/checkoutProcess/CheckoutCustomerDetailsStep.php index 6bccd6e83..a51cb3169 100644 --- a/classes/checkoutProcess/CheckoutCustomerDetailsStep.php +++ b/classes/checkoutProcess/CheckoutCustomerDetailsStep.php @@ -39,9 +39,9 @@ class CheckoutCustomerDetailsStepCore extends AbstractCheckoutStepCore } } elseif (Tools::getValue('proceed_to_payment')) { $guestInfoComplete = true; - if ($id_customer_guest_detail = CartCustomerGuestDetail::getCartCustomerGuest($this->context->cart->id)) { + if ($id_customer_guest_detail = CustomerGuestDetail::getCartCustomerGuest($this->context->cart->id)) { $guestInfoComplete = false; - $objCustomerGuestDetail = new CartCustomerGuestDetail($id_customer_guest_detail); + $objCustomerGuestDetail = new CustomerGuestDetail($id_customer_guest_detail); if ($objCustomerGuestDetail->validateGuestInfo()) { $guestInfoComplete = true; } diff --git a/controllers/admin/AdminCustomerPreferencesController.php b/controllers/admin/AdminCustomerPreferencesController.php index 0526cc277..14d091f91 100644 --- a/controllers/admin/AdminCustomerPreferencesController.php +++ b/controllers/admin/AdminCustomerPreferencesController.php @@ -121,6 +121,15 @@ class AdminCustomerPreferencesControllerCore extends AdminController 'cast' => 'intval', 'type' => 'bool' ), + 'PS_CUSTOMER_GUEST_MAX_NUM' => array( + 'title' => $this->l('Maximum Customers guests'), + 'hint' => $this->l('Maximum number of guests a customer can add using the "booking for someone else" feature. Set as 0 for no limit.'), + 'validation' => 'isUnsignedInt', + 'cast' => 'intval', + 'size' => 5, + 'type' => 'text', + 'class' => 'fixed-width-xl', + ), ), 'submit' => array( 'title' => $this->l('Save'), diff --git a/controllers/admin/AdminCustomersController.php b/controllers/admin/AdminCustomersController.php index c8e20a62e..381a7cde2 100644 --- a/controllers/admin/AdminCustomersController.php +++ b/controllers/admin/AdminCustomersController.php @@ -939,6 +939,9 @@ class AdminCustomersControllerCore extends AdminController $customerLanguage = new Language($customer->id_lang); $shop = new Shop($customer->id_shop); + + $objCustomerGuestDetail = new CustomerGuestDetail(); + $customerGuests = $objCustomerGuestDetail->getCustomerRelatedGuestDetails($customer->id); $this->tpl_view_vars = array( 'customer' => $customer, 'gender' => $gender, @@ -985,7 +988,8 @@ class AdminCustomersControllerCore extends AdminController 'connections' => $connections, // Referrers 'referrers' => $referrers, - 'show_toolbar' => true + 'show_toolbar' => true, + 'customer_guests' => $customerGuests, ); return parent::renderView(); } @@ -1002,11 +1006,11 @@ class AdminCustomersControllerCore extends AdminController $this->errors[] = Tools::displayError('Some error ocurred while deleting the Customer'); return; } else { - if ($idCustomerGuest = CartCustomerGuestDetail::getIdCustomerGuest($customerEmail)) { - $objCartCustomerGuestDetail = new CartCustomerGuestDetail($idCustomerGuest); - $objCartCustomerGuestDetail->phone = preg_replace('/[0-9]/', '0', $objCustomer->phone); - $objCartCustomerGuestDetail->email = $objCustomer->email; - $objCartCustomerGuestDetail->save(); + if ($idCustomerGuest = CustomerGuestDetail::getIdCustomerGuest($customerEmail, false)) { + $objCustomerGuestDetail = new CustomerGuestDetail($idCustomerGuest); + $objCustomerGuestDetail->phone = preg_replace('/[0-9]/', '0', $objCustomer->phone); + $objCustomerGuestDetail->email = $objCustomer->email; + $objCustomerGuestDetail->save(); } } @@ -1176,7 +1180,7 @@ class AdminCustomersControllerCore extends AdminController $this->errors[] = Tools::displayError('Phone number is required.'); } } - $className = 'CartCustomerGuestDetail'; + $className = 'CustomerGuestDetail'; $rules = call_user_func(array($className, 'getValidationRules'), $className); if ($phone && !Validate::isPhoneNumber($phone)) { $this->errors[] = Tools::displayError('Invaid phone number.'); @@ -1389,17 +1393,113 @@ class AdminCustomersControllerCore extends AdminController $this->ajaxDie(json_encode($response)); } + public function ajaxProcessInitGuestModal() + { + $response['hasError'] = 1; + if (Validate::isLoadedObject($objCustomerGuestDetail = new CustomerGuestDetail((int) Tools::getValue('id_customer_guest_detail')))) { + $this->context->smarty->assign( + array( + 'genders' => Gender::getGenders(), + 'customerGuestDetail' => $objCustomerGuestDetail, + ) + ); + $modal = array( + 'modal_id' => 'customer-guest-modal', + 'modal_class' => 'customer_guest_modal', + 'modal_title' => '  '.$this->l('Guest details'), + 'modal_content' => $this->context->smarty->fetch('controllers/customers/modals/_customer_guest_form.tpl'), + 'modal_actions' => array( + array( + 'type' => 'button', + 'value' => 'submitGuestInfo', + 'class' => 'submitGuestInfoInfo btn-primary pull-right', + 'label' => ' '.$this->l('Save Guest'), + ), + ), + ); + + $this->context->smarty->assign($modal); + $response['hasError'] = 0; + $response['modalHtml'] = $this->context->smarty->fetch('modal.tpl'); + } + + $this->ajaxDie(json_encode($response)); + } + + + public function ajaxProcessUpdateGuestDetails() + { + $response = array('hasError' => 0, 'errors' => array()); + // Check tab access is allowed to edit + if ($this->tabAccess['edit'] == 1) { + if (Validate::isLoadedObject($objCustomerGuestDetail = new CustomerGuestDetail((int) Tools::getValue('id_customer_guest_detail')))) { + $objCustomerGuestDetail->id_gender = Tools::getValue('id_gender'); + $objCustomerGuestDetail->firstname = Tools::getValue('firstname'); + $objCustomerGuestDetail->lastname = Tools::getValue('lastname'); + $objCustomerGuestDetail->phone = Tools::getValue('phone'); + if ($objCustomerGuestDetail->save()) { + $gender = new Gender($objCustomerGuestDetail->id_gender, $this->context->language->id); + $response['data']['gender'] = $gender->name; + $response['data']['firstname'] = $objCustomerGuestDetail->firstname; + $response['data']['lastname'] = $objCustomerGuestDetail->lastname ; + $response['data']['email'] = $objCustomerGuestDetail->email; + $response['data']['phone'] = $objCustomerGuestDetail->phone; + $response['data']['id'] = $objCustomerGuestDetail->id; + $response['msg'] = $this->l('Guest details are updated.'); + } else { + $response['errors'][] = $this->l('Unable to save guest details.'); + } + } else { + $response['errors'][] = $this->l('Guest details not found.'); + } + } else { + $response['errors'][] = $this->l('You do not have permission to edit this.'); + } + + if ($response['errors']) { + $response['hasError'] = 1; + $this->context->smarty->assign('errors', $response['errors']); + $response['errorsHtml'] = $this->context->smarty->fetch('alerts.tpl'); + } + + $this->ajaxDie(json_encode($response)); + } + + public function ajaxProcessDeleteGuest() + { + $response = array('hasError' => 1, 'errors' => array()); + // Check tab access is allowed to edit + if ($this->tabAccess['delete'] == 1) { + if (Validate::isLoadedObject($objCustomerGuestDetail = new CustomerGuestDetail((int) Tools::getValue('id_customer_guest_detail')))) { + if ($objCustomerGuestDetail->delete()) { + $response['hasError'] = false; + $response['msg'] = $this->l('Successful deletion.'); + } else { + $response['msg'] = $this->l('Unable to delete guest details.'); + } + } else { + $response['msg'] = $this->l('Guest details not found.'); + } + } else { + $response['msg'] = $this->l('You do not have permission to delete this.'); + } + + $this->ajaxDie(json_encode($response)); + } + public function setMedia() { parent::setMedia(); if ($this->loadObject(true) - && ($this->display == 'edit' || $this->display == 'add') + && ($this->display == 'edit' || $this->display == 'add' || $this->display == 'view') ) { $idCustomer = $this->object->id ? $this->object->id : 0; Media::addJSDef( array( 'customer_controller_url' => self::$currentIndex.'&token='.$this->token, - 'id_customer' => $idCustomer + 'id_customer' => $idCustomer, + 'txtSomeErr' => $this->l('Some error occurred. Please try again.', null, true), + 'confirmTxt' => $this->l('Are you sure you want to delete this guest details?', null, true) ) ); $this->addJS(_PS_JS_DIR_.'admin/customers.js'); diff --git a/controllers/front/AuthController.php b/controllers/front/AuthController.php index ff254b67a..573ba8a0f 100644 --- a/controllers/front/AuthController.php +++ b/controllers/front/AuthController.php @@ -422,7 +422,7 @@ class AuthControllerCore extends FrontController $addresses_types[] = 'address_invoice'; } - $className = 'CartCustomerGuestDetail'; + $className = 'CustomerGuestDetail'; $rules = call_user_func(array($className, 'getValidationRules'), $className); if (Configuration::get('PS_ONE_PHONE_AT_LEAST') && !Tools::getValue('phone')) { $this->errors[] = Tools::displayError('Phone number is required.'); diff --git a/controllers/front/IdentityController.php b/controllers/front/IdentityController.php index 65ce13b27..04fb339de 100644 --- a/controllers/front/IdentityController.php +++ b/controllers/front/IdentityController.php @@ -88,7 +88,8 @@ class IdentityControllerCore extends FrontController $this->errors[] = Tools::displayError('Phone number is required.'); } } - $className = 'CartCustomerGuestDetail'; + + $className = 'CustomerGuestDetail'; $rules = call_user_func(array($className, 'getValidationRules'), $className); if ($phone && !Validate::isPhoneNumber($phone)) { $this->errors[] = Tools::displayError('Invaid phone number.'); diff --git a/controllers/front/OrderOpcController.php b/controllers/front/OrderOpcController.php index d8baeb03c..ba225a5e9 100644 --- a/controllers/front/OrderOpcController.php +++ b/controllers/front/OrderOpcController.php @@ -119,7 +119,8 @@ class OrderOpcControllerCore extends ParentOrderController if (Configuration::get('PS_ONE_PHONE_AT_LEAST') && !$phone) { $this->errors[] = Tools::displayError('Phone number is a required field.', false); } - $className = 'CartCustomerGuestDetail'; + + $className = 'CustomerGuestDetail'; $rules = call_user_func(array($className, 'getValidationRules'), $className); if (!Validate::isPhoneNumber($phone)) { $this->errors[] = Tools::displayError('Please enter a valid Mobile phone number.', false); diff --git a/install/data/db_structure.sql b/install/data/db_structure.sql index b21a85bfd..ab0c0b31e 100644 --- a/install/data/db_structure.sql +++ b/install/data/db_structure.sql @@ -686,9 +686,9 @@ CREATE TABLE `PREFIX_order_customer_guest_detail` ( PRIMARY KEY (`id_order_customer_guest_detail`) ) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8 COLLATION; -CREATE TABLE `PREFIX_cart_customer_guest_detail` ( +CREATE TABLE `PREFIX_customer_guest_detail` ( `id_customer_guest_detail` int(10) unsigned NOT NULL auto_increment, - `id_cart` int(10) unsigned NOT NULL, + `id_customer` int(10) unsigned NOT NULL, `id_gender` int(10) unsigned NOT NULL, `firstname` varchar(32) NOT NULL, `lastname` varchar(32) NOT NULL, @@ -699,10 +699,10 @@ CREATE TABLE `PREFIX_cart_customer_guest_detail` ( PRIMARY KEY (`id_customer_guest_detail`) ) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8 COLLATION; -CREATE TABLE `PREFIX_customer_guest_detail_mapping` ( +CREATE TABLE `PREFIX_cart_customer_guest` ( `id_customer_guest_detail` int(10) unsigned NOT NULL, - `id_customer` int(10) unsigned NOT NULL, - PRIMARY KEY (`id_customer_guest_detail`, `id_customer`) + `id_cart` int(10) unsigned NOT NULL, + PRIMARY KEY (`id_customer_guest_detail`, `id_cart`) ) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8 COLLATION; CREATE TABLE `PREFIX_customization` ( diff --git a/install/data/xml/configuration.xml b/install/data/xml/configuration.xml index 490e2b691..7dae52982 100644 --- a/install/data/xml/configuration.xml +++ b/install/data/xml/configuration.xml @@ -946,6 +946,9 @@ 0 + + + 5 diff --git a/js/admin/customers.js b/js/admin/customers.js index 5eaa94c7d..d29e97ed7 100644 --- a/js/admin/customers.js +++ b/js/admin/customers.js @@ -48,4 +48,129 @@ $(document).ready(function() { }); } }); + + $(document).on('click', '.edit-customer-guest-detail', function(e) { + e.preventDefault(); + GuestModal.show($(this).data('id_customer_guest_detail')); + }); + $(document).on('click', '.delete-customer-guest-detail', function(e) { + e.preventDefault(); + if (confirm(confirmTxt)) { + GuestModal.deleteGuest($(this).data('id_customer_guest_detail')); + } + }); + + const GuestModal = { + show: function(id_customer_guest_detail) { + $("#page-loader").show(); + $('#customer-guest-modal').remove(); + $.ajax({ + url: customer_controller_url, + method: 'POST', + dataType: 'json', + data: { + ajax : 1, + id_customer_guest_detail: id_customer_guest_detail, + action: 'InitGuestModal' + }, + beforeSend: function() { + $("#page-loader").show(); + }, + success: function(result) { + if (result.hasError == 0 && result.modalHtml) { + $('#footer').next('.bootstrap').append(result.modalHtml); + $('#customer-guest-modal').modal('show'); + } else { + showErrorMessage(txtSomeErr); + } + }, + complete: function() { + $("#page-loader").hide(); + } + }); + }, + close: function() { + $('#customer-guest-modal').modal('hide'); + }, + submit: function() { + $("#page-loader").show(); + GuestModal.hideErrors(); + $.ajax({ + headers: { + "cache-control": "no-cache" + }, + url: customer_controller_url, + method: 'POST', + dataType: 'json', + cache: false, + data: $('#customer-guest-details-form').serialize()+'&ajax=true&action=updateGuestDetails', + success: function(result) { + if (result.hasError == true) { + GuestModal.showErrors(result.errorsHtml); + } else { + if (result.msg) { + showSuccessMessage(result.msg); + } + let tr = $(''); + tr.append('' + result.data.email + '') + .append('' + result.data.firstname + '') + .append('' + result.data.lastname + '') + .append('' + result.data.phone + '') + .append('') + .append(''); + $('.customer_guest_detail_'+result.data.id).html($(tr).html()); + } + }, + complete: function() { + $("#page-loader").hide(); + } + }); + }, + deleteGuest: function(id_customer_guest_detail) { + $("#page-loader").show(); + GuestModal.hideErrors(); + $.ajax({ + headers: { + "cache-control": "no-cache" + }, + url: customer_controller_url, + method: 'POST', + dataType: 'json', + cache: false, + data: { + ajax : 1, + id_customer_guest_detail: id_customer_guest_detail, + action: 'DeleteGuest' + }, + success: function(result) { + if (result.hasError == true) { + showErrorMessage(result.msg); + } else if (result.msg) { + showSuccessMessage(result.msg); + $('.customer_guest_detail_'+id_customer_guest_detail).remove(); + if ($('.customer_guest_details').length == 0) { + $('.customer-guests').closest('.panel').hide(); + } else { + $('.customer-guests-count').text(parseInt($('.customer_guest_details').length)); + } + } + }, + complete: function() { + $("#page-loader").hide(); + } + }); + }, + showErrors: function(errorsHtml) { + $('#customer-guest-modal .errors-wrap').html(errorsHtml); + $('#customer-guest-modal .errors-wrap').show(200); + }, + hideErrors: function(cb) { + $('#customer-guest-modal .errors-wrap').hide(200); + $('#customer-guest-modal .errors-wrap').html(''); + } + }; + $(document).on('click', '.submitGuestInfoInfo', function(e) { + e.preventDefault(); + GuestModal.submit(); + }); }); diff --git a/themes/hotel-reservation-theme/js/order-opc.js b/themes/hotel-reservation-theme/js/order-opc.js index c8af2ab06..df1ea54ac 100644 --- a/themes/hotel-reservation-theme/js/order-opc.js +++ b/themes/hotel-reservation-theme/js/order-opc.js @@ -94,9 +94,11 @@ $(document).ready(function() if ($("#customer_guest_detail:checked").val() == 1) { $('#checkout-guest-info-block').hide('slow'); $('#customer-guest-detail-container').show('slow'); + $('#customer_guest_detail_errors').show('slow'); } else { $('#customer-guest-detail-container').hide('slow'); $('#checkout-guest-info-block').show('slow'); + $('#customer_guest_detail_errors').hide('hide'); } } function validateCustomerGuestDetailForm() { @@ -115,30 +117,24 @@ $(document).ready(function() if ($("#customer_guest_detail:checked").val() == 1) { validateCustomerGuestDetailForm(); } - - function submitCustomerGuestDetailForm(postData) { - $.ajax({ - type: 'POST', - url: orderOpcUrl, - async: false, - cache: false, - dataType : "json", - data: postData - }); - } - - $('#customer_guest_detail_form').on('change', function(e) { - let postData = $(this).serialize()+'&method=submitCustomerGuestDetail&ajax=true&token=' + static_token; - submitCustomerGuestDetailForm(postData); + $(document).on('click', '.submit-guest-details', function(e) { + e.preventDefault(); + if ($('#customer_guest_detail').prop('checked')) { + validateCustomerGuestDetailForm(); + if ($('#customer_guest_detail_form').find('.form-error').length == 0) { + $('#customer_guest_detail_form').get(0).submit(); + } + } else { + $('#customer_guest_detail_form').get(0).submit(); + } }); + function setCustomerGuestDetailForm(guestDetail) { $('#customer_guest_detail_firstname').val(guestDetail.firstname); $('#customer_guest_detail_lastname').val(guestDetail.lastname); $('#customer_guest_detail_email').val(guestDetail.email); $('#customer_guest_detail_phone').val(guestDetail.phone); validateCustomerGuestDetailForm(); - let postData = $('#customer_guest_detail_form').serialize()+'&method=submitCustomerGuestDetail&ajax=true&token=' + static_token; - submitCustomerGuestDetailForm(postData); $('.customer_guest_detail_ul').remove(); } @@ -194,9 +190,17 @@ $(document).ready(function() $('.customer_guest_detail_ul').remove(); clearTimeout(debounceTimeout); // Clear the existing timeout debounceTimeout = setTimeout(() => { - let firstName = $('#customer_guest_detail_firstname').val(); - let lastName = $('#customer_guest_detail_lastname').val(); - let email = $('#customer_guest_detail_email').val(); + let firstName = ''; + let lastName = ''; + let email = ''; + if ($(e.target).prop('id') == 'customer_guest_detail_firstname') { + firstName = $('#customer_guest_detail_firstname').val() + } else if ($(e.target).prop('id') == 'customer_guest_detail_lastname') { + lastName = $('#customer_guest_detail_lastname').val() + } else if ($(e.target).prop('id') == 'customer_guest_detail_email') { + email = $('#customer_guest_detail_email').val() + } + let targetElem = $(this); customerGuestDetailAjax = $.ajax({ type: 'POST', @@ -220,7 +224,6 @@ $(document).ready(function() $(itemElem).attr('data-guest_detail', JSON.stringify(guestDetail)); $(listElem).append(itemElem); }); - $(targetElem).closest('.form-group').append($(listElem).prop('outerHTML')); } } diff --git a/themes/hotel-reservation-theme/order-opc.tpl b/themes/hotel-reservation-theme/order-opc.tpl index 3f342c077..f412b1bee 100644 --- a/themes/hotel-reservation-theme/order-opc.tpl +++ b/themes/hotel-reservation-theme/order-opc.tpl @@ -98,11 +98,22 @@ {if $is_logged || $isGuest} {if $is_logged} {block name='order_opc_guest_detail_form'} -
+ +

+ {if isset($customerGuestDetailErrors) && $customerGuestDetailErrors} +
+

{if $customerGuestDetailErrors|@count > 1}{l s='There are %d errors' sprintf=$customerGuestDetailErrors|@count}{else}{l s='There is %d error' sprintf=$customerGuestDetailErrors|@count}{/if}

+
    + {foreach from=$customerGuestDetailErrors key=k item=customerGuestDetailError} +
  1. {$customerGuestDetailError}
  2. + {/foreach} +
+
+ {/if}
@@ -173,7 +184,7 @@