vikappointments
/
site
/
helpers
/
libraries
/
statistics
/
widgets
/
subscriptions
Last commit date
coupons
3 days ago
items
3 days ago
payments
3 days ago
revenue
3 days ago
status
3 days ago
index.html
3 days ago
overall.php
3 days ago
rog.php
3 days ago
overall.php
267 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 | * Widget used to fetch the overall revenue coming from the subscriptions. |
| 16 | * |
| 17 | * @since 1.7 |
| 18 | */ |
| 19 | class VAPStatisticsWidgetSubscriptionsOverall extends VAPStatisticsWidget |
| 20 | { |
| 21 | /** |
| 22 | * Internal flag used to check whether the current user |
| 23 | * owns enough permissions to access the financial data. |
| 24 | * |
| 25 | * Use true by default in case the widget is displayed |
| 26 | * without checking the permissions. |
| 27 | * |
| 28 | * @var boolean |
| 29 | */ |
| 30 | protected $hasFinanceAccess = true; |
| 31 | |
| 32 | /** |
| 33 | * Use the layout provided by "Appointments - Overall" widget. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | protected $layoutId = 'appointments.overall'; |
| 38 | |
| 39 | /** |
| 40 | * Checks whether the specified group is supported by the widget. |
| 41 | * |
| 42 | * @param string $group The group to check. |
| 43 | * |
| 44 | * @return boolean True if supported, false otherwise. |
| 45 | */ |
| 46 | public function isSupported($group) |
| 47 | { |
| 48 | return empty($group) || $group == '*' || $group == 'dashboard' || $group == 'subscriptions'; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Checks whether the specified user is capable to access this widget. |
| 53 | * |
| 54 | * @param JUser $user The user instance. |
| 55 | * |
| 56 | * @return boolean True if capable, false otherwise. |
| 57 | */ |
| 58 | public function checkPermissions($user) |
| 59 | { |
| 60 | // detected finance visibility |
| 61 | $this->hasFinanceAccess = $user->authorise('core.access.analytics.finance', 'com_vikappointments'); |
| 62 | |
| 63 | return $user->authorise('core.access.analytics.subscriptions', 'com_vikappointments'); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Override this method to return a configuration form of the widget. |
| 68 | * |
| 69 | * @return array |
| 70 | */ |
| 71 | public function getForm() |
| 72 | { |
| 73 | return array( |
| 74 | /** |
| 75 | * The initial date to take when a new session starts. |
| 76 | * |
| 77 | * @var select |
| 78 | */ |
| 79 | 'range' => array( |
| 80 | 'type' => 'select', |
| 81 | 'label' => JText::translate('VAP_CHART_INITIAL_RANGE_FIELD'), |
| 82 | 'help' => JText::translate('VAP_CHART_INITIAL_RANGE_FIELD_HELP'), |
| 83 | 'default' => 'year.curr', |
| 84 | 'options' => array( |
| 85 | 'day.curr' => JText::translate('VAPTODAY'), |
| 86 | 'day.prev' => JText::plural('VAP_LAST_N_DAYS', 1), |
| 87 | 'month.curr' => JText::translate('VAP_CURR_MONTH'), |
| 88 | 'month.prev' => JText::plural('VAP_LAST_N_MONTHS', 1), |
| 89 | 'year.curr' => JText::translate('VAP_CURR_YEAR'), |
| 90 | 'year.prev' => JText::plural('VAP_LAST_N_YEARS', 1), |
| 91 | 'all' => JText::translate('VAPMANAGECONFIGCRON4'), |
| 92 | ), |
| 93 | ), |
| 94 | |
| 95 | /** |
| 96 | * The initial date of the range. |
| 97 | * |
| 98 | * The parameter is VOLATILE because, every time the session |
| 99 | * ends, we need to restore the field to an empty value, just |
| 100 | * to obtain the current date. |
| 101 | * |
| 102 | * @var date |
| 103 | */ |
| 104 | 'datefrom' => array( |
| 105 | 'type' => 'date', |
| 106 | 'label' => JText::translate('VAPEXPORTRES3'), |
| 107 | 'volatile' => true, |
| 108 | ), |
| 109 | |
| 110 | /** |
| 111 | * The ending date of the range. |
| 112 | * |
| 113 | * The parameter is VOLATILE because, every time the session |
| 114 | * ends, we need to restore the field to an empty value, just |
| 115 | * to obtain the current date. |
| 116 | * |
| 117 | * @var date |
| 118 | */ |
| 119 | 'dateto' => array( |
| 120 | 'type' => 'date', |
| 121 | 'label' => JText::translate('VAPEXPORTRES4'), |
| 122 | 'volatile' => true, |
| 123 | ), |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Loads the dataset(s) that will be recovered asynchronously |
| 129 | * for being displayed within the widget. |
| 130 | * |
| 131 | * It is possible to return an array of records to be passed |
| 132 | * to a chart or directly the HTML to replace. |
| 133 | * |
| 134 | * @return mixed |
| 135 | */ |
| 136 | public function getData() |
| 137 | { |
| 138 | $filters = array(); |
| 139 | $filters['datefrom'] = $this->getOption('datefrom'); |
| 140 | $filters['dateto'] = $this->getOption('dateto'); |
| 141 | $filters['range'] = $this->getOption('range', '-6 months'); |
| 142 | |
| 143 | $tz = JFactory::getUser()->getTimezone(); |
| 144 | |
| 145 | $summary = null; |
| 146 | |
| 147 | // use default range in case both the specified dates are empty |
| 148 | if (VAPDateHelper::isNull($filters['datefrom']) && VAPDateHelper::isNull($filters['dateto'])) |
| 149 | { |
| 150 | // init start date |
| 151 | $filters['datefrom'] = JFactory::getDate('today 00:00:00', $tz); |
| 152 | |
| 153 | switch ($filters['range']) |
| 154 | { |
| 155 | case 'day.curr': |
| 156 | // go to the end of the current day |
| 157 | $filters['dateto'] = clone $filters['datefrom']; |
| 158 | $filters['dateto']->modify('23:59:59'); |
| 159 | break; |
| 160 | |
| 161 | case 'day.prev': |
| 162 | // back to the beginning of the previous day |
| 163 | $filters['datefrom']->modify('-1 day'); |
| 164 | // go to the end of the previous day |
| 165 | $filters['dateto'] = clone $filters['datefrom']; |
| 166 | $filters['dateto']->modify('23:59:59'); |
| 167 | break; |
| 168 | |
| 169 | case 'month.curr': |
| 170 | // back to the first day of the current month |
| 171 | $filters['datefrom']->modify($filters['datefrom']->format('Y-m-01', true)); |
| 172 | // go to the last day of the current month |
| 173 | $filters['dateto'] = clone $filters['datefrom']; |
| 174 | $filters['dateto']->modify($filters['dateto']->format('Y-m-t', true)); |
| 175 | |
| 176 | // get rid of month day |
| 177 | $format = preg_replace("/[^a-z]?d[^a-z]?/", '', JText::translate('DATE_FORMAT_LC3')); |
| 178 | $summary = JHtml::fetch('date', $filters['datefrom']->format('Y-m-d', true), $format); |
| 179 | break; |
| 180 | |
| 181 | case 'month.prev': |
| 182 | // back to the first day of the previous month |
| 183 | $filters['datefrom']->modify($filters['datefrom']->format('Y-m-01', true)); |
| 184 | $filters['datefrom']->modify('-1 month'); |
| 185 | // go to the last day of the previous month |
| 186 | $filters['dateto'] = clone $filters['datefrom']; |
| 187 | $filters['dateto']->modify($filters['datefrom']->format('Y-m-t', true) . ' 23:59:59'); |
| 188 | |
| 189 | // get rid of month day |
| 190 | $format = preg_replace("/[^a-z]?d[^a-z]?/", '', JText::translate('DATE_FORMAT_LC3')); |
| 191 | $summary = JHtml::fetch('date', $filters['datefrom']->format('Y-m-d', true), $format); |
| 192 | break; |
| 193 | |
| 194 | case 'year.curr': |
| 195 | // back to the first day of the current year |
| 196 | $filters['datefrom']->modify($filters['datefrom']->format('Y-01-01', true)); |
| 197 | // go to the last day of the current year |
| 198 | $filters['dateto'] = clone $filters['datefrom']; |
| 199 | $filters['dateto']->modify($filters['dateto']->format('Y-12-31', true)); |
| 200 | |
| 201 | $summary = $filters['datefrom']->format('Y', true); |
| 202 | break; |
| 203 | |
| 204 | case 'year.prev': |
| 205 | // back to the first day of the previous year |
| 206 | $filters['datefrom']->modify($filters['datefrom']->format('Y-01-01', true)); |
| 207 | $filters['datefrom']->modify('-1 year'); |
| 208 | // go to the last day of the previous year |
| 209 | $filters['dateto'] = clone $filters['datefrom']; |
| 210 | $filters['dateto']->modify($filters['datefrom']->format('Y-12-31', true) . ' 23:59:59'); |
| 211 | |
| 212 | $summary = $filters['datefrom']->format('Y', true); |
| 213 | break; |
| 214 | |
| 215 | default: |
| 216 | // do not use date filters |
| 217 | $filters['datefrom'] = $filters['dateto'] = null; |
| 218 | } |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | if (!VAPDateHelper::isNull($filters['datefrom'])) |
| 223 | { |
| 224 | // convert specified date to SQL format |
| 225 | $filters['datefrom'] = JFactory::getDate(VAPDateHelper::getDate($filters['datefrom'], 0, 0, 0), $tz); |
| 226 | } |
| 227 | |
| 228 | if (!VAPDateHelper::isNull($filters['dateto'])) |
| 229 | { |
| 230 | // convert specified date to SQL format |
| 231 | $filters['dateto'] = JFactory::getDate(VAPDateHelper::getDate($filters['dateto'], 23, 59, 59), $tz); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | $columns = ['count']; |
| 236 | |
| 237 | if ($this->hasFinanceAccess) |
| 238 | { |
| 239 | // fetch total gross only if the user owns financial permissions |
| 240 | $columns[] = 'total'; |
| 241 | } |
| 242 | |
| 243 | // import subscriptions helper |
| 244 | VAPLoader::import('libraries.statistics.helpers.subscriptions'); |
| 245 | // fetch revenue |
| 246 | $data = VAPStatisticsHelperSubscriptions::getTotalRevenue($filters['datefrom'], $filters['dateto'], $columns); |
| 247 | |
| 248 | if ($this->hasFinanceAccess) |
| 249 | { |
| 250 | // include formatted total |
| 251 | $data['formattedTotal'] = VAPFactory::getCurrency()->format($data['total']); |
| 252 | } |
| 253 | |
| 254 | // include formatted count |
| 255 | $data['formattedCount'] = JText::plural('VAP_N_ORDERS', $data['count']); |
| 256 | |
| 257 | // include range dates |
| 258 | $data['from'] = VAPDateHelper::isNull($filters['datefrom']) ? null : JHtml::fetch('date', $filters['datefrom']->format('Y-m-d', true), JText::translate('DATE_FORMAT_LC4')); |
| 259 | $data['to'] = VAPDateHelper::isNull($filters['dateto']) ? null : JHtml::fetch('date', $filters['dateto']->format('Y-m-d', true), JText::translate('DATE_FORMAT_LC4')); |
| 260 | |
| 261 | // include quick summary |
| 262 | $data['summary'] = $summary; |
| 263 | |
| 264 | return $data; |
| 265 | } |
| 266 | } |
| 267 |