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