view.html.php
185 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * VikAppointments appointments confirmation view. |
| 16 | * |
| 17 | * @since 1.0 |
| 18 | */ |
| 19 | class VikAppointmentsViewconfirmapp extends JViewVAP |
| 20 | { |
| 21 | /** |
| 22 | * VikAppointments view display method. |
| 23 | * |
| 24 | * @return void |
| 25 | */ |
| 26 | function display($tpl = null) |
| 27 | { |
| 28 | $app = JFactory::getApplication(); |
| 29 | $input = $app->input; |
| 30 | |
| 31 | // get cart model |
| 32 | $model = JModelVAP::getInstance('cart'); |
| 33 | |
| 34 | // get cart instance |
| 35 | $this->cart = $model->getCart(); |
| 36 | |
| 37 | $this->itemid = $input->getUint('Itemid', 0); |
| 38 | |
| 39 | $items = $this->cart->getItemsList(); |
| 40 | |
| 41 | // check if all the services booked owns the same employee |
| 42 | $same_emp_id = VAPCartUtils::isSameEmployee($items); |
| 43 | // obtain the list of all the services booked |
| 44 | $services_id = VAPCartUtils::getServices($items); |
| 45 | |
| 46 | if ($same_emp_id) |
| 47 | { |
| 48 | // we can obtain the custom payments of the booked employee (just take the first one) |
| 49 | $id_employee = $items[0]->getEmployeeID(); |
| 50 | } |
| 51 | else |
| 52 | { |
| 53 | // the cart owns different employees (or maybe a single one hidden), we |
| 54 | // need to get the global payments (use null to exclude the employee filter). |
| 55 | $id_employee = null; |
| 56 | } |
| 57 | |
| 58 | // load payments and translate them |
| 59 | $this->payments = VikAppointments::getAllEmployeePayments($id_employee); |
| 60 | VikAppointments::translatePayments($this->payments); |
| 61 | |
| 62 | // import custom fields renderer and loader (as dependency) |
| 63 | VAPLoader::import('libraries.customfields.renderer'); |
| 64 | |
| 65 | // get all custom fields for the selected services |
| 66 | $cf = VAPCustomFieldsLoader::getInstance() |
| 67 | ->translate() |
| 68 | ->setLanguageFilter() |
| 69 | ->forService($services_id) |
| 70 | ->onPage('confirm'); |
| 71 | |
| 72 | if ($same_emp_id) |
| 73 | { |
| 74 | // obtain custom fields of the specified employee |
| 75 | $cf->ofEmployee($id_employee); |
| 76 | } |
| 77 | |
| 78 | // fetch custom fields |
| 79 | $this->customFields = $cf->fetch(); |
| 80 | |
| 81 | // get customer details of currently logged-in user |
| 82 | $this->user = VikAppointments::getCustomer(); |
| 83 | |
| 84 | // always refresh remaining user credit |
| 85 | $this->cart->removeDiscount('credit'); |
| 86 | |
| 87 | if ($this->user && $this->user->credit > 0) |
| 88 | { |
| 89 | // register user credit as discount |
| 90 | $this->cart->addDiscount(new VAPCartDiscount('credit', $this->user->credit, $percent = false)); |
| 91 | } |
| 92 | |
| 93 | $this->cart->store(); |
| 94 | |
| 95 | // check whether the form used to redeem the coupons should be displayed or not |
| 96 | $this->anyCoupon = VikAppointments::hasCoupon('appointments'); |
| 97 | |
| 98 | // fetch the field that should be used to validate the ZIP |
| 99 | $this->zipFieldID = VikAppointments::getZipCodeValidationFieldId($id_employee, $services_id); |
| 100 | |
| 101 | // get order total |
| 102 | $total = $this->cart->getTotalGross(); |
| 103 | |
| 104 | // skip payment in case there are no gateways or the total is equals to 0 |
| 105 | $this->skipPayments = !$this->payments || $total <= 0 ? 1 : 0; |
| 106 | |
| 107 | // print conversion code if needed |
| 108 | VAPLoader::import('libraries.models.conversion'); |
| 109 | VAPConversion::getInstance(array('page' => 'confirmapp'))->trackCode(array('total_cost' => $total)); |
| 110 | |
| 111 | /** |
| 112 | * Prepare view contents and microdata. |
| 113 | * |
| 114 | * @since 1.7 |
| 115 | */ |
| 116 | VikAppointments::prepareContent($this); |
| 117 | |
| 118 | // extend pathway for breadcrumbs module |
| 119 | $this->extendPathway($app); |
| 120 | |
| 121 | // display the template |
| 122 | parent::display($tpl); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Groups the items by service. |
| 127 | * |
| 128 | * @return array |
| 129 | * |
| 130 | * @since 1.7 |
| 131 | */ |
| 132 | protected function groupItems() |
| 133 | { |
| 134 | // sort items by service and date |
| 135 | $items = VAPCartUtils::sortItemsByServiceDate($this->cart->getItemsList()); |
| 136 | |
| 137 | $arr = array(); |
| 138 | |
| 139 | foreach ($this->cart->getItemsList() as $i) |
| 140 | { |
| 141 | $id_service = $i->getServiceID(); |
| 142 | |
| 143 | if (!isset($arr[$id_service])) |
| 144 | { |
| 145 | $service = new stdClass; |
| 146 | $service->id = $id_service; |
| 147 | $service->name = $i->getServiceName(); |
| 148 | $service->list = array(); |
| 149 | |
| 150 | $arr[$id_service] = $service; |
| 151 | } |
| 152 | |
| 153 | $arr[$id_service]->list[] = $i; |
| 154 | } |
| 155 | |
| 156 | return array_values($arr); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Extends the pathway for breadcrumbs module. |
| 161 | * |
| 162 | * @param mixed $app The application instance. |
| 163 | * |
| 164 | * @return void |
| 165 | * |
| 166 | * @since 1.7 |
| 167 | */ |
| 168 | protected function extendPathway($app) |
| 169 | { |
| 170 | $pathway = $app->getPathway(); |
| 171 | $items = $pathway->getPathway(); |
| 172 | $last = end($items); |
| 173 | |
| 174 | // Make sure the confirmation page is not a menu item, otherwise |
| 175 | // the pathway will display something like: |
| 176 | // Home > Menu > Confirmation > Confirm Order |
| 177 | if ($last && strpos($last->link, '&view=confirmapp') === false) |
| 178 | { |
| 179 | // register link into the Breadcrumb |
| 180 | $link = 'index.php?option=com_vikappointments&view=confirmapp' . ($this->itemid ? '&Itemid=' . $this->itemid : ''); |
| 181 | $pathway->addItem(JText::translate('VAPCONFIRMRESBUTTON'), $link); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 |