2015-02-07 18:15:15 -06:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* AccountFormRequest.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 07:44:05 -06:00
|
|
|
* along with Firefly III. If not, see <http://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;
|
|
|
|
use FireflyIII\Rules\UniqueIban;
|
2018-10-16 23:06:36 -05:00
|
|
|
use FireflyIII\Rules\ZeroOrMore;
|
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
|
|
|
*/
|
|
|
|
class AccountFormRequest extends Request
|
|
|
|
{
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Verify the request.
|
|
|
|
*
|
2015-02-11 00:35:10 -06:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-07-08 05:08:53 -05:00
|
|
|
public function authorize(): bool
|
2015-02-07 18:15:15 -06:00
|
|
|
{
|
|
|
|
// Only allow logged in users
|
2016-09-16 05:07:45 -05:00
|
|
|
return auth()->check();
|
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 = [
|
2017-06-05 04:12:50 -05:00
|
|
|
'name' => $this->string('name'),
|
|
|
|
'active' => $this->boolean('active'),
|
|
|
|
'accountType' => $this->string('what'),
|
2018-02-21 14:06:59 -06:00
|
|
|
'account_type_id' => 0,
|
2017-06-05 04:12:50 -05:00
|
|
|
'currency_id' => $this->integer('currency_id'),
|
2017-11-17 22:45:58 -06:00
|
|
|
'virtualBalance' => $this->string('virtualBalance'),
|
2017-06-05 04:12:50 -05:00
|
|
|
'iban' => $this->string('iban'),
|
|
|
|
'BIC' => $this->string('BIC'),
|
|
|
|
'accountNumber' => $this->string('accountNumber'),
|
|
|
|
'accountRole' => $this->string('accountRole'),
|
2017-11-17 22:45:58 -06:00
|
|
|
'openingBalance' => $this->string('openingBalance'),
|
2017-06-05 04:12:50 -05:00
|
|
|
'openingBalanceDate' => $this->date('openingBalanceDate'),
|
|
|
|
'ccType' => $this->string('ccType'),
|
|
|
|
'ccMonthlyPaymentDate' => $this->string('ccMonthlyPaymentDate'),
|
2017-12-30 14:04:04 -06:00
|
|
|
'notes' => $this->string('notes'),
|
2018-08-04 10:30:06 -05:00
|
|
|
'interest' => $this->string('interest'),
|
|
|
|
'interest_period' => $this->string('interest_period'),
|
2018-08-26 11:40:38 -05:00
|
|
|
'include_net_worth' => '1',
|
2016-10-22 14:40:31 -05:00
|
|
|
];
|
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.
|
2018-08-06 12:14:30 -05:00
|
|
|
if ('liabilities' === $data['accountType']) {
|
2018-08-04 10:30:06 -05:00
|
|
|
$data['accountType'] = null;
|
|
|
|
$data['account_type_id'] = $this->integer('liability_type_id');
|
|
|
|
// also reverse the opening balance:
|
|
|
|
if ('' !== $data['openingBalance']) {
|
|
|
|
$data['openingBalance'] = bcmul($data['openingBalance'], '-1');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = [
|
|
|
|
'name' => 'required|min:1|uniqueAccountForUser',
|
2017-09-12 14:44:31 -05:00
|
|
|
'openingBalance' => 'numeric|required_with:openingBalanceDate|nullable',
|
|
|
|
'openingBalanceDate' => 'date|required_with:openingBalance|nullable',
|
2018-03-19 02:16:54 -05:00
|
|
|
'iban' => ['iban', 'nullable', new UniqueIban(null, $this->string('what'))],
|
2017-09-12 14:44:31 -05:00
|
|
|
'BIC' => 'bic|nullable',
|
|
|
|
'virtualBalance' => 'numeric|nullable',
|
2016-11-25 11:00:29 -06:00
|
|
|
'currency_id' => 'exists:transaction_currencies,id',
|
2017-09-12 14:44:31 -05:00
|
|
|
'accountNumber' => 'between:1,255|uniqueAccountNumberForUser|nullable',
|
2015-12-25 10:10:04 -06:00
|
|
|
'accountRole' => 'in:' . $accountRoles,
|
|
|
|
'active' => 'boolean',
|
|
|
|
'ccType' => 'in:' . $ccPaymentTypes,
|
|
|
|
'ccMonthlyPaymentDate' => 'date',
|
|
|
|
'amount_currency_id_openingBalance' => 'exists:transaction_currencies,id',
|
|
|
|
'amount_currency_id_virtualBalance' => 'exists:transaction_currencies,id',
|
2016-01-15 16:12:52 -06:00
|
|
|
'what' => 'in:' . $types,
|
2018-08-04 10:30:06 -05:00
|
|
|
'interest_period' => 'in:daily,monthly,yearly',
|
2015-02-07 18:15:15 -06:00
|
|
|
];
|
2018-02-22 13:07:14 -06:00
|
|
|
|
2018-08-04 10:30:06 -05:00
|
|
|
if ('liabilities' === $this->get('what')) {
|
2018-10-16 23:06:36 -05:00
|
|
|
$rules['openingBalance'] = ['numeric', 'required', new ZeroOrMore];
|
2018-08-04 10:30:06 -05:00
|
|
|
$rules['openingBalanceDate'] = 'date|required';
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|