Expand repeated expenses.

This commit is contained in:
James Cole
2014-11-24 17:01:37 +01:00
parent bfda4bc199
commit b051278d2e
6 changed files with 161 additions and 7 deletions

View File

@@ -12,6 +12,8 @@ class PiggybankPart
public $amountPerBar;
/** @var int */
public $currentamount;
/** @var \Reminder */
public $reminder;
/** @var \PiggybankRepetition */
public $repetition;
/** @var Carbon */
@@ -19,6 +21,22 @@ class PiggybankPart
/** @var Carbon */
public $targetdate;
/**
* @return \Reminder
*/
public function getReminder()
{
return $this->reminder;
}
/**
* @param \Reminder $reminder
*/
public function setReminder($reminder)
{
$this->reminder = $reminder;
}
/**
* @return \PiggybankRepetition
*/
@@ -67,6 +85,11 @@ class PiggybankPart
$this->targetdate = $targetdate;
}
public function hasReminder()
{
return !is_null($this->reminder);
}
public function percentage()
{
if ($this->getCurrentamount() < $this->getAmount()) {
@@ -130,6 +153,4 @@ class PiggybankPart
{
$this->amountPerBar = $amountPerBar;
}
}

View File

@@ -116,6 +116,26 @@ class RepeatedExpense implements CUD, CommonDatabaseCalls, PiggybankInterface
// }
for ($i = 0; $i < $parts; $i++) {
/*
* If it's not the first repetition, jump the start date a [period]
* and jump the target date a [period]
*/
if($i > 0) {
$currentStart = clone $currentTarget;
$currentStart->addDay();
$currentTarget = \DateKit::addPeriod($currentStart, $piggyBank->reminder,0);
}
/*
* If it's the first one, and has reminders, jump to the end of the [period]
*/
if($i == 0 && !is_null($piggyBank->reminder)) {
$currentTarget = \DateKit::endOfX($currentStart, $piggyBank->reminder);
}
if($currentStart > $repetition->targetdate) {
break;
}
/*
* Jump one month ahead after the first instance:
*/
@@ -148,6 +168,17 @@ class RepeatedExpense implements CUD, CommonDatabaseCalls, PiggybankInterface
$part->setCurrentamount($repetition->currentamount);
$part->setStartdate($currentStart);
$part->setTargetdate($currentTarget);
if (!is_null($piggyBank->reminder)) {
// might be a reminder for this range?
$reminder = $piggyBank->reminders()
->where('startdate',$currentStart->format('Y-m-d'))
->where('enddate',$currentTarget->format('Y-m-d'))
->first();
if($reminder) {
$part->setReminder($reminder);
}
}
// if (!is_null($piggyBank->reminder)) {
// $currentStart = \DateKit::addPeriod($currentStart, $piggyBank->reminder, 0);

View File

@@ -98,6 +98,51 @@ class Date
return $currentEnd;
}
/**
* @param Carbon $theCurrentEnd
* @param $repeatFreq
*
* @return mixed
* @throws FireflyException
*/
public function endOfX(Carbon $theCurrentEnd, $repeatFreq)
{
$currentEnd = clone $theCurrentEnd;
switch ($repeatFreq) {
default:
throw new FireflyException('Cannot do endOfPeriod for $repeat_freq ' . $repeatFreq);
break;
case 'daily':
$currentEnd->endOfDay();
break;
case 'week':
case 'weekly':
$currentEnd->endOfWeek();
break;
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;
}
return $currentEnd;
}
public function periodShow(Carbon $date, $repeatFrequency)
{
switch ($repeatFrequency) {
@@ -107,13 +152,18 @@ class Date
case 'daily':
return $date->format('j F Y');
break;
case 'week':
case 'weekly':
return $date->format('\W\e\e\k W, Y');
break;
case 'quarter':
return $date->format('F Y');
break;
case 'monthly':
case 'month':
return $date->format('F Y');
break;
case 'year':
case 'yearly':
return $date->format('Y');
break;

View File

@@ -85,6 +85,8 @@ class Reminders
}
}
);
$today = Carbon::now();
//$today = new Carbon('15-12-2014');
/** @var \Piggybank $piggybank */
foreach ($set as $piggybank) {
@@ -95,8 +97,8 @@ class Reminders
*/
/** @var \PiggybankRepetition $repetition */
$repetition = $piggybank->currentRelevantRep();
$start = \DateKit::startOfPeriod(Carbon::now(), $piggybank->reminder);
if ($repetition->targetdate && $repetition->targetdate <= Carbon::now()) {
$start = \DateKit::startOfPeriod($today, $piggybank->reminder);
if ($repetition->targetdate && $repetition->targetdate <= $today) {
// break when no longer relevant:
continue;
}