Code cleanup.

This commit is contained in:
James Cole
2014-12-14 20:40:02 +01:00
parent 8e6ca0dd05
commit 900dea2c66
36 changed files with 384 additions and 1094 deletions

View File

@@ -9,6 +9,7 @@ use FireflyIII\Exception\FireflyException;
* Class Date
*
* @package FireflyIII\Shared\Toolkit
* @SuppressWarnings("ExcessiveClassComplexity")
*/
class Date
{
@@ -16,6 +17,7 @@ class Date
* @param Carbon $theDate
* @param $repeatFreq
* @param $skip
* @SuppressWarnings("Cyclomatic")
*
* @return Carbon
* @throws FireflyException
@@ -24,6 +26,7 @@ class Date
{
$date = clone $theDate;
$add = ($skip + 1);
switch ($repeatFreq) {
default:
throw new FireflyException('Cannot do addPeriod for $repeat_freq ' . $repeatFreq);
@@ -60,6 +63,7 @@ class Date
/**
* @param Carbon $theCurrentEnd
* @param $repeatFreq
* @SuppressWarnings("Cyclomatic")
*
* @return mixed
* @throws FireflyException
@@ -101,11 +105,14 @@ class Date
/**
* @param Carbon $theCurrentEnd
* @param $repeatFreq
* @param Carbon $maxDate
* @SuppressWarnings("Cyclomatic")
* @SuppressWarnings("MethodLength")
*
* @return mixed
* @throws FireflyException
*/
public function endOfX(Carbon $theCurrentEnd, $repeatFreq)
public function endOfX(Carbon $theCurrentEnd, $repeatFreq, Carbon $maxDate)
{
$currentEnd = clone $theCurrentEnd;
switch ($repeatFreq) {
@@ -139,6 +146,9 @@ class Date
$currentEnd->endOfYear();
break;
}
if ($currentEnd > $maxDate) {
return clone $maxDate;
}
return $currentEnd;
}
@@ -146,6 +156,7 @@ class Date
/**
* @param Carbon $date
* @param $repeatFrequency
* @SuppressWarnings("Cyclomatic")
*
* @return string
* @throws FireflyException
@@ -180,6 +191,7 @@ class Date
/**
* @param Carbon $theDate
* @param $repeatFreq
* @SuppressWarnings("Cyclomatic")
*
* @return Carbon
* @throws FireflyException
@@ -226,10 +238,10 @@ class Date
* @param Carbon $theDate
* @param $repeatFreq
* @param int $subtract
* @SuppressWarnings("Cyclomatic")
*
* @return Carbon
* @throws FireflyException
* @internal param Carbon $date
*/
public function subtractPeriod(Carbon $theDate, $repeatFreq, $subtract = 1)
{

View File

@@ -17,41 +17,14 @@ class Filter
*/
public function setSessionDateRange()
{
/*
* Get the current range.
*/
$range = $this->setSessionRangeValue();
$start = \Session::has('start') ? \Session::get('start') : new Carbon;
/*
* Force start date to at the start of the $range.
* Ie. the start of the week, month, year. This also to protect against nefarious users
* who change their session data (I just wanted to use the word "nefarious").
*/
$start = $this->updateStartDate($range, $start);
/*
* Force end date to at the END of the $range. Always based on $start.
* Ie. the END of the week, month, year.
*/
$end = $this->updateEndDate($range, $start);
#\Log::debug('After update, session end is : ' . $end->format('Y-m-d'));
/*
* get the name of the month, depending on the range. Purely for astetics
*/
$range = $this->setSessionRangeValue();
$start = \Session::has('start') ? \Session::get('start') : new Carbon;
$start = $this->updateStartDate($range, $start);
$end = $this->updateEndDate($range, $start);
$period = $this->periodName($range, $start);
$prev = $this->previous($range, clone $start);
$next = $this->next($range, clone $start);
/*
* Get the date for the previous and next period.
* Ie. next week, next month, etc.
*/
$prev = $this->previous($range, clone $start);
$next = $this->next($range, clone $start);
/*
* Save everything in the session:
*/
\Session::put('start', $start);
\Session::put('end', $end);
\Session::put('range', $range);
@@ -90,6 +63,7 @@ class Filter
/**
* @param $range
* @param Carbon $start
* @SuppressWarnings("Cyclomatic")
*
* @return Carbon
* @throws FireflyException
@@ -215,6 +189,7 @@ class Filter
/**
* @param $range
* @param Carbon $date
* @SuppressWarnings("CyclomaticComplexity")
*
* @return Carbon
* @throws FireflyException

View File

@@ -16,37 +16,33 @@ class Form
*
* @param Collection $set
* @param null $titleField
* @param bool $addEmpty
* @SuppressWarnings("CyclomaticComplexity")
*
* @return mixed
*/
public function makeSelectList(Collection $set, $titleField = null)
public function makeSelectList(Collection $set, $titleField = null, $addEmpty = false)
{
$selectList = [];
/** @var Model $entry */
if ($addEmpty) {
$selectList[0] = '(none)';
}
$fields = ['title', 'name', 'description'];
/** @var \Eloquent $entry */
foreach ($set as $entry) {
/** @noinspection PhpUndefinedFieldInspection */
$id = intval($entry->id);
$title = null;
if (is_null($titleField)) {
// try 'title' field.
if (isset($entry->title)) {
$title = $entry->title;
}
// try 'name' field
if (is_null($title)) {
$title = $entry->name;
}
$title = $titleField;
// try 'description' field
if (is_null($title)) {
$title = $entry->description;
foreach ($fields as $field) {
if (is_null($title) && isset($entry->$field)) {
$title = $entry->$field;
}
} else {
$title = $entry->$titleField;
}
$selectList[$id] = $title;
}
return $selectList;
}
}
}

View File

@@ -64,57 +64,41 @@ class Reminders
public function updateReminders()
{
/*
* Reminder capable objects are (so far) only piggy banks.
*/
/** @var \FireflyIII\Database\PiggyBank\PiggyBank $repository */
$repository = \App::make('FireflyIII\Database\PiggyBank\PiggyBank');
/** @var \FireflyIII\Database\PiggyBank\RepeatedExpense $repeatedRepository */
$repeatedRepository = \App::make('FireflyIII\Database\PiggyBank\RepeatedExpense');
/** @var Collection $piggybanks */
$piggybanks = $repository->get()->merge($repeatedRepository->get());
/** @var Collection $set */
$set = \Piggybank::leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')
->where('accounts.user_id', \Auth::user()->id)
->whereNotNull('reminder')->get(['piggybanks.*']);
$set = $piggybanks->filter(
function (\Piggybank $piggybank) {
if (!is_null($piggybank->reminder)) {
return $piggybank;
}
return null;
}
);
$today = Carbon::now();
//$today = new Carbon('14-12-2014');
/** @var \Piggybank $piggybank */
foreach ($set as $piggybank) {
foreach ($set as $piggyBank) {
/*
* Try to find a reminder that is valid in the current [period]
* aka between [start of period] and [end of period] as denoted
* by the piggy's repeat_freq.
*/
/** @var \PiggybankRepetition $repetition */
$repetition = $piggybank->currentRelevantRep();
$start = \DateKit::startOfPeriod($today, $piggybank->reminder);
$repetition = $piggyBank->currentRelevantRep();
$start = \DateKit::startOfPeriod($today, $piggyBank->reminder);
if ($repetition->targetdate && $repetition->targetdate <= $today) {
// break when no longer relevant:
continue;
}
$end = \DateKit::endOfPeriod(clone $start, $piggybank->reminder);
$end = \DateKit::endOfPeriod(clone $start, $piggyBank->reminder);
// should have a reminder for this period:
/** @var Collection $reminders */
$reminders = $piggybank->reminders()->dateIs($start, $end)->get();
$reminders = $piggyBank->reminders()->dateIs($start, $end)->get();
if ($reminders->count() == 0) {
// create new!
$reminder = new \Reminder;
$reminder->startdate = $start;
$reminder->enddate = $end;
$reminder->active = 1;
$reminder->user()->associate($repository->getUser());
$reminder->remindersable_id= $piggybank->id;
$reminder->user()->associate(\Auth::getUser());
$reminder->remindersable_id = $piggyBank->id;
$reminder->remindersable_type = 'Piggybank';
$reminder->save();
}

View File

@@ -58,4 +58,13 @@ class Steam
}
}
public function removeEmptyBudgetLimits()
{
$user = \Auth::user();
if ($user) {
\BudgetLimit::leftJoin('components', 'components.id', '=', 'budget_limits.component_id')->where('components.user_id', $user->id)
->where('budget_limits.amount', 0)->delete();
}
}
}