2015-02-07 18:15:15 -06:00
|
|
|
<?php
|
2022-12-29 12:42:26 -06:00
|
|
|
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* AccountFormRequest.php
|
2020-01-31 00:32:04 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* 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
|
|
|
*
|
2019-10-01 23:37:26 -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
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -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/>.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2015-02-07 18:15:15 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Requests;
|
|
|
|
|
2023-09-19 23:17:56 -05:00
|
|
|
use FireflyIII\Enums\UserRoleEnum;
|
2018-02-22 13:07:14 -06:00
|
|
|
use FireflyIII\Models\Account;
|
2019-12-31 07:01:53 -06:00
|
|
|
use FireflyIII\Models\Location;
|
2018-02-22 13:07:14 -06:00
|
|
|
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;
|
2015-02-07 18:15:15 -06:00
|
|
|
|
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class AccountFormRequest.
|
2015-02-07 18:15:15 -06:00
|
|
|
*/
|
2020-10-24 00:55:09 -05:00
|
|
|
class AccountFormRequest extends FormRequest
|
2015-02-07 18:15:15 -06:00
|
|
|
{
|
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];
|
2015-02-07 18:15:15 -06:00
|
|
|
|
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'),
|
2019-06-16 06:16:04 -05:00
|
|
|
'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'),
|
2019-06-16 06:16:04 -05:00
|
|
|
'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
|
|
|
];
|
2020-01-02 12:12:40 -06:00
|
|
|
|
|
|
|
$data = $this->appendLocationData($data, 'location');
|
2018-08-26 11:40:38 -05:00
|
|
|
if (false === $this->boolean('include_net_worth')) {
|
|
|
|
$data['include_net_worth'] = '0';
|
|
|
|
}
|
2021-09-18 03:26:12 -05:00
|
|
|
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']) {
|
2023-01-15 00:20:58 -06:00
|
|
|
// 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
|
2015-02-07 18:15:15 -06:00
|
|
|
{
|
2018-03-28 12:37:59 -05:00
|
|
|
$accountRoles = implode(',', config('firefly.accountRoles'));
|
|
|
|
$types = implode(',', array_keys(config('firefly.subTitlesByIdentifier')));
|
|
|
|
$ccPaymentTypes = implode(',', array_keys(config('firefly.ccTypes')));
|
2018-02-22 13:07:14 -06:00
|
|
|
$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',
|
2019-06-16 06:16:04 -05:00
|
|
|
'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'))],
|
2019-06-16 06:16:04 -05:00
|
|
|
'BIC' => 'bic|nullable',
|
2019-09-12 00:11:20 -05:00
|
|
|
'virtual_balance' => 'numeric|nullable|max:1000000000',
|
2019-06-16 06:16:04 -05:00
|
|
|
'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,
|
2019-06-16 06:16:04 -05:00
|
|
|
'active' => 'boolean',
|
2023-06-21 05:34:58 -05:00
|
|
|
'cc_type' => 'in:' . $ccPaymentTypes,
|
2019-06-16 06:16:04 -05:00
|
|
|
'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,
|
2019-06-16 06:16:04 -05:00
|
|
|
'interest_period' => 'in:daily,monthly,yearly',
|
2015-02-07 18:15:15 -06:00
|
|
|
];
|
2020-03-17 09:02:57 -05:00
|
|
|
$rules = Location::requestRules($rules);
|
2018-02-22 13:07:14 -06:00
|
|
|
|
2023-11-05 09:55:16 -06:00
|
|
|
/** @var Account|null $account */
|
2018-02-22 13:07:14 -06:00
|
|
|
$account = $this->route()->parameter('account');
|
2018-04-02 08:10:40 -05:00
|
|
|
if (null !== $account) {
|
2018-02-22 13:07:14 -06:00
|
|
|
// 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)];
|
2018-02-22 13:07:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return $rules;
|
2015-02-07 18:15:15 -06:00
|
|
|
}
|
2015-03-25 09:55:39 -05:00
|
|
|
}
|