mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-22 09:53:35 +02:00
Show the price in base currency if exchange rates are available.
This commit is contained in:
parent
7e713b3e1a
commit
a479dc81c4
5 changed files with 116 additions and 13 deletions
|
@ -56,6 +56,10 @@ services:
|
|||
arguments:
|
||||
$base_currency: '%default_currency%'
|
||||
|
||||
App\Services\PricedetailHelper:
|
||||
arguments:
|
||||
$base_currency: '%default_currency%'
|
||||
|
||||
App\Services\MoneyFormatter:
|
||||
arguments:
|
||||
$base_currency: '%default_currency%'
|
||||
|
|
|
@ -35,6 +35,7 @@ use App\Entity\Parts\Part;
|
|||
use App\Form\AttachmentFormType;
|
||||
use App\Form\Part\PartBaseType;
|
||||
use App\Services\AttachmentHelper;
|
||||
use App\Services\PricedetailHelper;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
|
@ -51,14 +52,15 @@ class PartController extends AbstractController
|
|||
* @param AttachmentHelper $attachmentHelper
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function show(Part $part, AttachmentHelper $attachmentHelper)
|
||||
public function show(Part $part, AttachmentHelper $attachmentHelper, PricedetailHelper $pricedetailHelper)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('read', $part);
|
||||
|
||||
return $this->render('Parts/info/show_part_info.html.twig',
|
||||
[
|
||||
'part' => $part,
|
||||
'attachment_helper' => $attachmentHelper
|
||||
'attachment_helper' => $attachmentHelper,
|
||||
'pricedetail_helper' => $pricedetailHelper
|
||||
]
|
||||
);
|
||||
}
|
||||
|
@ -102,7 +104,7 @@ class PartController extends AbstractController
|
|||
[
|
||||
'part' => $part,
|
||||
'form' => $form->createView(),
|
||||
'attachment_helper' => $attachmentHelper
|
||||
'attachment_helper' => $attachmentHelper,
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
@ -34,6 +34,7 @@ namespace App\Entity\PriceInformations;
|
|||
|
||||
use App\Entity\Base\StructuralDBElement;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use s9e\TextFormatter\Configurator\TemplateNormalizations\AbstractChooseOptimization;
|
||||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
|
@ -47,6 +48,8 @@ use Symfony\Component\Validator\Constraints as Assert;
|
|||
class Currency extends StructuralDBElement
|
||||
{
|
||||
|
||||
public const PRICE_SCALE = 5;
|
||||
|
||||
/**
|
||||
* @var string The 3 letter ISO code of the currency.
|
||||
* @ORM\Column(type="string")
|
||||
|
@ -55,7 +58,7 @@ class Currency extends StructuralDBElement
|
|||
protected $iso_code;
|
||||
|
||||
/**
|
||||
* @var float|null The exchange rate between this currency and the base currency
|
||||
* @var string|null The exchange rate between this currency and the base currency
|
||||
* (how many base units the current currency is worth)
|
||||
* @ORM\Column(type="decimal", precision=11, scale=5, nullable=true)
|
||||
* @Assert\Positive()
|
||||
|
@ -94,34 +97,34 @@ class Currency extends StructuralDBElement
|
|||
|
||||
/**
|
||||
* Returns the inverse exchange rate (how many of the current currency the base unit is worth)
|
||||
* @return float|null
|
||||
* @return string|null
|
||||
*/
|
||||
public function getInverseExchangeRate(): ?float
|
||||
public function getInverseExchangeRate(): ?string
|
||||
{
|
||||
$tmp = $this->getExchangeRate();
|
||||
|
||||
if ($tmp === null) {
|
||||
if ($tmp === null || (float) $tmp === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return 1 / $tmp;
|
||||
return bcdiv(1, $tmp, static::PRICE_SCALE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns The exchange rate between this currency and the base currency
|
||||
* (how many base units the current currency is worth)
|
||||
* @return float|null
|
||||
* @return string|null
|
||||
*/
|
||||
public function getExchangeRate(): ?float
|
||||
public function getExchangeRate(): ?string
|
||||
{
|
||||
return $this->exchange_rate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float|null $exchange_rate
|
||||
* @param string|null $exchange_rate
|
||||
* @return Currency
|
||||
*/
|
||||
public function setExchangeRate(?float $exchange_rate): Currency
|
||||
public function setExchangeRate(?string $exchange_rate): Currency
|
||||
{
|
||||
$this->exchange_rate = $exchange_rate;
|
||||
return $this;
|
||||
|
|
89
src/Services/PricedetailHelper.php
Normal file
89
src/Services/PricedetailHelper.php
Normal file
|
@ -0,0 +1,89 @@
|
|||
<?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\PriceInformations\Currency;
|
||||
use App\Entity\PriceInformations\Pricedetail;
|
||||
use Locale;
|
||||
use Money\Number;
|
||||
use Symfony\Component\Intl\Currencies;
|
||||
|
||||
class PricedetailHelper
|
||||
{
|
||||
protected $base_currency;
|
||||
protected $locale;
|
||||
|
||||
public function __construct(string $base_currency)
|
||||
{
|
||||
$this->base_currency = $base_currency;
|
||||
$this->locale = Locale::getDefault();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given value in origin currency to the choosen target currency
|
||||
* @param $value float|string The value that should be converted
|
||||
* @param Currency|null $originCurrency The currency the $value is given in.
|
||||
* Set to null, to use global base currency.
|
||||
* @param Currency|null $targetCurrency The target currency, to which $value should be converted.
|
||||
* Set to null, to use global base currency.
|
||||
* @return string|null The value in $targetCurrency given as bcmath string.
|
||||
* Returns null, if it was not possible to convert between both values (e.g. when the exchange rates are missing)
|
||||
*/
|
||||
public function convertMoneyToCurrency($value, ?Currency $originCurrency = null, ?Currency $targetCurrency = null) : ?string
|
||||
{
|
||||
$value = (string) $value;
|
||||
|
||||
//Convert value to base currency
|
||||
$val_base = $value;
|
||||
if($originCurrency !== null) {
|
||||
//Without an exchange rate we can not calculate the exchange rate
|
||||
if ((float) $originCurrency->getExchangeRate() === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$val_base = bcmul($value, $originCurrency->getExchangeRate(), Pricedetail::PRICE_PRECISION);
|
||||
}
|
||||
|
||||
//Convert value in base currency to target currency
|
||||
$val_target = $val_base;
|
||||
if ($targetCurrency !== null) {
|
||||
//Without an exchange rate we can not calculate the exchange rate
|
||||
if ($targetCurrency->getExchangeRate() === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$val_target = bcmul($val_base, $targetCurrency->getInverseExchangeRate(), Pricedetail::PRICE_PRECISION);
|
||||
}
|
||||
|
||||
return $val_target;
|
||||
}
|
||||
}
|
|
@ -41,10 +41,15 @@
|
|||
</td>
|
||||
<td>
|
||||
{{ detail.price | moneyFormat(detail.currency) }} / {{ detail.PriceRelatedQuantity | amountFormat(part.partUnit) }}
|
||||
{# {{ detail.Price | moneyFormat }} <i>{% trans %}part.order.price_per{% endtrans %}</i> {{ detail.PriceRelatedQuantity }} #}
|
||||
{% if detail.currency and pricedetail_helper.convertMoneyToCurrency(detail.price, detail.currency) > 0 %}
|
||||
<span class="text-muted">({{ pricedetail_helper.convertMoneyToCurrency(detail.price, detail.currency) | moneyFormat() }})</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>
|
||||
{{ detail.PricePerUnit | moneyFormat(detail.currency) }}
|
||||
{% if detail.currency and pricedetail_helper.convertMoneyToCurrency(detail.PricePerUnit, detail.currency) > 0 %}
|
||||
<span class="text-muted">({{ pricedetail_helper.convertMoneyToCurrency(detail.PricePerUnit, detail.currency) | moneyFormat() }})</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue