view.html.php
227 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 | jimport('joomla.html.pagination'); |
| 15 | VAPLoader::import('libraries.order.factory'); |
| 16 | |
| 17 | /** |
| 18 | * VikAppointments customer info view. |
| 19 | * |
| 20 | * @since 1.3 |
| 21 | */ |
| 22 | class VikAppointmentsViewcustomerinfo extends JViewVAP |
| 23 | { |
| 24 | /** |
| 25 | * VikAppointments view display method. |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | function display($tpl = null) |
| 30 | { |
| 31 | $app = JFactory::getApplication(); |
| 32 | $input = $app->input; |
| 33 | $dbo = JFactory::getDbo(); |
| 34 | |
| 35 | // force blank component template |
| 36 | $input->set('tmpl', 'component'); |
| 37 | |
| 38 | $id = $input->getUint('cid', array(0), 'uint'); |
| 39 | $id = $id[0]; |
| 40 | |
| 41 | // load customer data |
| 42 | $this->customer = VikAppointments::getCustomer($id); |
| 43 | |
| 44 | if (!$this->customer) |
| 45 | { |
| 46 | throw new Exception('Customer not found', 404); |
| 47 | } |
| 48 | |
| 49 | $langtag = JFactory::getLanguage()->getTag(); |
| 50 | |
| 51 | //////////////////////// |
| 52 | ///// APPOINTMENTS ///// |
| 53 | //////////////////////// |
| 54 | |
| 55 | $app_start = $this->getListLimitStart(array(), $id, 'appointments'); |
| 56 | $app_limit = 5; |
| 57 | $app_nav = ''; |
| 58 | $app_count = 0; |
| 59 | $app_total = 0; |
| 60 | |
| 61 | $this->appointments = array(); |
| 62 | |
| 63 | // get any reserved codes |
| 64 | $reserved = JHtml::fetch('vaphtml.status.find', 'code', array('appointments' => 1, 'reserved' => 1)); |
| 65 | |
| 66 | $q = $dbo->getQuery(true) |
| 67 | ->select('SQL_CALC_FOUND_ROWS r.id') |
| 68 | ->from($dbo->qn('#__vikappointments_reservation', 'r')) |
| 69 | ->where($dbo->qn('r.id_parent') . ' > 0') |
| 70 | ->andWhere(array( |
| 71 | $dbo->qn('r.id_user') . ' = ' . $this->customer->id, |
| 72 | $dbo->qn('r.id_user') . ' <= 0 AND ' . $dbo->qn('r.purchaser_mail') . ' = ' . $dbo->q($this->customer->billing_mail), |
| 73 | ), 'OR') |
| 74 | ->order($dbo->qn('r.id') . ' DESC'); |
| 75 | |
| 76 | if ($reserved) |
| 77 | { |
| 78 | // filter by reserved status |
| 79 | $q->where($dbo->qn('r.status') . ' IN (' . implode(',', array_map(array($dbo, 'q'), $reserved)) . ')'); |
| 80 | } |
| 81 | |
| 82 | $totalQuery = $q; |
| 83 | |
| 84 | $dbo->setQuery($q, $app_start, $app_limit); |
| 85 | $column = $dbo->loadColumn(); |
| 86 | |
| 87 | if ($column) |
| 88 | { |
| 89 | // get total number of appointments |
| 90 | $dbo->setQuery('SELECT FOUND_ROWS();'); |
| 91 | $app_count = (int) $dbo->loadResult(); |
| 92 | |
| 93 | // iterate orders found |
| 94 | foreach ($column as $id_res) |
| 95 | { |
| 96 | // get appointment details |
| 97 | $this->appointments[] = VAPOrderFactory::getAppointments($id_res, $langtag); |
| 98 | } |
| 99 | |
| 100 | // create appointments pagination |
| 101 | $pageNav = new JPagination($app_count, $app_start, $app_limit, 'appointments'); |
| 102 | $app_nav = JLayoutHelper::render('blocks.pagination', ['pageNav' => $pageNav]); |
| 103 | |
| 104 | // calculate total spent |
| 105 | $totalQuery->clear('select') |
| 106 | ->clear('limit') |
| 107 | ->select(sprintf('SUM(%s) AS %s', $dbo->qn('total_cost'), $dbo->qn('total'))); |
| 108 | |
| 109 | $dbo->setQuery($q); |
| 110 | $app_total = (float) $dbo->loadResult(); |
| 111 | } |
| 112 | |
| 113 | $this->appNav = $app_nav; |
| 114 | $this->appCount = $app_count; |
| 115 | $this->appTotal = $app_total; |
| 116 | |
| 117 | //////////////////////// |
| 118 | /////// PACKAGES /////// |
| 119 | //////////////////////// |
| 120 | |
| 121 | $pack_start = $this->getListLimitStart(array(), $id, 'packages'); |
| 122 | $pack_limit = 5; |
| 123 | $pack_nav = ''; |
| 124 | $pack_count = 0; |
| 125 | $pack_total = 0; |
| 126 | |
| 127 | $this->packages = array(); |
| 128 | |
| 129 | // get any approved codes |
| 130 | $approved = JHtml::fetch('vaphtml.status.find', 'code', array('packages' => 1, 'approved' => 1)); |
| 131 | |
| 132 | $q = $dbo->getQuery(true) |
| 133 | ->select('SQL_CALC_FOUND_ROWS o.id') |
| 134 | ->from($dbo->qn('#__vikappointments_package_order', 'o')) |
| 135 | ->where(1) |
| 136 | ->andWhere(array( |
| 137 | $dbo->qn('o.id_user') . ' = ' . $this->customer->id, |
| 138 | $dbo->qn('o.id_user') . ' <= 0 AND ' . $dbo->qn('o.purchaser_mail') . ' = ' . $dbo->q($this->customer->billing_mail), |
| 139 | ), 'OR') |
| 140 | ->order($dbo->qn('o.id') . ' DESC'); |
| 141 | |
| 142 | if ($approved) |
| 143 | { |
| 144 | // filter by approved status |
| 145 | $q->where($dbo->qn('o.status') . ' IN (' . implode(',', array_map(array($dbo, 'q'), $approved)) . ')'); |
| 146 | } |
| 147 | |
| 148 | $totalQuery = $q; |
| 149 | |
| 150 | $dbo->setQuery($q, $pack_start, $pack_limit); |
| 151 | $column = $dbo->loadColumn(); |
| 152 | |
| 153 | if ($column) |
| 154 | { |
| 155 | // get total number of packages |
| 156 | $dbo->setQuery('SELECT FOUND_ROWS();'); |
| 157 | $pack_count = (int) $dbo->loadResult(); |
| 158 | |
| 159 | // iterate orders found |
| 160 | foreach ($column as $id_ord) |
| 161 | { |
| 162 | // get package details |
| 163 | $this->packages[] = VAPOrderFactory::getPackages($id_ord, $langtag); |
| 164 | } |
| 165 | |
| 166 | // create packages pagination |
| 167 | $pageNav = new JPagination($pack_count, $pack_start, $pack_limit, 'packages'); |
| 168 | $pack_nav = JLayoutHelper::render('blocks.pagination', ['pageNav' => $pageNav]); |
| 169 | |
| 170 | // calculate total spent |
| 171 | $totalQuery->clear('select') |
| 172 | ->clear('limit') |
| 173 | ->select(sprintf('SUM(%s) AS %s', $dbo->qn('total_cost'), $dbo->qn('total'))); |
| 174 | |
| 175 | $dbo->setQuery($q); |
| 176 | $pack_total = (float) $dbo->loadResult(); |
| 177 | } |
| 178 | |
| 179 | $this->packNav = $pack_nav; |
| 180 | $this->packCount = $pack_count; |
| 181 | $this->packTotal = $pack_total; |
| 182 | |
| 183 | /** |
| 184 | * Count the total number of purchased/used packages. |
| 185 | * |
| 186 | * @since 1.7.4 |
| 187 | */ |
| 188 | $this->packUserCount = JModelVAP::getInstance('customer')->countPackages($this->customer->id); |
| 189 | |
| 190 | //////////////////////// |
| 191 | ////// USER NOTES ////// |
| 192 | //////////////////////// |
| 193 | |
| 194 | $this->notes = array(); |
| 195 | $this->notesCount = 0; |
| 196 | |
| 197 | // load only the latest 4 modified notes (without blank content) |
| 198 | $q = $dbo->getQuery(true) |
| 199 | ->select('SQL_CALC_FOUND_ROWS n.*') |
| 200 | ->from($dbo->qn('#__vikappointments_user_notes', 'n')) |
| 201 | ->where($dbo->qn('n.id_user') . ' = ' . $this->customer->id) |
| 202 | ->where($dbo->qn('n.content') . ' <> ' . $dbo->q('')) |
| 203 | ->order(sprintf( |
| 204 | 'IFNULL(%s, %s) %s', |
| 205 | $dbo->qn('n.modifiedon'), |
| 206 | $dbo->qn('n.createdon'), |
| 207 | 'DESC' |
| 208 | )); |
| 209 | |
| 210 | $dbo->setQuery($q, 0, 4); |
| 211 | $this->notes = $dbo->loadObjectList(); |
| 212 | |
| 213 | if ($this->notes) |
| 214 | { |
| 215 | // get total number of notes |
| 216 | $dbo->setQuery('SELECT FOUND_ROWS();'); |
| 217 | $this->notesCount = (int) $dbo->loadResult(); |
| 218 | } |
| 219 | |
| 220 | // get tag model |
| 221 | $this->tagModel = JModelVAP::getInstance('tag'); |
| 222 | |
| 223 | // display the template |
| 224 | parent::display($tpl); |
| 225 | } |
| 226 | } |
| 227 |