Make sure user interface works for new recurring transactions.

This commit is contained in:
James Cole
2019-08-27 06:36:16 +02:00
parent ce5fcbbda2
commit 4ff8b3b556
10 changed files with 387 additions and 363 deletions

View File

@@ -127,9 +127,10 @@ class EditController extends Controller
'apply_rules' => $hasOldInput ? (bool)$request->old('apply_rules') : $recurrence->apply_rules,
'deposit_source_id' => $array['transactions'][0]['source_id'],
'withdrawal_destination_id' => $array['transactions'][0]['destination_id'],
];
$array['transactions'][0]['tags'] = implode(',', $array['transactions'][0]['tags']);
return view(
'recurring.edit',
compact('recurrence', 'array', 'weekendResponses', 'budgets', 'preFilled', 'currentRepType', 'repetitionEnd', 'repetitionEnds')
@@ -148,6 +149,7 @@ class EditController extends Controller
public function update(RecurrenceFormRequest $request, Recurrence $recurrence)
{
$data = $request->getAll();
$this->recurring->update($recurrence, $data);
$request->session()->flash('success', (string)trans('firefly.updated_recurrence', ['title' => $recurrence->title]));

View File

@@ -68,6 +68,7 @@ class IndexController extends Controller
}
/**
* TODO the notes of a recurrence are pretty pointless at this moment.
* Show all recurring transactions.
*
* @param Request $request
@@ -108,33 +109,4 @@ class IndexController extends Controller
return view('recurring.index', compact('paginator', 'page', 'pageSize', 'total'));
}
/**
* Show a single recurring transaction.
*
* @param Recurrence $recurrence
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws FireflyException
*/
public function show(Recurrence $recurrence)
{
/** @var RecurrenceTransformer $transformer */
$transformer = app(RecurrenceTransformer::class);
$transformer->setParameters(new ParameterBag);
$array = $transformer->transform($recurrence);
$groups = $this->recurring->getTransactions($recurrence);
// transform dates back to Carbon objects:
foreach ($array['recurrence_repetitions'] as $index => $repetition) {
foreach ($repetition['occurrences'] as $item => $occurrence) {
$array['recurrence_repetitions'][$index]['occurrences'][$item] = new Carbon($occurrence);
}
}
$subTitle = (string)trans('firefly.overview_for_recurrence', ['title' => $recurrence->title]);
return view('recurring.show', compact('recurrence', 'subTitle', 'array', 'groups'));
}
}

View File

@@ -0,0 +1,96 @@
<?php
/**
* ShowController.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Recurring;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Recurrence;
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
use FireflyIII\Transformers\RecurrenceTransformer;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
*
* Class ShowController
*/
class ShowController extends Controller
{
use GetConfigurationData;
/** @var RecurringRepositoryInterface Recurring repository */
private $recurring;
/**
* IndexController constructor.
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
app('view')->share('mainTitleIcon', 'fa-paint-brush');
app('view')->share('title', (string)trans('firefly.recurrences'));
$this->recurring = app(RecurringRepositoryInterface::class);
return $next($request);
}
);
}
/**
* Show a single recurring transaction.
*
* @param Recurrence $recurrence
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws FireflyException
*/
public function show(Recurrence $recurrence)
{
/** @var RecurrenceTransformer $transformer */
$transformer = app(RecurrenceTransformer::class);
$transformer->setParameters(new ParameterBag);
$array = $transformer->transform($recurrence);
$groups = $this->recurring->getTransactions($recurrence);
// transform dates back to Carbon objects:
foreach ($array['repetitions'] as $index => $repetition) {
foreach ($repetition['occurrences'] as $item => $occurrence) {
$array['repetitions'][$index]['occurrences'][$item] = new Carbon($occurrence);
}
}
$subTitle = (string)trans('firefly.overview_for_recurrence', ['title' => $recurrence->title]);
return view('recurring.show', compact('recurrence', 'subTitle', 'array', 'groups'));
}
}

View File

