diff --git a/app/Http/Controllers/Settings/Currencies.php b/app/Http/Controllers/Settings/Currencies.php index bce09e152..79e675606 100644 --- a/app/Http/Controllers/Settings/Currencies.php +++ b/app/Http/Controllers/Settings/Currencies.php @@ -212,4 +212,20 @@ class Currencies extends Controller return response()->json($json); } + + public function config() + { + $json = new \stdClass(); + + $code = request('code'); + + if ($code) { + $currency = config('money.' . $code); + $currency['symbol_first'] = $currency['symbol_first'] ? 1 : 0; + + $json = (object) $currency; + } + + return response()->json($json); + } } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 72f9d998d..bedca45ae 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -36,6 +36,7 @@ class Kernel extends HttpKernel \Illuminate\Routing\Middleware\SubstituteBindings::class, \App\Http\Middleware\RedirectIfNotInstalled::class, \App\Http\Middleware\LoadSettings::class, + \App\Http\Middleware\LoadCurrencies::class, \App\Http\Middleware\AddXHeader::class, ], diff --git a/app/Http/Middleware/LoadCurrencies.php b/app/Http/Middleware/LoadCurrencies.php new file mode 100644 index 000000000..334788a66 --- /dev/null +++ b/app/Http/Middleware/LoadCurrencies.php @@ -0,0 +1,46 @@ +precision)) { + continue; + } + + config(['money.' . $currency->code . '.precision' => $currency->precision]); + config(['money.' . $currency->code . '.symbol' => $currency->symbol]); + config(['money.' . $currency->code . '.symbol_first' => $currency->symbol_first]); + config(['money.' . $currency->code . '.decimal_mark' => $currency->decimal_mark]); + config(['money.' . $currency->code . '.thousands_separator' => $currency->thousands_separator]); + } + + // Set currencies with new settings + Currency::setCurrencies(config('money')); + + return $next($request); + } + +} \ No newline at end of file diff --git a/app/Listeners/Updates/Version113.php b/app/Listeners/Updates/Version113.php new file mode 100644 index 000000000..ec04aeeb4 --- /dev/null +++ b/app/Listeners/Updates/Version113.php @@ -0,0 +1,41 @@ +check($event)) { + return; + } + + // Update currencies + $currencies = Currency::all(); + + foreach ($currencies as $currency) { + $currency->precision = config('money.' . $currency->code . '.precision'); + $currency->symbol = config('money.' . $currency->code . '.symbol'); + $currency->symbol_first = config('money.' . $currency->code . '.symbol_first') ? 1 : 0; + $currency->decimal_mark = config('money.' . $currency->code . '.decimal_mark'); + $currency->thousands_separator = config('money.' . $currency->code . '.thousands_separator'); + + $currency->save(); + } + + } +} diff --git a/app/Models/Setting/Currency.php b/app/Models/Setting/Currency.php index 704194b80..96ac2d1aa 100644 --- a/app/Models/Setting/Currency.php +++ b/app/Models/Setting/Currency.php @@ -14,7 +14,7 @@ class Currency extends Model * * @var array */ - protected $fillable = ['company_id', 'name', 'code', 'rate', 'enabled']; + protected $fillable = ['company_id', 'name', 'code', 'rate', 'enabled', 'precision', 'symbol', 'symbol_first', 'decimal_mark', 'thousands_separator']; /** * Sortable columns. diff --git a/app/Transformers/Setting/Currency.php b/app/Transformers/Setting/Currency.php index 836a8bf62..5f235fec8 100644 --- a/app/Transformers/Setting/Currency.php +++ b/app/Transformers/Setting/Currency.php @@ -20,6 +20,11 @@ class Currency extends TransformerAbstract 'code' => $model->code, 'rate' => $model->rate, 'enabled' => $model->enabled, + 'precision' => $model->precision, + 'symbol' => $model->symbol, + 'symbol_first' => $model->symbol_first, + 'decimal_mark' => $model->decimal_mark, + 'thousands_separator' => $model->thousands_separator, 'created_at' => $model->created_at->toIso8601String(), 'updated_at' => $model->updated_at->toIso8601String(), ]; diff --git a/database/migrations/2017_12_09_000000_add_currency_columns.php b/database/migrations/2017_12_09_000000_add_currency_columns.php new file mode 100644 index 000000000..0da22dcef --- /dev/null +++ b/database/migrations/2017_12_09_000000_add_currency_columns.php @@ -0,0 +1,38 @@ +string('precision')->nullable(); + $table->string('symbol')->nullable(); + $table->integer('symbol_first')->default(1); + $table->string('decimal_mark')->nullable(); + $table->string('thousands_separator')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('currencies', function ($table) { + $table->dropColumn('precision'); + $table->dropColumn('symbol'); + $table->dropColumn('symbol_first'); + $table->dropColumn('decimal_mark'); + $table->dropColumn('thousands_separator'); + }); + } +} diff --git a/resources/lang/en-GB/currencies.php b/resources/lang/en-GB/currencies.php index b50d6434b..959079f3a 100644 --- a/resources/lang/en-GB/currencies.php +++ b/resources/lang/en-GB/currencies.php @@ -5,5 +5,14 @@ return [ 'code' => 'Code', 'rate' => 'Rate', 'default' => 'Default Currency', + 'decimal_mark' => 'Decimal Mark', + 'thousands_separator' => 'Thousands Separator', + 'precision' => 'Precision', + 'symbol' => [ + 'symbol' => 'Symbol', + 'position' => 'Symbol Position', + 'before' => 'Before Amount', + 'after' => 'After Amount', + ] ]; diff --git a/resources/views/settings/currencies/create.blade.php b/resources/views/settings/currencies/create.blade.php index b4d70f050..1923307fc 100644 --- a/resources/views/settings/currencies/create.blade.php +++ b/resources/views/settings/currencies/create.blade.php @@ -10,13 +10,23 @@
{{ Form::textGroup('name', trans('general.name'), 'id-card-o') }} - {{ Form::radioGroup('enabled', trans('general.enabled')) }} - - {{ Form::selectGroup('code', trans('currencies.code'), 'code', $codes, setting('currencies.code')) }} - - {{ Form::radioGroup('default_currency', trans('currencies.default')) }} + {{ Form::selectGroup('code', trans('currencies.code'), 'code', $codes) }} {{ Form::textGroup('rate', trans('currencies.rate'), 'money') }} + + {{ Form::textGroup('precision', trans('currencies.precision'), 'bullseye') }} + + {{ Form::textGroup('symbol', trans('currencies.symbol.symbol'), 'font') }} + + {{ Form::selectGroup('symbol_first', trans('currencies.symbol.position'), 'text-width', ['1' => trans('currencies.symbol.before'), '0' => trans('currencies.symbol.after')]) }} + + {{ Form::textGroup('decimal_mark', trans('currencies.decimal_mark'), 'columns') }} + + {{ Form::textGroup('thousands_separator', trans('currencies.thousands_separator'), 'columns', []) }} + + {{ Form::radioGroup('enabled', trans('general.enabled')) }} + + {{ Form::radioGroup('default_currency', trans('currencies.default')) }}
@@ -42,6 +52,25 @@ $("#code").select2({ placeholder: "{{ trans('general.form.select.field', ['field' => trans('currencies.code')]) }}" }); + + $('#code').change(function() { + $.ajax({ + url: '{{ url("settings/currencies/config") }}', + type: 'GET', + dataType: 'JSON', + data: 'code=' + $(this).val(), + success: function(data) { + $('#precision').val(data.precision); + $('#symbol').val(data.symbol); + $('#symbol_first').val(data.symbol_first); + $('#decimal_mark').val(data.decimal_mark); + $('#thousands_separator').val(data.thousands_separator); + + // This event Select2 Stylesheet + $('#symbol_first').trigger('change'); + } + }); + }); }); @endpush diff --git a/resources/views/settings/currencies/edit.blade.php b/resources/views/settings/currencies/edit.blade.php index 89b234a22..58bbdcb54 100644 --- a/resources/views/settings/currencies/edit.blade.php +++ b/resources/views/settings/currencies/edit.blade.php @@ -14,13 +14,23 @@
{{ Form::textGroup('name', trans('general.name'), 'id-card-o') }} - {{ Form::radioGroup('enabled', trans('general.enabled')) }} - - {{ Form::selectGroup('code', trans('currencies.code'), 'code', $codes, setting('currencies.code')) }} - - {{ Form::radioGroup('default_currency', trans('currencies.default')) }} + {{ Form::selectGroup('code', trans('currencies.code'), 'code', $codes) }} {{ Form::textGroup('rate', trans('currencies.rate'), 'money') }} + + {{ Form::textGroup('precision', trans('currencies.precision'), 'bullseye') }} + + {{ Form::textGroup('symbol', trans('currencies.symbol.symbol'), 'font') }} + + {{ Form::selectGroup('symbol_first', trans('currencies.symbol.position'), 'text-width', ['1' => trans('currencies.symbol.before'), '0' => trans('currencies.symbol.after')]) }} + + {{ Form::textGroup('decimal_mark', trans('currencies.decimal_mark'), 'columns') }} + + {{ Form::textGroup('thousands_separator', trans('currencies.thousands_separator'), 'columns', []) }} + + {{ Form::radioGroup('enabled', trans('general.enabled')) }} + + {{ Form::radioGroup('default_currency', trans('currencies.default')) }}
@@ -44,6 +54,25 @@ $("#code").select2({ placeholder: "{{ trans('general.form.select.field', ['field' => trans('currencies.code')]) }}" }); + + $('#code').change(function() { + $.ajax({ + url: '{{ url("settings/currencies/config") }}', + type: 'GET', + dataType: 'JSON', + data: 'code=' + $(this).val(), + success: function(data) { + $('#precision').val(data.precision); + $('#symbol').val(data.symbol); + $('#symbol_first').val(data.symbol_first); + $('#decimal_mark').val(data.decimal_mark); + $('#thousands_separator').val(data.thousands_separator); + + // This event Select2 Stylesheet + $('#symbol_first').trigger('change'); + } + }); + }); }); @endpush diff --git a/routes/web.php b/routes/web.php index a78f4e130..bdc56748d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -104,6 +104,7 @@ Route::group(['middleware' => 'language'], function () { Route::group(['prefix' => 'settings'], function () { Route::resource('categories', 'Settings\Categories'); Route::get('currencies/currency', 'Settings\Currencies@currency'); + Route::get('currencies/config', 'Settings\Currencies@config'); Route::resource('currencies', 'Settings\Currencies'); Route::get('settings', 'Settings\Settings@edit'); Route::patch('settings', 'Settings\Settings@update');