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

95 lines
3.2 KiB
PHP
Raw Normal View History

2015-02-24 22:53:38 +01:00
<?php
namespace FireflyIII\Http\Requests;
2015-05-26 12:08:46 +02:00
use App;
2015-02-24 22:53:38 +01:00
use Auth;
2015-03-29 11:51:26 +02:00
use Carbon\Carbon;
2015-03-29 07:43:20 +02:00
use Exception;
2015-03-29 11:51:26 +02:00
use Input;
2015-02-24 22:53:38 +01:00
/**
* Class JournalFormRequest
*
2015-05-10 08:08:07 +02:00
* @codeCoverageIgnore
2015-02-24 22:53:38 +01:00
* @package FireflyIII\Http\Requests
*/
class JournalFormRequest extends Request
{
/**
* @return bool
*/
public function authorize()
{
// Only allow logged in users
return Auth::check();
}
2015-03-29 11:51:26 +02:00
/**
* @return array
*/
public function getJournalData()
{
return [
'what' => $this->get('what'),
'description' => $this->get('description'),
'account_id' => intval($this->get('account_id')),
'account_from_id' => intval($this->get('account_from_id')),
'account_to_id' => intval($this->get('account_to_id')),
'expense_account' => $this->get('expense_account'),
'revenue_account' => $this->get('revenue_account'),
'amount' => floatval($this->get('amount')),
'user' => Auth::user()->id,
'amount_currency_id' => intval($this->get('amount_currency_id')),
'date' => new Carbon($this->get('date')),
'budget_id' => intval($this->get('budget_id')),
'category' => $this->get('category'),
2015-04-28 10:36:13 +02:00
'tags' => explode(',', $this->get('tags')),
2015-03-29 11:51:26 +02:00
];
}
2015-02-24 22:53:38 +01:00
/**
* @return array
2015-03-29 07:51:56 +02:00
* @throws Exception
2015-05-17 10:30:18 +02:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2015-02-24 22:53:38 +01:00
*/
public function rules()
{
2015-05-17 10:30:18 +02:00
$what = Input::get('what');
2015-02-24 22:53:38 +01:00
$rules = [
'description' => 'required|min:1,max:255',
2015-05-05 07:28:04 +02:00
'what' => 'required|in:withdrawal,deposit,transfer',
2015-02-24 22:53:38 +01:00
'amount' => 'numeric|required|min:0.01',
'date' => 'required|date',
'amount_currency_id' => 'required|exists:transaction_currencies,id',
];
switch ($what) {
case 'withdrawal':
$rules['account_id'] = 'required|exists:accounts,id|belongsToUser:accounts';
$rules['expense_account'] = 'between:1,255';
$rules['category'] = 'between:1,255';
if (intval(Input::get('budget_id')) != 0) {
$rules['budget_id'] = 'exists:budgets,id|belongsToUser:budgets';
}
break;
case 'deposit':
$rules['category'] = 'between:1,255';
$rules['account_id'] = 'required|exists:accounts,id|belongsToUser:accounts';
$rules['revenue_account'] = 'between:1,255';
break;
case 'transfer':
$rules['account_from_id'] = 'required|exists:accounts,id|belongsToUser:accounts|different:account_to_id';
$rules['account_to_id'] = 'required|exists:accounts,id|belongsToUser:accounts|different:account_from_id';
$rules['category'] = 'between:1,255';
break;
default:
2015-05-26 12:08:46 +02:00
App::abort(500, 'Cannot handle ' . $what);
2015-02-24 22:53:38 +01:00
break;
}
return $rules;
}
2015-03-29 08:14:32 +02:00
}