From 226fe2f4baecd03316c7df2164d91516c367216d Mon Sep 17 00:00:00 2001 From: Hannes Matuschek Date: Sun, 30 Aug 2020 13:57:33 +0200 Subject: [PATCH] Added 'low stock' statistics. --- src/Repository/PartRepository.php | 28 +++++++++++++++++++++++ src/Services/StatisticsHelper.php | 11 +++++++++ templates/Statistics/statistics.html.twig | 4 ++++ 3 files changed, 43 insertions(+) diff --git a/src/Repository/PartRepository.php b/src/Repository/PartRepository.php index 1d5c1a7c..d6432a4c 100644 --- a/src/Repository/PartRepository.php +++ b/src/Repository/PartRepository.php @@ -44,6 +44,7 @@ namespace App\Repository; use App\Entity\Parts\PartLot; use Doctrine\ORM\QueryBuilder; +use Doctrine\ORM\Query\ResultSetMapping; class PartRepository extends NamedDBElementRepository { @@ -84,4 +85,31 @@ class PartRepository extends NamedDBElementRepository return (int) ($query->getSingleScalarResult() ?? 0); } + + /** + * Gets the number of parts that are low in stock. + * + * That is, it's total amount is smaller than the minimal amount. + * + * @throws \Doctrine\ORM\NoResultException + * @throws \Doctrine\ORM\NonUniqueResultException + */ + public function getPartCountWithLowStock(): int + { + /* Query to get total amount for every part. + * As sub-queries are not supported -> resort to native SQL request.*/ + $rsm = new ResultSetMapping; + $rsm->addScalarResult("cnt", "count", 'integer'); + $query = $this->getEntityManager()->createNativeQuery(' + SELECT COUNT(DISTINCT parts.id) as cnt FROM parts + INNER JOIN ( + SELECT parts.id FROM part_lots + INNER JOIN parts ON parts.id=part_lots.id_part + GROUP BY parts.id + HAVING SUM(part_lots.amount)getSingleScalarResult() ?? 0); + } } diff --git a/src/Services/StatisticsHelper.php b/src/Services/StatisticsHelper.php index 2afa3068..a22b1c7d 100644 --- a/src/Services/StatisticsHelper.php +++ b/src/Services/StatisticsHelper.php @@ -80,6 +80,17 @@ class StatisticsHelper return $this->part_repo->getPartsCountWithPrice(); } + /** + * Returns the number of all parts with low stock. + * + * @throws \Doctrine\ORM\NoResultException + * @throws \Doctrine\ORM\NonUniqueResultException + */ + public function getPartCountWithLowStock(): int + { + return $this->part_repo->getPartCountWithLowStock(); + } + /** * Returns the number of datastructures for the given type. */ diff --git a/templates/Statistics/statistics.html.twig b/templates/Statistics/statistics.html.twig index 5cc2f259..622856c6 100644 --- a/templates/Statistics/statistics.html.twig +++ b/templates/Statistics/statistics.html.twig @@ -48,6 +48,10 @@ {% trans %}statistics.parts_with_price{% endtrans %} {{ helper.partsCountWithPrice }} + + {% trans %}statistics.parts_low_stock{% endtrans %} + {{ helper.partCountWithLowStock }} +