16971 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace LeadersLinked\Controller;
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
9 |
|
|
|
10 |
use Laminas\Mvc\Controller\AbstractActionController;
|
|
|
11 |
use Laminas\Log\LoggerInterface;
|
|
|
12 |
use Laminas\Mvc\I18n\Translator;
|
|
|
13 |
|
|
|
14 |
use Laminas\View\Model\JsonModel;
|
|
|
15 |
use Laminas\View\Model\ViewModel;
|
|
|
16 |
use LeadersLinked\Mapper\DiscoveryContactBlackListMapper;
|
|
|
17 |
use LeadersLinked\Mapper\UserMapper;
|
|
|
18 |
use LeadersLinked\Mapper\QueryMapper;
|
|
|
19 |
use LeadersLinked\Mapper\DiscoveryContactMapper;
|
|
|
20 |
use Laminas\Db\Sql\Expression;
|
|
|
21 |
use LeadersLinked\Mapper\DiscoveryContactInteractionMapper;
|
|
|
22 |
use LeadersLinked\Mapper\DiscoveryContactInteractionTypeMapper;
|
|
|
23 |
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
|
24 |
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
// Create an action controller.
|
|
|
28 |
class DiscoveryContactProgressUserController extends AbstractActionController
|
|
|
29 |
{
|
|
|
30 |
/**
|
|
|
31 |
*
|
|
|
32 |
* @var \Laminas\Db\Adapter\AdapterInterface
|
|
|
33 |
*/
|
|
|
34 |
private $adapter;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
*
|
|
|
38 |
* @var \LeadersLinked\Cache\CacheInterface
|
|
|
39 |
*/
|
|
|
40 |
private $cache;
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
*
|
|
|
45 |
* @var \Laminas\Log\LoggerInterface
|
|
|
46 |
*/
|
|
|
47 |
private $logger;
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
*
|
|
|
51 |
* @var array
|
|
|
52 |
*/
|
|
|
53 |
private $config;
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
*
|
|
|
58 |
* @var \Laminas\Mvc\I18n\Translator
|
|
|
59 |
*/
|
|
|
60 |
private $translator;
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
*
|
|
|
65 |
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
|
|
|
66 |
* @param \LeadersLinked\Cache\CacheInterface $cache
|
|
|
67 |
* @param \Laminas\Log\LoggerInterface LoggerInterface $logger
|
|
|
68 |
* @param array $config
|
|
|
69 |
* @param \Laminas\Mvc\I18n\Translator $translator
|
|
|
70 |
*/
|
|
|
71 |
public function __construct($adapter, $cache, $logger, $config, $translator)
|
|
|
72 |
{
|
|
|
73 |
$this->adapter = $adapter;
|
|
|
74 |
$this->cache = $cache;
|
|
|
75 |
$this->logger = $logger;
|
|
|
76 |
$this->config = $config;
|
|
|
77 |
$this->translator = $translator;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
public function indexAction()
|
|
|
84 |
{
|
|
|
85 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
86 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
87 |
|
|
|
88 |
$request = $this->getRequest();
|
|
|
89 |
|
|
|
90 |
if ($request->isGet()) {
|
|
|
91 |
$headers = $request->getHeaders();
|
|
|
92 |
$isJson = false;
|
|
|
93 |
|
|
|
94 |
if ($headers->has('Accept')) {
|
|
|
95 |
$accept = $headers->get('Accept');
|
|
|
96 |
|
|
|
97 |
$prioritized = $accept->getPrioritized();
|
|
|
98 |
|
|
|
99 |
foreach ($prioritized as $key => $value) {
|
|
|
100 |
$raw = trim($value->getRaw());
|
|
|
101 |
|
|
|
102 |
if (!$isJson) {
|
|
|
103 |
$isJson = strpos($raw, 'json');
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
//$isJson = true;
|
|
|
110 |
if ($isJson) {
|
|
|
111 |
$user_uuid = $this->params()->fromQuery('user');
|
|
|
112 |
if(!$user_uuid) {
|
|
|
113 |
|
|
|
114 |
return new JsonModel([
|
|
|
115 |
'success' => false,
|
|
|
116 |
'data' => 'ERROR_PARAMETERS_ARE_INVALID'
|
|
|
117 |
]);
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
121 |
$user = $userMapper->fetchOneByUuid($user_uuid);
|
|
|
122 |
|
|
|
123 |
if(!$user) {
|
|
|
124 |
return new JsonModel([
|
|
|
125 |
'success' => false,
|
|
|
126 |
'data' => 'ERROR_USER_NOT_FOUND'
|
|
|
127 |
]);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
$startDate = $this->params()->fromQuery('startDate');
|
|
|
132 |
if (empty($startDate)) {
|
|
|
133 |
$startDate = date('Y-m-d');
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
$endDate = $this->params()->fromQuery('endDate');
|
|
|
137 |
if (empty($endDate)) {
|
|
|
138 |
$endDate = date('Y-m-d');
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
$dtStartDate = \DateTime::createFromFormat('Y-n-d', $startDate);
|
|
|
142 |
$dtEndDate = \DateTime::createFromFormat('Y-n-d', $endDate);
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
|
146 |
if (!$dtStartDate || !$dtEndDate) {
|
|
|
147 |
$startDate = date('Y-m-d');
|
|
|
148 |
$endDate = date('Y-m-d');
|
|
|
149 |
} else {
|
|
|
150 |
|
|
|
151 |
if ($dtStartDate->getTimestamp() > $dtEndDate->getTimestamp()) {
|
|
|
152 |
$startDate = date('Y-m-d');
|
|
|
153 |
$endDate = date('Y-m-d');
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
//echo ' $startDate = ' . $startDate . ' $endDate = ' . $endDate; exit;
|
|
|
159 |
|
|
|
160 |
$table = [];
|
|
|
161 |
|
|
|
162 |
|
|
|
163 |
$dt = \DateTime::createFromFormat('Y-m-d', $startDate);
|
|
|
164 |
|
|
|
165 |
do {
|
|
|
166 |
$tempDate = $dt->format('Y-m-d');
|
|
|
167 |
$table[ $tempDate ] = ['label' => $dt->format('d/m/Y'), 'value' => 0];
|
|
|
168 |
$dt->add(new \DateInterval('P1D'));
|
|
|
169 |
|
|
|
170 |
|
|
|
171 |
} while($endDate > $tempDate);
|
|
|
172 |
|
|
|
173 |
|
|
|
174 |
$discoveryContactBlackListMapper = DiscoveryContactBlackListMapper::getInstance($this->adapter);
|
|
|
175 |
$contact_ids = $discoveryContactBlackListMapper->fetchAllContactIdsByCompanyId($currentCompany->id);
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
$queryMapper = QueryMapper::getInstance($this->adapter);
|
|
|
180 |
|
|
|
181 |
|
|
|
182 |
$discoveryContactInteractionTypeMapper = DiscoveryContactInteractionTypeMapper::getInstance($this->adapter);
|
|
|
183 |
$discoveryContactInteractionType = $discoveryContactInteractionTypeMapper->fetchOneDefaultByCompanyId($currentCompany->id);
|
|
|
184 |
if(!$discoveryContactInteractionType) {
|
|
|
185 |
$discoveryContactInteractionType = $discoveryContactInteractionTypeMapper->fetchOneFirstActiveByCompanyId($currentCompany->id);
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
|
|
|
190 |
$select = $queryMapper->getSql()->select(DiscoveryContactInteractionMapper::_TABLE);
|
|
|
191 |
$select->columns([
|
|
|
192 |
'total' => new Expression('COUNT(*)'),
|
|
|
193 |
'date' => new Expression('DATE(added_on)'),
|
|
|
194 |
]);
|
|
|
195 |
$select->where->equalTo('interaction_type_id', $discoveryContactInteractionType->id);
|
|
|
196 |
$select->where->between( new Expression('DATE(added_on)') , $startDate, $endDate);
|
|
|
197 |
$select->where->equalTo('user_id', $user->id);
|
|
|
198 |
|
|
|
199 |
if($contact_ids) {
|
|
|
200 |
$select->where->notIn('contact_id', $contact_ids);
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
$select->group([ new Expression('DATE(added_on)'), 'user_id']);
|
|
|
204 |
$select->order('date ASC');
|
|
|
205 |
|
|
|
206 |
$records = $queryMapper->fetchAll($select);
|
|
|
207 |
foreach($records as $record)
|
|
|
208 |
{
|
|
|
209 |
$table[ $record['date'] ][ 'value' ] = $record['total'];
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
$records = array_values($table);
|
|
|
213 |
|
|
|
214 |
$labels = [];
|
|
|
215 |
$values = [];
|
|
|
216 |
|
|
|
217 |
|
|
|
218 |
|
|
|
219 |
$table = [];
|
|
|
220 |
foreach($records as $record)
|
|
|
221 |
{
|
|
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
|
225 |
array_push($labels, $record['label']);
|
|
|
226 |
array_push($values, $record['value']);
|
|
|
227 |
|
|
|
228 |
array_push($table, [
|
|
|
229 |
'total' => $record['value'],
|
|
|
230 |
'date' => $record['label']
|
|
|
231 |
]);
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
|
|
|
237 |
|
|
|
238 |
return new JsonModel([
|
|
|
239 |
'success' => true,
|
|
|
240 |
'data' => [
|
|
|
241 |
'table' => $table,
|
|
|
242 |
'graph' => [
|
|
|
243 |
'labels' => $labels,
|
|
|
244 |
'values' => $values
|
|
|
245 |
]
|
|
|
246 |
]
|
|
|
247 |
|
|
|
248 |
]);
|
|
|
249 |
} else {
|
|
|
250 |
$discoveryContactInteractionMapper = DiscoveryContactInteractionMapper::getInstance($this->adapter);
|
|
|
251 |
$user_ids = $discoveryContactInteractionMapper->fetchAllUserIdsForCompanyId($currentCompany->id);
|
|
|
252 |
|
|
|
253 |
$users = [];
|
|
|
254 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
255 |
$records = $userMapper->fetchAllByIds($user_ids);
|
|
|
256 |
foreach($records as $record)
|
|
|
257 |
{
|
|
|
258 |
$users[ $record->uuid ] = trim($record->first_name . ' ' . $record->last_name) . ' (' . $record->email . ')';
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
|
|
|
262 |
|
|
|
263 |
|
|
|
264 |
|
|
|
265 |
$this->layout()->setTemplate('layout/layout-backend');
|
|
|
266 |
$viewModel = new ViewModel();
|
|
|
267 |
$viewModel->setTemplate('leaders-linked/discovery-contact-progress-user/index.phtml');
|
|
|
268 |
$viewModel->setVariables([
|
|
|
269 |
'users' => $users
|
|
|
270 |
]);
|
|
|
271 |
return $viewModel;
|
|
|
272 |
}
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
return new JsonModel([
|
|
|
276 |
'success' => false,
|
|
|
277 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
278 |
]);
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
public function downloadAction()
|
|
|
282 |
{
|
|
|
283 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
284 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
285 |
|
|
|
286 |
$request = $this->getRequest();
|
|
|
287 |
|
|
|
288 |
if ($request->isGet()) {
|
|
|
289 |
$user_uuid = $this->params()->fromQuery('user');
|
|
|
290 |
if(!$user_uuid) {
|
|
|
291 |
|
|
|
292 |
return new JsonModel([
|
|
|
293 |
'success' => false,
|
|
|
294 |
'data' => 'ERROR_PARAMETERS_ARE_INVALID'
|
|
|
295 |
]);
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
299 |
$user = $userMapper->fetchOneByUuid($user_uuid);
|
|
|
300 |
|
|
|
301 |
if(!$user) {
|
|
|
302 |
return new JsonModel([
|
|
|
303 |
'success' => false,
|
|
|
304 |
'data' => 'ERROR_USER_NOT_FOUND'
|
|
|
305 |
]);
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
|
|
|
309 |
$startDate = $this->params()->fromQuery('startDate');
|
|
|
310 |
if (empty($startDate)) {
|
|
|
311 |
$startDate = date('Y-m-d');
|
|
|
312 |
}
|
|
|
313 |
|
|
|
314 |
$endDate = $this->params()->fromQuery('endDate');
|
|
|
315 |
if (empty($endDate)) {
|
|
|
316 |
$endDate = date('Y-m-d');
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
$dtStartDate = \DateTime::createFromFormat('Y-n-d', $startDate);
|
|
|
320 |
$dtEndDate = \DateTime::createFromFormat('Y-n-d', $endDate);
|
|
|
321 |
|
|
|
322 |
|
|
|
323 |
|
|
|
324 |
if (!$dtStartDate || !$dtEndDate) {
|
|
|
325 |
$startDate = date('Y-m-d');
|
|
|
326 |
$endDate = date('Y-m-d');
|
|
|
327 |
} else {
|
|
|
328 |
|
|
|
329 |
if ($dtStartDate->getTimestamp() > $dtEndDate->getTimestamp()) {
|
|
|
330 |
$startDate = date('Y-m-d');
|
|
|
331 |
$endDate = date('Y-m-d');
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
//echo ' $startDate = ' . $startDate . ' $endDate = ' . $endDate; exit;
|
|
|
337 |
|
|
|
338 |
$graph = [];
|
|
|
339 |
|
|
|
340 |
|
|
|
341 |
$dt = \DateTime::createFromFormat('Y-m-d', $startDate);
|
|
|
342 |
|
|
|
343 |
do {
|
|
|
344 |
$tempDate = $dt->format('Y-m-d');
|
|
|
345 |
$graph[ $tempDate ] = ['label' => $dt->format('d/m/Y'), 'value' => 0];
|
|
|
346 |
$dt->add(new \DateInterval('P1D'));
|
|
|
347 |
|
|
|
348 |
|
|
|
349 |
} while($endDate > $tempDate);
|
|
|
350 |
|
|
|
351 |
$discoveryContactBlackListMapper = DiscoveryContactBlackListMapper::getInstance($this->adapter);
|
|
|
352 |
$contact_ids = $discoveryContactBlackListMapper->fetchAllContactIdsByCompanyId($currentCompany->id);
|
|
|
353 |
|
|
|
354 |
|
|
|
355 |
|
|
|
356 |
$queryMapper = QueryMapper::getInstance($this->adapter);
|
|
|
357 |
|
|
|
358 |
|
|
|
359 |
$discoveryContactInteractionTypeMapper = DiscoveryContactInteractionTypeMapper::getInstance($this->adapter);
|
|
|
360 |
$discoveryContactInteractionType = $discoveryContactInteractionTypeMapper->fetchOneDefaultByCompanyId($currentCompany->id);
|
|
|
361 |
if(!$discoveryContactInteractionType) {
|
|
|
362 |
$discoveryContactInteractionType = $discoveryContactInteractionTypeMapper->fetchOneFirstActiveByCompanyId($currentCompany->id);
|
|
|
363 |
}
|
|
|
364 |
|
|
|
365 |
|
|
|
366 |
|
|
|
367 |
$select = $queryMapper->getSql()->select(DiscoveryContactInteractionMapper::_TABLE);
|
|
|
368 |
$select->columns([
|
|
|
369 |
'total' => new Expression('COUNT(*)'),
|
|
|
370 |
'date' => new Expression('DATE(added_on)'),
|
|
|
371 |
]);
|
|
|
372 |
$select->where->equalTo('interaction_type_id', $discoveryContactInteractionType->id);
|
|
|
373 |
$select->where->between( new Expression('DATE(added_on)') , $startDate, $endDate);
|
|
|
374 |
$select->where->equalTo('user_id', $user->id);
|
|
|
375 |
|
|
|
376 |
if($contact_ids) {
|
|
|
377 |
$select->where->notIn('id', $contact_ids);
|
|
|
378 |
}
|
|
|
379 |
|
|
|
380 |
|
|
|
381 |
$select->group([ new Expression('DATE(added_on)'), 'user_id']);
|
|
|
382 |
$select->order('date ASC');
|
|
|
383 |
|
|
|
384 |
$records = $queryMapper->fetchAll($select);
|
|
|
385 |
foreach($records as $record)
|
|
|
386 |
{
|
|
|
387 |
$graph[ $record['date'] ][ 'value' ] = $record['total'];
|
|
|
388 |
}
|
|
|
389 |
|
|
|
390 |
$records = array_values($graph);
|
|
|
391 |
|
|
|
392 |
$spreadsheet = new Spreadsheet();
|
|
|
393 |
$spreadsheet->getProperties()->setTitle('Relevamiento de Contacto por usuario');
|
|
|
394 |
|
|
|
395 |
$spreadsheet->setActiveSheetIndex(0);
|
|
|
396 |
|
|
|
397 |
$dt = \DateTime::createFromFormat('Y-m-d', $startDate);
|
|
|
398 |
$spreadsheet->getActiveSheet()->SetCellValue('A1', 'Desde:');
|
|
|
399 |
$spreadsheet->getActiveSheet()->SetCellValue('B1', $dt->format('d/m/Y'));
|
|
|
400 |
|
|
|
401 |
|
|
|
402 |
$dt = \DateTime::createFromFormat('Y-m-d', $startDate);
|
|
|
403 |
$spreadsheet->getActiveSheet()->SetCellValue('C1', 'Hasta:');
|
|
|
404 |
$spreadsheet->getActiveSheet()->SetCellValue('D1', $dt->format('d/m/Y'));
|
|
|
405 |
|
|
|
406 |
|
|
|
407 |
$spreadsheet->getActiveSheet()->SetCellValue('A2', trim($user->first_name . ' ' . $user->last_name) . ' (' . $user->email . ')');
|
|
|
408 |
$spreadsheet->getActiveSheet()->setMergeCells(['A2', 'B2', 'C2', 'D2']);
|
|
|
409 |
|
|
|
410 |
$spreadsheet->getActiveSheet()->SetCellValue('A4', 'Fecha');
|
|
|
411 |
$spreadsheet->getActiveSheet()->SetCellValue('B4', 'Total');
|
|
|
412 |
|
|
|
413 |
|
|
|
414 |
|
|
|
415 |
$row = 5;
|
|
|
416 |
foreach($records as $record)
|
|
|
417 |
{
|
|
|
418 |
$spreadsheet->getActiveSheet()->setCellValue('A' . $row, $record['label']);
|
|
|
419 |
$spreadsheet->getActiveSheet()->setCellValue('B' . $row, $record['value']);
|
|
|
420 |
$row++;
|
|
|
421 |
}
|
|
|
422 |
|
|
|
423 |
|
|
|
424 |
|
|
|
425 |
$fileName = 'reporte_relevamiento_de_contact_progreso_por_dia_por_usuario_' . date('d-m-Y-h-i-a', time()) . '.xls';
|
|
|
426 |
$tempFilename = tempnam(sys_get_temp_dir(), 'reporte_relevamiento_de_contact_progreso_por_dia_por_usuario_' . time());
|
|
|
427 |
|
|
|
428 |
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
|
|
|
429 |
$writer->save($tempFilename);
|
|
|
430 |
|
|
|
431 |
$content = file_get_contents($tempFilename);
|
|
|
432 |
@unlink($tempFilename);
|
|
|
433 |
|
|
|
434 |
return new JsonModel([
|
|
|
435 |
'success' => true,
|
|
|
436 |
'data' => [
|
|
|
437 |
'content' => base64_encode($content),
|
|
|
438 |
'basename' => $fileName
|
|
|
439 |
|
|
|
440 |
]
|
|
|
441 |
]);
|
|
|
442 |
|
|
|
443 |
}
|
|
|
444 |
|
|
|
445 |
return new JsonModel([
|
|
|
446 |
'success' => false,
|
|
|
447 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
448 |
]);
|
|
|
449 |
}
|
|
|
450 |
}
|