diff --git a/app/Http/Controllers/Transaction/SplitController.php b/app/Http/Controllers/Transaction/SplitController.php index 275318fc26..e4830db142 100644 --- a/app/Http/Controllers/Transaction/SplitController.php +++ b/app/Http/Controllers/Transaction/SplitController.php @@ -14,6 +14,7 @@ use ExpandedForm; use FireflyIII\Crud\Split\JournalInterface; use FireflyIII\Events\TransactionJournalUpdated; use FireflyIII\Exceptions\FireflyException; +use FireflyIII\Helpers\Attachments\AttachmentHelperInterface; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Requests\SplitJournalFormRequest; use FireflyIII\Models\Transaction; @@ -150,21 +151,31 @@ class SplitController extends Controller } /** - * @param TransactionJournal $journal - * @param SplitJournalFormRequest $request - * @param JournalInterface $repository + * @param TransactionJournal $journal + * @param SplitJournalFormRequest $request + * @param JournalInterface $repository + * @param AttachmentHelperInterface $att * * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ - public function update(TransactionJournal $journal, SplitJournalFormRequest $request, JournalInterface $repository) + public function update(TransactionJournal $journal, SplitJournalFormRequest $request, JournalInterface $repository, AttachmentHelperInterface $att) { $data = $request->getSplitData(); $journal = $repository->updateJournal($journal, $data); + // save attachments: + $att->saveAttachmentsForModel($journal); + event(new TransactionJournalUpdated($journal)); // update, get events by date and sort DESC + // flash messages + if (count($att->getMessages()->get('attachments')) > 0) { + Session::flash('info', $att->getMessages()->get('attachments')); + } + + $type = strtolower($journal->transaction_type_type ?? TransactionJournal::transactionTypeStr($journal)); Session::flash('success', strval(trans('firefly.updated_' . $type, ['description' => e($data['journal_description'])]))); Preferences::mark(); diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index ec3f07f866..6e731b4d68 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -22,6 +22,7 @@ use FireflyIII\Http\Requests\MassDeleteJournalRequest; use FireflyIII\Http\Requests\MassEditJournalRequest; use FireflyIII\Models\PiggyBank; use FireflyIII\Models\PiggyBankEvent; +use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; @@ -408,7 +409,7 @@ class TransactionController extends Controller * * @return \Illuminate\View\View */ - public function show(TransactionJournal $journal) + public function show(TransactionJournal $journal, JournalRepositoryInterface $repository) { /** @var Collection $set */ @@ -420,11 +421,21 @@ class TransactionController extends Controller ); // TODO different for each transaction type! + /** @var Collection $transactions */ $transactions = $journal->transactions()->groupBy('transactions.account_id')->orderBy('amount', 'ASC')->get( ['transactions.*', DB::raw('SUM(`transactions`.`amount`) as `sum`')] ); - $what = strtolower($journal->transaction_type_type ?? $journal->transactionType->type); - $subTitle = trans('firefly.' . $what) . ' "' . e($journal->description) . '"'; + + // foreach do balance thing + $transactions->each( + function (Transaction $t) use ($repository) { + $t->before = $repository->balanceBeforeTransaction($t); + } + ); + + + $what = strtolower($journal->transaction_type_type ?? $journal->transactionType->type); + $subTitle = trans('firefly.' . $what) . ' "' . e($journal->description) . '"'; return view('transactions.show', compact('journal', 'events', 'subTitle', 'what', 'transactions')); } diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php index 2557b67407..92a5e45e59 100644 --- a/app/Models/TransactionJournal.php +++ b/app/Models/TransactionJournal.php @@ -319,6 +319,9 @@ class TransactionJournal extends TransactionJournalSupport return $query->where('transaction_journals.date', '<=', $date->format('Y-m-d 00:00:00')); } + /** + * @param EloquentBuilder $query + */ public function scopeSortCorrectly(EloquentBuilder $query) { $query->orderBy('transaction_journals.date', 'DESC'); diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index d0f8631b8a..426b996ae8 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -435,6 +435,7 @@ class AccountRepository implements AccountRepositoryInterface } /** + * * @param Account $account * @param Carbon $date * diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index 7d05411f8c..f24b9ea15b 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -41,6 +41,44 @@ class JournalRepository implements JournalRepositoryInterface $this->user = $user; } + /** + * Returns the amount in the account before the specified transaction took place. + * + * @param Transaction $transaction + * + * @return string + */ + public function balanceBeforeTransaction(Transaction $transaction): string + { + // some dates from journal + $journal = $transaction->transactionJournal; + $query = Transaction:: + leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') + ->where('transactions.account_id', $transaction->account_id) + ->where('transaction_journals.user_id', $this->user->id) + ->where( + function (Builder $q) use ($journal) { + $q->where('transaction_journals.date', '<', $journal->date->format('Y-m-d')); + $q->orWhere( + function (Builder $qq) use ($journal) { + $qq->where('transaction_journals.date', '=', $journal->date->format('Y-m-d')); + $qq->where('transaction_journals.order', '>', $journal->order); + } + ); + + } + ) + ->where('transactions.id', '!=', $transaction->id) + ->whereNull('transactions.deleted_at') + ->whereNull('transaction_journals.deleted_at') + ->orderBy('transaction_journals.date', 'DESC') + ->orderBy('transaction_journals.order', 'ASC') + ->orderBy('transaction_journals.id', 'DESC'); + $sum = $query->sum('transactions.amount'); + + return strval($sum); + } + /** * @param TransactionJournal $journal * diff --git a/app/Repositories/Journal/JournalRepositoryInterface.php b/app/Repositories/Journal/JournalRepositoryInterface.php index a31c8cf076..76677f8a21 100644 --- a/app/Repositories/Journal/JournalRepositoryInterface.php +++ b/app/Repositories/Journal/JournalRepositoryInterface.php @@ -41,6 +41,16 @@ interface JournalRepositoryInterface */ public function first(): TransactionJournal; + + /** + * Returns the amount in the account before the specified transaction took place. + * + * @param Transaction $transaction + * + * @return string + */ + public function balanceBeforeTransaction(Transaction $transaction): string; + /** * Returns the amount in the account before the specified transaction took place. * diff --git a/resources/views/split/journals/edit.twig b/resources/views/split/journals/edit.twig index 8f94f1a411..6299967e51 100644 --- a/resources/views/split/journals/edit.twig +++ b/resources/views/split/journals/edit.twig @@ -4,7 +4,8 @@ {{ Breadcrumbs.renderIfExists }} {% endblock %} {% block content %} - {{ Form.model(journal, {'class' : 'form-horizontal','id' : 'update','url' : route('split.journal.update',journal.id) } ) }} +