partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 954887af44d1eb9e79475b53271455a9c3408dda
parent 693ac3256c628d480b26d786a5929887a7868ecc
Author: Felicitus <felicitus@felicitus.org>
Date:   Sat,  7 Nov 2015 19:35:02 +0100

Refactored current statistics call, added function to create statistic snapshots

Diffstat:
Msrc/PartKeepr/FrontendBundle/Resources/public/js/Components/Statistics/SummaryStatisticsPanel.js | 2+-
Asrc/PartKeepr/StatisticBundle/Command/CreateStatisticSnapshotCommand.php | 22++++++++++++++++++++++
Msrc/PartKeepr/StatisticBundle/Services/StatisticService.php | 31++++++++++++++++++++++++++++++-
3 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/src/PartKeepr/FrontendBundle/Resources/public/js/Components/Statistics/SummaryStatisticsPanel.js b/src/PartKeepr/FrontendBundle/Resources/public/js/Components/Statistics/SummaryStatisticsPanel.js @@ -47,7 +47,7 @@ Ext.define('PartKeepr.SummaryStatisticsPanel', { '<table>', '<tpl for="units">', '<tr>', - '<td style="width: 200px;" class="{[xindex % 2 === 0 ? "e" : "o"]}">{name}</td>', + '<td style="width: 200px;" class="{[xindex % 2 === 0 ? "e" : "o"]}">{partMeasurementUnit.name}</td>', '<td style="width: 200px;" class="{[xindex % 2 === 0 ? "e" : "o"]}">{stockLevel}</td>', '</tr>', '</tpl>', diff --git a/src/PartKeepr/StatisticBundle/Command/CreateStatisticSnapshotCommand.php b/src/PartKeepr/StatisticBundle/Command/CreateStatisticSnapshotCommand.php @@ -0,0 +1,22 @@ +<?php +namespace PartKeepr\StatisticBundle\Command; + +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class CreateStatisticSnapshotCommand extends ContainerAwareCommand +{ + public function configure() + { + parent::configure(); + $this->setName('partkeepr:statistics:create-snapshot'); + $this->setDescription("Creates a statistic snapshot"); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $this->getContainer()->get("partkeepr.statistic.service")->createStatisticSnapshot(); + + } +} diff --git a/src/PartKeepr/StatisticBundle/Services/StatisticService.php b/src/PartKeepr/StatisticBundle/Services/StatisticService.php @@ -3,6 +3,8 @@ namespace PartKeepr\StatisticBundle\Services; use Doctrine\ORM\EntityManager; use PartKeepr\PartBundle\Entity\PartMeasurementUnit; +use PartKeepr\StatisticBundle\Entity\StatisticSnapshot; +use PartKeepr\StatisticBundle\Entity\StatisticSnapshotUnit; class StatisticService { @@ -77,7 +79,7 @@ class StatisticService */ public function getUnitCounts() { - $dql = 'SELECT SUM(p.stockLevel) AS stockLevel, pu.name AS name FROM '; + $dql = 'SELECT SUM(p.stockLevel) AS stockLevel, pu AS partMeasurementUnit FROM '; $dql .= 'PartKeepr\PartBundle\Entity\PartMeasurementUnit pu LEFT JOIN pu.parts p GROUP BY pu.id'; return $this->entityManager->createQuery($dql)->getArrayResult(); @@ -175,4 +177,31 @@ class StatisticService return $aRecords; } + + public function createStatisticSnapshot () { + + $snapshot = new StatisticSnapshot(); + $snapshot->setParts($this->getPartCount()); + $snapshot->setCategories($this->getPartCategoryCount()); + + $unitCounts = $this->getUnitCounts(); + $partUnitRepository = $this->entityManager->getRepository("PartKeeprPartBundle:PartMeasurementUnit"); + + foreach ($unitCounts as $unitCount) { + $snapshotUnit = new StatisticSnapshotUnit(); + $snapshotUnit->setPartUnit($partUnitRepository->findOneBy(array("id" => $unitCount["partMeasurementUnit"]["id"]))); + $snapshotUnit->setStatisticSnapshot($snapshot); + + if ($unitCount["stockLevel"] !== null) { + $snapshotUnit->setStockLevel($unitCount["stockLevel"]); + } else { + $snapshotUnit->setStockLevel(0); + } + + $snapshot->getUnits()->add($snapshotUnit); + } + + $this->entityManager->persist($snapshot); + $this->entityManager->flush(); + } }