Various last minute bug fixes.

Signed-off-by: James Cole <thegrumpydictator@gmail.com>
This commit is contained in:
James Cole 2016-05-22 16:38:32 +02:00
parent 44960e8e42
commit 7e6d3c9d6b
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 123 additions and 24 deletions

View File

@ -18,9 +18,13 @@ use FireflyIII\Events\TransactionJournalUpdated;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\SplitJournalFormRequest;
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Log;
use Preferences;
use Session;
use Steam;
@ -225,9 +229,99 @@ class SplitController extends Controller
'budget_id' => [],
'category' => [],
];
$index = 0;
/** @var Transaction $transaction */
foreach ($journal->transactions()->get() as $transaction) {
// number of transactions present in old input:
$previousCount = count($request->old('description'));
if ($previousCount === 0) {
// build from scratch
$transactions = $this->transactionsFromJournal($request, $journal);
$array['description'] = $transactions['description'];
$array['source_account_id'] = $transactions['source_account_id'];
$array['source_account_name'] = $transactions['source_account_name'];
$array['destination_account_id'] = $transactions['destination_account_id'];
$array['destination_account_name'] = $transactions['destination_account_name'];
$array['amount'] = $transactions['amount'];
$array['budget_id'] = $transactions['budget_id'];
$array['category'] = $transactions['category'];
return $array;
}
$index = 0;
while ($index < $previousCount) {
$description = $request->old('description')[$index] ?? '';
$destinationId = $request->old('destination_account_id')[$index] ?? 0;
$destinationName = $request->old('destination_account_name')[$index] ?? '';
$sourceId = $request->old('source_account_id')[$index] ?? 0;
$sourceName = $request->old('source_account_name')[$index] ?? '';
$amount = $request->old('amount')[$index] ?? '';
$budgetId = $request->old('budget_id')[$index] ?? 0;
$categoryName = $request->old('category')[$index] ?? '';
// any transfer not from the source:
$array['description'][] = $description;
$array['source_account_id'][] = $sourceId;
$array['source_account_name'][] = $sourceName;
$array['destination_account_id'][] = $destinationId;
$array['destination_account_name'][] = $destinationName;
$array['amount'][] = $amount;
$array['budget_id'][] = intval($budgetId);
$array['category'][] = $categoryName;
$index++;
}
return $array;
}
/**
* @param Request $request
* @param TransactionJournal $journal
*
* @return array
*/
private function transactionsFromJournal(Request $request, TransactionJournal $journal): array
{
/** @var Collection $transactions */
$transactions = $journal->transactions()->get();
/*
* Splitted journals always have ONE source OR ONE destination.
* Withdrawals have ONE source (asset account)
* Deposits have ONE destination (asset account)
* Transfers have ONE of both (asset account)
*/
/** @var Account $singular */
$singular = TransactionJournal::sourceAccountList($journal)->first();
if ($journal->transactionType->type == TransactionType::DEPOSIT) {
/** @var Account $singular */
$singular = TransactionJournal::destinationAccountList($journal)->first();
}
/*
* Loop all transactions. Collect info ONLY from the transaction that is NOT related to
* the singular account.
*/
$index = 0;
$return = [
'description' => [],
'source_account_id' => [],
'source_account_name' => [],
'destination_account_id' => [],
'destination_account_name' => [],
'amount' => [],
'budget_id' => [],
'category' => [],
];
Log::debug('now at transactionsFromJournal');
/**
* @var int $current
* @var Transaction $transaction
*/
foreach ($transactions as $current => $transaction) {
$budget = $transaction->budgets()->first();
$category = $transaction->categories()->first();
$budgetId = 0;
@ -245,22 +339,23 @@ class SplitController extends Controller
$amount = $request->old('amount')[$index] ?? $transaction->amount;
$description = $request->old('description')[$index] ?? $transaction->description;
$destinationName = $request->old('destination_account_name')[$index] ?? $transaction->account->name;
$sourceName = $request->old('source_account_name')[$index] ?? $transaction->account->name;
$amount = bccomp($amount, '0') === -1 ? bcmul($amount, '-1') : $amount;
// any transfer not from the source:
if ($transaction->account_id !== $sourceAccounts->first()->id) {
$array['description'][] = $description;
$array['destination_account_id'][] = $transaction->account_id;
$array['destination_account_name'][] = $destinationName;
$array['amount'][] = $amount;
$array['budget_id'][] = intval($budgetId);
$array['category'][] = $categoryName;
if ($transaction->account_id !== $singular->id) {
$return['description'][] = $description;
$return['destination_account_id'][] = $transaction->account_id;
$return['destination_account_name'][] = $destinationName;
$return['source_account_name'][] = $sourceName;
$return['amount'][] = $amount;
$return['budget_id'][] = intval($budgetId);
$return['category'][] = $categoryName;
// only add one when "valid" transaction
$index++;
}
}
return $array;
return $return;
}
}

View File

@ -46,9 +46,13 @@
{{ ExpandedForm.select('journal_source_account_id', assetAccounts, preFilled.journal_source_account_id) }}
{% endif %}
<!-- show destination account id, if deposit (is asset): -->
{% if preFilled.what == 'deposit' %}
{{ ExpandedForm.select('journal_destination_account_id', assetAccounts, preFilled.journal_destination_account_id) }}
{% endif %}
<!-- show static source if deposit: -->
{% if preFilled.what == 'deposit' %}
{{ ExpandedForm.text('journal_source_account_name', preFilled.journal_source_account_name) }}
{% endif %}
<!-- show static destination if transfer -->
@ -119,10 +123,10 @@
</td>
{% endif %}
<!-- deposit has several destination id's -->
<!-- deposit has several source names -->
{% if preFilled.what == 'deposit' %}
<td>
{{ Form.select('destination_account_id[]', assetAccounts, preFilled.destination_account_id[index], {class: 'form-control'}) }}
<input type="text" name="source_account_name[]" value="{{ preFilled.source_account_name[index] }}" class="form-control"/>
</td>
{% endif %}

View File

@ -901,8 +901,8 @@
],
"source_id": 1,
"amounts": [
14,
35,
15,
34,
51
],
"category_ids": [
@ -948,8 +948,8 @@
],
"destination_id": 1,
"amounts": [
14,
35,
15,
34,
51
],
"category_ids": [
@ -962,7 +962,7 @@
"multi-transfers": [
{
"user_id": 1,
"date": "2016-03-02",
"date": "2016-01-18",
"description": "Even multi-transfer (50, 50)",
"source_ids": [
4,
@ -983,7 +983,7 @@
},
{
"user_id": 1,
"date": "2016-05-02",
"date": "2016-03-28",
"description": "Uneven multi-transfer (15,34,51)",
"source_ids": [
4,
@ -996,8 +996,8 @@
5
],
"amounts": [
14,
35,
15,
34,
51
],
"category_ids": [