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

127 lines
5.4 KiB
PHP
Raw Normal View History

<?php
/**
* 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\Models\Account;
use FireflyIII\Models\Location;
use FireflyIII\Rules\UniqueIban;
2020-07-18 01:42:13 -05:00
use FireflyIII\Support\Request\AppendsLocationData;
use FireflyIII\Support\Request\ConvertsDataTypes;
/**
2017-11-15 05:25:49 -06:00
* Class AccountFormRequest.
*/
2020-10-18 13:24:47 -05:00
class AccountFormRequest extends LoggedInRequest
{
2020-07-18 01:42:13 -05:00
use ConvertsDataTypes, AppendsLocationData;
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 = [
'name' => $this->string('name'),
'active' => $this->boolean('active'),
2019-06-21 22:51:32 -05:00
'account_type' => $this->string('objectType'),
'account_type_id' => 0,
'currency_id' => $this->integer('currency_id'),
2019-06-21 12:10:02 -05:00
'virtual_balance' => $this->string('virtual_balance'),
'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'),
'notes' => $this->nlString('notes'),
'interest' => $this->string('interest'),
'interest_period' => $this->string('interest_period'),
'include_net_worth' => '1',
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';
}
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.
if ('liabilities' === $data['account_type']) {
$data['account_type'] = null;
2018-08-04 10:30:06 -05:00
$data['account_type_id'] = $this->integer('liability_type_id');
}
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 = [
'name' => 'required|min:1|uniqueAccountForUser',
2019-09-12 00:11:20 -05:00
'opening_balance' => 'numeric|required_with:opening_balance_date|nullable|max:1000000000',
'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'))],
'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',
'account_role' => 'in:' . $accountRoles,
'active' => 'boolean',
'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',
'what' => 'in:' . $types,
'interest_period' => 'in:daily,monthly,yearly',
];
$rules = Location::requestRules($rules);
2019-06-22 03:25:34 -05:00
if ('liabilities' === $this->get('objectType')) {
$rules['opening_balance'] = ['numeric', 'required', 'max:1000000000'];
$rules['opening_balance_date'] = 'date|required';
2018-08-04 10:30:06 -05:00
}
/** @var Account $account */
$account = $this->route()->parameter('account');
2018-04-02 08:10:40 -05:00
if (null !== $account) {
// 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)];
}
return $rules;
}
}