Show expired lot sum in parts table in parenthesis

This commit is contained in:
Jan Böhmer 2022-11-28 23:59:01 +01:00
parent 1f6f39172f
commit bd12185b44
2 changed files with 32 additions and 1 deletions

View file

@ -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(' <span title="%s" class="text-muted">(+%s)</span>',
$this->translator->trans('part_lots.is_expired'),
$this->amountFormatter->format($expiredAmount, $context->getPartUnit()));
}
return $ret;
},
'orderField' => 'amountSum'
])

View file

@ -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.