2018-06-16 14:47:51 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* RecurrenceFormRequest.php
|
|
|
|
* Copyright (c) 2018 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\Requests;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2018-06-21 11:57:51 -05:00
|
|
|
use FireflyIII\Models\Recurrence;
|
2018-06-16 14:47:51 -05:00
|
|
|
use FireflyIII\Models\TransactionType;
|
|
|
|
use FireflyIII\Rules\ValidRecurrenceRepetitionType;
|
2018-06-17 08:14:34 -05:00
|
|
|
use FireflyIII\Rules\ValidRecurrenceRepetitionValue;
|
2019-07-01 13:22:35 -05:00
|
|
|
use FireflyIII\Validation\AccountValidator;
|
|
|
|
use Illuminate\Validation\Validator;
|
|
|
|
use Log;
|
2018-06-16 14:47:51 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class RecurrenceFormRequest
|
|
|
|
*/
|
|
|
|
class RecurrenceFormRequest extends Request
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Verify the request.
|
|
|
|
*
|
2018-06-16 14:47:51 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize(): bool
|
|
|
|
{
|
|
|
|
// Only allow logged in users
|
|
|
|
return auth()->check();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Get the data required by the controller.
|
|
|
|
*
|
2018-06-16 14:47:51 -05:00
|
|
|
* @return array
|
|
|
|
* @throws FireflyException
|
2018-07-20 07:34:56 -05:00
|
|
|
*
|
2018-06-16 14:47:51 -05:00
|
|
|
*/
|
|
|
|
public function getAll(): array
|
|
|
|
{
|
2018-06-17 08:14:34 -05:00
|
|
|
$repetitionData = $this->parseRepetitionData();
|
|
|
|
$return = [
|
2018-06-16 14:47:51 -05:00
|
|
|
'recurrence' => [
|
2018-06-21 11:57:51 -05:00
|
|
|
'type' => $this->string('transaction_type'),
|
|
|
|
'title' => $this->string('title'),
|
|
|
|
'description' => $this->string('recurring_description'),
|
|
|
|
'first_date' => $this->date('first_date'),
|
|
|
|
'repeat_until' => $this->date('repeat_until'),
|
|
|
|
'repetitions' => $this->integer('repetitions'),
|
|
|
|
'apply_rules' => $this->boolean('apply_rules'),
|
|
|
|
'active' => $this->boolean('active'),
|
|
|
|
'repetition_end' => $this->string('repetition_end'),
|
2018-06-16 14:47:51 -05:00
|
|
|
],
|
|
|
|
'transactions' => [
|
|
|
|
[
|
2018-06-29 23:14:39 -05:00
|
|
|
'currency_id' => $this->integer('transaction_currency_id'),
|
|
|
|
'currency_code' => null,
|
|
|
|
'type' => $this->string('transaction_type'),
|
|
|
|
'description' => $this->string('transaction_description'),
|
|
|
|
'amount' => $this->string('amount'),
|
|
|
|
'foreign_amount' => null,
|
|
|
|
'foreign_currency_id' => null,
|
|
|
|
'foreign_currency_code' => null,
|
|
|
|
'budget_id' => $this->integer('budget_id'),
|
|
|
|
'budget_name' => null,
|
|
|
|
'category_id' => null,
|
|
|
|
'category_name' => $this->string('category'),
|
2018-06-16 14:47:51 -05:00
|
|
|
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'meta' => [
|
|
|
|
// tags and piggy bank ID.
|
2018-06-29 23:14:39 -05:00
|
|
|
'tags' => '' !== $this->string('tags') ? explode(',', $this->string('tags')) : [],
|
|
|
|
'piggy_bank_id' => $this->integer('piggy_bank_id'),
|
|
|
|
'piggy_bank_name' => null,
|
2018-06-16 14:47:51 -05:00
|
|
|
],
|
|
|
|
'repetitions' => [
|
|
|
|
[
|
2018-06-26 22:37:56 -05:00
|
|
|
'type' => $repetitionData['type'],
|
|
|
|
'moment' => $repetitionData['moment'],
|
|
|
|
'skip' => $this->integer('skip'),
|
|
|
|
'weekend' => $this->integer('weekend'),
|
2018-06-16 14:47:51 -05:00
|
|
|
],
|
|
|
|
],
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
// fill in foreign currency data
|
|
|
|
if (null !== $this->float('foreign_amount')) {
|
|
|
|
$return['transactions'][0]['foreign_amount'] = $this->string('foreign_amount');
|
|
|
|
$return['transactions'][0]['foreign_currency_id'] = $this->integer('foreign_currency_id');
|
|
|
|
}
|
2018-06-29 23:14:39 -05:00
|
|
|
// default values:
|
|
|
|
$return['transactions'][0]['source_id'] = null;
|
|
|
|
$return['transactions'][0]['source_name'] = null;
|
|
|
|
$return['transactions'][0]['destination_id'] = null;
|
|
|
|
$return['transactions'][0]['destination_name'] = null;
|
2018-06-16 14:47:51 -05:00
|
|
|
// fill in source and destination account data
|
|
|
|
switch ($this->string('transaction_type')) {
|
|
|
|
default:
|
2018-09-02 13:13:25 -05:00
|
|
|
throw new FireflyException(sprintf('Cannot handle transaction type "%s"', $this->string('transaction_type'))); // @codeCoverageIgnore
|
2018-06-16 14:47:51 -05:00
|
|
|
case 'withdrawal':
|
2019-06-29 12:47:40 -05:00
|
|
|
$return['transactions'][0]['source_id'] = $this->integer('source_id');
|
|
|
|
$return['transactions'][0]['destination_id'] = $this->integer('withdrawal_destination_id');
|
2018-06-16 14:47:51 -05:00
|
|
|
break;
|
2018-06-17 08:14:34 -05:00
|
|
|
case 'deposit':
|
2019-06-29 12:47:40 -05:00
|
|
|
$return['transactions'][0]['source_id'] = $this->integer('deposit_source_id');
|
2018-06-29 22:21:21 -05:00
|
|
|
$return['transactions'][0]['destination_id'] = $this->integer('destination_id');
|
2018-06-17 08:14:34 -05:00
|
|
|
break;
|
|
|
|
case 'transfer':
|
2018-06-29 22:21:21 -05:00
|
|
|
$return['transactions'][0]['source_id'] = $this->integer('source_id');
|
|
|
|
$return['transactions'][0]['destination_id'] = $this->integer('destination_id');
|
2018-06-17 08:14:34 -05:00
|
|
|
break;
|
2018-06-16 14:47:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2019-07-01 13:22:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 14:47:51 -05:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* The rules for this request.
|
|
|
|
*
|
2018-06-16 14:47:51 -05:00
|
|
|
* @return array
|
|
|
|
* @throws FireflyException
|
2018-07-20 07:34:56 -05:00
|
|
|
*
|
2019-08-17 03:47:10 -05:00
|
|
|
*
|
2018-06-16 14:47:51 -05:00
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
$today = new Carbon;
|
2018-08-04 10:30:06 -05:00
|
|
|
$tomorrow = Carbon::now()->addDay();
|
2018-08-06 12:14:30 -05:00
|
|
|
$rules = [
|
2018-06-16 14:47:51 -05:00
|
|
|
// mandatory info for recurrence.
|
2018-06-29 22:21:21 -05:00
|
|
|
'title' => 'required|between:1,255|uniqueObjectForUser:recurrences,title',
|
|
|
|
'first_date' => 'required|date|after:' . $today->format('Y-m-d'),
|
|
|
|
'repetition_type' => ['required', new ValidRecurrenceRepetitionValue, new ValidRecurrenceRepetitionType, 'between:1,20'],
|
|
|
|
'skip' => 'required|numeric|between:0,31',
|
2018-06-16 14:47:51 -05:00
|
|
|
|
|
|
|
// optional for recurrence:
|
2018-06-29 22:21:21 -05:00
|
|
|
'recurring_description' => 'between:0,65000',
|
|
|
|
'active' => 'numeric|between:0,1',
|
|
|
|
'apply_rules' => 'numeric|between:0,1',
|
2018-06-16 14:47:51 -05:00
|
|
|
|
|
|
|
// mandatory for transaction:
|
2018-06-29 22:21:21 -05:00
|
|
|
'transaction_description' => 'required|between:1,255',
|
|
|
|
'transaction_type' => 'required|in:withdrawal,deposit,transfer',
|
|
|
|
'transaction_currency_id' => 'required|exists:transaction_currencies,id',
|
|
|
|
'amount' => 'numeric|required|more:0',
|
2018-06-16 14:47:51 -05:00
|
|
|
// mandatory account info:
|
2018-06-29 22:21:21 -05:00
|
|
|
'source_id' => 'numeric|belongsToUser:accounts,id|nullable',
|
|
|
|
'source_name' => 'between:1,255|nullable',
|
|
|
|
'destination_id' => 'numeric|belongsToUser:accounts,id|nullable',
|
|
|
|
'destination_name' => 'between:1,255|nullable',
|
2018-06-16 14:47:51 -05:00
|
|
|
|
|
|
|
// foreign amount data:
|
2018-06-29 22:21:21 -05:00
|
|
|
'foreign_amount' => 'nullable|more:0',
|
2018-06-16 14:47:51 -05:00
|
|
|
|
|
|
|
// optional fields:
|
2018-06-29 22:21:21 -05:00
|
|
|
'budget_id' => 'mustExist:budgets,id|belongsToUser:budgets,id|nullable',
|
|
|
|
'category' => 'between:1,255|nullable',
|
|
|
|
'tags' => 'between:1,255|nullable',
|
2018-06-16 14:47:51 -05:00
|
|
|
];
|
2018-06-18 14:07:09 -05:00
|
|
|
if ($this->integer('foreign_currency_id') > 0) {
|
|
|
|
$rules['foreign_currency_id'] = 'exists:transaction_currencies,id';
|
|
|
|
}
|
2018-06-16 14:47:51 -05:00
|
|
|
|
|
|
|
// if ends after X repetitions, set another rule
|
2018-07-08 00:59:58 -05:00
|
|
|
if ('times' === $this->string('repetition_end')) {
|
2018-06-16 14:47:51 -05:00
|
|
|
$rules['repetitions'] = 'required|numeric|between:0,254';
|
|
|
|
}
|
|
|
|
// if foreign amount, currency must be different.
|
2018-07-26 21:46:21 -05:00
|
|
|
if (null !== $this->float('foreign_amount')) {
|
2018-06-16 14:47:51 -05:00
|
|
|
$rules['foreign_currency_id'] = 'exists:transaction_currencies,id|different:transaction_currency_id';
|
|
|
|
}
|
|
|
|
|
|
|
|
// if ends at date X, set another rule.
|
2018-07-08 00:59:58 -05:00
|
|
|
if ('until_date' === $this->string('repetition_end')) {
|
2018-06-16 14:47:51 -05:00
|
|
|
$rules['repeat_until'] = 'required|date|after:' . $tomorrow->format('Y-m-d');
|
|
|
|
}
|
|
|
|
|
|
|
|
// switchc on type to expand rules for source and destination accounts:
|
|
|
|
switch ($this->string('transaction_type')) {
|
|
|
|
case strtolower(TransactionType::WITHDRAWAL):
|
2018-06-29 22:21:21 -05:00
|
|
|
$rules['source_id'] = 'required|exists:accounts,id|belongsToUser:accounts';
|
|
|
|
$rules['destination_name'] = 'between:1,255|nullable';
|
2018-06-16 14:47:51 -05:00
|
|
|
break;
|
|
|
|
case strtolower(TransactionType::DEPOSIT):
|
2018-06-29 22:21:21 -05:00
|
|
|
$rules['source_name'] = 'between:1,255|nullable';
|
|
|
|
$rules['destination_id'] = 'required|exists:accounts,id|belongsToUser:accounts';
|
2018-06-16 14:47:51 -05:00
|
|
|
break;
|
|
|
|
case strtolower(TransactionType::TRANSFER):
|
|
|
|
// this may not work:
|
2018-06-29 22:21:21 -05:00
|
|
|
$rules['source_id'] = 'required|exists:accounts,id|belongsToUser:accounts|different:destination_id';
|
|
|
|
$rules['destination_id'] = 'required|exists:accounts,id|belongsToUser:accounts|different:source_id';
|
2018-06-16 14:47:51 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new FireflyException(sprintf('Cannot handle transaction type of type "%s"', $this->string('transaction_type'))); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
2018-06-21 11:57:51 -05:00
|
|
|
// update some rules in case the user is editing a post:
|
|
|
|
/** @var Recurrence $recurrence */
|
|
|
|
$recurrence = $this->route()->parameter('recurrence');
|
|
|
|
if ($recurrence instanceof Recurrence) {
|
|
|
|
$rules['id'] = 'required|numeric|exists:recurrences,id';
|
|
|
|
$rules['title'] = 'required|between:1,255|uniqueObjectForUser:recurrences,title,' . $recurrence->id;
|
|
|
|
$rules['first_date'] = 'required|date';
|
|
|
|
}
|
2019-02-13 10:38:41 -06:00
|
|
|
|
2018-06-16 14:47:51 -05:00
|
|
|
return $rules;
|
|
|
|
}
|
2018-06-17 08:14:34 -05:00
|
|
|
|
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Parses repetition data.
|
|
|
|
*
|
2018-06-17 08:14:34 -05:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function parseRepetitionData(): array
|
|
|
|
{
|
|
|
|
$value = $this->string('repetition_type');
|
|
|
|
$return = [
|
|
|
|
'type' => '',
|
|
|
|
'moment' => '',
|
|
|
|
];
|
|
|
|
|
2018-07-08 00:59:58 -05:00
|
|
|
if ('daily' === $value) {
|
2018-06-17 08:14:34 -05:00
|
|
|
$return['type'] = $value;
|
|
|
|
}
|
|
|
|
//monthly,17
|
|
|
|
//ndom,3,7
|
2019-06-21 12:10:02 -05:00
|
|
|
if (in_array(substr($value, 0, 6), ['yearly', 'weekly'])) {
|
2018-06-17 08:14:34 -05:00
|
|
|
$return['type'] = substr($value, 0, 6);
|
|
|
|
$return['moment'] = substr($value, 7);
|
|
|
|
}
|
|
|
|
if (0 === strpos($value, 'monthly')) {
|
|
|
|
$return['type'] = substr($value, 0, 7);
|
|
|
|
$return['moment'] = substr($value, 8);
|
|
|
|
}
|
|
|
|
if (0 === strpos($value, 'ndom')) {
|
|
|
|
$return['type'] = substr($value, 0, 4);
|
|
|
|
$return['moment'] = substr($value, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2018-07-22 13:32:02 -05:00
|
|
|
}
|