Cleanup and refactor

This commit is contained in:
James Cole
2015-01-18 09:49:32 +01:00
parent 10a93df653
commit 1068dcb8a4
8 changed files with 91 additions and 150 deletions

View File

@@ -156,10 +156,7 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte
*/ */
public function destroy(Eloquent $model) public function destroy(Eloquent $model)
{ {
$journals = \TransactionJournal::whereIn(
// delete piggy banks
// delete journals:
$journals = \TransactionJournal::whereIn(
'id', function (QueryBuilder $query) use ($model) { 'id', function (QueryBuilder $query) use ($model) {
$query->select('transaction_journal_id') $query->select('transaction_journal_id')
->from('transactions')->whereIn( ->from('transactions')->whereIn(
@@ -183,9 +180,6 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte
)->get(); )->get();
} }
)->get(); )->get();
/*
* Get all transactions.
*/
$transactions = []; $transactions = [];
/** @var \TransactionJournal $journal */ /** @var \TransactionJournal $journal */
foreach ($journals as $journal) { foreach ($journals as $journal) {
@@ -195,18 +189,10 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte
} }
$journal->delete(); $journal->delete();
} }
// also delete transactions.
if (count($transactions) > 0) { if (count($transactions) > 0) {
\Transaction::whereIn('id', $transactions)->delete(); \Transaction::whereIn('id', $transactions)->delete();
} }
/*
* Trigger deletion:
*/
\Event::fire('account.destroy', [$model]); \Event::fire('account.destroy', [$model]);
// delete accounts:
\Account::where( \Account::where(
function (EloquentBuilder $q) use ($model) { function (EloquentBuilder $q) use ($model) {
$q->where('id', $model->id); $q->where('id', $model->id);

View File

@@ -114,7 +114,7 @@ class Bill implements CUDInterface, CommonDatabaseCallsInterface, BillInterface
$warnings = new MessageBag; $warnings = new MessageBag;
$successes = new MessageBag; $successes = new MessageBag;
$errors = new MessageBag; $errors = new MessageBag;
if (isset($model['amount_min']) && isset($model['amount_max']) && floatval($model['amount_min']) > floatval($model['amount_max'])) { if (floatval($model['amount_min']) > floatval($model['amount_max'])) {
$errors->add('amount_max', 'Maximum amount can not be less than minimum amount.'); $errors->add('amount_max', 'Maximum amount can not be less than minimum amount.');
$errors->add('amount_min', 'Minimum amount can not be more than maximum amount.'); $errors->add('amount_min', 'Minimum amount can not be more than maximum amount.');
} }

View File

@@ -158,31 +158,13 @@ class TransactionJournal implements TransactionJournalInterface, CUDInterface, C
$journal->isValid(); $journal->isValid();
$errors = $journal->getErrors(); $errors = $journal->getErrors();
/*
* Is not in rules.
*/
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!');
} }
/*
* Is not in rules.
*/
$errors = $errors->merge($this->_validateAmount($model)); $errors = $errors->merge($this->_validateAmount($model));
$errors = $errors->merge($this->_validateBudget($model)); $errors = $errors->merge($this->_validateBudget($model));
$errors = $errors->merge($this->_validateAccount($model)); $errors = $errors->merge($this->_validateAccount($model));
/*
* Add "OK"
*/
/**
* else {
* $successes->add('account_from_id', 'OK');
* $successes->add('account_to_id', 'OK');
* }
* else {
*/
$list = ['date', 'description', 'amount', 'budget_id', 'from', 'to', 'account_from_id', 'account_to_id', 'category', 'account_id', 'expense_account', $list = ['date', 'description', 'amount', 'budget_id', 'from', 'to', 'account_from_id', 'account_to_id', 'category', 'account_id', 'expense_account',
'revenue_account']; 'revenue_account'];
foreach ($list as $entry) { foreach ($list as $entry) {

View File

@@ -197,7 +197,6 @@ class PiggyBank
foreach ($list as $entry) { foreach ($list as $entry) {
$start = $entry->startdate; $start = $entry->startdate;
$target = $entry->targetdate; $target = $entry->targetdate;
// find a repetition on this date:
$count = $entry->piggyBankrepetitions()->starts($start)->targets($target)->count(); $count = $entry->piggyBankrepetitions()->starts($start)->targets($target)->count();
if ($count == 0) { if ($count == 0) {
$repetition = new \PiggyBankRepetition; $repetition = new \PiggyBankRepetition;
@@ -207,14 +206,11 @@ class PiggyBank
$repetition->currentamount = 0; $repetition->currentamount = 0;
$repetition->save(); $repetition->save();
} }
// then continue and do something in the current relevant time frame.
$currentTarget = clone $target; $currentTarget = clone $target;
$currentStart = null; $currentStart = null;
while ($currentTarget < $today) { while ($currentTarget < $today) {
$currentStart = \DateKit::subtractPeriod($currentTarget, $entry->rep_length, 0); $currentStart = \DateKit::subtractPeriod($currentTarget, $entry->rep_length, 0);
$currentTarget = \DateKit::addPeriod($currentTarget, $entry->rep_length, 0); $currentTarget = \DateKit::addPeriod($currentTarget, $entry->rep_length, 0);
// create if not exists:
$count = $entry->piggyBankRepetitions()->starts($currentStart)->targets($currentTarget)->count(); $count = $entry->piggyBankRepetitions()->starts($currentStart)->targets($currentTarget)->count();
if ($count == 0) { if ($count == 0) {
$repetition = new \PiggyBankRepetition; $repetition = new \PiggyBankRepetition;

View File

@@ -228,6 +228,8 @@ class Report implements ReportInterface
} }
/** /**
* This method gets all incomes (journals) in a list.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.UnusedFormalParameter)
* *
* @param Carbon $date * @param Carbon $date
@@ -357,7 +359,6 @@ class Report implements ReportInterface
} }
/** /**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* *
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
@@ -367,7 +368,7 @@ class Report implements ReportInterface
*/ */
public function revenueGroupedByAccount(Carbon $start, Carbon $end, $limit = 15) public function revenueGroupedByAccount(Carbon $start, Carbon $end, $limit = 15)
{ {
return $this->_queries->journalsByRevenueAccount($start, $end); return $this->_queries->journalsByRevenueAccount($start, $end, $limit);

View File

@@ -102,6 +102,8 @@ class Date
} }
/** /**
* @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind.
*
* @param Carbon $theCurrentEnd * @param Carbon $theCurrentEnd
* @param $repeatFreq * @param $repeatFreq
* @param Carbon $maxDate * @param Carbon $maxDate
@@ -111,37 +113,32 @@ class Date
*/ */
public function endOfX(Carbon $theCurrentEnd, $repeatFreq, Carbon $maxDate) public function endOfX(Carbon $theCurrentEnd, $repeatFreq, Carbon $maxDate)
{ {
$functionMap = [
'daily' => 'endOfDay',
'week' => 'endOfWeek',
'weekly' => 'endOfWeek',
'month' => 'endOfMonth',
'monthly' => 'endOfMonth',
'quarter' => 'lastOfQuarter',
'quarterly' => 'lastOfQuarter',
'year' => 'endOfYear',
'yearly' => 'endOfYear',
];
$specials = ['mont', 'monthly'];
$currentEnd = clone $theCurrentEnd; $currentEnd = clone $theCurrentEnd;
switch ($repeatFreq) {
default: if (isset($functionMap[$repeatFreq])) {
throw new FireflyException('Cannot do endOfPeriod for $repeat_freq ' . $repeatFreq); $function = $functionMap[$repeatFreq];
break; $currentEnd->$function();
case 'daily':
$currentEnd->endOfDay(); }
break; if (isset($specials[$repeatFreq])) {
case 'week': $month = intval($theCurrentEnd->format('m'));
case 'weekly': $currentEnd->endOfYear();
$currentEnd->endOfWeek(); if ($month <= 6) {
break; $currentEnd->subMonths(6);
case 'month': }
case 'monthly':
$currentEnd->endOfMonth();
break;
case 'quarter':
case 'quarterly':
$currentEnd->lastOfQuarter();
break;
case 'half-year':
$month = intval($theCurrentEnd->format('m'));
$currentEnd->endOfYear();
if ($month <= 6) {
$currentEnd->subMonths(6);
}
break;
case 'year':
case 'yearly':
$currentEnd->endOfYear();
break;
} }
if ($currentEnd > $maxDate) { if ($currentEnd > $maxDate) {
return clone $maxDate; return clone $maxDate;
@@ -159,29 +156,21 @@ class Date
*/ */
public function periodShow(Carbon $date, $repeatFrequency) public function periodShow(Carbon $date, $repeatFrequency)
{ {
switch ($repeatFrequency) { $formatMap = [
default: 'daily' => 'j F Y',
throw new FireflyException('No date formats for frequency "' . $repeatFrequency . '"!'); 'week' => '\W\e\e\k W, Y',
break; 'weekly' => '\W\e\e\k W, Y',
case 'daily': 'quarter' => 'F Y',
return $date->format('j F Y'); 'month' => 'F Y',
break; 'monthly' => 'F Y',
case 'week': 'year' => 'Y',
case 'weekly': 'yearly' => 'Y',
return $date->format('\W\e\e\k W, Y');
break; ];
case 'quarter': if (isset($formatMap[$repeatFrequency])) {
return $date->format('F Y'); return $date->format($formatMap[$repeatFrequency]);
break;
case 'monthly':
case 'month':
return $date->format('F Y');
break;
case 'year':
case 'yearly':
return $date->format('Y');
break;
} }
throw new FireflyException('No date formats for frequency "' . $repeatFrequency . '"!');
} }
/** /**

View File

@@ -103,37 +103,31 @@ class Filter
*/ */
protected function updateEndDate($range, Carbon $start) protected function updateEndDate($range, Carbon $start)
{ {
$end = clone $start; $functionMap = [
switch ($range) { '1D' => 'endOfDay',
default: '1W' => 'endOfWeek',
throw new FireflyException('updateEndDate cannot handle $range ' . $range); '1M' => 'endOfMonth',
break; '3M' => 'lastOfQuarter',
case '1D': '1Y' => 'endOfYear',
$end->endOfDay(); ];
break; $end = clone $start;
case '1W':
$end->endOfWeek();
break;
case '1M':
$end->endOfMonth();
break;
case '3M':
$end->lastOfQuarter();
break;
case '6M':
if (intval($start->format('m')) >= 7) {
$end->endOfYear();
} else {
$end->startOfYear()->addMonths(6);
}
break;
case '1Y':
$end->endOfYear();
break;
if (isset($functionMap[$range])) {
$function = $functionMap[$range];
$end->$function();
return $end;
} }
if ($range == '6M') {
if (intval($start->format('m')) >= 7) {
$end->endOfYear();
} else {
$end->startOfYear()->addMonths(6);
}
return $end; return $end;
}
throw new FireflyException('updateEndDate cannot handle $range ' . $range);
} }
/** /**
@@ -145,37 +139,28 @@ class Filter
*/ */
protected function periodName($range, Carbon $date) protected function periodName($range, Carbon $date)
{ {
switch ($range) { $formatMap = [
default: '1D' => 'jS F Y',
throw new FireflyException('No _periodName() for range "' . $range . '"'); '1W' => '\w\e\ek W, Y',
break; '1M' => 'F Y',
case '1D': '1Y' => 'Y',
return $date->format('jS F Y'); ];
break; if (isset($formatMap[$range])) {
case '1W': return $date->format($formatMap[$range]);
return 'week ' . $date->format('W, Y');
break;
case '1M':
return $date->format('F Y');
break;
case '3M':
$month = intval($date->format('m'));
return 'Q' . ceil(($month / 12) * 4) . ' ' . $date->format('Y');
break;
case '6M':
$month = intval($date->format('m'));
$half = ceil(($month / 12) * 2);
$halfName = $half == 1 ? 'first' : 'second';
return $halfName . ' half of ' . $date->format('d-m-Y');
break;
case '1Y':
return $date->format('Y');
break;
} }
if ($range == '3M') {
$month = intval($date->format('m'));
return 'Q' . ceil(($month / 12) * 4) . ' ' . $date->format('Y');
}
if ($range == '6M') {
$month = intval($date->format('m'));
$half = ceil(($month / 12) * 2);
$halfName = $half == 1 ? 'first' : 'second';
return $halfName . ' half of ' . $date->format('d-m-Y');
}
throw new FireflyException('No _periodName() for range "' . $range . '"');
} }
/** /**

View File

@@ -12,6 +12,8 @@ use Illuminate\Support\Collection;
class Form class Form
{ {
/** /**
* @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind.
*
* 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 Collection $set * @param Collection $set