src/Controller/IndexController.php line 21

Open in your IDE?
  1. <?php
  2. namespace FMS\eLibreria\CmsBundle\Controller;
  3. use FMS\eLibreria\CoreBundle\Model\Entity\Buy;
  4. use FMS\eLibreria\CoreBundle\Model\Entity\StatisticData;
  5. use FMS\eLibreria\CoreBundle\Model\Object\DateRange;
  6. use FMS\eLibreria\CoreBundle\Model\Repository\BuyRepository;
  7. class IndexController extends CmsController {
  8. const INDEX_KEY_STATISTICS_DATA = "statistics_data";
  9. const INDEX_KEY_BUYS_RETURN_ON_STATISTICS = "buys_return_on_statistics";
  10. const INDEX_KEY_LAST_3_MONTHS_STATISTICS_DATA = "last_3_months_statistics_data";
  11. const INDEX_KEY_MONTH = "month";
  12. const INDEX_KEY_ANNUAL = "annual";
  13. const INDEX_KEY_DATA = "data";
  14. const INDEX_KEY_BY_PROVIDERS = "by_providers";
  15. const INDEX_KEY_TOTAL = "total";
  16. public function index(){
  17. $options = $this->getBasicOptions();
  18. $statisticsService = $this->getStatistics();
  19. //Estadisticas
  20. $statisticsDataMonth = $statisticsService->getStatisticsMonth();
  21. $statisticsDataAnnual = new StatisticData();
  22. $billed = 0.0;
  23. $costProviders = 0.0;
  24. $costItemsAdquired = 0.0;
  25. $costPromos = 0.0;
  26. $taxes = 0.0;
  27. $totalCost = 0.0;
  28. $profit = 0.0;
  29. /** @var StatisticData $statisticDataAn */
  30. foreach($statisticsService->getStatisticsAnnual() as $statisticDataAn){
  31. $billed += $statisticDataAn->getBilled();
  32. $costProviders += $statisticDataAn->getCostProviders();
  33. $costItemsAdquired += $statisticDataAn->getCostItemsAdquired();
  34. $costPromos += $statisticDataAn->getCostPromos();
  35. $taxes += $statisticDataAn->getTaxes();
  36. $totalCost += $statisticDataAn->getTotalCost();
  37. $profit += $statisticDataAn->getProfit();
  38. }
  39. $statisticsDataAnnual->setBilled($billed);
  40. $statisticsDataAnnual->setCostProviders($costProviders);
  41. $statisticsDataAnnual->setCostItemsAdquired($costItemsAdquired);
  42. $statisticsDataAnnual->setCostPromos($costPromos);
  43. $statisticsDataAnnual->setTaxes($taxes);
  44. $statisticsDataAnnual->setTotalCost($totalCost);
  45. $statisticsDataAnnual->setProfit($profit);
  46. $now = new \DateTime();
  47. $start = \DateTime::createFromFormat("d/m/Y H:i:s", sprintf("1/%s 00:00:00", $now->format("m/Y")));
  48. $end = \DateTime::createFromFormat("d/m/Y H:i:s", sprintf("%s 23:59:59", $now->format("t/m/Y")));
  49. $monthRange = new DateRange($start, $end);
  50. //Devoluciones del mes
  51. /** @var BuyRepository $buyRepository */
  52. $buyRepository = $this->getEntityManager()->getRepository(Buy::class);
  53. $buysToReturnThisMonth = $buyRepository->findBuysReturnOnRange($monthRange);
  54. $byProviders = array();
  55. /** @var Buy $buy */
  56. foreach($buysToReturnThisMonth as $buy){
  57. $provider = $buy->getProvider();
  58. // A purchase without a contract is counted under «Sin contrato», never a 500 (change merch-09; M-88).
  59. $hashProvider = null === $provider ? "" : $provider->getHash();
  60. $providerData = array(
  61. self::INDEX_KEY_DATA => $provider ?? array("nameStoreLegal" => $this->getTranslator()->trans("buys.without_contract", array(), "cms")),
  62. self::INDEX_KEY_TOTAL => 0,
  63. );
  64. if(array_key_exists($hashProvider, $byProviders)){
  65. $providerData = $byProviders[$hashProvider];
  66. }
  67. $providerData[self::INDEX_KEY_TOTAL]++;
  68. $byProviders[$hashProvider] = $providerData;
  69. }
  70. $statisticsData = array(
  71. self::INDEX_KEY_MONTH => $statisticsDataMonth,
  72. self::INDEX_KEY_ANNUAL => $statisticsDataAnnual,
  73. );
  74. //Link al listado de albaranes filtrado:
  75. $filters = array(
  76. "returnOn" => $now->format("m/Y")
  77. );
  78. $filter = $this->getFilters()->registerFilter($filters);
  79. $linkToBuys = $this->generateUrl("cms_utools_store_buys_index",
  80. array(
  81. self::QUERY_KEY_FILTERS => $filter->getHash()
  82. ));
  83. $buysToReturnStatistics = array(
  84. self::INDEX_KEY_BY_PROVIDERS => $byProviders,
  85. self::INDEX_KEY_TOTAL => count($buysToReturnThisMonth),
  86. "link" => $linkToBuys,
  87. );
  88. //Evolución últimos 3 meses
  89. $day3MonthsAgo = (clone $now)->sub(new \DateInterval("P3M"));
  90. $start3Months = \DateTime::createFromFormat("d/m/Y H:i:s", sprintf("1/%s 00:00:00", $day3MonthsAgo->format("m/Y")));
  91. $last3Months = new DateRange($start3Months,$end);
  92. $histoLast3Months = $statisticsService->getStatisticsOnRange($last3Months);
  93. $options[self::INDEX_KEY_STATISTICS_DATA] = $statisticsData;
  94. $options[self::INDEX_KEY_BUYS_RETURN_ON_STATISTICS] = $buysToReturnStatistics;
  95. $options[self::INDEX_KEY_LAST_3_MONTHS_STATISTICS_DATA] = $histoLast3Months;
  96. return $this->render("index\index.html.twig", $options);
  97. }
  98. }