Allow user to set a currency in which every price is shown for him.

This commit is contained in:
Jan Böhmer 2019-09-18 18:17:17 +02:00
parent 01da705b54
commit 2d34649ae6
6 changed files with 50 additions and 14 deletions

View file

@ -173,7 +173,7 @@ class UserController extends AdminPages\BaseAdminController
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$em->persist($user); //$em->persist($user);
$em->flush(); $em->flush();
$this->addFlash('success', 'user.settings.saved_flash'); $this->addFlash('success', 'user.settings.saved_flash');
} }

View file

@ -62,6 +62,7 @@ declare(strict_types=1);
namespace App\Entity\UserSystem; namespace App\Entity\UserSystem;
use App\Entity\Base\NamedDBElement; use App\Entity\Base\NamedDBElement;
use App\Entity\PriceInformations\Currency;
use App\Security\Interfaces\HasPermissionsInterface; use App\Security\Interfaces\HasPermissionsInterface;
use App\Validator\Constraints\Selectable; use App\Validator\Constraints\Selectable;
use App\Validator\Constraints\ValidPermission; use App\Validator\Constraints\ValidPermission;
@ -172,18 +173,23 @@ class User extends NamedDBElement implements UserInterface, HasPermissionsInterf
*/ */
protected $settings = []; protected $settings = [];
/**
* @var Currency|null The currency the user wants to see prices in.
* Dont use fetch=EAGER here, this will cause problems with setting the currency setting.
* TODO: This is most likely a bug in doctrine/symfony related to the UniqueEntity constraint (it makes a db call).
* TODO: Find a way to use fetch EAGER (this improves performance a bit)
* @ORM\ManyToOne(targetEntity="App\Entity\PriceInformations\Currency")
* @ORM\JoinColumn(name="currency_id", referencedColumnName="id")
* @Selectable()
*/
protected $currency = null;
/** @var PermissionsEmbed /** @var PermissionsEmbed
* @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_") * @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_")
* @ValidPermission() * @ValidPermission()
*/ */
protected $permissions; protected $permissions;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\PriceInformations\Currency", fetch="EAGER")
* @ORM\JoinColumn(name="currency_id", referencedColumnName="id")
*/
protected $currency = '';
/** /**
* @ORM\Column(type="text", name="config_image_path") * @ORM\Column(type="text", name="config_image_path")
*/ */
@ -284,6 +290,28 @@ class User extends NamedDBElement implements UserInterface, HasPermissionsInterf
// $this->plainPassword = null; // $this->plainPassword = null;
} }
/**
* Gets the currency the user prefers when showing him prices.
* @return Currency|null The currency the user prefers, or null if the global currency should be used.
*/
public function getCurrency(): ?Currency
{
return $this->currency;
}
/**
* Sets the currency the users prefers to see prices in.
* @param Currency|null $currency
* @return User
*/
public function setCurrency(?Currency $currency): User
{
$this->currency = $currency;
return $this;
}
/** /**
* Returns the ID as an string, defined by the element class. * Returns the ID as an string, defined by the element class.
* This should have a form like P000014, for a part with ID 14. * This should have a form like P000014, for a part with ID 14.

View file

@ -2,7 +2,10 @@
namespace App\Form; namespace App\Form;
use App\Entity\PriceInformations\Currency;
use App\Entity\UserSystem\User; use App\Entity\UserSystem\User;
use App\Form\Type\CurrencyEntityType;
use App\Form\Type\StructuralEntityType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\EmailType;
@ -75,6 +78,10 @@ class UserSettingsType extends AbstractType
'placeholder' => $this->trans->trans('user_settings.theme.placeholder'), 'placeholder' => $this->trans->trans('user_settings.theme.placeholder'),
'label' => $this->trans->trans('user.theme.label'), 'label' => $this->trans->trans('user.theme.label'),
]) ])
->add('currency', CurrencyEntityType::class, [
'required' => false,
'label' => $this->trans->trans('user.currency.label')
])
//Buttons //Buttons
->add('save', SubmitType::class, ['label' => 'save']) ->add('save', SubmitType::class, ['label' => 'save'])

View file

@ -53,13 +53,13 @@
{% set min_order_amount = pricedetail_helper.minOrderAmount(part) %} {% set min_order_amount = pricedetail_helper.minOrderAmount(part) %}
{% set max_order_amount = pricedetail_helper.maxDiscountAmount(part) %} {% set max_order_amount = pricedetail_helper.maxDiscountAmount(part) %}
{% set max_order_price = pricedetail_helper.calculateAvgPrice(part, max_order_amount) %} {% set max_order_price = pricedetail_helper.calculateAvgPrice(part, max_order_amount, app.user.currency ?? null) %}
{% if max_order_price is not null %} {% if max_order_price is not null %}
<span title="{% trans %}part.avg_price.label{% endtrans %} {{ max_order_amount | amountFormat(part.partUnit) }}">{{ max_order_price | moneyFormat }}</span> <span title="{% trans %}part.avg_price.label{% endtrans %} {{ max_order_amount | amountFormat(part.partUnit) }}">{{ max_order_price | moneyFormat(app.user.currency ?? null) }}</span>
{% if min_order_amount < max_order_amount %} {% if min_order_amount < max_order_amount %}
<span> - </span> <span> - </span>
<span title="{% trans %}part.avg_price.label{% endtrans %} {{ min_order_amount | amountFormat(part.partUnit) }}">{{pricedetail_helper.calculateAvgPrice(part, min_order_amount) | moneyFormat }}</span> <span title="{% trans %}part.avg_price.label{% endtrans %} {{ min_order_amount | amountFormat(part.partUnit) }}">{{pricedetail_helper.calculateAvgPrice(part, min_order_amount, app.user.currency ?? null ) | moneyFormat(app.user.currency ?? null) }}</span>
{% endif %} {% endif %}
{% endif %} {% endif %}

View file

@ -43,14 +43,14 @@
</td> </td>
<td> <td>
{{ detail.price | moneyFormat(detail.currency) }} / {{ detail.PriceRelatedQuantity | amountFormat(part.partUnit) }} {{ detail.price | moneyFormat(detail.currency) }} / {{ detail.PriceRelatedQuantity | amountFormat(part.partUnit) }}
{% if detail.currency and pricedetail_helper.convertMoneyToCurrency(detail.price, detail.currency) > 0 %} {% if detail.currency != (app.user.currency ?? null) and pricedetail_helper.convertMoneyToCurrency(detail.price, detail.currency) > 0 %}
<span class="text-muted">({{ pricedetail_helper.convertMoneyToCurrency(detail.price, detail.currency) | moneyFormat() }})</span> <span class="text-muted">({{ pricedetail_helper.convertMoneyToCurrency(detail.price, detail.currency, app.user.currency ?? null) | moneyFormat(app.user.currency ?? null) }})</span>
{% endif %} {% endif %}
</td> </td>
<td> <td>
{{ detail.PricePerUnit | moneyFormat(detail.currency) }} {{ detail.PricePerUnit | moneyFormat(detail.currency) }}
{% if detail.currency and pricedetail_helper.convertMoneyToCurrency(detail.PricePerUnit, detail.currency) > 0 %} {% if detail.currency != (app.user.currency ?? null) and pricedetail_helper.convertMoneyToCurrency(detail.PricePerUnit, detail.currency) > 0 %}
<span class="text-muted">({{ pricedetail_helper.convertMoneyToCurrency(detail.PricePerUnit, detail.currency) | moneyFormat() }})</span> <span class="text-muted">({{ pricedetail_helper.convertMoneyToCurrency(detail.PricePerUnit, detail.currency, app.user.currency ?? null) | moneyFormat(app.user.currency ?? null) }})</span>
{% endif %} {% endif %}
</td> </td>
</tr> </tr>

View file

@ -30,6 +30,7 @@
{{ form_row(settings_form.language) }} {{ form_row(settings_form.language) }}
{{ form_row(settings_form.timezone) }} {{ form_row(settings_form.timezone) }}
{{ form_row(settings_form.theme) }} {{ form_row(settings_form.theme) }}
{{ form_row(settings_form.currency) }}
</div> </div>
</div> </div>