<?php
namespace FMS\eLibreria\CmsBundle\Controller;
use FMS\eLibreria\CoreBundle\Model\Entity\Buy;
use FMS\eLibreria\CoreBundle\Model\Entity\StatisticData;
use FMS\eLibreria\CoreBundle\Model\Object\DateRange;
use FMS\eLibreria\CoreBundle\Model\Repository\BuyRepository;
class IndexController extends CmsController {
const INDEX_KEY_STATISTICS_DATA = "statistics_data";
const INDEX_KEY_BUYS_RETURN_ON_STATISTICS = "buys_return_on_statistics";
const INDEX_KEY_LAST_3_MONTHS_STATISTICS_DATA = "last_3_months_statistics_data";
const INDEX_KEY_MONTH = "month";
const INDEX_KEY_ANNUAL = "annual";
const INDEX_KEY_DATA = "data";
const INDEX_KEY_BY_PROVIDERS = "by_providers";
const INDEX_KEY_TOTAL = "total";
public function index(){
$options = $this->getBasicOptions();
$statisticsService = $this->getStatistics();
//Estadisticas
$statisticsDataMonth = $statisticsService->getStatisticsMonth();
$statisticsDataAnnual = new StatisticData();
$billed = 0.0;
$costProviders = 0.0;
$costItemsAdquired = 0.0;
$costPromos = 0.0;
$taxes = 0.0;
$totalCost = 0.0;
$profit = 0.0;
/** @var StatisticData $statisticDataAn */
foreach($statisticsService->getStatisticsAnnual() as $statisticDataAn){
$billed += $statisticDataAn->getBilled();
$costProviders += $statisticDataAn->getCostProviders();
$costItemsAdquired += $statisticDataAn->getCostItemsAdquired();
$costPromos += $statisticDataAn->getCostPromos();
$taxes += $statisticDataAn->getTaxes();
$totalCost += $statisticDataAn->getTotalCost();
$profit += $statisticDataAn->getProfit();
}
$statisticsDataAnnual->setBilled($billed);
$statisticsDataAnnual->setCostProviders($costProviders);
$statisticsDataAnnual->setCostItemsAdquired($costItemsAdquired);
$statisticsDataAnnual->setCostPromos($costPromos);
$statisticsDataAnnual->setTaxes($taxes);
$statisticsDataAnnual->setTotalCost($totalCost);
$statisticsDataAnnual->setProfit($profit);
$now = new \DateTime();
$start = \DateTime::createFromFormat("d/m/Y H:i:s", sprintf("1/%s 00:00:00", $now->format("m/Y")));
$end = \DateTime::createFromFormat("d/m/Y H:i:s", sprintf("%s 23:59:59", $now->format("t/m/Y")));
$monthRange = new DateRange($start, $end);
//Devoluciones del mes
/** @var BuyRepository $buyRepository */
$buyRepository = $this->getEntityManager()->getRepository(Buy::class);
$buysToReturnThisMonth = $buyRepository->findBuysReturnOnRange($monthRange);
$byProviders = array();
/** @var Buy $buy */
foreach($buysToReturnThisMonth as $buy){
$provider = $buy->getProvider();
// A purchase without a contract is counted under «Sin contrato», never a 500 (change merch-09; M-88).
$hashProvider = null === $provider ? "" : $provider->getHash();
$providerData = array(
self::INDEX_KEY_DATA => $provider ?? array("nameStoreLegal" => $this->getTranslator()->trans("buys.without_contract", array(), "cms")),
self::INDEX_KEY_TOTAL => 0,
);
if(array_key_exists($hashProvider, $byProviders)){
$providerData = $byProviders[$hashProvider];
}
$providerData[self::INDEX_KEY_TOTAL]++;
$byProviders[$hashProvider] = $providerData;
}
$statisticsData = array(
self::INDEX_KEY_MONTH => $statisticsDataMonth,
self::INDEX_KEY_ANNUAL => $statisticsDataAnnual,
);
//Link al listado de albaranes filtrado:
$filters = array(
"returnOn" => $now->format("m/Y")
);
$filter = $this->getFilters()->registerFilter($filters);
$linkToBuys = $this->generateUrl("cms_utools_store_buys_index",
array(
self::QUERY_KEY_FILTERS => $filter->getHash()
));
$buysToReturnStatistics = array(
self::INDEX_KEY_BY_PROVIDERS => $byProviders,
self::INDEX_KEY_TOTAL => count($buysToReturnThisMonth),
"link" => $linkToBuys,
);
//Evolución últimos 3 meses
$day3MonthsAgo = (clone $now)->sub(new \DateInterval("P3M"));
$start3Months = \DateTime::createFromFormat("d/m/Y H:i:s", sprintf("1/%s 00:00:00", $day3MonthsAgo->format("m/Y")));
$last3Months = new DateRange($start3Months,$end);
$histoLast3Months = $statisticsService->getStatisticsOnRange($last3Months);
$options[self::INDEX_KEY_STATISTICS_DATA] = $statisticsData;
$options[self::INDEX_KEY_BUYS_RETURN_ON_STATISTICS] = $buysToReturnStatistics;
$options[self::INDEX_KEY_LAST_3_MONTHS_STATISTICS_DATA] = $histoLast3Months;
return $this->render("index\index.html.twig", $options);
}
}