PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / models / reportsemp.php
vikappointments / admin / models Last commit date
apiban.php 4 years ago apilog.php 2 years ago apiplugin.php 4 years ago apiuser.php 2 years ago apiuseroptions.php 2 years ago backup.php 4 months ago caldays.php 1 month ago calendar.php 1 month ago city.php 4 years ago closure.php 1 month ago configapp.php 4 years ago configcldays.php 4 years ago configcron.php 4 years ago configemp.php 4 years ago configsmsapi.php 4 years ago configuration.php 4 months ago conversion.php 4 years ago country.php 2 years ago coupon.php 2 years ago couponemployee.php 4 years ago coupongroup.php 2 years ago couponservice.php 4 years ago cronjob.php 2 years ago cronjoblog.php 4 years ago customer.php 4 months ago customf.php 2 years ago customfservice.php 4 years ago customizer.php 4 years ago empgroup.php 2 years ago employee.php 2 years ago empsettings.php 4 years ago file.php 4 years ago findreservation.php 1 month ago group.php 2 years ago import.php 4 years ago index.html 4 years ago invoice.php 1 month ago langcustomf.php 4 years ago langempgroup.php 4 years ago langemployee.php 4 years ago langgroup.php 4 years ago langmedia.php 4 years ago langoption.php 2 years ago langoptiongroup.php 4 years ago langoptionvar.php 4 years ago langpackage.php 4 years ago langpackgroup.php 4 years ago langpayment.php 4 years ago langservice.php 4 years ago langstatuscode.php 4 years ago langsubscr.php 4 years ago langtax.php 2 years ago langtaxrule.php 4 years ago location.php 2 years ago mailtext.php 2 years ago makerecurrence.php 1 month ago media.php 2 years ago multiorder.php 1 month ago option.php 1 year ago optiongroup.php 2 years ago optionvar.php 1 year ago orderstatus.php 2 years ago package.php 2 years ago packageservice.php 4 years ago packgroup.php 2 years ago packorder.php 2 years ago packorderitem.php 1 month ago payment.php 2 years ago rate.php 2 years ago reportsemp.php 4 months ago reportsser.php 4 months ago reservation.php 1 month ago resoptassoc.php 2 years ago restriction.php 2 years ago review.php 3 years ago serempassoc.php 1 year ago seroptassoc.php 4 years ago serrateassoc.php 4 years ago serrestrassoc.php 4 years ago service.php 2 years ago state.php 2 years ago statswidget.php 2 years ago statuscode.php 2 years ago subscription.php 1 month ago subscrorder.php 1 month ago tag.php 4 years ago tax.php 2 years ago taxrule.php 2 years ago updateprogram.php 4 years ago usernote.php 2 years ago waitinglist.php 1 month ago webhook.php 1 year ago worktime.php 1 month ago
reportsemp.php
218 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 VAPLoader::import('libraries.mvc.model');
15
16 /**
17 * VikAppointments employees reports model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelReportsemp extends JModelVAP
22 {
23 /**
24 * Loads the filters from the input request.
25 *
26 * @return array
27 */
28 public function getFiltersFromRequest()
29 {
30 $app = JFactory::getApplication();
31
32 $filters = array();
33 $filters['datefrom'] = $app->getUserStateFromRequest('vap.reportsemp.datefrom', 'datefrom', null, 'string');
34 $filters['dateto'] = $app->getUserStateFromRequest('vap.reportsemp.dateto', 'dateto', null, 'string');
35 $filters['valuetype'] = $app->getUserStateFromRequest('vap.reportsemp.valuetype', 'valuetype', null, 'string');
36 $filters['checkin'] = $app->getUserStateFromRequest('vap.reportsemp.checkin', 'checkin', 1, 'uint');
37
38 // obtain services filter
39 $filters['services'] = $app->input->get('services', array(), 'uint');
40
41 if (VAPDateHelper::isNull($filters['datefrom']))
42 {
43 $filters['datefrom'] = null;
44 }
45
46 if (VAPDateHelper::isNull($filters['dateto']))
47 {
48 $filters['dateto'] = null;
49 }
50
51 if (!$filters['valuetype'])
52 {
53 $dbo = JFactory::getDbo();
54
55 // auto-detect value type according to the services price
56 $q = $dbo->getQuery(true)
57 ->select(1)
58 ->from($dbo->qn('#__vikappointments_service'))
59 ->where($dbo->qn('published') . ' = 1')
60 ->where($dbo->qn('price') . ' > 0');
61
62 $dbo->setQuery($q, 0, 1);
63 $dbo->execute();
64
65 // use total earning in case at least a service has a cost
66 $filters['valuetype'] = $dbo->getNumRows() ? 'total' : 'count';
67 }
68
69 return $filters;
70 }
71
72 /**
73 * Returns the form data of the specified employees.
74 *
75 * @param mixed $id Either an employee ID or an array.
76 * @param array $filters An array of filters.
77 *
78 * @return array An array of employees.
79 */
80 public function getFormData($id, ?array $filters = null)
81 {
82 $id = (array) $id;
83
84 // load employees list
85 $employees = array();
86
87 if ($id)
88 {
89 $dbo = JFactory::getDbo();
90
91 $q = $dbo->getQuery(true)
92 ->select($dbo->qn(array('id', 'nickname')))
93 ->from($dbo->qn('#__vikappointments_employee'))
94 ->where($dbo->qn('id') . ' IN (' . implode(',', array_map('intval', $id)) . ')');
95
96 $dbo->setQuery($q);
97 $employees = $dbo->loadObjectList();
98 }
99
100 // import statistics framework
101 VAPLoader::import('libraries.statistics.factory');
102
103 if (!$filters)
104 {
105 // load filters from request
106 $filters = $this->getFiltersFromRequest();
107 }
108
109 foreach ($employees as $employee)
110 {
111 // prepare widget settings
112 $filters['employee'] = $employee->id;
113 $filters['chart'] = 'line';
114
115 // get widget instance
116 $employee->lineChart = VAPStatisticsFactory::getInstance('employees_services_chart', $filters);
117
118 // get widget instance
119 $employee->pieChart = VAPStatisticsFactory::getInstance('employees_services_count', $filters);
120 }
121
122 return $employees;
123 }
124
125 /**
126 * Downloads the reports of the specified employees.
127 *
128 * @param mixed $id Either a employee ID or an array.
129 * @param array $filters An array of filters.
130 *
131 * @return void
132 */
133 public function download($id, ?array $filters = null)
134 {
135 // get available widgets
136 $data = $this->getFormData($id, $filters);
137
138 // count number of employees
139 $count = count($data);
140
141 if ($count == 0)
142 {
143 // nothing to export here...
144 throw new UnexpectedValueException('Nothing to export', 400);
145 }
146
147 if ($count == 1)
148 {
149 // Directly download the report.
150 // This method automatically terminates the session.
151 $data[0]->lineChart->export();
152 }
153
154 // make sure the class to compress files exists
155 if (!class_exists('ZipArchive'))
156 {
157 // class not found, unable to create archive
158 throw new RuntimeException('The ZipArchive class is not installed on your server.', 500);
159 }
160
161 $files = array();
162
163 // iterate all widgets and save the exported reports in a file
164 foreach ($data as $employee)
165 {
166 // save and obtain file path
167 $path = $employee->lineChart->export('file');
168
169 // make sure the file exists
170 if (is_file($path))
171 {
172 $files[] = $path;
173 }
174 }
175
176 $app = JFactory::getApplication();
177
178 // obtain temporary folder
179 $path = rtrim($app->get('tmp_path'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
180 // create ZIP path
181 $zipname = $path . 'reports-' . time() . '.zip';
182
183 // create archive
184 $zip = new ZipArchive;
185 $zip->open($zipname, ZipArchive::CREATE);
186
187 // include all the created report files
188 foreach ($files as $file)
189 {
190 // get readable file name
191 $name = preg_replace("/-[\d]+\.csv$/i", '.csv', basename($file));
192 // include file
193 $zip->addFile($file, $name);
194 }
195
196 // compress archive
197 $zip->close();
198
199 // send headers for download
200 $app->setHeader('Content-Type', 'application/zip');
201 $app->setHeader('Content-Disposition', 'attachment; filename=reports.zip');
202 $app->setHeader('Content-Length', filesize($zipname));
203 $app->sendHeaders();
204
205 // start downloading the file
206 readfile($zipname);
207
208 // delete archive on completion
209 unlink($zipname);
210
211 // delete single reports too
212 foreach ($files as $file)
213 {
214 unlink($file);
215 }
216 }
217 }
218