mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-23 10:18:56 +02:00
Show formatted amount values on part info page.
This commit is contained in:
parent
f5ebce2a77
commit
5cc08af7b6
5 changed files with 147 additions and 6 deletions
|
@ -727,7 +727,7 @@ class Part extends AttachmentContainingDBElement
|
||||||
{
|
{
|
||||||
//TODO: Find a method to do this natively in SQL, the current method could be a bit slow
|
//TODO: Find a method to do this natively in SQL, the current method could be a bit slow
|
||||||
$sum = 0;
|
$sum = 0;
|
||||||
foreach($this->getPartLots() as $lot) {
|
foreach ($this->getPartLots() as $lot) {
|
||||||
//Dont use the instock value, if it is unkown
|
//Dont use the instock value, if it is unkown
|
||||||
if ($lot->isInstockUnknown()) {
|
if ($lot->isInstockUnknown()) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -736,7 +736,7 @@ class Part extends AttachmentContainingDBElement
|
||||||
$sum += $lot->getAmount();
|
$sum += $lot->getAmount();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$this->useFloatAmount()) {
|
if ($this->useFloatAmount()) {
|
||||||
return $sum;
|
return $sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
131
src/Services/AmountFormatter.php
Normal file
131
src/Services/AmountFormatter.php
Normal file
|
@ -0,0 +1,131 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* part-db version 0.1
|
||||||
|
* Copyright (C) 2005 Christoph Lechner
|
||||||
|
* http://www.cl-projects.de/
|
||||||
|
*
|
||||||
|
* part-db version 0.2+
|
||||||
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
|
||||||
|
* http://code.google.com/p/part-db/
|
||||||
|
*
|
||||||
|
* Part-DB Version 0.4+
|
||||||
|
* Copyright (C) 2016 - 2019 Jan Böhmer
|
||||||
|
* https://github.com/jbtronics
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Entity\Parts\MeasurementUnit;
|
||||||
|
use Symfony\Component\OptionsResolver\Options;
|
||||||
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This service formats an part amout using a Measurement Unit.
|
||||||
|
* @package App\Services
|
||||||
|
*/
|
||||||
|
class AmountFormatter
|
||||||
|
{
|
||||||
|
protected $siFormatter;
|
||||||
|
|
||||||
|
public function __construct(SIFormatter $siFormatter)
|
||||||
|
{
|
||||||
|
$this->siFormatter = $siFormatter;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configureOptions(OptionsResolver $resolver)
|
||||||
|
{
|
||||||
|
$resolver->setDefaults([
|
||||||
|
'show_prefix' => function (Options $options) {
|
||||||
|
if ($options['measurement_unit'] !== null) {
|
||||||
|
/** @var MeasurementUnit $unit */
|
||||||
|
$unit = $options['measurement_unit'];
|
||||||
|
return $unit->isUseSIPrefix();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
'is_integer' => function (Options $options) {
|
||||||
|
if ($options['measurement_unit'] !== null) {
|
||||||
|
/** @var MeasurementUnit $unit */
|
||||||
|
$unit = $options['measurement_unit'];
|
||||||
|
return $unit->isInteger();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
'unit' => function (Options $options) {
|
||||||
|
if ($options['measurement_unit'] !== null) {
|
||||||
|
/** @var MeasurementUnit $unit */
|
||||||
|
$unit = $options['measurement_unit'];
|
||||||
|
return $unit->getUnit();
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
},
|
||||||
|
'decimals' => 2,
|
||||||
|
'error_mapping' => [ '.' => 'value']
|
||||||
|
]);
|
||||||
|
|
||||||
|
$resolver->setAllowedTypes('decimals', 'int');
|
||||||
|
|
||||||
|
$resolver->setNormalizer('decimals', function (Options $options, $value) {
|
||||||
|
// If the unit is integer based, then dont show any decimals
|
||||||
|
if ($options['is_integer']) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return $value;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats the given value using the measurement unit and options.
|
||||||
|
* @param $value float|int The value that should be formatted. Must be numeric.
|
||||||
|
* @param MeasurementUnit|null $unit The measurement unit, whose unit symbol should be used for formatting.
|
||||||
|
* If set to null, it is assumed that the part amount is measured in pieces.
|
||||||
|
* @param array $options
|
||||||
|
* @return string The formatted string
|
||||||
|
* @throws \InvalidArgumentException Thrown if $value is not numeric.
|
||||||
|
*/
|
||||||
|
public function format($value, ?MeasurementUnit $unit = null, array $options = [])
|
||||||
|
{
|
||||||
|
if (!is_numeric($value)) {
|
||||||
|
throw new \InvalidArgumentException('$value must be an numeric value!');
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = (float) $value;
|
||||||
|
|
||||||
|
//Find out what options to use
|
||||||
|
$resolver = new OptionsResolver();
|
||||||
|
$resolver->setDefault('measurement_unit', $unit);
|
||||||
|
$this->configureOptions($resolver);
|
||||||
|
|
||||||
|
$options = $resolver->resolve($options);
|
||||||
|
|
||||||
|
if ($options['is_integer']) {
|
||||||
|
$value = round($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
//If the measurement unit uses a SI prefix format it that way.
|
||||||
|
if ($options['show_prefix']) {
|
||||||
|
return $this->siFormatter->format($value, $options['unit'], $options['decimals']);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Otherwise just output it
|
||||||
|
$format_string = '%.' . $options['decimals'] . 'f ' . $options['unit'];
|
||||||
|
return sprintf($format_string, $value);
|
||||||
|
}
|
||||||
|
}
|
|
@ -31,6 +31,8 @@ namespace App\Twig;
|
||||||
|
|
||||||
use App\Entity\Attachments\Attachment;
|
use App\Entity\Attachments\Attachment;
|
||||||
use App\Entity\Base\DBElement;
|
use App\Entity\Base\DBElement;
|
||||||
|
use App\Entity\Parts\MeasurementUnit;
|
||||||
|
use App\Services\AmountFormatter;
|
||||||
use App\Services\EntityURLGenerator;
|
use App\Services\EntityURLGenerator;
|
||||||
use App\Services\MoneyFormatter;
|
use App\Services\MoneyFormatter;
|
||||||
use App\Services\SIFormatter;
|
use App\Services\SIFormatter;
|
||||||
|
@ -51,11 +53,12 @@ class AppExtension extends AbstractExtension
|
||||||
protected $treeBuilder;
|
protected $treeBuilder;
|
||||||
protected $moneyFormatter;
|
protected $moneyFormatter;
|
||||||
protected $siformatter;
|
protected $siformatter;
|
||||||
|
protected $amountFormatter;
|
||||||
|
|
||||||
public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache,
|
public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache,
|
||||||
SerializerInterface $serializer, TreeBuilder $treeBuilder,
|
SerializerInterface $serializer, TreeBuilder $treeBuilder,
|
||||||
MoneyFormatter $moneyFormatter,
|
MoneyFormatter $moneyFormatter,
|
||||||
SIFormatter $SIFormatter)
|
SIFormatter $SIFormatter, AmountFormatter $amountFormatter)
|
||||||
{
|
{
|
||||||
$this->entityURLGenerator = $entityURLGenerator;
|
$this->entityURLGenerator = $entityURLGenerator;
|
||||||
$this->cache = $cache;
|
$this->cache = $cache;
|
||||||
|
@ -63,6 +66,7 @@ class AppExtension extends AbstractExtension
|
||||||
$this->treeBuilder = $treeBuilder;
|
$this->treeBuilder = $treeBuilder;
|
||||||
$this->moneyFormatter = $moneyFormatter;
|
$this->moneyFormatter = $moneyFormatter;
|
||||||
$this->siformatter = $SIFormatter;
|
$this->siformatter = $SIFormatter;
|
||||||
|
$this->amountFormatter = $amountFormatter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFilters()
|
public function getFilters()
|
||||||
|
@ -72,6 +76,7 @@ class AppExtension extends AbstractExtension
|
||||||
new TwigFilter('bbCode', [$this, 'parseBBCode'], ['pre_escape' => 'html', 'is_safe' => ['html']]),
|
new TwigFilter('bbCode', [$this, 'parseBBCode'], ['pre_escape' => 'html', 'is_safe' => ['html']]),
|
||||||
new TwigFilter('moneyFormat', [$this, 'formatCurrency']),
|
new TwigFilter('moneyFormat', [$this, 'formatCurrency']),
|
||||||
new TwigFilter('siFormat', [$this, 'siFormat']),
|
new TwigFilter('siFormat', [$this, 'siFormat']),
|
||||||
|
new TwigFilter('amountFormat', [$this, 'amountFormat'])
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,4 +132,9 @@ class AppExtension extends AbstractExtension
|
||||||
{
|
{
|
||||||
return $this->siformatter->format($value, $unit, $decimals);
|
return $this->siformatter->format($value, $unit, $decimals);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function amountFormat($value, ?MeasurementUnit $unit, array $options = [])
|
||||||
|
{
|
||||||
|
return $this->amountFormatter->format($value, $unit, $options);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,9 +25,9 @@
|
||||||
</h6> #}
|
</h6> #}
|
||||||
<h6><i class="fas fa-shapes fa-fw"></i>
|
<h6><i class="fas fa-shapes fa-fw"></i>
|
||||||
<span class="text-muted">
|
<span class="text-muted">
|
||||||
<span title="{% trans %}instock.label{% endtrans %}">{{ part.amountSum }}</span>
|
<span title="{% trans %}instock.label{% endtrans %}">{{ part.amountSum | amountFormat(part.partUnit) }}</span>
|
||||||
/
|
/
|
||||||
<span title="{% trans %}mininstock.label{% endtrans %}">{{ part.minAmount }}</span>
|
<span title="{% trans %}mininstock.label{% endtrans %}">{{ part.minAmount | amountFormat(part.partUnit) }}</span>
|
||||||
</span>
|
</span>
|
||||||
</h6>
|
</h6>
|
||||||
<h6 class="" title="{% trans %}footprint.label{% endtrans %}">
|
<h6 class="" title="{% trans %}footprint.label{% endtrans %}">
|
||||||
|
|
|
@ -28,7 +28,7 @@
|
||||||
<i class="fas fa-question-circle fa-fw"></i> {% trans %}part_lots.instock_unknown{% endtrans %}
|
<i class="fas fa-question-circle fa-fw"></i> {% trans %}part_lots.instock_unknown{% endtrans %}
|
||||||
</span>
|
</span>
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ lot.amount }}
|
{{ lot.amount | amountFormat(part.partUnit, {'decimals': 5}) }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue