2016-04-29 20:59:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* SplitJournalFormRequest.php
|
2017-10-21 08:40:00 +02:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-04-29 20:59:28 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02: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 14:44:05 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-04-29 20:59:28 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2016-05-20 12:41:23 +02:00
|
|
|
|
2016-04-29 20:59:28 +02:00
|
|
|
namespace FireflyIII\Http\Requests;
|
|
|
|
|
2018-03-10 09:44:04 +01:00
|
|
|
use Illuminate\Validation\Validator;
|
|
|
|
|
2016-04-29 20:59:28 +02:00
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class SplitJournalFormRequest.
|
2016-04-29 20:59:28 +02:00
|
|
|
*/
|
|
|
|
class SplitJournalFormRequest extends Request
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize(): bool
|
|
|
|
{
|
|
|
|
// Only allow logged in users
|
2016-09-16 12:07:45 +02:00
|
|
|
return auth()->check();
|
2016-04-29 20:59:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-02-25 16:04:25 +01:00
|
|
|
public function getAll(): array
|
2016-04-29 20:59:28 +02:00
|
|
|
{
|
|
|
|
$data = [
|
2018-03-07 10:18:36 +01:00
|
|
|
'description' => $this->string('journal_description'),
|
2018-02-25 16:04:25 +01:00
|
|
|
'type' => $this->string('what'),
|
|
|
|
'date' => $this->date('date'),
|
|
|
|
'tags' => explode(',', $this->string('tags')),
|
|
|
|
'bill_id' => null,
|
|
|
|
'bill_name' => null,
|
|
|
|
'piggy_bank_id' => null,
|
|
|
|
'piggy_bank_name' => null,
|
|
|
|
'notes' => $this->string('notes'),
|
|
|
|
'transactions' => [],
|
2016-04-29 20:59:28 +02:00
|
|
|
];
|
2018-02-25 16:04:25 +01:00
|
|
|
// switch type to get correct source / destination info:
|
|
|
|
$sourceId = null;
|
|
|
|
$sourceName = null;
|
|
|
|
$destinationId = null;
|
|
|
|
$destinationName = null;
|
|
|
|
|
|
|
|
foreach ($this->get('transactions') as $index => $transaction) {
|
|
|
|
switch ($data['type']) {
|
|
|
|
case 'withdrawal':
|
|
|
|
$sourceId = $this->integer('journal_source_account_id');
|
|
|
|
$destinationName = $transaction['destination_account_name'];
|
|
|
|
break;
|
|
|
|
case 'deposit':
|
|
|
|
$sourceName = $transaction['source_account_name'];
|
|
|
|
$destinationId = $this->integer('journal_destination_account_id');
|
|
|
|
break;
|
|
|
|
case 'transfer':
|
|
|
|
$sourceId = $this->integer('journal_source_account_id');
|
|
|
|
$destinationId = $this->integer('journal_destination_account_id');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$foreignAmount = $transaction['foreign_amount'] ?? null;
|
2018-03-09 04:47:43 +01:00
|
|
|
$foreignCurrencyId = intval($transaction['foreign_currency_id'] ?? 0);
|
2018-02-25 16:04:25 +01:00
|
|
|
$set = [
|
|
|
|
'source_id' => $sourceId,
|
|
|
|
'source_name' => $sourceName,
|
|
|
|
'destination_id' => $destinationId,
|
|
|
|
'destination_name' => $destinationName,
|
|
|
|
'foreign_amount' => $foreignAmount,
|
2018-03-09 04:47:43 +01:00
|
|
|
'foreign_currency_id' => $foreignCurrencyId,
|
2018-02-25 16:04:25 +01:00
|
|
|
'foreign_currency_code' => null,
|
|
|
|
'reconciled' => false,
|
|
|
|
'identifier' => $index,
|
|
|
|
'currency_id' => $this->integer('journal_currency_id'),
|
|
|
|
'currency_code' => null,
|
|
|
|
'description' => $transaction['description'],
|
|
|
|
'amount' => $transaction['amount'],
|
|
|
|
'budget_id' => intval($transaction['budget_id'] ?? 0),
|
|
|
|
'budget_name' => null,
|
|
|
|
'category_id' => null,
|
|
|
|
'category_name' => $transaction['category'],
|
|
|
|
];
|
|
|
|
$data['transactions'][] = $set;
|
|
|
|
}
|
2016-05-05 18:59:46 +02:00
|
|
|
|
2016-04-29 20:59:28 +02:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return [
|
2017-09-09 06:25:20 +02:00
|
|
|
'what' => 'required|in:withdrawal,deposit,transfer',
|
|
|
|
'journal_description' => 'required|between:1,255',
|
|
|
|
'id' => 'numeric|belongsToUser:transaction_journals,id',
|
|
|
|
'journal_source_account_id' => 'numeric|belongsToUser:accounts,id',
|
|
|
|
'journal_source_account_name.*' => 'between:1,255',
|
|
|
|
'journal_currency_id' => 'required|exists:transaction_currencies,id',
|
|
|
|
'date' => 'required|date',
|
2017-09-12 21:44:31 +02:00
|
|
|
'interest_date' => 'date|nullable',
|
|
|
|
'book_date' => 'date|nullable',
|
|
|
|
'process_date' => 'date|nullable',
|
2017-09-09 06:25:20 +02:00
|
|
|
'transactions.*.description' => 'required|between:1,255',
|
|
|
|
'transactions.*.destination_account_id' => 'numeric|belongsToUser:accounts,id',
|
2017-09-12 21:44:31 +02:00
|
|
|
'transactions.*.destination_account_name' => 'between:1,255|nullable',
|
2017-09-09 06:25:20 +02:00
|
|
|
'transactions.*.amount' => 'required|numeric',
|
|
|
|
'transactions.*.budget_id' => 'belongsToUser:budgets,id',
|
2017-09-12 21:44:31 +02:00
|
|
|
'transactions.*.category' => 'between:1,255|nullable',
|
|
|
|
'transactions.*.piggy_bank_id' => 'between:1,255|nullable',
|
2016-04-29 20:59:28 +02:00
|
|
|
];
|
|
|
|
}
|
2016-08-12 15:50:52 +02:00
|
|
|
|
2018-03-10 09:44:04 +01:00
|
|
|
/**
|
|
|
|
* Configure the validator instance.
|
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function withValidator(Validator $validator): void
|
|
|
|
{
|
|
|
|
$validator->after(
|
|
|
|
function (Validator $validator) {
|
|
|
|
$this->sameAccounts($validator);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function sameAccounts(Validator $validator): void
|
|
|
|
{
|
|
|
|
$data = $this->getAll();
|
|
|
|
$transactions = $data['transactions'] ?? [];
|
|
|
|
/** @var array $array */
|
|
|
|
foreach ($transactions as $array) {
|
|
|
|
if ($array['destination_id'] !== null && $array['source_id'] !== null) {
|
|
|
|
if ($array['destination_id'] === $array['source_id']) {
|
|
|
|
$validator->errors()->add('journal_source_account_id', trans('validation.source_equals_destination'));
|
|
|
|
$validator->errors()->add('journal_destination_account_id', trans('validation.source_equals_destination'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-12 15:10:03 +02:00
|
|
|
}
|