2016-04-29 13:59:28 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* SplitJournalFormRequest.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-04-29 13:59:28 -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-04-29 13:59:28 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-05-20 05:41:23 -05:00
|
|
|
|
2016-04-29 13:59:28 -05:00
|
|
|
namespace FireflyIII\Http\Requests;
|
|
|
|
|
2017-02-13 13:39:23 -06:00
|
|
|
use Steam;
|
2016-04-29 13:59:28 -05:00
|
|
|
|
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class SplitJournalFormRequest.
|
2016-04-29 13:59:28 -05:00
|
|
|
*/
|
|
|
|
class SplitJournalFormRequest extends Request
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize(): bool
|
|
|
|
{
|
|
|
|
// Only allow logged in users
|
2016-09-16 05:07:45 -05:00
|
|
|
return auth()->check();
|
2016-04-29 13:59:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSplitData(): array
|
|
|
|
{
|
|
|
|
$data = [
|
2017-01-21 01:32:23 -06:00
|
|
|
'id' => $this->integer('id'),
|
|
|
|
'journal_description' => $this->string('journal_description'),
|
|
|
|
'journal_currency_id' => $this->integer('journal_currency_id'),
|
|
|
|
'journal_source_account_id' => $this->integer('journal_source_account_id'),
|
|
|
|
'journal_source_account_name' => $this->string('journal_source_account_name'),
|
|
|
|
'journal_destination_account_id' => $this->integer('journal_destination_account_id'),
|
|
|
|
'journal_destination_account_name' => $this->string('journal_source_destination_name'),
|
|
|
|
'date' => $this->date('date'),
|
|
|
|
'what' => $this->string('what'),
|
|
|
|
'interest_date' => $this->date('interest_date'),
|
|
|
|
'book_date' => $this->date('book_date'),
|
|
|
|
'process_date' => $this->date('process_date'),
|
2016-08-12 08:50:52 -05:00
|
|
|
'transactions' => $this->getTransactionData(),
|
2016-04-29 13:59:28 -05:00
|
|
|
];
|
2016-05-05 11:59:46 -05:00
|
|
|
|
2016-04-29 13:59:28 -05:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return [
|
2017-09-08 23:25:20 -05: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 14:44:31 -05:00
|
|
|
'interest_date' => 'date|nullable',
|
|
|
|
'book_date' => 'date|nullable',
|
|
|
|
'process_date' => 'date|nullable',
|
2017-09-08 23:25:20 -05:00
|
|
|
'transactions.*.description' => 'required|between:1,255',
|
|
|
|
'transactions.*.destination_account_id' => 'numeric|belongsToUser:accounts,id',
|
2017-09-12 14:44:31 -05:00
|
|
|
'transactions.*.destination_account_name' => 'between:1,255|nullable',
|
2017-09-08 23:25:20 -05:00
|
|
|
'transactions.*.amount' => 'required|numeric',
|
|
|
|
'transactions.*.budget_id' => 'belongsToUser:budgets,id',
|
2017-09-12 14:44:31 -05:00
|
|
|
'transactions.*.category' => 'between:1,255|nullable',
|
|
|
|
'transactions.*.piggy_bank_id' => 'between:1,255|nullable',
|
2016-04-29 13:59:28 -05:00
|
|
|
];
|
|
|
|
}
|
2016-08-12 08:50:52 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getTransactionData(): array
|
|
|
|
{
|
2017-01-21 01:32:23 -06:00
|
|
|
$descriptions = $this->getArray('description', 'string');
|
|
|
|
$categories = $this->getArray('category', 'string');
|
|
|
|
$amounts = $this->getArray('amount', 'float');
|
|
|
|
$budgets = $this->getArray('amount', 'integer');
|
|
|
|
$srcAccountIds = $this->getArray('source_account_id', 'integer');
|
|
|
|
$srcAccountNames = $this->getArray('source_account_name', 'string');
|
|
|
|
$dstAccountIds = $this->getArray('destination_account_id', 'integer');
|
|
|
|
$dstAccountNames = $this->getArray('destination_account_name', 'string');
|
|
|
|
$piggyBankIds = $this->getArray('piggy_bank_id', 'integer');
|
|
|
|
|
2016-08-12 08:50:52 -05:00
|
|
|
$return = [];
|
|
|
|
// description is leading because it is one of the mandatory fields.
|
2017-01-21 01:32:23 -06:00
|
|
|
foreach ($descriptions as $index => $description) {
|
|
|
|
$category = $categories[$index] ?? '';
|
2016-08-12 08:50:52 -05:00
|
|
|
$transaction = [
|
|
|
|
'description' => $description,
|
2017-02-13 13:39:23 -06:00
|
|
|
'amount' => Steam::positive($amounts[$index]),
|
2017-01-21 01:32:23 -06:00
|
|
|
'budget_id' => $budgets[$index] ?? 0,
|
|
|
|
'category' => $category,
|
|
|
|
'source_account_id' => $srcAccountIds[$index] ?? $this->get('journal_source_account_id'),
|
|
|
|
'source_account_name' => $srcAccountNames[$index] ?? '',
|
|
|
|
'piggy_bank_id' => $piggyBankIds[$index] ?? 0,
|
|
|
|
'destination_account_id' => $dstAccountIds[$index] ?? $this->get('journal_destination_account_id'),
|
|
|
|
'destination_account_name' => $dstAccountNames[$index] ?? '',
|
2016-08-12 08:50:52 -05:00
|
|
|
];
|
|
|
|
$return[] = $transaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
2016-08-12 08:10:03 -05:00
|
|
|
}
|