1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Api\Controller; |
4
|
|
|
|
5
|
|
|
use Zend\Db\Adapter\AdapterInterface; |
6
|
|
|
use Zend\Mvc\Controller\AbstractRestfulController; |
7
|
|
|
use Zend\View\Model\JsonModel; |
8
|
|
|
use Zend\Validator\StaticValidator; |
9
|
|
|
use Api\Helper\Utils; |
10
|
|
|
use Api\Entity\Emission; |
11
|
|
|
use Api\Entity\Sector; |
12
|
|
|
|
13
|
|
|
class DistributionReportController extends AbstractRestfulController |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Entity manager. |
17
|
|
|
* @var Doctrine\ORM\EntityManager |
18
|
|
|
*/ |
19
|
|
|
private $entityManager; |
20
|
|
|
|
21
|
4 |
|
public function __construct($entityManager) |
22
|
|
|
{ |
23
|
4 |
|
$this->entityManager = $entityManager; |
24
|
4 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @SWG\Get( |
28
|
|
|
* path="/informe/distribucion-sectores/{year}", |
29
|
|
|
* @SWG\Response(response="200", description="Get distribution by Sectors") |
30
|
|
|
* ) |
31
|
|
|
*/ |
32
|
1 |
|
public function getWholeSectoralDistributionAction() |
33
|
|
|
{ |
34
|
1 |
|
$year = (int) $this->params()->fromRoute('year'); |
35
|
|
|
|
36
|
1 |
|
$response = []; |
37
|
|
|
|
38
|
1 |
|
$results = $this->entityManager->getRepository(Emission::class) |
39
|
1 |
|
->findSectorByYear($year); |
40
|
|
|
|
41
|
1 |
|
$i = 1; |
42
|
|
|
|
43
|
1 |
|
foreach ($results as $result) { |
44
|
1 |
|
$response['sector_'.$i][] = $result['name']; |
45
|
1 |
|
$response['sector_'.$i][] = $result['total']; |
46
|
1 |
|
$response['colores'][] = $result['color']; |
47
|
1 |
|
$response['descripciones'][] = $result['description']; |
48
|
1 |
|
$i++; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
return new JsonModel($response); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* DISTRIBUCION POR SECTOR |
56
|
|
|
*/ |
57
|
1 |
|
public function getSectoralDistributionAction() |
58
|
|
|
{ |
59
|
1 |
|
$year = (int) $this->params()->fromRoute('year'); |
60
|
1 |
|
$sector = (int) $this->params()->fromRoute('sector'); |
61
|
|
|
|
62
|
1 |
|
$response = []; |
63
|
1 |
|
$arrGraphData = []; |
64
|
|
|
|
65
|
1 |
|
$results = $this->entityManager->getRepository(Emission::class) |
66
|
1 |
|
->findActivityByYearAndSector($year, $sector); |
67
|
|
|
|
68
|
1 |
|
$totalActivities = 0; |
69
|
|
|
|
70
|
1 |
|
foreach ($results as $result) { |
71
|
|
|
$arrActividades = [ |
72
|
1 |
|
'label' => $result['name'], |
73
|
1 |
|
'value' => round($result['total'], 2) |
74
|
|
|
]; |
75
|
|
|
|
76
|
1 |
|
$totalActivities += $result['total']; |
77
|
|
|
|
78
|
|
|
// EN CADA ACTIVIDAD HAGO QUE HACER EL SEARCH DE LA SUBACTIVIDAD |
79
|
1 |
|
$results2 = $this->entityManager->getRepository(Emission::class) |
80
|
1 |
|
->findSubactivityByYearSectorAndActivity($year, $sector, $result['activity']); |
81
|
|
|
|
82
|
1 |
|
if (count($results2) > 0) { |
83
|
1 |
|
$arrSubactividades = []; |
84
|
|
|
|
85
|
1 |
|
$i = 0; |
86
|
|
|
|
87
|
1 |
|
foreach ($results2 as $result2) { |
88
|
1 |
|
$arrSubactividades[$i] = [ |
89
|
1 |
|
'label' => $result2['name'], |
90
|
1 |
|
'value' => round($result2['total'], 2) |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
|
94
|
1 |
|
if ($sector == 3 || $sector == 4) { |
95
|
|
|
continue; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// TODO: ACA EN CADA SUBACTIVIDAD TENDRIA QUE HACER EL SEARCH DE LA CATEGORIA |
99
|
1 |
|
$results3 = $this->entityManager->getRepository(Emission::class) |
100
|
1 |
|
->findCategoryByYearSectorActivityAndSubactivity($year, $sector, $result['activity'], $result2['subactivity']); |
101
|
|
|
|
102
|
1 |
|
if (count($results3) > 0) { |
103
|
1 |
|
$arrCategorias = []; |
104
|
|
|
|
105
|
1 |
|
foreach ($results3 as $result3) { |
106
|
1 |
|
$arrCategorias[] = [ |
107
|
1 |
|
'label'=>$result3['name'], |
108
|
1 |
|
'value'=>round($result3['total'], 2) |
109
|
|
|
]; |
110
|
|
|
} |
111
|
1 |
|
$arrSubactividades[$i]['inner'] = $arrCategorias; |
112
|
|
|
} |
113
|
1 |
|
$i++; |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
$arrActividades['inner'] = $arrSubactividades; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
$arrGraphData[] = $arrActividades; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
1 |
|
$response['graph_data'] = $arrGraphData; |
124
|
1 |
|
$response['totalActividades'] = $totalActivities; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* BUSCO LA INFO DEL SECTOR |
128
|
|
|
*/ |
129
|
1 |
|
$results = $this->entityManager->getRepository(Sector::class)->getSector($sector); |
130
|
|
|
|
131
|
1 |
|
$response['sector'] = $results[0]; |
132
|
|
|
|
133
|
1 |
|
return new JsonModel($response); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* |
138
|
|
|
*/ |
139
|
1 |
|
public function getGasesDistributionAction() |
140
|
|
|
{ |
141
|
1 |
|
$year = (int) $this->params()->fromRoute('year'); |
142
|
|
|
|
143
|
1 |
|
$response = []; |
144
|
|
|
|
145
|
1 |
|
$results = $this->entityManager->getRepository(Emission::class)->findGasesByYear($year); |
146
|
|
|
|
147
|
1 |
|
$response['gases'][] = 'x'; |
148
|
1 |
|
$response['valores'][] = 'Gases'; |
149
|
|
|
|
150
|
1 |
|
foreach ($results as $result) { |
151
|
1 |
|
$response['gases'][] = (strpos($result['name'], ',')) ? '"'.$result['name'].'"' : $result['name']; |
152
|
1 |
|
$response['valores'][] = round($result['total']); |
153
|
1 |
|
$response['colores'][] = $result['color']; |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
return new JsonModel($response); |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
public function getSectoralGasesDistributionAction() |
160
|
|
|
{ |
161
|
1 |
|
$year = (int) $this->params()->fromRoute('year'); |
162
|
|
|
|
163
|
1 |
|
$response = []; |
164
|
|
|
|
165
|
1 |
|
$arrGases = $this->entityManager->getRepository(Emission::class)->findGasesByYear($year); |
166
|
|
|
|
167
|
1 |
|
$arrSectores = $this->entityManager->getRepository(Sector::class) |
168
|
1 |
|
->getSectorsOrderedyName(); |
169
|
|
|
|
170
|
1 |
|
$arr = $this->entityManager->getRepository(Emission::class) |
171
|
1 |
|
->findGasesAndSectorByYear($year); |
172
|
|
|
|
173
|
1 |
|
$column = 2; |
174
|
|
|
|
175
|
1 |
|
foreach ($arrSectores as $sector) { |
176
|
1 |
|
$response['column_'.$column][] = $sector['name']; |
177
|
|
|
|
178
|
1 |
|
foreach ($arrGases as $gas) { |
179
|
1 |
|
$response['column_'.$column][] = Utils::returnSectorGas($arr, $sector['name'], $gas['name']); |
180
|
|
|
} |
181
|
1 |
|
$column++; |
182
|
|
|
} |
183
|
|
|
|
184
|
1 |
|
$arrReturnGases = ['x']; |
185
|
|
|
|
186
|
1 |
|
foreach ($arrGases as $gas) { |
187
|
1 |
|
$arrReturnGases[] = $gas['name']; |
188
|
|
|
} |
189
|
|
|
|
190
|
1 |
|
$response['column_1'] = $arrReturnGases; |
191
|
|
|
|
192
|
1 |
|
return new JsonModel($response); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|