Lots of code cleanup.

This commit is contained in:
James Cole
2014-11-15 07:46:01 +01:00
parent 3e02b50ea1
commit 6832f2ebd0
23 changed files with 245 additions and 819 deletions

View File

@@ -90,7 +90,15 @@ class Piggybank extends Ardent
*/
public function currentRelevantRep()
{
$query = $this->piggybankrepetitions()->where(
if($this->currentRep) {
return $this->currentRep;
}
if ($this->repeats == 0) {
$rep = $this->piggybankrepetitions()->first();
$this->currentRep = $rep;
return $rep;
} else {
$query = $this->piggybankrepetitions()->where(
function ($q) {
$q->where(
@@ -100,12 +108,12 @@ class Piggybank extends Ardent
$q->orWhere('startdate', '<=', $today->format('Y-m-d'));
}
)->where(
function ($q) {
$today = new Carbon;
$q->whereNull('targetdate');
$q->orWhere('targetdate', '>=', $today->format('Y-m-d'));
}
);
function ($q) {
$today = new Carbon;
$q->whereNull('targetdate');
$q->orWhere('targetdate', '>=', $today->format('Y-m-d'));
}
);
}
)->orWhere(
function ($q) {
@@ -114,9 +122,11 @@ class Piggybank extends Ardent
$q->where('targetdate', '>=', $today->format('Y-m-d'));
}
)->orderBy('startdate', 'ASC');
$result = $query->first();
$result = $query->first();
$this->currentRep = $result;
return $result;
return $result;
}
}
@@ -155,26 +165,26 @@ class Piggybank extends Ardent
public function repetitionForDate(Carbon $date)
{
$query = $this->piggybankrepetitions()->where(
function ($q) use ($date) {
function ($q) use ($date) {
$q->where(
function ($q) use ($date) {
$q->whereNull('startdate');
$q->orWhere('startdate', '<=', $date->format('Y-m-d'));
}
)->where(
function ($q) use ($date) {
$q->whereNull('targetdate');
$q->orWhere('targetdate', '>=', $date->format('Y-m-d'));
}
);
}
)->orWhere(
function ($q) use ($date) {
$q->where('startdate', '>=', $date->format('Y-m-d'));
$q->where('targetdate', '>=', $date->format('Y-m-d'));
}
)->orderBy('startdate', 'ASC');
$q->where(
function ($q) use ($date) {
$q->whereNull('startdate');
$q->orWhere('startdate', '<=', $date->format('Y-m-d'));
}
)->where(
function ($q) use ($date) {
$q->whereNull('targetdate');
$q->orWhere('targetdate', '>=', $date->format('Y-m-d'));
}
);
}
)->orWhere(
function ($q) use ($date) {
$q->where('startdate', '>=', $date->format('Y-m-d'));
$q->where('targetdate', '>=', $date->format('Y-m-d'));
}
)->orderBy('startdate', 'ASC');
$result = $query->first();
return $result;

View File

@@ -51,44 +51,66 @@ class RecurringTransaction extends Ardent
return ['created_at', 'updated_at', 'date'];
}
public function lastFoundMatch() {
$last = $this->transactionjournals()->orderBy('date','DESC')->first();
if($last) {
return $last->date;
}
return null;
}
/**
* @return Carbon
* Find the next expected match based on the set journals and the date stuff from the recurring
* transaction.
*/
public function next()
public function nextExpectedMatch()
{
$today = new Carbon;
$start = clone $this->date;
$skip = $this->skip == 0 ? 1 : $this->skip;
if ($today < $start) {
return $start;
}
while ($start <= $this->date) {
switch ($this->repeat_freq) {
case 'daily':
$start->addDays($skip);
break;
case 'weekly':
$start->addWeeks($skip);
break;
case 'monthly':
$start->addMonths($skip);
break;
case 'quarterly':
$start->addMonths($skip * 3);
break;
case 'half-year':
$start->addMonths($skip * 6);
break;
case 'yearly':
$start->addYears($skip);
break;
/** @var \FireflyIII\Shared\Toolkit\Date $dateKit */
$dateKit = App::make('FireflyIII\Shared\Toolkit\Date');
/*
* The date Firefly tries to find. If this stays null, it's "unknown".
*/
$finalDate = null;
/*
* $today is the start of the next period, to make sure FF3 won't miss anything
* when the current period has a transaction journal.
*/
$today = $dateKit->addPeriod(new Carbon, $this->repeat_freq, 0);
/*
* FF3 loops from the $start of the recurring transaction, and to make sure
* $skip works, it adds one (for modulo).
*/
$skip = $this->skip + 1;
$start = $dateKit->startOfPeriod(new Carbon, $this->repeat_freq);
/*
* go back exactly one month/week/etc because FF3 does not care about 'next'
* recurring transactions if they're too far into the past.
*/
// echo 'Repeat freq is: ' . $recurringTransaction->repeat_freq . '<br />';
// echo 'Start: ' . $start . ' <br />';
$counter = 0;
while ($start <= $today) {
if (($counter % $skip) == 0) {
// do something.
$end = $dateKit->endOfPeriod(clone $start, $this->repeat_freq);
$journalCount = $this->transactionjournals()->before($end)->after($start)->count();
if ($journalCount == 0) {
$finalDate = clone $start;
break;
}
}
}
return $start;
// add period for next round!
$start = $dateKit->addPeriod($start, $this->repeat_freq, 0);
$counter++;
}
return $finalDate;
}
/**

View File

@@ -78,29 +78,6 @@ class Transaction extends Ardent
return $this->belongsToMany('Component');
}
/**
* @param Piggybank $piggybank
*
* @return bool
*/
public function connectPiggybank(\Piggybank $piggybank = null)
{
// TODO connect a piggy bank to a transaction.
throw new NotImplementedException;
// if (is_null($piggybank)) {
// return true;
// }
// /** @var \Firefly\Storage\Piggybank\PiggybankRepositoryInterface $piggyRepository */
// $piggyRepository = \App::make('Firefly\Storage\Piggybank\PiggybankRepositoryInterface');
// if ($this->account_id == $piggybank->account_id) {
// $this->piggybank()->associate($piggybank);
// $this->save();
// \Event::fire('piggybanks.createRelatedTransfer', [$piggybank, $this->transactionJournal, $this]);
// return true;
// }
// return false;
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/