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

View File

@ -63,11 +63,12 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
*/ */
public function store(array $data) public function store(array $data)
{ {
$journalType = $this->getJournalType($data['what']); $currency = $this->getJournalCurrency($data['currency']);
$currency = $this->getJournalCurrency($data['currency']); $journal = new \TransactionJournal(
$journal = new \TransactionJournal( [
['transaction_type_id' => $journalType->id, 'transaction_currency_id' => $currency->id, 'user_id' => $this->getUser()->id, 'transaction_type_id' => $data['transaction_type_id'],
'description' => $data['description'], 'date' => $data['date'], 'completed' => 0] 'transaction_currency_id' => $currency->id, 'user_id' => $this->getUser()->id,
'description' => $data['description'], 'date' => $data['date'], 'completed' => 0]
); );
$journal->save(); $journal->save();
@ -147,39 +148,14 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
$warnings = new MessageBag; $warnings = new MessageBag;
$successes = new MessageBag; $successes = new MessageBag;
$errors = new MessageBag;
$journal = new \TransactionJournal($model);
$journal->isValid();
$errors = $journal->getErrors();
if (!isset($model['what'])) { if (!isset($model['what'])) {
$errors->add('description', 'Internal error: need to know type of transaction!'); $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: * 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" * 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 * @param $currency
* *
@ -397,6 +353,20 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
return; 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. * Returns an object with id $id.
* *

View File

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

View File

@ -11,6 +11,16 @@ use Illuminate\Support\Collection;
*/ */
class Helper implements HelperInterface 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. * 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); public function getAssetAccount($what, Collection $transactions);
/**
* @param $what
*
* @return int
*/
public function getTransactionTypeIdByWhat($what);
/** /**
* @return Collection * @return Collection
*/ */

View File

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

View File

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

View File

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

View File

@ -155,7 +155,7 @@ class TransactionControllerCest
'post_submit_action' => 'store' '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) public function update(FunctionalTester $I)
@ -195,7 +195,7 @@ class TransactionControllerCest
'post_submit_action' => 'update' '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) public function updateAndReturn(FunctionalTester $I)

View File

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