Change currency from settings #671 (#672)

This commit is contained in:
Nabeel S
2020-04-26 11:55:20 -04:00
committed by GitHub
parent 03cfc648b0
commit 99f4f3b3d8
15 changed files with 63 additions and 29 deletions

View File

@@ -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');

View File

@@ -12,7 +12,7 @@ trait JournalTrait
public static function bootJournalTrait()
{
static::created(function ($model) {
$model->initJournal(config('phpvms.currency'));
$model->initJournal(setting('units.currency'));
});
}

View File

@@ -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,

View File

@@ -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

View File

@@ -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);
}
}

View File

@@ -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;
}

View File

@@ -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');
}