Merge pull request #136 from denisdulici/currency-format

Improved currency format
This commit is contained in:
Denis Duliçi 2017-12-09 16:39:55 +03:00 committed by GitHub
commit 546bd20af2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 226 additions and 11 deletions

View File

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

View File

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

View File

@ -0,0 +1,46 @@
<?php
namespace App\Http\Middleware;
use Akaunting\Money\Currency;
use App\Models\Setting\Currency as Model;
use Closure;
class LoadCurrencies
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$company_id = session('company_id');
if (empty($company_id)) {
return $next($request);
}
$currencies = Model::all();
foreach ($currencies as $currency) {
if (!isset($currency->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);
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace App\Listeners\Updates;
use App\Events\UpdateFinished;
use App\Models\Setting\Currency;
class Version113 extends Listener
{
const ALIAS = 'core';
const VERSION = '1.1.3';
/**
* Handle the event.
*
* @param $event
* @return void
*/
public function handle(UpdateFinished $event)
{
// Check if should listen
if (!$this->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();
}
}
}

View File

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

View File

@ -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(),
];

View File

@ -0,0 +1,38 @@
<?php
use Illuminate\Database\Migrations\Migration;
class AddCurrencyColumns extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('currencies', function ($table) {
$table->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');
});
}
}

View File

@ -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',
]
];

View File

@ -10,13 +10,23 @@
<div class="box-body">
{{ 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')) }}
</div>
<!-- /.box-body -->
@ -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');
}
});
});
});
</script>
@endpush

View File

@ -14,13 +14,23 @@
<div class="box-body">
{{ 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')) }}
</div>
<!-- /.box-body -->
@ -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');
}
});
});
});
</script>
@endpush

View File

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