2016-04-29 13:59:28 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* SplitJournalFormRequest.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
|
|
|
* of the MIT license. See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
2016-05-20 05:41:23 -05:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2016-04-29 13:59:28 -05:00
|
|
|
namespace FireflyIII\Http\Requests;
|
|
|
|
|
|
|
|
use Auth;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class SplitJournalFormRequest
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Requests
|
|
|
|
*/
|
|
|
|
class SplitJournalFormRequest extends Request
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize(): bool
|
|
|
|
{
|
|
|
|
// Only allow logged in users
|
|
|
|
return Auth::check();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSplitData(): array
|
|
|
|
{
|
|
|
|
$data = [
|
2016-05-13 00:33:04 -05:00
|
|
|
'id' => $this->get('id') ?? 0,
|
2016-05-05 11:59:46 -05:00
|
|
|
'journal_description' => $this->get('journal_description'),
|
|
|
|
'journal_currency_id' => intval($this->get('journal_currency_id')),
|
|
|
|
'journal_source_account_id' => intval($this->get('journal_source_account_id')),
|
|
|
|
'journal_source_account_name' => $this->get('journal_source_account_name'),
|
2016-05-15 05:08:41 -05:00
|
|
|
'journal_destination_account_id' => intval($this->get('journal_destination_account_id')),
|
2016-05-05 11:59:46 -05:00
|
|
|
'journal_destination_account_name' => $this->get('journal_source_destination_name'),
|
|
|
|
'date' => new Carbon($this->get('date')),
|
|
|
|
'what' => $this->get('what'),
|
|
|
|
'interest_date' => $this->get('interest_date') ? new Carbon($this->get('interest_date')) : null,
|
|
|
|
'book_date' => $this->get('book_date') ? new Carbon($this->get('book_date')) : null,
|
|
|
|
'process_date' => $this->get('process_date') ? new Carbon($this->get('process_date')) : null,
|
|
|
|
'transactions' => [],
|
2016-04-29 13:59:28 -05:00
|
|
|
];
|
2016-05-05 11:59:46 -05:00
|
|
|
|
2016-04-29 13:59:28 -05:00
|
|
|
// description is leading because it is one of the mandatory fields.
|
|
|
|
foreach ($this->get('description') as $index => $description) {
|
|
|
|
$transaction = [
|
|
|
|
'description' => $description,
|
|
|
|
'amount' => round($this->get('amount')[$index], 2),
|
2016-05-05 11:59:46 -05:00
|
|
|
'budget_id' => $this->get('budget_id')[$index] ? intval($this->get('budget_id')[$index]) : 0,
|
2016-04-29 13:59:28 -05:00
|
|
|
'category' => $this->get('category')[$index] ?? '',
|
2016-07-26 13:26:46 -05:00
|
|
|
'source_account_id' => isset($this->get('source_account_id')[$index]) ? intval($this->get('source_account_id')[$index]) : intval($this->get('journal_source_account_id')),
|
|
|
|
'source_account_name' => $this->get('source_account_name')[$index] ?? '',
|
|
|
|
'piggy_bank_id' => isset($this->get('piggy_bank_id')[$index]) ? intval($this->get('piggy_bank_id')[$index]) : 0,
|
|
|
|
'destination_account_id' => isset($this->get('destination_account_id')[$index]) ? intval($this->get('destination_account_id')[$index]) : intval($this->get('journal_destination_account_id')),
|
2016-04-30 02:48:39 -05:00
|
|
|
'destination_account_name' => $this->get('destination_account_name')[$index] ?? '',
|
2016-04-29 13:59:28 -05:00
|
|
|
];
|
|
|
|
$data['transactions'][] = $transaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return [
|
2016-05-05 11:59:46 -05:00
|
|
|
'what' => 'required|in:withdrawal,deposit,transfer',
|
|
|
|
'journal_description' => 'required|between:1,255',
|
2016-05-12 15:44:31 -05:00
|
|
|
'id' => 'numeric|belongsToUser:transaction_journals,id',
|
2016-05-05 11:59:46 -05:00
|
|
|
'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',
|
|
|
|
'interest_date' => 'date',
|
|
|
|
'book_date' => 'date',
|
|
|
|
'process_date' => 'date',
|
|
|
|
|
2016-04-29 13:59:28 -05:00
|
|
|
'description.*' => 'required|between:1,255',
|
2016-04-30 02:48:39 -05:00
|
|
|
'destination_account_id.*' => 'numeric|belongsToUser:accounts,id',
|
2016-04-29 13:59:28 -05:00
|
|
|
'destination_account_name.*' => 'between:1,255',
|
|
|
|
'amount.*' => 'required|numeric',
|
2016-05-05 11:59:46 -05:00
|
|
|
'budget_id.*' => 'belongsToUser:budgets,id',
|
2016-04-29 13:59:28 -05:00
|
|
|
'category.*' => 'between:1,255',
|
2016-05-05 11:59:46 -05:00
|
|
|
'piggy_bank_id.*' => 'between:1,255',
|
2016-04-29 13:59:28 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|