Complexity cleanup [skip ci]

This commit is contained in:
James Cole 2014-12-31 16:45:12 +01:00
parent 8ec8042045
commit 6fa73ee28d
10 changed files with 92 additions and 104 deletions

View File

@ -165,8 +165,8 @@ class TransactionController extends BaseController
*/
public function doRelate()
{
$brother = intval(Input::get('id'));
$sister = intval(Input::get('relateTo'));
$brother = intval(Input::get('id'));
$sister = intval(Input::get('relateTo'));
$journal = $this->_repository->find($brother);
$sis = $this->_repository->find($sister);
@ -358,9 +358,14 @@ class TransactionController extends BaseController
*/
public function store($what)
{
$data = Input::except('_token');
$data['what'] = $what;
$data['currency'] = 'EUR'; // TODO allow custom currency
$data = Input::except('_token');
$transactionType = $this->_repository->getJournalType($what);
$transactionCurrency = $this->_repository->getJournalCurrency('EUR');
$data['transaction_type_id'] = $transactionType->id;
$data['transaction_currency_id'] = $transactionCurrency->id;
$data['completed'] = 0;
$data['what'] = $what;
$data['currency'] = 'EUR'; // TODO allow custom currency
// always validate:
$messages = $this->_repository->validate($data);
@ -434,10 +439,13 @@ class TransactionController extends BaseController
*/
public function update(TransactionJournal $journal)
{
$data = Input::except('_token');
$data['currency'] = 'EUR';
$data['what'] = strtolower($journal->transactionType->type);
$messages = $this->_repository->validate($data);
$data = Input::except('_token');
$data['currency'] = 'EUR';
$data['what'] = strtolower($journal->transactionType->type);
$data['transaction_type_id'] = $journal->transaction_type_id;
$data['transaction_currency_id'] = $journal->transaction_currency_id;
$data['completed'] = 1;
$messages = $this->_repository->validate($data);
Session::flash('warnings', $messages['warnings']);
Session::flash('successes', $messages['successes']);

View File

@ -63,11 +63,12 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
*/
public function store(array $data)
{
$journalType = $this->getJournalType($data['what']);
$currency = $this->getJournalCurrency($data['currency']);
$journal = new \TransactionJournal(
['transaction_type_id' => $journalType->id, 'transaction_currency_id' => $currency->id, 'user_id' => $this->getUser()->id,
'description' => $data['description'], 'date' => $data['date'], 'completed' => 0]
$currency = $this->getJournalCurrency($data['currency']);
$journal = new \TransactionJournal(
[
'transaction_type_id' => $data['transaction_type_id'],
'transaction_currency_id' => $currency->id, 'user_id' => $this->getUser()->id,
'description' => $data['description'], 'date' => $data['date'], 'completed' => 0]
);
$journal->save();
@ -147,39 +148,14 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
$warnings = new MessageBag;
$successes = new MessageBag;
$errors = new MessageBag;
$journal = new \TransactionJournal($model);
$journal->isValid();
$errors = $journal->getErrors();
if (!isset($model['what'])) {
$errors->add('description', 'Internal error: need to know type of transaction!');
}
if (isset($model['bill_id']) && intval($model['bill_id']) < 0) {
$errors->add('bill_id', 'Bill is invalid.');
}
if (!isset($model['description'])) {
$errors->add('description', 'This field is mandatory.');
}
if (isset($model['description']) && strlen($model['description']) == 0) {
$errors->add('description', 'This field is mandatory.');
}
if (isset($model['description']) && strlen($model['description']) > 255) {
$errors->add('description', 'Description is too long.');
}
if (!isset($model['currency'])) {
$errors->add('description', 'Internal error: currency is mandatory!');
}
if (isset($model['date']) && !($model['date'] instanceof Carbon) && strlen($model['date']) > 0) {
try {
new Carbon($model['date']);
} catch (\Exception $e) {
$errors->add('date', 'This date is invalid.');
}
}
if (!isset($model['date'])) {
$errors->add('date', 'This date is invalid.');
}
/*
* Amount:
*/
@ -254,12 +230,6 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
}
$validator = \Validator::make([$model], \TransactionJournal::$rules);
if ($validator->invalid()) {
$errors->merge($errors);
}
/*
* Add "OK"
*/
@ -275,20 +245,6 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
}
/**
* @param $type
*
* @return \AccountType|null
* @throws FireflyException
*/
public function getJournalType($type)
{
/** @var \FireflyIII\Database\TransactionType\TransactionType $typeRepository */
$typeRepository = \App::make('FireflyIII\Database\TransactionType\TransactionType');
return $typeRepository->findByWhat($type);
}
/**
* @param $currency
*
@ -397,6 +353,20 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
return;
}
/**
* @param $type
*
* @return \TransactionType|null
* @throws FireflyException
*/
public function getJournalType($type)
{
/** @var \FireflyIII\Database\TransactionType\TransactionType $typeRepository */
$typeRepository = \App::make('FireflyIII\Database\TransactionType\TransactionType');
return $typeRepository->findByWhat($type);
}
/**
* Returns an object with id $id.
*

View File

@ -95,25 +95,16 @@ class TransactionType implements CUD, CommonDatabaseCalls
*/
public function findByWhat($what)
{
switch ($what) {
case 'opening':
return \TransactionType::whereType('Opening balance')->first();
break;
case 'transfer':
return \TransactionType::whereType('Transfer')->first();
break;
case 'withdrawal':
return \TransactionType::whereType('Withdrawal')->first();
break;
case 'deposit':
return \TransactionType::whereType('Deposit')->first();
break;
default:
throw new FireflyException('Cannot find transaction type described as "' . e($what) . '".');
break;
$translation = [
'opening' => 'Opening balance',
'transfer' => 'Transfer',
'withdrawal' => 'Withdrawal',
'deposit' => 'Deposit',
];
if(!isset($translation[$what])) {
throw new FireflyException('Cannot find transaction type described as "' . e($what) . '".');
}
return \TransactionType::whereType($translation[$what])->first();
}
/**

View File

@ -11,6 +11,16 @@ use Illuminate\Support\Collection;
*/
class Helper implements HelperInterface
{
/**
* @param $what
*
* @return int
*/
public function getTransactionTypeIdByWhat($what) {
}
/**
*
* Get the account_id, which is the asset account that paid for the transaction.

View File

@ -22,6 +22,13 @@ interface HelperInterface
*/
public function getAssetAccount($what, Collection $transactions);
/**
* @param $what
*
* @return int
*/
public function getTransactionTypeIdByWhat($what);
/**
* @return Collection
*/

View File

@ -2,6 +2,7 @@
namespace FireflyIII\Shared\Toolkit;
use Carbon\Carbon;
use FireflyIII\Exception\FireflyException;
/**
@ -12,14 +13,14 @@ use FireflyIII\Exception\FireflyException;
class Date
{
/**
* @param \Carbon\Carbon $theDate
* @param Carbon $theDate
* @param $repeatFreq
* @param $skip
*
* @return \Carbon\Carbon
* @throws FireflyException
*/
public function addPeriod(\Carbon\Carbon $theDate, $repeatFreq, $skip)
public function addPeriod(Carbon $theDate, $repeatFreq, $skip)
{
$date = clone $theDate;
$add = ($skip + 1);
@ -58,13 +59,13 @@ class Date
}
/**
* @param \Carbon\Carbon $theCurrentEnd
* @param Carbon $theCurrentEnd
* @param $repeatFreq
*
* @return \Carbon\Carbon
* @return Carbon
* @throws FireflyException
*/
public function endOfPeriod(\Carbon\Carbon $theCurrentEnd, $repeatFreq)
public function endOfPeriod(Carbon $theCurrentEnd, $repeatFreq)
{
$currentEnd = clone $theCurrentEnd;
switch ($repeatFreq) {
@ -99,14 +100,14 @@ class Date
}
/**
* @param \Carbon\Carbon $theCurrentEnd
* @param Carbon $theCurrentEnd
* @param $repeatFreq
* @param \Carbon\Carbon $maxDate
* @param Carbon $maxDate
*
* @return \Carbon\Carbon
* @return Carbon
* @throws FireflyException
*/
public function endOfX(\Carbon\Carbon $theCurrentEnd, $repeatFreq, \Carbon\Carbon $maxDate)
public function endOfX(Carbon $theCurrentEnd, $repeatFreq, Carbon $maxDate)
{
$currentEnd = clone $theCurrentEnd;
switch ($repeatFreq) {
@ -148,13 +149,13 @@ class Date
}
/**
* @param \Carbon\Carbon $date
* @param Carbon $date
* @param $repeatFrequency
*
* @return string
* @throws FireflyException
*/
public function periodShow(\Carbon\Carbon $date, $repeatFrequency)
public function periodShow(Carbon $date, $repeatFrequency)
{
switch ($repeatFrequency) {
default:
@ -182,13 +183,13 @@ class Date
}
/**
* @param \Carbon\Carbon $theDate
* @param Carbon $theDate
* @param $repeatFreq
*
* @return \Carbon\Carbon
* @return Carbon
* @throws FireflyException
*/
public function startOfPeriod(\Carbon\Carbon $theDate, $repeatFreq)
public function startOfPeriod(Carbon $theDate, $repeatFreq)
{
$date = clone $theDate;
switch ($repeatFreq) {
@ -227,14 +228,14 @@ class Date
}
/**
* @param \Carbon\Carbon $theDate
* @param Carbon $theDate
* @param $repeatFreq
* @param int $subtract
*
* @return \Carbon\Carbon
* @return Carbon
* @throws FireflyException
*/
public function subtractPeriod(\Carbon\Carbon $theDate, $repeatFreq, $subtract = 1)
public function subtractPeriod(Carbon $theDate, $repeatFreq, $subtract = 1)
{
$date = clone $theDate;
switch ($repeatFreq) {

View File

@ -1,6 +1,7 @@
<?php
namespace FireflyIII\Shared\Toolkit;
use Illuminate\Support\Collection;
/**
@ -13,12 +14,12 @@ class Form
/**
* Takes any collection and tries to make a sensible select list compatible array of it.
*
* @param \Illuminate\Support\Collection $set
* @param bool $addEmpty
* @param Collection $set
* @param bool $addEmpty
*
* @return mixed
*/
public function makeSelectList(\Illuminate\Support\Collection $set, $addEmpty = false)
public function makeSelectList(Collection $set, $addEmpty = false)
{
$selectList = [];
if ($addEmpty) {

View File

@ -13,7 +13,7 @@ class TransactionJournal extends Eloquent
{
use SoftDeletingTrait, ValidatingTrait;
public static $rules
protected $rules
= ['transaction_type_id' => 'required|exists:transaction_types,id',
'transaction_currency_id' => 'required|exists:transaction_currencies,id',
'description' => 'required|between:1,255',

View File

@ -155,7 +155,7 @@ class TransactionControllerCest
'post_submit_action' => 'store'
]
);
$I->see('Could not store transaction: This field is mandatory.');
$I->see('Could not store transaction: The description field is required.');
}
public function update(FunctionalTester $I)
@ -195,7 +195,7 @@ class TransactionControllerCest
'post_submit_action' => 'update'
]
);
$I->see('Could not update transaction: This field is mandatory.');
$I->see('Could not update transaction: The description field is required.');
}
public function updateAndReturn(FunctionalTester $I)

View File

@ -134,7 +134,7 @@ class UserControllerCest
{
$I->wantTo('reset my password and fail');
$I->amOnPage('/reset/123');
$I->see('Yo no hablo reset code!');
$I->see('No reset code found!');
}
/**