diff --git a/app/Http/Controllers/Admin/SettingsController.php b/app/Http/Controllers/Admin/SettingsController.php index 8308a528..5f6c74a2 100644 --- a/app/Http/Controllers/Admin/SettingsController.php +++ b/app/Http/Controllers/Admin/SettingsController.php @@ -4,12 +4,23 @@ namespace App\Http\Controllers\Admin; use App\Contracts\Controller; use App\Models\Setting; +use App\Services\FinanceService; use Igaster\LaravelTheme\Facades\Theme; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; class SettingsController extends Controller { + private $financeSvc; + + /** + * @param FinanceService $financeSvc + */ + public function __construct(FinanceService $financeSvc) + { + $this->financeSvc = $financeSvc; + } + /** * Get a list of themes formatted for a select box * @@ -30,6 +41,22 @@ class SettingsController extends Controller return $theme_list; } + /** + * Return the currency list + * + * @return array + */ + private function getCurrencyList(): array + { + $curr = []; + foreach (config('money') as $currency => $attrs) { + $name = $attrs['name'].' ('.$attrs['symbol'].'/'.$currency.')'; + $curr[$currency] = $name; + } + + return $curr; + } + /** * Display the settings. Group them by the setting group */ @@ -39,6 +66,7 @@ class SettingsController extends Controller $settings = $settings->groupBy('group'); return view('admin.settings.index', [ + 'currencies' => $this->getCurrencyList(), 'grouped_settings' => $settings, 'themes' => $this->getThemes(), ]); @@ -68,6 +96,8 @@ class SettingsController extends Controller $setting->save(); } + $this->financeSvc->changeJournalCurrencies(); + flash('Settings saved!'); return redirect('/admin/settings'); diff --git a/app/Models/Traits/JournalTrait.php b/app/Models/Traits/JournalTrait.php index 46aedad6..9198360a 100644 --- a/app/Models/Traits/JournalTrait.php +++ b/app/Models/Traits/JournalTrait.php @@ -12,7 +12,7 @@ trait JournalTrait public static function bootJournalTrait() { static::created(function ($model) { - $model->initJournal(config('phpvms.currency')); + $model->initJournal(setting('units.currency')); }); } diff --git a/app/Repositories/JournalRepository.php b/app/Repositories/JournalRepository.php index 18f5308d..1758a292 100644 --- a/app/Repositories/JournalRepository.php +++ b/app/Repositories/JournalRepository.php @@ -110,7 +110,7 @@ class JournalRepository extends Repository implements CacheableInterface 'journal_id' => $journal->id, 'credit' => $credit ? $credit->getAmount() : null, 'debit' => $debit ? $debit->getAmount() : null, - 'currency' => config('phpvms.currency'), + 'currency' => setting('units.currency', 'USD'), 'memo' => $memo, 'post_date' => $post_date, 'transaction_group' => $transaction_group, diff --git a/app/Services/Finance/PirepFinanceService.php b/app/Services/Finance/PirepFinanceService.php index c2412599..a5d03854 100644 --- a/app/Services/Finance/PirepFinanceService.php +++ b/app/Services/Finance/PirepFinanceService.php @@ -63,11 +63,11 @@ class PirepFinanceService extends Service public function processFinancesForPirep(Pirep $pirep) { if (!$pirep->airline->journal) { - $pirep->airline->journal = $pirep->airline->initJournal(config('phpvms.currency')); + $pirep->airline->journal = $pirep->airline->initJournal(setting('units.currency', 'USD')); } if (!$pirep->user->journal) { - $pirep->user->journal = $pirep->user->initJournal(config('phpvms.currency')); + $pirep->user->journal = $pirep->user->initJournal(setting('units.currency', 'USD')); } // Clean out the expenses first diff --git a/app/Services/FinanceService.php b/app/Services/FinanceService.php index 489b8eba..8866e716 100644 --- a/app/Services/FinanceService.php +++ b/app/Services/FinanceService.php @@ -168,4 +168,16 @@ class FinanceService extends Service 'transactions' => $transactions, ]; } + + /** + * Change the currencies on the journals and transactions to the current currency value + */ + public function changeJournalCurrencies(): void + { + $currency = setting('units.currency', 'USD'); + $update = ['currency' => $currency]; + + Journal::query()->update($update); + JournalTransaction::query()->update($update); + } } diff --git a/app/Services/Installer/SeederService.php b/app/Services/Installer/SeederService.php index b38ebbf4..d82dd7ff 100644 --- a/app/Services/Installer/SeederService.php +++ b/app/Services/Installer/SeederService.php @@ -248,7 +248,7 @@ class SeederService extends Service // See if any of the options have changed if ($row->type === 'select') { - if ($row->options !== $setting['options']) { + if (!empty($row->options) && $row->options !== $setting['options']) { Log::info('Options for '.$id.' changed, update available'); return true; } diff --git a/app/Support/Money.php b/app/Support/Money.php index d243521f..6cb25e83 100644 --- a/app/Support/Money.php +++ b/app/Support/Money.php @@ -56,7 +56,7 @@ class Money */ public static function convertToSubunit($amount) { - $currency = config('phpvms.currency'); + $currency = setting('units.currency', 'USD'); return (int) $amount * config('money.'.$currency.'.subunit'); } @@ -71,7 +71,7 @@ class Money public static function currency() { try { - return new Currency(config('phpvms.currency', 'USD')); + return new Currency(setting('units.currency', 'USD')); } catch (\OutOfBoundsException $e) { return new Currency('USD'); } diff --git a/config/phpvms.php b/config/phpvms.php index 4a7e81e5..11d42f13 100644 --- a/config/phpvms.php +++ b/config/phpvms.php @@ -35,14 +35,6 @@ return [ */ 'registration_redirect' => '/profile', - /* - * The ISO "Currency Code" to use, the list is in config/money.php - * - * Note, do not change this after you've set it, unless you don't - * care that the currencies aren't "exchanged" into the new format - */ - 'currency' => 'USD', - /* * Point to the class to use to retrieve the METAR string. If this * goes inactive at some date, it can be replaced diff --git a/resources/stubs/installer/config.stub b/resources/stubs/installer/config.stub index 1d947ea0..75b8680c 100644 --- a/resources/stubs/installer/config.stub +++ b/resources/stubs/installer/config.stub @@ -23,13 +23,7 @@ return [ // Overrides config/phpvms.php 'phpvms' => [ - /** - * The ISO "Currency Code" to use, the list is in config/money.php - * - * Note, do not change this after you've set it, unless you don't - * care that the currencies aren't "exchanged" into the new format - */ - 'currency' => 'USD', + ], // diff --git a/resources/views/admin/aircraft/expenses.blade.php b/resources/views/admin/aircraft/expenses.blade.php index 703d5e77..a01fd474 100644 --- a/resources/views/admin/aircraft/expenses.blade.php +++ b/resources/views/admin/aircraft/expenses.blade.php @@ -14,7 +14,7 @@ @if(count($aircraft->expenses) > 0) Name - Cost {{ currency(config('phpvms.currency')) }} + Cost {{ currency(setting('units.currency')) }} Type diff --git a/resources/views/admin/airports/expenses.blade.php b/resources/views/admin/airports/expenses.blade.php index bfa4ef5a..a4349891 100644 --- a/resources/views/admin/airports/expenses.blade.php +++ b/resources/views/admin/airports/expenses.blade.php @@ -11,7 +11,7 @@ @if(count($airport->expenses)) Name - Cost {{ currency(config('phpvms.currency')) }} + Cost {{ currency(setting('units.currency')) }} Type diff --git a/resources/views/admin/pireps/transactions.blade.php b/resources/views/admin/pireps/transactions.blade.php index 64ed2614..5125b1a6 100644 --- a/resources/views/admin/pireps/transactions.blade.php +++ b/resources/views/admin/pireps/transactions.blade.php @@ -7,12 +7,12 @@ {{ $entry->memo }} @if($entry->credit) - {{ money($entry->credit, config('phpvms.currency')) }} + {{ money($entry->credit, setting('units.currency')) }} @endif @if($entry->debit) - {{ money($entry->debit, config('phpvms.currency')) }} + {{ money($entry->debit, setting('units.currency')) }} @endif diff --git a/resources/views/admin/settings/table.blade.php b/resources/views/admin/settings/table.blade.php index f1aac3d1..669788f7 100644 --- a/resources/views/admin/settings/table.blade.php +++ b/resources/views/admin/settings/table.blade.php @@ -37,6 +37,12 @@ list_to_assoc($themes), $setting->value, ['class' => 'select2', 'style' => 'width: 100%; text-align: left;']) }} + @elseif($setting->id === 'units_currency') + {{ Form::select( + $setting->id, + $currencies, + $setting->value, + ['class' => 'select2', 'style' => 'width: 100%; text-align: left;']) }} @else {{ Form::select( $setting->id, diff --git a/resources/views/admin/subfleets/expenses.blade.php b/resources/views/admin/subfleets/expenses.blade.php index 9a889eb0..82f3ba03 100644 --- a/resources/views/admin/subfleets/expenses.blade.php +++ b/resources/views/admin/subfleets/expenses.blade.php @@ -10,7 +10,7 @@ @if(count($subfleet->expenses)) Name - Cost {{ currency(config('phpvms.currency')) }} + Cost {{ currency(setting('units.currency', 'USD')) }} Type diff --git a/tests/FinanceTest.php b/tests/FinanceTest.php index 3db0fff1..e6abeaba 100644 --- a/tests/FinanceTest.php +++ b/tests/FinanceTest.php @@ -689,7 +689,7 @@ class FinanceTest extends TestCase $journalRepo = app(JournalRepository::class); [$user, $pirep, $fares] = $this->createFullPirep(); - $user->airline->initJournal(config('phpvms.currency')); + $user->airline->initJournal(setting('units.currency', 'USD')); // Override the fares $fare_counts = []; @@ -744,7 +744,7 @@ class FinanceTest extends TestCase ]); [$user, $pirep, $fares] = $this->createFullPirep(); - $user->airline->initJournal(config('phpvms.currency')); + $user->airline->initJournal(setting('units.currency', 'USD')); // Override the fares $fare_counts = [];