diff --git a/app/Http/Controllers/Transaction/SplitController.php b/app/Http/Controllers/Transaction/SplitController.php index aedbc0589b..ee63c6a2ed 100644 --- a/app/Http/Controllers/Transaction/SplitController.php +++ b/app/Http/Controllers/Transaction/SplitController.php @@ -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; } } \ No newline at end of file diff --git a/resources/views/split/journals/edit.twig b/resources/views/split/journals/edit.twig index afa285cb7e..44deb1c9e1 100644 --- a/resources/views/split/journals/edit.twig +++ b/resources/views/split/journals/edit.twig @@ -46,9 +46,13 @@ {{ ExpandedForm.select('journal_source_account_id', assetAccounts, preFilled.journal_source_account_id) }} {% endif %} + + {% if preFilled.what == 'deposit' %} + {{ ExpandedForm.select('journal_destination_account_id', assetAccounts, preFilled.journal_destination_account_id) }} + {% endif %} + {% if preFilled.what == 'deposit' %} - {{ ExpandedForm.text('journal_source_account_name', preFilled.journal_source_account_name) }} {% endif %} @@ -119,10 +123,10 @@ {% endif %} - + {% if preFilled.what == 'deposit' %} - {{ Form.select('destination_account_id[]', assetAccounts, preFilled.destination_account_id[index], {class: 'form-control'}) }} + {% endif %} diff --git a/storage/database/seed.local.json b/storage/database/seed.local.json index 740ca45205..0a22dcf358 100644 --- a/storage/database/seed.local.json +++ b/storage/database/seed.local.json @@ -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": [