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

137 lines
5.9 KiB
PHP
Raw Normal View History

<?php
2022-12-29 12:42:26 -06:00
/**
* AccountFormRequest.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.
2017-10-21 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05:00
* 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.
2017-10-21 01:40:00 -05:00
*
* 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\Enums\UserRoleEnum;
use FireflyIII\Models\Account;
use FireflyIII\Models\Location;
use FireflyIII\Rules\UniqueIban;
2020-07-18 01:42:13 -05:00
use FireflyIII\Support\Request\AppendsLocationData;
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;
2020-10-24 00:55:09 -05:00
use Illuminate\Foundation\Http\FormRequest;
/**
2017-11-15 05:25:49 -06:00
* Class AccountFormRequest.
*/
2020-10-24 00:55:09 -05:00
class AccountFormRequest extends FormRequest
{
2022-10-30 08:24:28 -05:00
use AppendsLocationData;
use ChecksLogin;
2023-11-04 08:18:49 -05:00
use ConvertsDataTypes;
2023-10-28 08:03:33 -05:00
2023-09-23 00:54:01 -05:00
protected array $acceptedRoles = [UserRoleEnum::MANAGE_TRANSACTIONS];
2016-10-22 14:40:31 -05:00
/**
2018-07-22 01:10:16 -05:00
* Get all data.
*
2016-10-22 14:40:31 -05:00
* @return array
*/
2016-10-22 15:03:00 -05:00
public function getAccountData(): array
2016-10-22 14:40:31 -05:00
{
2018-08-04 10:30:06 -05:00
$data = [
2022-05-02 12:35:35 -05:00
'name' => $this->convertString('name'),
'active' => $this->boolean('active'),
2022-05-02 12:35:35 -05:00
'account_type_name' => $this->convertString('objectType'),
2022-09-30 13:07:01 -05:00
'currency_id' => $this->convertInteger('currency_id'),
2022-05-02 12:35:35 -05:00
'virtual_balance' => $this->convertString('virtual_balance'),
'iban' => $this->convertString('iban'),
'BIC' => $this->convertString('BIC'),
'account_number' => $this->convertString('account_number'),
'account_role' => $this->convertString('account_role'),
'opening_balance' => $this->convertString('opening_balance'),
2021-12-28 13:42:50 -06:00
'opening_balance_date' => $this->getCarbonDate('opening_balance_date'),
2022-05-02 12:35:35 -05:00
'cc_type' => $this->convertString('cc_type'),
'cc_monthly_payment_date' => $this->convertString('cc_monthly_payment_date'),
2021-04-06 06:30:09 -05:00
'notes' => $this->stringWithNewlines('notes'),
2022-05-02 12:35:35 -05:00
'interest' => $this->convertString('interest'),
'interest_period' => $this->convertString('interest_period'),
'include_net_worth' => '1',
2022-05-02 12:35:35 -05:00
'liability_direction' => $this->convertString('liability_direction'),
2016-10-22 14:40:31 -05:00
];
$data = $this->appendLocationData($data, 'location');
if (false === $this->boolean('include_net_worth')) {
$data['include_net_worth'] = '0';
}
if ('0' === $data['opening_balance']) {
2021-06-12 01:46:06 -05:00
$data['opening_balance'] = '';
}
2018-08-04 10:30:06 -05:00
// if the account type is "liabilities" there are actually four types of liability
// that could have been selected.
2021-04-05 03:56:08 -05:00
if ('liabilities' === $data['account_type_name']) {
$data['account_type_name'] = null;
2022-09-30 13:07:01 -05:00
$data['account_type_id'] = $this->convertInteger('liability_type_id');
2021-04-11 23:08:21 -05:00
if ('' !== $data['opening_balance']) {
// opening balance is always positive for liabilities
$data['opening_balance'] = app('steam')->positive($data['opening_balance']);
2021-04-11 23:08:21 -05:00
}
2018-08-04 10:30:06 -05:00
}
return $data;
2016-10-22 14:40:31 -05:00
}
2015-02-11 00:35:10 -06:00
/**
2018-07-22 01:10:16 -05:00
* Rules for this request.
*
2015-02-11 00:35:10 -06:00
* @return array
*/
2018-07-08 05:08:53 -05:00
public function rules(): array
{
$accountRoles = implode(',', config('firefly.accountRoles'));
$types = implode(',', array_keys(config('firefly.subTitlesByIdentifier')));
$ccPaymentTypes = implode(',', array_keys(config('firefly.ccTypes')));
$rules = [
2023-04-25 23:17:04 -05:00
'name' => 'required|max:1024|min:1|uniqueAccountForUser',
2021-06-12 01:46:06 -05:00
'opening_balance' => 'numeric|nullable|max:1000000000',
'opening_balance_date' => 'date|required_with:opening_balance|nullable',
2022-05-02 12:35:35 -05:00
'iban' => ['iban', 'nullable', new UniqueIban(null, $this->convertString('objectType'))],
'BIC' => 'bic|nullable',
2019-09-12 00:11:20 -05:00
'virtual_balance' => 'numeric|nullable|max:1000000000',
'currency_id' => 'exists:transaction_currencies,id',
'account_number' => 'between:1,255|uniqueAccountNumberForUser|nullable',
2023-06-21 05:34:58 -05:00
'account_role' => 'in:' . $accountRoles,
'active' => 'boolean',
2023-06-21 05:34:58 -05:00
'cc_type' => 'in:' . $ccPaymentTypes,
'amount_currency_id_opening_balance' => 'exists:transaction_currencies,id',
2019-06-21 12:10:02 -05:00
'amount_currency_id_virtual_balance' => 'exists:transaction_currencies,id',
2023-06-21 05:34:58 -05:00
'what' => 'in:' . $types,
'interest_period' => 'in:daily,monthly,yearly',
];
$rules = Location::requestRules($rules);
2023-11-05 09:55:16 -06:00
/** @var Account|null $account */
$account = $this->route()->parameter('account');
2018-04-02 08:10:40 -05:00
if (null !== $account) {
// add rules:
$rules['id'] = 'belongsToUser:accounts';
2023-06-21 05:34:58 -05:00
$rules['name'] = 'required|max:1024|min:1|uniqueAccountForUser:' . $account->id;
2018-03-19 02:16:54 -05:00
$rules['iban'] = ['iban', 'nullable', new UniqueIban($account, $account->accountType->type)];
}
return $rules;
}
}