From 7a7f1e7d97db0e2e519c5d9115fcfe5b5090e084 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 7 Aug 2020 11:38:31 +0200 Subject: [PATCH] Fix #3435 --- .../Recurring/CreateController.php | 93 +++++++++++++++++++ resources/lang/en_US/firefly.php | 1 + resources/views/v1/recurring/create.twig | 8 +- resources/views/v1/transactions/show.twig | 4 + routes/breadcrumbs.php | 8 ++ routes/web.php | 1 + 6 files changed, 111 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Recurring/CreateController.php b/app/Http/Controllers/Recurring/CreateController.php index 37d7325c40..e91ba1fbc0 100644 --- a/app/Http/Controllers/Recurring/CreateController.php +++ b/app/Http/Controllers/Recurring/CreateController.php @@ -29,6 +29,8 @@ use FireflyIII\Helpers\Attachments\AttachmentHelperInterface; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Requests\RecurrenceFormRequest; use FireflyIII\Models\RecurrenceRepetition; +use FireflyIII\Models\Transaction; +use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface; use Illuminate\Contracts\View\Factory; @@ -75,6 +77,97 @@ class CreateController extends Controller ); } + /** + * @param Request $request + * @param TransactionJournal $journal + */ + public function createFromJournal(Request $request, TransactionJournal $journal) + { + $budgets = app('expandedform')->makeSelectListWithEmpty($this->budgets->getActiveBudgets()); + $defaultCurrency = app('amount')->getDefaultCurrency(); + $tomorrow = new Carbon; + $oldRepetitionType = $request->old('repetition_type'); + $tomorrow->addDay(); + + // put previous url in session if not redirect from store (not "create another"). + if (true !== session('recurring.create.fromStore')) { + $this->rememberPreviousUri('recurring.create.uri'); + } + $request->session()->forget('recurring.create.fromStore'); + $repetitionEnds = [ + 'forever' => (string) trans('firefly.repeat_forever'), + 'until_date' => (string) trans('firefly.repeat_until_date'), + 'times' => (string) trans('firefly.repeat_times'), + ]; + $weekendResponses = [ + RecurrenceRepetition::WEEKEND_DO_NOTHING => (string) trans('firefly.do_nothing'), + RecurrenceRepetition::WEEKEND_SKIP_CREATION => (string) trans('firefly.skip_transaction'), + RecurrenceRepetition::WEEKEND_TO_FRIDAY => (string) trans('firefly.jump_to_friday'), + RecurrenceRepetition::WEEKEND_TO_MONDAY => (string) trans('firefly.jump_to_monday'), + ]; + + /** @var Transaction $source */ + /** @var Transaction $dest */ + + // fill prefilled with journal info + $type = strtolower($journal->transactionType->type); + + $source = $journal->transactions()->where('amount', '<', 0)->first(); + $dest = $journal->transactions()->where('amount', '>', 0)->first(); + $category = $journal->categories()->first() ? $journal->categories()->first()->name : ''; + $budget = $journal->budgets()->first() ? $journal->budgets()->first()->id : 0; + $hasOldInput = null !== $request->old('_token'); // flash some data + $preFilled = []; + if (true === $hasOldInput) { + $preFilled = [ + 'title' => $request->old('title'), + 'transaction_description' => $request->old('description'), + 'transaction_currency_id' => $request->old('transaction_currency_id'), + 'amount' => $request->old('amount'), + 'foreign_currency_id' => $request->old('foreign_currency_id'), + 'foreign_amount' => $request->old('foreign_amount'), + 'source_id' => $request->old('source_id'), + 'deposit_source_id' => $request->old('deposit_source_id'), + 'destination_id' => $request->old('destination_id'), + 'withdrawal_destination_id' => $request->old('withdrawal_destination_id'), + 'first_date' => $request->old('first_date'), + 'transaction_type' => $request->old('transaction_type'), + 'category' => $request->old('category'), + 'budget_id' => $request->old('budget_id'), + 'active' => (bool) $request->old('active'), + 'apply_rules' => (bool) $request->old('apply_rules'), + ]; + } + if (false === $hasOldInput) { + $preFilled = [ + 'title' => $journal->description, + 'transaction_description' => $journal->description, + 'transaction_currency_id' => $journal->transaction_currency_id, + 'amount' => $dest->amount, + 'foreign_currency_id' => $dest->foreign_currency_id, + 'foreign_amount' => $dest->foreign_amount, + 'source_id' => $source->account_id, + 'deposit_source_id' => $source->account_id, + 'destination_id' => $dest->account_id, + 'withdrawal_destination_id' => $dest->account_id, + 'first_date' => $tomorrow->format('Y-m-d'), + 'transaction_type' => $type, + 'category' => $category, + 'budget_id' => $budget, + 'active' => true, + 'apply_rules' => true, + ]; + } + + + $request->session()->flash('preFilled', $preFilled); + + return view( + 'recurring.create', + compact('tomorrow', 'oldRepetitionType', 'weekendResponses', 'preFilled', 'repetitionEnds', 'defaultCurrency', 'budgets') + ); + } + /** * Create a new recurring transaction. * diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 05feb049de..d91e55a4c7 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -555,6 +555,7 @@ return [ 'select_tags_to_delete' => 'Don\'t forget to select some tags.', 'deleted_x_tags' => 'Deleted :count tag.|Deleted :count tags.', 'create_rule_from_transaction' => 'Create rule based on transaction', + 'create_recurring_from_transaction' => 'Create recurring transaction based on transaction', // preferences diff --git a/resources/views/v1/recurring/create.twig b/resources/views/v1/recurring/create.twig index e8f3990e72..b972eca6d4 100644 --- a/resources/views/v1/recurring/create.twig +++ b/resources/views/v1/recurring/create.twig @@ -87,7 +87,7 @@ {{ ExpandedForm.text('transaction_description') }} {# transaction information (mandatory) #} {{ CurrencyForm.currencyList('transaction_currency_id', defaultCurrency.id) }} - {{ ExpandedForm.amountNoCurrency('amount', []) }} + {{ ExpandedForm.amountNoCurrency('amount') }} {# source account if withdrawal, or if transfer: #} {{ AccountForm.activeLongAccountList('source_id', null, {label: trans('form.asset_source_account')}) }} @@ -115,8 +115,8 @@
{# transaction information (optional) #} - {{ CurrencyForm.currencyListEmpty('foreign_currency_id', 0) }} - {{ ExpandedForm.amountNoCurrency('foreign_amount', []) }} + {{ CurrencyForm.currencyListEmpty('foreign_currency_id') }} + {{ ExpandedForm.amountNoCurrency('foreign_amount') }} {# BUDGET ONLY WHEN CREATING A WITHDRAWAL #} {% if budgets|length > 1 %} @@ -132,7 +132,7 @@ {{ ExpandedForm.text('tags') }} {# RELATE THIS TRANSFER TO A PIGGY BANK #} - {{ PiggyBankForm.piggyBankList('piggy_bank_id',0) }} + {{ PiggyBankForm.piggyBankList('piggy_bank_id') }}
diff --git a/resources/views/v1/transactions/show.twig b/resources/views/v1/transactions/show.twig index 1e0d26d00e..f42c73df69 100644 --- a/resources/views/v1/transactions/show.twig +++ b/resources/views/v1/transactions/show.twig @@ -310,6 +310,10 @@ {{ 'create_rule_from_transaction'|_ }} + + + {{ 'create_recurring_from_transaction'|_ }} + diff --git a/routes/breadcrumbs.php b/routes/breadcrumbs.php index 068bce92b9..275f31c29a 100644 --- a/routes/breadcrumbs.php +++ b/routes/breadcrumbs.php @@ -851,6 +851,14 @@ try { } ); + Breadcrumbs::register( + 'recurring.create-from-journal', + static function (BreadcrumbsGenerator $breadcrumbs) { + $breadcrumbs->parent('recurring.index'); + $breadcrumbs->push(trans('firefly.create_new_recurrence'), route('recurring.create')); + } + ); + // Rules Breadcrumbs::register( 'rules.index', diff --git a/routes/web.php b/routes/web.php index 1b43fd4337..ac660c2f99 100644 --- a/routes/web.php +++ b/routes/web.php @@ -724,6 +724,7 @@ Route::group( Route::get('show/{recurrence}', ['uses' => 'Recurring\ShowController@show', 'as' => 'show']); Route::get('create', ['uses' => 'Recurring\CreateController@create', 'as' => 'create']); + Route::get('create-from-transaction/{tj}', ['uses' => 'Recurring\CreateController@createFromJournal', 'as' => 'create-from-journal']); Route::get('edit/{recurrence}', ['uses' => 'Recurring\EditController@edit', 'as' => 'edit']); Route::get('delete/{recurrence}', ['uses' => 'Recurring\DeleteController@delete', 'as' => 'delete']);