Skip to content

Commit

Permalink
Merge branch 'feature/currencies_settings' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Volmarg committed Jan 24, 2020
2 parents a98f5bd + e590108 commit c44735e
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Controller/Utils/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Settings {
/**
* @var SettingsSaver $settings_saver
*/
private $settings_saver;
public $settings_saver;

/**
* @var SettingsLoader $settings_loader
Expand Down
2 changes: 2 additions & 0 deletions src/DataFixtures/Modules/Payments/MyPaymentsOwedFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ public function load(ObjectManager $manager)
$info = $data[PaymentsOwed::KEY_INFO];
$date = $data[PaymentsOwed::KEY_DATE];
$owed_by_me = $data[PaymentsOwed::KEY_OWED_BY_ME];
$currency = $data[PaymentsOwed::KEY_CURRENCY];

$money_owed = new MyPaymentsOwed();
$money_owed->setTarget($target);
$money_owed->setAmount($amount);
$money_owed->setInformation($info);
$money_owed->setDate($date);
$money_owed->setOwedByMe($owed_by_me);
$money_owed->setCurrency($currency);

$manager->persist($money_owed);
}
Expand Down
7 changes: 7 additions & 0 deletions src/DataFixtures/Providers/Modules/PaymentsOwed.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php
namespace App\DataFixtures\Providers\Modules;

use App\DataFixtures\Providers\SettingProvider;

class PaymentsOwed {

const KEY_TARGET = 'target';
const KEY_AMOUNT = 'amount';
const KEY_OWED_BY_ME = 'owed_by_me';
const KEY_INFO = 'information';
const KEY_DATE = 'date';
const KEY_CURRENCY = 'currency';

const OWED_TARGET_ERLEN_SERVICE = 'Erlen Service';
const OWED_TARGET_TOMASZ = 'Tomasz Skrzypczyk';
Expand All @@ -21,27 +24,31 @@ class PaymentsOwed {
self::KEY_AMOUNT => 1500,
self::KEY_INFO => 'For the laptop',
self::KEY_DATE => '2017-05-03',
self::KEY_CURRENCY => SettingProvider::KEY_CURRENCY_NAME_PLN,
],
[
self::KEY_TARGET => self::OWED_TARGET_TOMASZ,
self::KEY_OWED_BY_ME => false,
self::KEY_AMOUNT => 150,
self::KEY_INFO => 'No idea, he just wanted to borrow',
self::KEY_DATE => '2018-05-03',
self::KEY_CURRENCY => SettingProvider::KEY_CURRENCY_NAME_PLN,
],
[
self::KEY_TARGET => self::OWNED_TARGET_BART,
self::KEY_OWED_BY_ME => false,
self::KEY_AMOUNT => 60,
self::KEY_INFO => "Products that I've bought him on Ebay",
self::KEY_DATE => '2018-05-03',
self::KEY_CURRENCY => SettingProvider::KEY_CURRENCY_NAME_EUR,
],
[
self::KEY_TARGET => self::OWNED_TARGET_RORONOA,
self::KEY_OWED_BY_ME => false,
self::KEY_AMOUNT => 350,
self::KEY_INFO => "Borrowed for mandate on highway",
self::KEY_DATE => '2019-02-13',
self::KEY_CURRENCY => SettingProvider::KEY_CURRENCY_NAME_CASH,
]
];
}
11 changes: 11 additions & 0 deletions src/DataFixtures/Providers/SettingProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\DataFixtures\Providers;

class SettingProvider {

const KEY_CURRENCY_NAME_PLN = "PLN";
const KEY_CURRENCY_NAME_EUR = "EUR";
const KEY_CURRENCY_NAME_CASH = "CASH";

}
122 changes: 122 additions & 0 deletions src/DataFixtures/SettingFixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace App\DataFixtures;

use App\Controller\Utils\Application;
use App\DataFixtures\Providers\SettingProvider;
use App\DTO\Settings\Finances\SettingsCurrencyDTO;
use App\DTO\Settings\Finances\SettingsFinancesDTO;
use App\Services\Exceptions\ExceptionValueNotAllowed;
use App\Services\Settings\SettingsLoader;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\DBAL\DBALException;

class SettingFixtures extends Fixture
{
const ENABLED = true;
const SALT = NULL;
const LAST_LOGIN = NULL;
const CONFIRMATION_TOKEN = NULL;
const PASSWORD_REQUESTED_AT = NULL;
const AVATAR = NULL;
const NICKNAME = NULL;
const PASSWORD = '$2y$13$.VnnN5tJ8evchXidKXZnZePceiQ1FFzr/9SLg8DNGyeKpbnqBelDW'; #admin
const ROLES = 'ROLE_SUPER_ADMIN';
const USERNAME = 'admin';

/**
* @var Application $app
*/
private $app;

/**
* SettingFixtures constructor.
* @param Application $app
*/
public function __construct(Application $app) {
$this->app = $app;
}

/**
* @param ObjectManager $manager
* @throws ExceptionValueNotAllowed
* @throws \Exception
*/
public function load(ObjectManager $manager)
{

$pln_currency_dto = $this->getPlnCurrency();
$eur_currency_dto = $this->getEurCurrency();
$cash_currency_dto = $this->getCashCurrency();

$currencies = [
$pln_currency_dto,
$eur_currency_dto,
$cash_currency_dto,
];

$finances_settings_dto = new SettingsFinancesDTO();
$finances_settings_dto->setSettingsCurrencyDtos($currencies);

$this->insertCurrenciesIntoDb($finances_settings_dto);
}

/**
* @throws ExceptionValueNotAllowed
*/
private function getPlnCurrency(): SettingsCurrencyDTO {
$settings_currency_dto = new SettingsCurrencyDTO();
$settings_currency_dto->setName(SettingProvider::KEY_CURRENCY_NAME_PLN);
$settings_currency_dto->setIsDefault(1);
$settings_currency_dto->setMultiplier(1);
$settings_currency_dto->setSymbol("z\u0142");

return $settings_currency_dto;
}

/**
* @throws ExceptionValueNotAllowed
*/
private function getEurCurrency(): SettingsCurrencyDTO {
$settings_currency_dto = new SettingsCurrencyDTO();
$settings_currency_dto->setName(SettingProvider::KEY_CURRENCY_NAME_EUR);
$settings_currency_dto->setIsDefault(false);
$settings_currency_dto->setMultiplier(4.3);
$settings_currency_dto->setSymbol("\u20ac");

return $settings_currency_dto;
}

/**
* @throws ExceptionValueNotAllowed
*/
private function getCashCurrency(): SettingsCurrencyDTO {
$settings_currency_dto = new SettingsCurrencyDTO();
$settings_currency_dto->setName(SettingProvider::KEY_CURRENCY_NAME_CASH);
$settings_currency_dto->setIsDefault(false);
$settings_currency_dto->setMultiplier(1.01);
$settings_currency_dto->setSymbol("\ud83d\udcb6");

return $settings_currency_dto;
}

/**
* This function will insert data into db as RAW text - this is required to prevent escaping slashes
* Info: this sql is here as it's the only - special use case - not adding it to repository as it's a fixture part
* @param SettingsFinancesDTO $finances_settings_dto
* @throws DBALException
*/
private function insertCurrenciesIntoDb(SettingsFinancesDTO $finances_settings_dto){

$setting_type = SettingsLoader::SETTING_NAME_FINANCES;
$json = $finances_settings_dto->toJson();

$sql = "
INSERT IGNORE INTO setting (`id`, `name`, `value`)
VALUES(2, '{$setting_type}', '{$json}')
";

$this->app->em->getConnection()->executeQuery($sql);
}
}
1 change: 1 addition & 0 deletions src/Services/Settings/SettingsSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\DTO\Settings\Finances\SettingsFinancesDTO;
use App\DTO\Settings\SettingsDashboardDTO;
use App\Entity\Setting;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManagerInterface;

/**
Expand Down
8 changes: 8 additions & 0 deletions templates/page-elements/components/forms/currency-select.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
{% set disabled = 'disabled' %}
{% endif %}

{% set matchingCurrencyWasFound = false %}

<select
class="form-control {{ disabled }} option-picker"
style="max-width: 65px;"
>

{% for finance_currency_dto in getFinancesCurrenciesDtos() %}

{% if
currency is defined
and currency == finance_currency_dto.getName()
%}
{% set selected = "selected" %}
{% set matchingCurrencyWasFound = true %}
{% else %}
{% set selected = "" %}
{% endif %}
Expand All @@ -23,4 +27,8 @@
{{ finance_currency_dto.getSymbol() }}
</option>
{% endfor %}

{% if not matchingCurrencyWasFound %}
<option value="" selected></option>
{% endif %}
</select>

0 comments on commit c44735e

Please sign in to comment.