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'),
],
],
'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.
*

View File

@@ -91,7 +91,7 @@ class RecurrenceUpdateService
// update all meta data:
//$this->updateMetaData($recurrence, $data);
if (null !== $data['recurrence']['notes']) {
if (isset($data['recurrence']['notes']) && null !== $data['recurrence']['notes']) {
$this->setNoteText($recurrence, $data['recurrence']['notes']);
}

View File

@@ -178,6 +178,8 @@ class RecurrenceTransformer extends AbstractTransformer
switch ($transactionMeta->name) {
default:
throw new FireflyException(sprintf('Recurrence transformer cant handle field "%s"', $transactionMeta->name));
case 'bill_id':
break;
case 'tags':
$array['tags'] = json_decode($transactionMeta->value);
break;

View File

@@ -40,6 +40,7 @@ return [
'lastActivity' => 'Last activity',
'balanceDiff' => 'Balance difference',
'matchesOn' => 'Matched on',
'other_meta_data' => 'Other meta data',
'account_type' => 'Account type',
'created_at' => 'Created at',
'account' => 'Account',

View File

@@ -125,20 +125,10 @@
{{ ExpandedForm.text('category',array.transactions[0].category_name) }}
{# TAGS #}
{% set tags = '' %}
{% set piggyBankId = 0 %}
{% for metaValue in array.meta %}
{% if metaValue.name == 'tags' %}
{% set tags = metaValue.value %}
{% endif %}
{% if metaValue.name == 'piggy_bank_id' %}
{% set piggyBankId = metaValue.value %}
{% endif %}
{% endfor %}
{{ ExpandedForm.text('tags', tags) }}
{{ ExpandedForm.text('tags', array.transactions[0].tags) }}
{# RELATE THIS TRANSFER TO A PIGGY BANK #}
{{ PiggyBankForm.piggyBankList('piggy_bank_id',piggyBankId) }}
{{ PiggyBankForm.piggyBankList('piggy_bank_id',array.transactions[0].piggy_bank_id) }}
</div>
</div>
</div>

View File

@@ -13,7 +13,7 @@
<h3 class="box-title">
{{ array.title }}
({{ array.transaction_type }})
({{ array.type }})
{% if array.active == false %}
({{ 'inactive'|_|lower }})
@@ -48,13 +48,13 @@
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">
{{ ('expected_'~array.transaction_type~'s')|_ }}
{{ ('expected_'~array.type~'s')|_ }}
</h3>
</div>
<div class="box-body">
<ul>
{% for rep in array.recurrence_repetitions %}
{% for rep in array.repetitions %}
<li>
{{ rep.description }}
{% if rep.repetition_skip == 1 %}
@@ -82,7 +82,7 @@
</div>
<div class="row">
<!-- transactions -->
<div class="col-lg-8 col-md-12 col-sm-12">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">
@@ -92,12 +92,15 @@
<div class="box-body no-padding">
<table class="table table-hover sortable">
<thead>
<tr>
<th data-defaultsign="az">{{ trans('list.description') }}</th>
<th data-defaultsign="az">{{ trans('list.source') }}</th>
<th data-defaultsign="az">{{ trans('list.destination') }}</th>
<th data-defaultsign="_19">{{ trans('list.amount') }}</th>
<th data-defaultsign="az">{{ trans('list.category') }}</th>
<th data-defaultsign="az">{{ trans('list.budget') }}</th>
<th>{{ trans('list.other_meta_data') }}</th>
</tr>
</thead>
<tbody>
{% for transaction in array.transactions %}
@@ -117,77 +120,39 @@
({{ formatAmountBySymbol(transaction.foreign_amount,transaction.foreign_currency_symbol,transaction.foreign_currency_decimal_places) }})
{% endif %}
</td>
<td data-value="{% for meta in transaction.meta %}{% if meta.name == 'category_name' %}{{ meta.category_id }}{% endif %}{% endfor %}">
{% for meta in transaction.meta %}
{% if meta.name == 'category_name' %}
<a href="{{ route('categories.show', [meta.category_id]) }}">
{{ meta.category_name }}
<td data-value="{{ transaction.category_id|default(0) }}">
{% if '' != transaction.category_name %}
<a href="{{ route('categories.show', [transaction.category_id]) }}">
{{ transaction.category_name }}
</a>
{% endif %}
{% endfor %}
</td>
<td data-value="{% for meta in transaction.meta %}{% if meta.name == 'budget_id' %}{{ meta.budget_id }}{% endif %}{% endfor %}">
{% for meta in transaction.meta %}
{% if meta.name == 'budget_id' %}
<a href="{{ route('budgets.show', [meta.budget_id]) }}">
{{ meta.budget_name }}
<td data-value="{{ transaction.budget_id|default(0) }}">
{% if '' != transaction.budget_name %}
<a href="{{ route('budgets.show', [transaction.budget_id]) }}">
{{ transaction.budget_name }}
</a>
{% endif %}
{% endfor %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
<!-- meta data -->
{% if array.meta|length > 0 %}
<div class="col-lg-4 col-md-12 col-sm-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">
{{ 'meta_data'|_ }}
</h3>
</div>
<div class="box-body no-padding">
<table class="table table-hover sortable">
<thead>
<th style="width:30%;" data-defaultsign="az">{{ trans('list.field') }}</th>
<th data-defaultsign="az">{{ trans('list.value') }}</th>
</thead>
<tbody>
{% for meta in array.meta %}
<tr>
<td>{{ trans('firefly.recurring_meta_field_'~meta.name) }}</td>
<td>
{% if meta.name == 'tags' %}
{% for tag in meta.tags %}
<span class="label label-info">{{ tag }}</span>
{% if transaction.tags|length > 0 %}
<p>
{% for tag in transaction.tags %}
<span class="label label-success">{{ tag }}</span>
{% endfor %}
</p>
{% endif %}
{% if meta.name == 'notes' %}
{{ meta.value|markdown }}
{% endif %}
{% if meta.name == 'bill_id' %}
<a href="{{ route('bills.show', [meta.bill_id]) }}">{{ meta.bill_name }}</a>
{% endif %}
{% if meta.name == 'piggy_bank_id' %}
<a href="{{ route('piggy-banks.show', [meta.piggy_bank_id]) }}">{{ meta.piggy_bank_name }}</a>
{% if 0 != transaction.piggy_bank_id and array.type == 'Transfer' %}
<a href="{{ route('piggy-banks.show', [transaction.piggy_bank_id]) }}">{{ transaction.piggy_bank_name }}</a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
{% endif %}
</div>
<div class="row">
<!-- meta data -->
<div class="col-lg-12 col-md-12 col-sm-12">

View File

@@ -657,7 +657,7 @@ Route::group(
Route::get('', ['uses' => 'Recurring\IndexController@index', 'as' => 'index']);
Route::get('show/{recurrence}', ['uses' => 'Recurring\IndexController@show', 'as' => 'show']);
Route::get('show/{recurrence}', ['uses' => 'Recurring\ShowController@show', 'as' => 'show']);
Route::get('create', ['uses' => 'Recurring\CreateController@create', 'as' => 'create']);
Route::get('edit/{recurrence}', ['uses' => 'Recurring\EditController@edit', 'as' => 'edit']);
Route::get('delete/{recurrence}', ['uses' => 'Recurring\DeleteController@delete', 'as' => 'delete']);