First code for #658

This commit is contained in:
James Cole 2017-07-07 17:51:14 +02:00
parent e62b979708
commit 19774f32c2
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
5 changed files with 64 additions and 37 deletions

View File

@ -21,6 +21,7 @@ use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\JournalFormRequest;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
@ -86,39 +87,41 @@ class SingleController extends Controller
public function cloneTransaction(TransactionJournal $journal)
{
$source = $journal->sourceAccountList()->first();
$destination = $journal->destinationAccountList()->first();
$budget = $journal->budgets()->first();
$budgetId = is_null($budget) ? 0 : $budget->id;
$category = $journal->categories()->first();
$categoryName = is_null($category) ? '' : $category->name;
$tags = join(',', $journal->tags()->get()->pluck('tag')->toArray());
$transaction = $journal->transactions()->first();
$amount = Steam::positive($transaction->amount);
$foreignAmount = is_null($transaction->foreign_amount) ? null : Steam::positive($transaction->foreign_amount);
$source = $journal->sourceAccountList()->first();
$destination = $journal->destinationAccountList()->first();
$budget = $journal->budgets()->first();
$budgetId = is_null($budget) ? 0 : $budget->id;
$category = $journal->categories()->first();
$categoryName = is_null($category) ? '' : $category->name;
$tags = join(',', $journal->tags()->get()->pluck('tag')->toArray());
/** @var Transaction $transaction */
$transaction = $journal->transactions()->first();
$amount = Steam::positive($transaction->amount);
$foreignAmount = is_null($transaction->foreign_amount) ? null : Steam::positive($transaction->foreign_amount);
$preFilled = [
'description' => $journal->description,
'source_account_id' => $source->id,
'source_account_name' => $source->name,
'destination_account_id' => $destination->id,
'destination_account_name' => $destination->name,
'amount' => $amount,
'source_amount' => $amount,
'destination_amount' => $foreignAmount,
'foreign_amount' => $foreignAmount,
'date' => $journal->date->format('Y-m-d'),
'budget_id' => $budgetId,
'category' => $categoryName,
'tags' => $tags,
'interest_date' => $journal->getMeta('interest_date'),
'book_date' => $journal->getMeta('book_date'),
'process_date' => $journal->getMeta('process_date'),
'due_date' => $journal->getMeta('due_date'),
'payment_date' => $journal->getMeta('payment_date'),
'invoice_date' => $journal->getMeta('invoice_date'),
'internal_reference' => $journal->getMeta('internal_reference'),
'notes' => $journal->getMeta('notes'),
'description' => $journal->description,
'source_account_id' => $source->id,
'source_account_name' => $source->name,
'destination_account_id' => $destination->id,
'destination_account_name' => $destination->name,
'amount' => $amount,
'source_amount' => $amount,
'destination_amount' => $foreignAmount,
'foreign_amount' => $foreignAmount,
'amount_currency_id_amount' => $transaction->foreign_currency_id ?? 0,
'date' => $journal->date->format('Y-m-d'),
'budget_id' => $budgetId,
'category' => $categoryName,
'tags' => $tags,
'interest_date' => $journal->getMeta('interest_date'),
'book_date' => $journal->getMeta('book_date'),
'process_date' => $journal->getMeta('process_date'),
'due_date' => $journal->getMeta('due_date'),
'payment_date' => $journal->getMeta('payment_date'),
'invoice_date' => $journal->getMeta('invoice_date'),
'internal_reference' => $journal->getMeta('internal_reference'),
'notes' => $journal->getMeta('notes'),
];
Session::flash('preFilled', $preFilled);

View File

@ -565,6 +565,19 @@ class ExpandedForm
unset($options['currency']);
unset($options['placeholder']);
// perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount)
$preFilled = session('preFilled');
$key = 'amount_currency_id_' . $name;
$sentCurrencyId = isset($preFilled[$key]) ? intval($preFilled[$key]) : $defaultCurrency->id;
// find this currency in set of currencies:
foreach ($currencies as $currency) {
if ($currency->id === $sentCurrencyId) {
$defaultCurrency = $currency;
break;
}
}
// make sure value is formatted nicely:
if (!is_null($value) && $value !== '') {
$value = round($value, $defaultCurrency->decimal_places);

View File

@ -23,7 +23,6 @@ $(document).ready(function () {
updateForm();
updateLayout();
updateDescription();
updateNativeCurrency();
// when user changes source account or destination, native currency may be different.
@ -60,9 +59,15 @@ function getExchangeInstructions() {
* There is an input that shows the currency symbol that is native to the selected
* acccount. So when the user changes the selected account, the native currency is updated:
*/
function updateNativeCurrency() {
var newAccountId = getAccountId();
var nativeCurrencyId = accountInfo[newAccountId].preferredCurrency;
function updateNativeCurrency(useAccountCurrency) {
var nativeCurrencyId;
if (useAccountCurrency) {
var newAccountId = getAccountId();
nativeCurrencyId = accountInfo[newAccountId].preferredCurrency;
}
if (!useAccountCurrency) {
nativeCurrencyId = overruleCurrency;
}
$('.currency-option[data-id="' + nativeCurrencyId + '"]').click();
$('[data-toggle="dropdown"]').parent().removeClass('open');
@ -180,7 +185,9 @@ function updateForm() {
default:
break;
}
updateNativeCurrency();
// get instructions all the time.
updateNativeCurrency(useAccountCurrency);
selectsForeignCurrency();
}
/**

View File

@ -7,7 +7,7 @@
<button type="button"
class="btn btn-default dropdown-toggle currency-dropdown" id="currency_dropdown_{{ name }}" data-toggle="dropdown"
aria-expanded="false">
<span id="currency_select_symbol_{{ name }}">{{ defaultCurrency.symbol|raw }}</span> <span class="caret"></span>
<span id="currency_select_symbol_{{ name }}">{{ defaultCurrency.symbol|raw }}X</span> <span class="caret"></span>
</button>
<ul class="dropdown-menu currency-dropdown-menu" role="menu">
{% for currency in currencies %}

View File

@ -231,6 +231,10 @@
button['{{ type }}'] = '{{ trans('form.store_new_' ~ type) }}';
{% endfor %}
// some code for the foreign amount logic:
var useAccountCurrency = {% if preFilled.amount_currency_id_amount > 0 %}false{% else %}true{% endif %};
var overruleCurrency = {{ preFilled.amount_currency_id_amount|default(0) }};
</script>
<script type="text/javascript" src="js/lib/bootstrap3-typeahead.min.js"></script>
<script type="text/javascript" src="js/lib/bootstrap-tagsinput.min.js"></script>