@@ -86,15 +86,11 @@ class RecurrenceFormRequest extends Request
'budget_name' => null,
'category_id' => null,
'category_name' => $this->string('category'),
'tags' => '' !== $this->string('tags') ? explode(',', $this->string('tags')) : [],
'piggy_bank_id' => $this->integer('piggy_bank_id'),
'piggy_bank_name' => null,
],
],
'meta' => [
// tags and piggy bank ID.
'tags' => '' !== $this->string('tags') ? explode(',', $this->string('tags')) : [],
'piggy_bank_id' => $this->integer('piggy_bank_id'),
'piggy_bank_name' => null,
],
'repetitions' => [
[
'type' => $repetitionData['type'],
@@ -137,86 +133,6 @@ class RecurrenceFormRequest extends Request
return $return;
}
/**
* Configure the validator instance with special rules for after the basic validation rules.
*
* @param Validator $validator
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
// validate all account info
$this->validateAccountInformation($validator);
}
);
}
/**
* Validates the given account information. Switches on given transaction type.
*
* @param Validator $validator
* @throws FireflyException
*/
public function validateAccountInformation(Validator $validator): void
{
Log::debug('Now in validateAccountInformation()');
/** @var AccountValidator $accountValidator */
$accountValidator = app(AccountValidator::class);
$data = $validator->getData();
$transactionType = $data['transaction_type'] ?? 'invalid';
$accountValidator->setTransactionType($transactionType);
// default values:
$sourceId = null;
$destinationId = null;
switch ($this->string('transaction_type')) {
default:
throw new FireflyException(sprintf('Cannot handle transaction type "%s"', $this->string('transaction_type'))); // @codeCoverageIgnore
case 'withdrawal':
$sourceId = (int)$data['source_id'];
$destinationId = (int)$data['withdrawal_destination_id'];
break;
case 'deposit':
$sourceId = (int)$data['deposit_source_id'];
$destinationId = (int)$data['destination_id'];
break;
case 'transfer':
$sourceId = (int)$data['source_id'];
$destinationId = (int)$data['destination_id'];
break;
}
// validate source account.
$validSource = $accountValidator->validateSource($sourceId, null);
// do something with result:
if (false === $validSource) {
$message = (string)trans('validation.generic_invalid_source');
$validator->errors()->add('source_id', $message);
$validator->errors()->add('deposit_source_id', $message);
return;
}
// validate destination account
$validDestination = $accountValidator->validateDestination($destinationId, null);
// do something with result:
if (false === $validDestination) {
$message = (string)trans('validation.generic_invalid_destination');
$validator->errors()->add('destination_id', $message);
$validator->errors()->add('withdrawal_destination_id', $message);
return;
}
}
/**
* The rules for this request.
*
@@ -310,6 +226,86 @@ class RecurrenceFormRequest extends Request
return $rules;
}
/**
* Validates the given account information. Switches on given transaction type.
*
* @param Validator $validator
*
* @throws FireflyException
*/
public function validateAccountInformation(Validator $validator): void
{
Log::debug('Now in validateAccountInformation()');
/** @var AccountValidator $accountValidator */
$accountValidator = app(AccountValidator::class);
$data = $validator->getData();
$transactionType = $data['transaction_type'] ?? 'invalid';
$accountValidator->setTransactionType($transactionType);
// default values:
$sourceId = null;
$destinationId = null;
switch ($this->string('transaction_type')) {
default:
throw new FireflyException(sprintf('Cannot handle transaction type "%s"', $this->string('transaction_type'))); // @codeCoverageIgnore
case 'withdrawal':
$sourceId = (int)$data['source_id'];
$destinationId = (int)$data['withdrawal_destination_id'];
break;
case 'deposit':
$sourceId = (int)$data['deposit_source_id'];
$destinationId = (int)$data['destination_id'];
break;
case 'transfer':
$sourceId = (int)$data['source_id'];
$destinationId = (int)$data['destination_id'];
break;
}
// validate source account.
$validSource = $accountValidator->validateSource($sourceId, null);
// do something with result:
if (false === $validSource) {
$message = (string)trans('validation.generic_invalid_source');
$validator->errors()->add('source_id', $message);
$validator->errors()->add('deposit_source_id', $message);
return;
}
// validate destination account
$validDestination = $accountValidator->validateDestination($destinationId, null);
// do something with result:
if (false === $validDestination) {
$message = (string)trans('validation.generic_invalid_destination');
$validator->errors()->add('destination_id', $message);
$validator->errors()->add('withdrawal_destination_id', $message);
return;
}
}
/**
* Configure the validator instance with special rules for after the basic validation rules.
*
* @param Validator $validator
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
// validate all account info
$this->validateAccountInformation($validator);
}
);
}
/**
* Parses repetition data.
*