firefly-iii/app/Http/Requests/RecurrenceFormRequest.php

357 lines
15 KiB
PHP
Raw Normal View History

<?php
/**
* RecurrenceFormRequest.php
2020-01-31 00:32:04 -06:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Requests;
use FireflyIII\Exceptions\FireflyException;
2021-03-15 02:51:21 -05:00
use FireflyIII\Factory\CategoryFactory;
use FireflyIII\Models\Recurrence;
use FireflyIII\Models\TransactionType;
use FireflyIII\Rules\ValidRecurrenceRepetitionType;
use FireflyIII\Rules\ValidRecurrenceRepetitionValue;
2020-10-24 00:55:09 -05:00
use FireflyIII\Support\Request\ChecksLogin;
2020-07-18 01:42:13 -05:00
use FireflyIII\Support\Request\ConvertsDataTypes;
use FireflyIII\Validation\AccountValidator;
2020-10-24 00:55:09 -05:00
use Illuminate\Foundation\Http\FormRequest;
2023-04-01 00:04:42 -05:00
use Illuminate\Support\Facades\Log;
2023-05-29 06:56:55 -05:00
use Illuminate\Validation\Validator;
/**
* Class RecurrenceFormRequest
*/
2020-10-24 00:55:09 -05:00
class RecurrenceFormRequest extends FormRequest
{
2022-10-30 08:24:28 -05:00
use ConvertsDataTypes;
use ChecksLogin;
/**
2018-07-22 01:10:16 -05:00
* Get the data required by the controller.
*
2020-10-24 00:55:09 -05:00
* @return array
* @throws FireflyException
2018-07-20 07:34:56 -05:00
*
*/
public function getAll(): array
{
$repetitionData = $this->parseRepetitionData();
$return = [
'recurrence' => [
2022-05-02 12:35:35 -05:00
'type' => $this->convertString('transaction_type'),
'title' => $this->convertString('title'),
'description' => $this->convertString('recurring_description'),
2021-12-28 13:42:50 -06:00
'first_date' => $this->getCarbonDate('first_date'),
'repeat_until' => $this->getCarbonDate('repeat_until'),
2022-09-30 13:07:01 -05:00
'nr_of_repetitions' => $this->convertInteger('repetitions'),
2021-04-04 05:48:44 -05:00
'apply_rules' => $this->boolean('apply_rules'),
'active' => $this->boolean('active'),
2022-05-02 12:35:35 -05:00
'repetition_end' => $this->convertString('repetition_end'),
],
'transactions' => [
[
2022-09-30 13:07:01 -05:00
'currency_id' => $this->convertInteger('transaction_currency_id'),
'currency_code' => null,
2022-05-02 12:35:35 -05:00
'type' => $this->convertString('transaction_type'),
'description' => $this->convertString('transaction_description'),
'amount' => $this->convertString('amount'),
'foreign_amount' => null,
'foreign_currency_id' => null,
'foreign_currency_code' => null,
2022-09-30 13:07:01 -05:00
'budget_id' => $this->convertInteger('budget_id'),
'budget_name' => null,
2022-09-30 13:07:01 -05:00
'bill_id' => $this->convertInteger('bill_id'),
2021-07-18 07:51:30 -05:00
'bill_name' => null,
'category_id' => null,
2022-05-02 12:35:35 -05:00
'category_name' => $this->convertString('category'),
'tags' => '' !== $this->convertString('tags') ? explode(',', $this->convertString('tags')) : [],
2022-09-30 13:07:01 -05:00
'piggy_bank_id' => $this->convertInteger('piggy_bank_id'),
'piggy_bank_name' => null,
],
],
'repetitions' => [
[
'type' => $repetitionData['type'],
'moment' => $repetitionData['moment'],
2022-09-30 13:07:01 -05:00
'skip' => $this->convertInteger('skip'),
'weekend' => $this->convertInteger('weekend'),
],
],
];
// fill in foreign currency data
2022-12-27 14:13:18 -06:00
if (null !== $this->convertFloat('foreign_amount')) { // intentional float, used because it defaults to null.
2022-05-02 12:35:35 -05:00
$return['transactions'][0]['foreign_amount'] = $this->convertString('foreign_amount');
2022-09-30 13:07:01 -05:00
$return['transactions'][0]['foreign_currency_id'] = $this->convertInteger('foreign_currency_id');
}
// 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;
2022-12-30 02:28:03 -06:00
$throwError = true;
$type = $this->convertString('transaction_type');
if ('withdrawal' === $type) {
$throwError = false;
$return['transactions'][0]['source_id'] = $this->convertInteger('source_id');
$return['transactions'][0]['destination_id'] = $this->convertInteger('withdrawal_destination_id');
}
if ('deposit' === $type) {
$throwError = false;
$return['transactions'][0]['source_id'] = $this->convertInteger('deposit_source_id');
$return['transactions'][0]['destination_id'] = $this->convertInteger('destination_id');
}
if ('transfer' === $type) {
$throwError = false;
$return['transactions'][0]['source_id'] = $this->convertInteger('source_id');
$return['transactions'][0]['destination_id'] = $this->convertInteger('destination_id');
}
if (true === $throwError) {
throw new FireflyException(sprintf('Cannot handle transaction type "%s"', $this->convertString('transaction_type')));
}
2021-03-15 02:51:21 -05:00
// replace category name with a new category:
$factory = app(CategoryFactory::class);
$factory->setUser(auth()->user());
2022-12-30 02:28:03 -06:00
/**
2023-06-21 05:34:58 -05:00
* @var int $index
2022-12-30 02:28:03 -06:00
* @var array $transaction
*/
2021-03-21 03:15:40 -05:00
foreach ($return['transactions'] as $index => $transaction) {
$categoryName = $transaction['category_name'] ?? null;
if (null !== $categoryName) {
2021-03-15 02:51:21 -05:00
$category = $factory->findOrCreate(null, $categoryName);
2021-03-21 03:15:40 -05:00
if (null !== $category) {
2021-03-15 02:51:21 -05:00
$return['transactions'][$index]['category_id'] = $category->id;
}
}
}
return $return;
}
2023-06-21 05:34:58 -05:00
/**
* Parses repetition data.
*
* @return array
*/
private function parseRepetitionData(): array
{
$value = $this->convertString('repetition_type');
$return = [
'type' => '',
'moment' => '',
];
if ('daily' === $value) {
$return['type'] = $value;
}
//monthly,17
//ndom,3,7
if (in_array(substr($value, 0, 6), ['yearly', 'weekly'], true)) {
$return['type'] = substr($value, 0, 6);
$return['moment'] = substr($value, 7);
}
if (str_starts_with($value, 'monthly')) {
$return['type'] = substr($value, 0, 7);
$return['moment'] = substr($value, 8);
}
if (str_starts_with($value, 'ndom')) {
$return['type'] = substr($value, 0, 4);
$return['moment'] = substr($value, 5);
}
return $return;
}
/**
2018-07-22 01:10:16 -05:00
* The rules for this request.
*
2020-10-24 00:55:09 -05:00
* @return array
2018-07-20 07:34:56 -05:00
*
*/
public function rules(): array
{
2020-09-11 00:12:33 -05:00
$today = today(config('app.timezone'));
2023-02-11 00:36:45 -06:00
$tomorrow = today(config('app.timezone'))->addDay();
2018-08-06 12:14:30 -05:00
$rules = [
// mandatory info for recurrence.
2018-06-29 22:21:21 -05:00
'title' => 'required|between:1,255|uniqueObjectForUser:recurrences,title',
2023-06-21 05:34:58 -05:00
'first_date' => 'required|date|after:' . $today->format('Y-m-d'),
2022-10-30 08:24:28 -05:00
'repetition_type' => ['required', new ValidRecurrenceRepetitionValue(), new ValidRecurrenceRepetitionType(), 'between:1,20'],
'skip' => 'required|numeric|integer|gte:0|lte:31',
// 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',
// 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',
2020-07-05 23:55:27 -05:00
'amount' => 'numeric|required|gt:0|max:1000000000',
// 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',
// foreign amount data:
2020-07-05 23:55:27 -05:00
'foreign_amount' => 'nullable|gt:0|max:1000000000',
// optional fields:
2018-06-29 22:21:21 -05:00
'budget_id' => 'mustExist:budgets,id|belongsToUser:budgets,id|nullable',
2021-07-18 07:51:30 -05:00
'bill_id' => 'mustExist:bills,id|belongsToUser:bills,id|nullable',
2018-06-29 22:21:21 -05:00
'category' => 'between:1,255|nullable',
'tags' => 'between:1,255|nullable',
];
2022-09-30 13:07:01 -05:00
if ($this->convertInteger('foreign_currency_id') > 0) {
2018-06-18 14:07:09 -05:00
$rules['foreign_currency_id'] = 'exists:transaction_currencies,id';
}
// if ends after X repetitions, set another rule
2022-05-02 12:35:35 -05:00
if ('times' === $this->convertString('repetition_end')) {
$rules['repetitions'] = 'required|numeric|between:0,254';
}
// if foreign amount, currency must be different.
2022-12-27 14:13:18 -06:00
if (null !== $this->convertFloat('foreign_amount')) { // intentional float, used because it defaults to null.
$rules['foreign_currency_id'] = 'exists:transaction_currencies,id|different:transaction_currency_id';
}
// if ends at date X, set another rule.
2022-05-02 12:35:35 -05:00
if ('until_date' === $this->convertString('repetition_end')) {
2023-06-21 05:34:58 -05:00
$rules['repeat_until'] = 'required|date|after:' . $tomorrow->format('Y-m-d');
}
// switch on type to expand rules for source and destination accounts:
2022-12-30 02:28:03 -06:00
$type = strtolower($this->convertString('transaction_type'));
if (strtolower(TransactionType::WITHDRAWAL) === $type) {
$rules['source_id'] = 'required|exists:accounts,id|belongsToUser:accounts';
$rules['destination_name'] = 'between:1,255|nullable';
}
if (strtolower(TransactionType::DEPOSIT) === $type) {
$rules['source_name'] = 'between:1,255|nullable';
$rules['destination_id'] = 'required|exists:accounts,id|belongsToUser:accounts';
}
if (strtolower(TransactionType::TRANSFER) === $type) {
// this may not work:
$rules['source_id'] = 'required|exists:accounts,id|belongsToUser:accounts|different:destination_id';
$rules['destination_id'] = 'required|exists:accounts,id|belongsToUser:accounts|different:source_id';
}
// 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';
2023-06-21 05:34:58 -05:00
$rules['title'] = 'required|between:1,255|uniqueObjectForUser:recurrences,title,' . $recurrence->id;
$rules['first_date'] = 'required|date';
}
2019-02-13 10:38:41 -06:00
return $rules;
}
2023-06-21 05:34:58 -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.
*
2023-06-21 05:34:58 -05:00
* @param Validator $validator
*
* @throws FireflyException
*/
public function validateAccountInformation(Validator $validator): void
{
2021-12-18 05:35:17 -06:00
Log::debug('Now in validateAccountInformation (RecurrenceFormRequest)()');
/** @var AccountValidator $accountValidator */
$accountValidator = app(AccountValidator::class);
$data = $validator->getData();
$transactionType = $data['transaction_type'] ?? 'invalid';
$accountValidator->setTransactionType($transactionType);
// default values:
$sourceId = null;
$destinationId = null;
2022-10-30 05:43:17 -05:00
// TODO typeOverrule: the account validator may have another opinion the transaction type.
2022-12-30 02:28:03 -06:00
// TODO either use 'withdrawal' or the strtolower() variant, not both.
2023-02-22 11:14:14 -06:00
$type = $this->convertString('transaction_type');
2022-12-30 02:28:03 -06:00
$throwError = true;
2022-12-30 13:38:54 -06:00
if ('withdrawal' === $type) {
2023-02-22 11:14:14 -06:00
$throwError = false;
2022-12-30 02:28:03 -06:00
$sourceId = (int)$data['source_id'];
$destinationId = (int)$data['withdrawal_destination_id'];
}
2022-12-30 13:38:54 -06:00
if ('deposit' === $type) {
2023-02-22 11:14:14 -06:00
$throwError = false;
2022-12-30 02:28:03 -06:00
$sourceId = (int)$data['deposit_source_id'];
$destinationId = (int)$data['destination_id'];
}
2022-12-30 13:38:54 -06:00
if ('transfer' === $type) {
2023-02-22 11:14:14 -06:00
$throwError = false;
2022-12-30 02:28:03 -06:00
$sourceId = (int)$data['source_id'];
$destinationId = (int)$data['destination_id'];
}
2022-12-30 13:38:54 -06:00
if (true === $throwError) {
2022-12-30 02:28:03 -06:00
throw new FireflyException(sprintf('Cannot handle transaction type "%s"', $this->convertString('transaction_type')));
}
// validate source account.
2021-12-18 05:35:17 -06:00
$validSource = $accountValidator->validateSource(['id' => $sourceId,]);
// do something with result:
if (false === $validSource) {
2022-12-29 12:42:26 -06:00
$message = (string)trans('validation.generic_invalid_source');
$validator->errors()->add('source_id', $message);
$validator->errors()->add('deposit_source_id', $message);
return;
}
// validate destination account
2021-12-18 05:35:17 -06:00
$validDestination = $accountValidator->validateDestination(['id' => $destinationId,]);
// do something with result:
if (false === $validDestination) {
2022-12-29 12:42:26 -06:00
$message = (string)trans('validation.generic_invalid_destination');
$validator->errors()->add('destination_id', $message);
$validator->errors()->add('withdrawal_destination_id', $message);
}
}
}