2015-02-07 18:15:15 -06:00
|
|
|
<?php
|
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;
|
|
|
|
|
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
|
|
|
{
|
2020-10-24 00:55:09 -05:00
|
|
|
use ConvertsDataTypes, AppendsLocationData, ChecksLogin;
|
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 = [
|
2019-06-16 06:16:04 -05:00
|
|
|
'name' => $this->string('name'),
|
|
|
|
'active' => $this->boolean('active'),
|
2021-04-05 03:56:08 -05:00
|
|
|
'account_type_name' => $this->string('objectType'),
|
2019-06-16 06:16:04 -05:00
|
|
|
'currency_id' => $this->integer('currency_id'),
|
2019-06-21 12:10:02 -05:00
|
|
|
'virtual_balance' => $this->string('virtual_balance'),
|
2019-06-16 06:16:04 -05:00
|
|
|
'iban' => $this->string('iban'),
|
|
|
|
'BIC' => $this->string('BIC'),
|
2019-06-21 12:10:02 -05:00
|
|
|
'account_number' => $this->string('account_number'),
|
|
|
|
'account_role' => $this->string('account_role'),
|
|
|
|
'opening_balance' => $this->string('opening_balance'),
|
|
|
|
'opening_balance_date' => $this->date('opening_balance_date'),
|
|
|
|
'cc_type' => $this->string('cc_type'),
|
|
|
|
'cc_monthly_payment_date' => $this->string('cc_monthly_payment_date'),
|
2021-04-06 06:30:09 -05:00
|
|
|
'notes' => $this->stringWithNewlines('notes'),
|
2019-06-16 06:16:04 -05:00
|
|
|
'interest' => $this->string('interest'),
|
|
|
|
'interest_period' => $this->string('interest_period'),
|
|
|
|
'include_net_worth' => '1',
|
2021-04-10 10:26:36 -05:00
|
|
|
'liability_direction' => $this->string('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';
|
|
|
|
}
|
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;
|
|
|
|
$data['account_type_id'] = $this->integer('liability_type_id');
|
2021-04-10 10:26:36 -05:00
|
|
|
$data['opening_balance'] = app('steam')->negative($data['opening_balance']);
|
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 = [
|
2019-06-16 06:16:04 -05:00
|
|
|
'name' => 'required|min:1|uniqueAccountForUser',
|
2019-09-12 00:11:20 -05:00
|
|
|
'opening_balance' => 'numeric|required_with:opening_balance_date|nullable|max:1000000000',
|
2019-06-16 06:16:04 -05:00
|
|
|
'opening_balance_date' => 'date|required_with:opening_balance|nullable',
|
2019-07-31 09:53:09 -05:00
|
|
|
'iban' => ['iban', 'nullable', new UniqueIban(null, $this->string('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',
|
|
|
|
'account_role' => 'in:' . $accountRoles,
|
|
|
|
'active' => 'boolean',
|
2019-07-05 12:43:16 -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',
|
2019-06-16 06:16:04 -05:00
|
|
|
'what' => 'in:' . $types,
|
|
|
|
'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
|
|
|
|
2019-06-22 03:25:34 -05:00
|
|
|
if ('liabilities' === $this->get('objectType')) {
|
2020-03-17 09:02:57 -05:00
|
|
|
$rules['opening_balance'] = ['numeric', 'required', 'max:1000000000'];
|
2019-06-16 06:16:04 -05:00
|
|
|
$rules['opening_balance_date'] = 'date|required';
|
2018-08-04 10:30:06 -05:00
|
|
|
}
|
|
|
|
|
2018-02-22 13:07:14 -06:00
|
|
|
/** @var Account $account */
|
|
|
|
$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';
|
2018-08-07 12:24:07 -05:00
|
|
|
$rules['name'] = 'required|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
|
|
|
}
|