From c8579660dfa06c624f84c099b3c552635407a876 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 21 Dec 2017 10:28:44 +0200 Subject: [PATCH] Convert currencies --- app/Http/Controllers/AccountController.php | 1 + ...17_12_13_074024_add_remember_2fa_token.php | 8 +++++ resources/lang/en/texts.php | 2 ++ resources/views/accounts/products.blade.php | 4 +++ resources/views/invoices/knockout.blade.php | 33 +++++++++++++------ 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 4f05ac926047..eb7ab3be919a 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -877,6 +877,7 @@ class AccountController extends BaseController $account->fill_products = Input::get('fill_products') ? true : false; $account->update_products = Input::get('update_products') ? true : false; + $account->convert_products = Input::get('convert_products') ? true : false; $account->save(); Session::flash('message', trans('texts.updated_settings')); diff --git a/database/migrations/2017_12_13_074024_add_remember_2fa_token.php b/database/migrations/2017_12_13_074024_add_remember_2fa_token.php index 07a82f878875..03104e0844cc 100644 --- a/database/migrations/2017_12_13_074024_add_remember_2fa_token.php +++ b/database/migrations/2017_12_13_074024_add_remember_2fa_token.php @@ -47,6 +47,10 @@ class AddRemember2faToken extends Migration Schema::table('currencies', function ($table) { $table->decimal('exchange_rate', 13, 4)->nullable(); }); + + Schema::table('accounts', function ($table) { + $table->boolean('convert_products')->default(false); + }); } /** @@ -74,5 +78,9 @@ class AddRemember2faToken extends Migration Schema::table('currencies', function ($table) { $table->dropColumn('exchange_rate'); }); + + Schema::table('accounts', function ($table) { + $table->dropColumn('convert_products'); + }); } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index f6309d1f076e..daeeb0dc60ad 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2620,6 +2620,8 @@ $LANG = array( 'add_status' => 'Add status', 'archive_status' => 'Archive Status', 'new_status' => 'New Status', + 'convert_products' => 'Convert Products', + 'convert_products_help' => 'Automatically convert product prices to the client\'s currency', ); diff --git a/resources/views/accounts/products.blade.php b/resources/views/accounts/products.blade.php index 8ac60263501a..d529869f74ac 100644 --- a/resources/views/accounts/products.blade.php +++ b/resources/views/accounts/products.blade.php @@ -6,8 +6,10 @@ @include('accounts.nav', ['selected' => ACCOUNT_PRODUCTS]) {!! Former::open()->addClass('warn-on-exit') !!} + {{ Former::populateField('fill_products', intval($account->fill_products)) }} {{ Former::populateField('update_products', intval($account->update_products)) }} + {{ Former::populateField('convert_products', intval($account->convert_products)) }}
@@ -19,6 +21,8 @@ {!! Former::checkbox('fill_products')->text(trans('texts.fill_products_help'))->value(1) !!} {!! Former::checkbox('update_products')->text(trans('texts.update_products_help'))->value(1) !!}   + {!! Former::checkbox('convert_products')->text(trans('texts.convert_products_help'))->value(1) !!} +   {!! Former::actions( Button::success(trans('texts.save'))->submit()->appendIcon(Icon::create('floppy-disk')) ) !!} {!! Former::close() !!}
diff --git a/resources/views/invoices/knockout.blade.php b/resources/views/invoices/knockout.blade.php index 5d082bbaf187..4e221c00c2af 100644 --- a/resources/views/invoices/knockout.blade.php +++ b/resources/views/invoices/knockout.blade.php @@ -1037,19 +1037,32 @@ ko.bindingHandlers.productTypeahead = { } if (parseFloat(datum.cost)) { if (! model.cost() || ! model.task_public_id()) { - // optionally handle curency conversion var cost = datum.cost; - var client = window.model.invoice().client(); - if (client) { - var clientCurrencyId = client.currency_id(); - if (clientCurrencyId) { + + // optionally handle curency conversion + @if ($account->convert_products) + var client = window.model.invoice().client(); + if (client) { + var clientCurrencyId = client.currency_id(); var accountCurrencyId = {{ $account->getCurrencyId() }}; - cost = fx.convert(cost, { - from: currencyMap[accountCurrencyId].code, - to: currencyMap[clientCurrencyId].code, - }); + if (clientCurrencyId && clientCurrencyId != accountCurrencyId) { + cost = fx.convert(cost, { + from: currencyMap[accountCurrencyId].code, + to: currencyMap[clientCurrencyId].code, + }); + var rate = fx.convert(1, { + from: currencyMap[accountCurrencyId].code, + to: currencyMap[clientCurrencyId].code, + }); + if ((account.custom_invoice_text_label1 || '').toLowerCase() == 'exchange rate') { + window.model.invoice().custom_text_value1(roundToFour(rate, true)); + } else if ((account.custom_invoice_text_label2 || '').toLowerCase() == 'exchange rate') { + window.model.invoice().custom_text_value2(roundToFour(rate, true)); + } + } } - } + @endif + model.cost(roundSignificant(cost, true)); } }