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

143 lines
5.2 KiB
PHP
Raw Normal View History

2015-02-24 15:53:38 -06:00
<?php
/**
* JournalFormRequest.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-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-02-24 15:53:38 -06:00
namespace FireflyIII\Http\Requests;
2015-03-29 04:51:26 -05:00
use Carbon\Carbon;
2015-03-29 00:43:20 -05:00
use Exception;
2016-02-17 08:52:46 -06:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionType;
2015-03-29 04:51:26 -05:00
use Input;
2015-02-24 15:53:38 -06:00
/**
* Class JournalFormRequest
*
2016-02-04 00:28:39 -06:00
*
2015-02-24 15:53:38 -06:00
* @package FireflyIII\Http\Requests
*/
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
/**
* @return array
*/
public function getJournalData()
{
2016-09-25 01:32:53 -05:00
$tags = $this->getFieldOrEmptyString('tags');
2016-02-17 08:52:46 -06:00
2015-03-29 04:51:26 -05:00
return [
2015-12-26 01:16:30 -06:00
'what' => $this->get('what'),
2016-10-01 01:48:13 -05:00
'description' => trim($this->get('description')),
2016-04-29 10:29:13 -05:00
'source_account_id' => intval($this->get('source_account_id')),
2016-10-01 01:48:13 -05:00
'source_account_name' => trim($this->getFieldOrEmptyString('source_account_name')),
'destination_account_id' => intval($this->get('destination_account_id')),
2016-10-01 01:48:13 -05:00
'destination_account_name' => trim($this->getFieldOrEmptyString('destination_account_name')),
2015-12-26 01:16:30 -06:00
'amount' => round($this->get('amount'), 2),
2016-09-16 05:15:58 -05:00
'user' => auth()->user()->id,
2015-12-26 01:23:52 -06:00
'amount_currency_id_amount' => intval($this->get('amount_currency_id_amount')),
2015-12-26 01:16:30 -06:00
'date' => new Carbon($this->get('date')),
2016-09-25 01:32:53 -05:00
'interest_date' => $this->getDateOrNull('interest_date'),
'book_date' => $this->getDateOrNull('book_date'),
'process_date' => $this->getDateOrNull('process_date'),
2015-12-26 01:16:30 -06:00
'budget_id' => intval($this->get('budget_id')),
2016-10-01 01:48:13 -05:00
'category' => trim($this->getFieldOrEmptyString('category')),
'tags' => explode(',', $tags),
2016-09-25 01:32:53 -05:00
'piggy_bank_id' => intval($this->get('piggy_bank_id')),
// new custom fields here:
2016-09-25 01:32:53 -05:00
'due_date' => $this->getDateOrNull('due_date'),
'payment_date' => $this->getDateOrNull('payment_date'),
'invoice_date' => $this->getDateOrNull('invoice_date'),
2016-10-01 01:48:13 -05:00
'internal_reference' => trim($this->get('internal_reference')),
'notes' => trim($this->get('notes')),
2015-03-29 04:51:26 -05:00
];
}
2015-02-24 15:53:38 -06:00
/**
* @return array
2015-03-29 00:51:56 -05:00
* @throws Exception
2015-02-24 15:53:38 -06:00
*/
public function rules()
{
2015-05-17 03:30:18 -05:00
$what = Input::get('what');
2015-02-24 15:53:38 -06:00
$rules = [
2015-12-26 01:16:30 -06:00
'description' => 'required|min:1,max:255',
'what' => 'required|in:withdrawal,deposit,transfer',
'amount' => 'numeric|required|min:0.01',
'date' => 'required|date',
'process_date' => 'date',
'book_date' => 'date',
'interest_date' => 'date',
2016-04-29 10:29:13 -05:00
'category' => 'between:1,255',
2015-12-26 01:16:30 -06:00
'amount_currency_id_amount' => 'required|exists:transaction_currencies,id',
2016-05-05 00:09:12 -05:00
'piggy_bank_id' => 'numeric',
// new custom fields here:
'due_date' => 'date',
'payment_date' => 'date',
'internal_reference' => 'min:1,max:255',
'notes' => 'min:1,max:65536',
2015-02-24 15:53:38 -06:00
];
switch ($what) {
case strtolower(TransactionType::WITHDRAWAL):
2016-04-29 10:29:13 -05:00
$rules['source_account_id'] = 'required|exists:accounts,id|belongsToUser:accounts';
$rules['destination_account_name'] = 'between:1,255';
2015-02-24 15:53:38 -06:00
if (intval(Input::get('budget_id')) != 0) {
$rules['budget_id'] = 'exists:budgets,id|belongsToUser:budgets';
}
break;
case strtolower(TransactionType::DEPOSIT):
2016-04-29 10:29:13 -05:00
$rules['source_account_name'] = 'between:1,255';
$rules['destination_account_id'] = 'required|exists:accounts,id|belongsToUser:accounts';
2015-02-24 15:53:38 -06:00
break;
case strtolower(TransactionType::TRANSFER):
2016-04-29 10:29:13 -05:00
$rules['source_account_id'] = 'required|exists:accounts,id|belongsToUser:accounts|different:destination_account_id';
$rules['destination_account_id'] = 'required|exists:accounts,id|belongsToUser:accounts|different:source_account_id';
2015-02-24 15:53:38 -06:00
break;
default:
2016-02-17 08:52:46 -06:00
throw new FireflyException('Cannot handle transaction type of type ' . e($what) . '.');
2015-02-24 15:53:38 -06:00
}
return $rules;
}
2016-09-25 01:32:53 -05:00
/**
* @param string $field
*
* @return Carbon|null
*/
private function getDateOrNull(string $field)
{
return $this->get($field) ? new Carbon($this->get($field)) : null;
}
/**
* @param string $field
*
* @return string
*/
private function getFieldOrEmptyString(string $field): string
{
return $this->get($field) ?? '';
}
2015-03-29 01:14:32 -05:00
}