Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @Route("/reports")
- * @Method("GET")
- */
- public function reportsAction(Request $request)
- {
- if (!$request->isXmlHttpRequest()) {
- return new Response(null, 400);
- }
- $context = $request->get('context');
- $declarationsRepository = $this->getDoctrine()->getRepository('AdpHcBundle:Declaration');
- // Get Declarations of the selected Period
- $declarationsOfThisYear = $declarationsRepository->findAllFilledByContext($context);
- // Sort these by Hotel
- $declarationsOfThisYearSortedByHotel = [];
- foreach ($declarationsOfThisYear as $declaration) {
- $declarationsOfThisYearSortedByHotel[$declaration->getHotel()->getId()][] = $declaration;
- }
- // Get Declarations of the previous year
- $declarationsOfPreviousYear = $declarationsRepository->findAllFilledFromPreviousYearByContext($context);
- // Sort these by Hotel
- $declarationsOfPreviousYearSortedByHotel = [];
- foreach ($declarationsOfPreviousYear as $declaration) {
- $declarationsOfPreviousYearSortedByHotel[$declaration->getHotel()->getId()][] = $declaration;
- }
- // Ready exploitable data
- $results = [];
- $calculator = $this->get('adp_hc.reporting.calculator');
- // Get Calculated of the selected Period
- foreach ($declarationsOfThisYearSortedByHotel as $hotel => $declarations) {
- $results['thisYear'][$hotel] = $calculator->calculate($declarations, $context->getPeriod());
- }
- // Get Calculated of the previous year
- foreach ($declarationsOfPreviousYearSortedByHotel as $hotel => $declarations) {
- $results['lastYear'][$hotel] = $calculator->calculate($declarations, $context->getPeriod());
- }
- // Get Calculated of the diff between selected Period and its previous year
- if (array_key_exists('thisYear', $results) && array_key_exists('lastYear', $results)) {
- foreach ($results['thisYear'] as $hotel => $calculated) {
- if (array_key_exists($hotel, $results['lastYear'])) {
- $results['diff'][$hotel] = $calculated->diff($results['lastYear'][$hotel]);
- }
- }
- }
- return new JsonResponse($results);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement