From bd12185b4450a632086e8976e8e69966f91eb9c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Mon, 28 Nov 2022 23:59:01 +0100 Subject: [PATCH] Show expired lot sum in parts table in parenthesis --- src/DataTables/PartsDataTable.php | 13 ++++++++++++- src/Entity/Parts/PartTraits/InstockTrait.php | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/DataTables/PartsDataTable.php b/src/DataTables/PartsDataTable.php index f87cb219..46ad8cf7 100644 --- a/src/DataTables/PartsDataTable.php +++ b/src/DataTables/PartsDataTable.php @@ -210,8 +210,19 @@ final class PartsDataTable implements DataTableTypeInterface 'label' => $this->translator->trans('part.table.amount'), 'render' => function ($value, Part $context) { $amount = $context->getAmountSum(); + $expiredAmount = $context->getExpiredAmountSum(); - return $this->amountFormatter->format($amount, $context->getPartUnit()); + $ret = $this->amountFormatter->format($amount, $context->getPartUnit()); + + //If we have expired lots, we show them in parentheses behind + if ($expiredAmount > 0) { + $ret .= sprintf(' (+%s)', + $this->translator->trans('part_lots.is_expired'), + $this->amountFormatter->format($expiredAmount, $context->getPartUnit())); + } + + + return $ret; }, 'orderField' => 'amountSum' ]) diff --git a/src/Entity/Parts/PartTraits/InstockTrait.php b/src/Entity/Parts/PartTraits/InstockTrait.php index f2d80559..1672fae9 100644 --- a/src/Entity/Parts/PartTraits/InstockTrait.php +++ b/src/Entity/Parts/PartTraits/InstockTrait.php @@ -193,6 +193,26 @@ trait InstockTrait return round($sum); } + /** + * Returns the summed amount of all part lots that are expired. If no part lots are expired 0 is returned. + * @return float + */ + public function getExpiredAmountSum(): float + { + $sum = 0.0; + foreach ($this->getPartLots() as $lot) { + if ($lot->isExpired() && !$lot->isInstockUnknown()) { + $sum += $lot->getAmount(); + } + } + + if ($this->useFloatAmount()) { + return $sum; + } + + return round($sum); + } + /** * Set the minimum amount of parts that have to be instock. * See getPartUnit() for the associated unit.