Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 KB | None | 0 0
  1.     /**
  2.      * @Route("/reports")
  3.      * @Method("GET")
  4.      */
  5.     public function reportsAction(Request $request)
  6.     {
  7.         if (!$request->isXmlHttpRequest()) {
  8.             return new Response(null, 400);
  9.         }
  10.  
  11.         $context = $request->get('context');
  12.  
  13.         $declarationsRepository = $this->getDoctrine()->getRepository('AdpHcBundle:Declaration');
  14.  
  15.         // Get Declarations of the selected Period
  16.         $declarationsOfThisYear = $declarationsRepository->findAllFilledByContext($context);
  17.  
  18.         // Sort these by Hotel
  19.         $declarationsOfThisYearSortedByHotel = [];
  20.  
  21.         foreach ($declarationsOfThisYear as $declaration) {
  22.             $declarationsOfThisYearSortedByHotel[$declaration->getHotel()->getId()][] = $declaration;
  23.         }
  24.  
  25.         // Get Declarations of the previous year
  26.         $declarationsOfPreviousYear = $declarationsRepository->findAllFilledFromPreviousYearByContext($context);
  27.  
  28.         // Sort these by Hotel
  29.         $declarationsOfPreviousYearSortedByHotel = [];
  30.  
  31.         foreach ($declarationsOfPreviousYear as $declaration) {
  32.             $declarationsOfPreviousYearSortedByHotel[$declaration->getHotel()->getId()][] = $declaration;
  33.         }
  34.  
  35.         // Ready exploitable data
  36.         $results = [];
  37.  
  38.         $calculator = $this->get('adp_hc.reporting.calculator');
  39.  
  40.         // Get Calculated of the selected Period
  41.         foreach ($declarationsOfThisYearSortedByHotel as $hotel => $declarations) {
  42.             $results['thisYear'][$hotel] = $calculator->calculate($declarations, $context->getPeriod());
  43.         }
  44.  
  45.         // Get Calculated of the previous year
  46.         foreach ($declarationsOfPreviousYearSortedByHotel as $hotel => $declarations) {
  47.             $results['lastYear'][$hotel] = $calculator->calculate($declarations, $context->getPeriod());
  48.         }
  49.  
  50.         // Get Calculated of the diff between selected Period and its previous year
  51.         if (array_key_exists('thisYear', $results) && array_key_exists('lastYear', $results)) {
  52.             foreach ($results['thisYear'] as $hotel => $calculated) {
  53.                 if (array_key_exists($hotel, $results['lastYear'])) {
  54.                     $results['diff'][$hotel] = $calculated->diff($results['lastYear'][$hotel]);
  55.                 }
  56.             }
  57.         }
  58.  
  59.         return new JsonResponse($results);
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement