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

303 lines
13 KiB
PHP
Raw Normal View History

2015-02-24 15:53:38 -06:00
<?php
/**
* JournalFormRequest.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
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/>.
*/
declare(strict_types=1);
2015-02-24 15:53:38 -06:00
namespace FireflyIII\Http\Requests;
2016-02-17 08:52:46 -06:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionType;
2018-07-02 08:37:56 -05:00
use Illuminate\Validation\Validator;
2018-07-02 22:52:27 -05:00
use Log;
2015-03-29 04:51:26 -05:00
2015-02-24 15:53:38 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class JournalFormRequest.
2015-02-24 15:53:38 -06:00
*/
class JournalFormRequest extends Request
{
/**
* @return bool
*/
public function authorize()
{
// Only allow logged in users
2016-09-16 05:07:45 -05:00
return auth()->check();
2015-02-24 15:53:38 -06:00
}
2015-03-29 04:51:26 -05:00
/**
* Returns and validates the data required to store a new journal. Can handle both single transaction journals and split journals.
*
2015-03-29 04:51:26 -05:00
* @return array
*/
public function getJournalData()
{
$currencyId = $this->integer('amount_currency_id_amount');
$data = [
2018-02-23 09:59:21 -06:00
'type' => $this->get('what'), // type. can be 'deposit', 'withdrawal' or 'transfer'
'date' => $this->date('date'),
'tags' => explode(',', $this->string('tags')),
'user' => auth()->user()->id,
2018-02-23 09:59:21 -06:00
// all custom fields:
2018-02-23 09:59:21 -06:00
'interest_date' => $this->date('interest_date'),
'book_date' => $this->date('book_date'),
'process_date' => $this->date('process_date'),
'due_date' => $this->date('due_date'),
'payment_date' => $this->date('payment_date'),
'invoice_date' => $this->date('invoice_date'),
'internal_reference' => $this->string('internal_reference'),
'notes' => $this->string('notes'),
2018-02-23 09:59:21 -06:00
// journal data:
'description' => $this->string('description'),
'piggy_bank_id' => $this->integer('piggy_bank_id'),
'piggy_bank_name' => null,
2018-02-23 09:59:21 -06:00
'bill_id' => null,
'bill_name' => null,
2018-02-23 09:59:21 -06:00
// transaction data:
'transactions' => [
[
'currency_id' => null,
'currency_code' => null,
'description' => null,
'amount' => $this->string('amount'),
'budget_id' => $this->integer('budget_id'),
'budget_name' => null,
'category_id' => null,
'category_name' => $this->string('category'),
2018-06-29 22:21:21 -05:00
'source_id' => $this->integer('source_id'),
'source_name' => $this->string('source_name'),
'destination_id' => $this->integer('destination_id'),
'destination_name' => $this->string('destination_name'),
2018-02-23 09:59:21 -06:00
'foreign_currency_id' => null,
'foreign_currency_code' => null,
'foreign_amount' => null,
'reconciled' => false,
'identifier' => 0,
],
],
2015-03-29 04:51:26 -05:00
];
switch (strtolower($data['type'])) {
case 'withdrawal':
$sourceCurrency = $this->integer('source_account_currency');
$data['transactions'][0]['currency_id'] = $sourceCurrency;
$data['transactions'][0]['destination_id'] = null; // clear destination ID (transfer)
if ($sourceCurrency !== $currencyId) {
// user has selected a foreign currency.
$data['transactions'][0]['foreign_currency_id'] = $currencyId;
$data['transactions'][0]['foreign_amount'] = $this->string('amount');
$data['transactions'][0]['amount'] = $this->string('native_amount');
}
break;
case 'deposit':
$destinationCurrency = $this->integer('destination_account_currency');
$data['transactions'][0]['currency_id'] = $destinationCurrency;
$data['transactions'][0]['source_id'] = null; // clear destination ID (transfer)
if ($destinationCurrency !== $currencyId) {
// user has selected a foreign currency.
$data['transactions'][0]['foreign_currency_id'] = $currencyId;
$data['transactions'][0]['foreign_amount'] = $this->string('amount');
$data['transactions'][0]['amount'] = $this->string('native_amount');
}
break;
case 'transfer':
// by default just assume source currency
$sourceCurrency = $this->integer('source_account_currency');
$destinationCurrency = $this->integer('destination_account_currency');
$data['transactions'][0]['currency_id'] = $sourceCurrency;
if ($sourceCurrency !== $destinationCurrency) {
// user has selected a foreign currency.
$data['transactions'][0]['foreign_currency_id'] = $destinationCurrency;
$data['transactions'][0]['foreign_amount'] = $this->string('destination_amount');
$data['transactions'][0]['amount'] = $this->string('source_amount');
}
break;
}
return $data;
2015-03-29 04:51:26 -05:00
}
2015-02-24 15:53:38 -06:00
/**
* @return array
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws FireflyException
2015-02-24 15:53:38 -06:00
*/
public function rules()
{
2016-10-23 05:19:32 -05:00
$what = $this->get('what');
2015-02-24 15:53:38 -06:00
$rules = [
2018-01-02 12:53:22 -06:00
'what' => 'required|in:withdrawal,deposit,transfer',
'date' => 'required|date',
'amount_currency_id_amount' => 'exists:transaction_currencies,id|required',
// then, custom fields:
2018-01-02 12:53:22 -06:00
'interest_date' => 'date|nullable',
'book_date' => 'date|nullable',
'process_date' => 'date|nullable',
'due_date' => 'date|nullable',
'payment_date' => 'date|nullable',
'invoice_date' => 'date|nullable',
'internal_reference' => 'min:1,max:255|nullable',
'notes' => 'min:1,max:50000|nullable',
// and then transaction rules:
2018-01-02 12:53:22 -06:00
'description' => 'required|between:1,255',
'amount' => 'numeric|required|more:0',
'budget_id' => 'mustExist:budgets,id|belongsToUser:budgets,id|nullable',
'category' => 'between:1,255|nullable',
2018-06-29 22:21:21 -05:00
'source_id' => 'numeric|belongsToUser:accounts,id|nullable',
'source_name' => 'between:1,255|nullable',
'destination_id' => 'numeric|belongsToUser:accounts,id|nullable',
'destination_name' => 'between:1,255|nullable',
'piggy_bank_id' => 'numeric|nullable',
// foreign currency amounts
2018-01-02 12:53:22 -06:00
'native_amount' => 'numeric|more:0|nullable',
'source_amount' => 'numeric|more:0|nullable',
'destination_amount' => 'numeric|more:0|nullable',
2015-02-24 15:53:38 -06:00
];
// some rules get an upgrade depending on the type of data:
$rules = $this->enhanceRules($what, $rules);
return $rules;
}
2018-07-02 08:37:56 -05:00
/**
* Configure the validator instance.
*
* @param Validator $validator
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
$this->validNativeAmount($validator);
}
);
}
/**
2017-11-15 05:25:49 -06:00
* Inspired by https://www.youtube.com/watch?v=WwnI0RS6J5A.
*
* @param string $what
* @param array $rules
*
* @return array
2017-11-15 05:25:49 -06:00
*
* @throws FireflyException
*/
private function enhanceRules(string $what, array $rules): array
{
2015-02-24 15:53:38 -06:00
switch ($what) {
case strtolower(TransactionType::WITHDRAWAL):
2018-06-29 22:21:21 -05:00
$rules['source_id'] = 'required|exists:accounts,id|belongsToUser:accounts';
$rules['destination_name'] = 'between:1,255|nullable';
2015-02-24 15:53:38 -06:00
break;
case strtolower(TransactionType::DEPOSIT):
2018-06-29 22:21:21 -05:00
$rules['source_name'] = 'between:1,255|nullable';
$rules['destination_id'] = 'required|exists:accounts,id|belongsToUser:accounts';
2015-02-24 15:53:38 -06:00
break;
case strtolower(TransactionType::TRANSFER):
// this may not work:
2018-06-29 22:21:21 -05:00
$rules['source_id'] = 'required|exists:accounts,id|belongsToUser:accounts|different:destination_id';
$rules['destination_id'] = 'required|exists:accounts,id|belongsToUser:accounts|different:source_id';
2016-04-29 10:29:13 -05:00
2015-02-24 15:53:38 -06:00
break;
default:
2018-03-03 10:16:47 -06:00
throw new FireflyException(sprintf('Cannot handle transaction type of type "%s"', $what)); // @codeCoverageIgnore
2015-02-24 15:53:38 -06:00
}
return $rules;
}
2018-07-02 08:37:56 -05:00
/**
* @param Validator $validator
*/
private function validNativeAmount(Validator $validator): void
{
$data = $validator->getData();
$type = $data['what'] ?? 'invalid';
2018-07-02 22:52:27 -05:00
Log::debug(sprintf('Type is %s', $type));
2018-07-08 00:59:58 -05:00
if ('withdrawal' === $type) {
2018-07-02 22:52:27 -05:00
2018-07-02 08:37:56 -05:00
$selectedCurrency = (int)($data['amount_currency_id_amount'] ?? 0);
$accountCurrency = (int)($data['source_account_currency'] ?? 0);
2018-07-02 22:52:27 -05:00
Log::debug(sprintf('Selected currency is %d, account currency is %d', $selectedCurrency, $accountCurrency));
$nativeAmount = (string)($data['native_amount'] ?? '');
if ($selectedCurrency !== $accountCurrency && '' === $nativeAmount
2018-07-08 00:59:58 -05:00
&& 0 !== $selectedCurrency
&& 0 !== $accountCurrency
2018-07-02 22:52:27 -05:00
) {
Log::debug('ADD validation error on native_amount');
$validator->errors()->add('native_amount', trans('validation.numeric_native'));
2018-07-02 08:37:56 -05:00
return;
}
}
// same thing for deposits:
2018-07-08 00:59:58 -05:00
if ('deposit' === $type) {
2018-07-02 08:37:56 -05:00
$selectedCurrency = (int)($data['amount_currency_id_amount'] ?? 0);
$accountCurrency = (int)($data['destination_account_currency'] ?? 0);
2018-07-02 22:52:27 -05:00
$nativeAmount = (string)($data['native_amount'] ?? '');
if ($selectedCurrency !== $accountCurrency && '' === $nativeAmount
2018-07-08 00:59:58 -05:00
&& 0 !== $selectedCurrency
&& 0 !== $accountCurrency
2018-07-02 22:52:27 -05:00
) {
$validator->errors()->add('native_amount', trans('validation.numeric_native'));
2018-07-02 08:37:56 -05:00
return;
}
}
// and for transfers
2018-07-08 00:59:58 -05:00
if ('transfer' === $type) {
2018-07-02 22:52:27 -05:00
2018-07-02 08:37:56 -05:00
$sourceCurrency = (int)($data['source_account_currency'] ?? 0);
$destinationCurrency = (int)($data['destination_account_currency'] ?? 0);
2018-07-02 22:52:27 -05:00
$sourceAmount = (string)($data['source_amount'] ?? '');
$destinationAmount = (string)($data['destination_amount'] ?? '');
Log::debug(sprintf('Source currency is %d, destination currency is %d', $sourceCurrency, $destinationCurrency));
if ($sourceCurrency !== $destinationCurrency && '' === $sourceAmount
2018-07-08 00:59:58 -05:00
&& 0 !== $sourceCurrency
&& 0 !== $destinationCurrency
2018-07-02 22:52:27 -05:00
) {
$validator->errors()->add('source_amount', trans('validation.numeric_source'));
2018-07-02 08:37:56 -05:00
}
2018-07-02 22:52:27 -05:00
if ($sourceCurrency !== $destinationCurrency && '' === $destinationAmount
2018-07-08 00:59:58 -05:00
&& 0 !== $sourceCurrency
&& 0 !== $destinationCurrency
2018-07-02 22:52:27 -05:00
) {
$validator->errors()->add('destination_amount', trans('validation.numeric_destination'));
2018-07-02 08:37:56 -05:00
$validator->errors()->add('destination_amount', trans('validation.numeric', ['attribute' => 'destination_amount']));
}
2018-07-02 22:52:27 -05:00
2018-07-02 08:37:56 -05:00
return;
}
}
2015-03-29 01:14:32 -05:00
}