From 2d34649ae6e643b35fa34c6da036ec78eb2864c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Wed, 18 Sep 2019 18:17:17 +0200 Subject: [PATCH] Allow user to set a currency in which every price is shown for him. --- src/Controller/UserController.php | 2 +- src/Entity/UserSystem/User.php | 40 +++++++++++++++++---- src/Form/UserSettingsType.php | 7 ++++ templates/Parts/info/_main_infos.html.twig | 6 ++-- templates/Parts/info/_order_infos.html.twig | 8 ++--- templates/Users/user_settings.html.twig | 1 + 6 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 1a4887c3..ec61210b 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -173,7 +173,7 @@ class UserController extends AdminPages\BaseAdminController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $em->persist($user); + //$em->persist($user); $em->flush(); $this->addFlash('success', 'user.settings.saved_flash'); } diff --git a/src/Entity/UserSystem/User.php b/src/Entity/UserSystem/User.php index 25b8d942..1505c0ea 100644 --- a/src/Entity/UserSystem/User.php +++ b/src/Entity/UserSystem/User.php @@ -62,6 +62,7 @@ declare(strict_types=1); namespace App\Entity\UserSystem; use App\Entity\Base\NamedDBElement; +use App\Entity\PriceInformations\Currency; use App\Security\Interfaces\HasPermissionsInterface; use App\Validator\Constraints\Selectable; use App\Validator\Constraints\ValidPermission; @@ -172,18 +173,23 @@ class User extends NamedDBElement implements UserInterface, HasPermissionsInterf */ 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 * @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_") * @ValidPermission() */ 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") */ @@ -284,6 +290,28 @@ class User extends NamedDBElement implements UserInterface, HasPermissionsInterf // $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. * This should have a form like P000014, for a part with ID 14. diff --git a/src/Form/UserSettingsType.php b/src/Form/UserSettingsType.php index 9424e4bf..f559b22b 100644 --- a/src/Form/UserSettingsType.php +++ b/src/Form/UserSettingsType.php @@ -2,7 +2,10 @@ namespace App\Form; +use App\Entity\PriceInformations\Currency; use App\Entity\UserSystem\User; +use App\Form\Type\CurrencyEntityType; +use App\Form\Type\StructuralEntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\EmailType; @@ -75,6 +78,10 @@ class UserSettingsType extends AbstractType 'placeholder' => $this->trans->trans('user_settings.theme.placeholder'), 'label' => $this->trans->trans('user.theme.label'), ]) + ->add('currency', CurrencyEntityType::class, [ + 'required' => false, + 'label' => $this->trans->trans('user.currency.label') + ]) //Buttons ->add('save', SubmitType::class, ['label' => 'save']) diff --git a/templates/Parts/info/_main_infos.html.twig b/templates/Parts/info/_main_infos.html.twig index 994bc659..7045f292 100644 --- a/templates/Parts/info/_main_infos.html.twig +++ b/templates/Parts/info/_main_infos.html.twig @@ -53,13 +53,13 @@ {% set min_order_amount = pricedetail_helper.minOrderAmount(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 %} - {{ max_order_price | moneyFormat }} + {{ max_order_price | moneyFormat(app.user.currency ?? null) }} {% if min_order_amount < max_order_amount %} - - {{pricedetail_helper.calculateAvgPrice(part, min_order_amount) | moneyFormat }} + {{pricedetail_helper.calculateAvgPrice(part, min_order_amount, app.user.currency ?? null ) | moneyFormat(app.user.currency ?? null) }} {% endif %} {% endif %} diff --git a/templates/Parts/info/_order_infos.html.twig b/templates/Parts/info/_order_infos.html.twig index 657a4db1..40069346 100644 --- a/templates/Parts/info/_order_infos.html.twig +++ b/templates/Parts/info/_order_infos.html.twig @@ -43,14 +43,14 @@ {{ detail.price | moneyFormat(detail.currency) }} / {{ detail.PriceRelatedQuantity | amountFormat(part.partUnit) }} - {% if detail.currency and pricedetail_helper.convertMoneyToCurrency(detail.price, detail.currency) > 0 %} - ({{ pricedetail_helper.convertMoneyToCurrency(detail.price, detail.currency) | moneyFormat() }}) + {% if detail.currency != (app.user.currency ?? null) and pricedetail_helper.convertMoneyToCurrency(detail.price, detail.currency) > 0 %} + ({{ pricedetail_helper.convertMoneyToCurrency(detail.price, detail.currency, app.user.currency ?? null) | moneyFormat(app.user.currency ?? null) }}) {% endif %} {{ detail.PricePerUnit | moneyFormat(detail.currency) }} - {% if detail.currency and pricedetail_helper.convertMoneyToCurrency(detail.PricePerUnit, detail.currency) > 0 %} - ({{ pricedetail_helper.convertMoneyToCurrency(detail.PricePerUnit, detail.currency) | moneyFormat() }}) + {% if detail.currency != (app.user.currency ?? null) and pricedetail_helper.convertMoneyToCurrency(detail.PricePerUnit, detail.currency) > 0 %} + ({{ pricedetail_helper.convertMoneyToCurrency(detail.PricePerUnit, detail.currency, app.user.currency ?? null) | moneyFormat(app.user.currency ?? null) }}) {% endif %} diff --git a/templates/Users/user_settings.html.twig b/templates/Users/user_settings.html.twig index e4508fc2..38ddffe7 100644 --- a/templates/Users/user_settings.html.twig +++ b/templates/Users/user_settings.html.twig @@ -30,6 +30,7 @@ {{ form_row(settings_form.language) }} {{ form_row(settings_form.timezone) }} {{ form_row(settings_form.theme) }} + {{ form_row(settings_form.currency) }}