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

@ -51,7 +51,17 @@ class RepeatedExpenseController extends BaseController
$subTitle = $piggyBank->name; $subTitle = $piggyBank->name;
$today = Carbon::now(); $today = Carbon::now();
return View::make('repeatedexpense.show', compact('piggyBank', 'today', 'subTitle')); /** @var \FireflyIII\Database\RepeatedExpense $repository */
$repository = App::make('FireflyIII\Database\RepeatedExpense');
$repetitions = $piggyBank->piggybankrepetitions()->get();
$repetitions->each(
function (PiggybankRepetition $repetition) use ($repository) {
$repetition = $repository->calculateParts($repetition);
}
);
return View::make('repeatedexpense.show', compact('repetitions','piggyBank', 'today', 'subTitle'));
} }
/** /**

View File

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

View File

@ -116,6 +116,26 @@ class RepeatedExpense implements CUD, CommonDatabaseCalls, PiggybankInterface
// } // }
for ($i = 0; $i < $parts; $i++) { 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: * Jump one month ahead after the first instance:
*/ */
@ -148,6 +168,17 @@ class RepeatedExpense implements CUD, CommonDatabaseCalls, PiggybankInterface
$part->setCurrentamount($repetition->currentamount); $part->setCurrentamount($repetition->currentamount);
$part->setStartdate($currentStart); $part->setStartdate($currentStart);
$part->setTargetdate($currentTarget); $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)) { // if (!is_null($piggyBank->reminder)) {
// $currentStart = \DateKit::addPeriod($currentStart, $piggyBank->reminder, 0); // $currentStart = \DateKit::addPeriod($currentStart, $piggyBank->reminder, 0);

View File

@ -98,6 +98,51 @@ class Date
return $currentEnd; 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) public function periodShow(Carbon $date, $repeatFrequency)
{ {
switch ($repeatFrequency) { switch ($repeatFrequency) {
@ -107,13 +152,18 @@ class Date
case 'daily': case 'daily':
return $date->format('j F Y'); return $date->format('j F Y');
break; break;
case 'week':
case 'weekly': case 'weekly':
return $date->format('\W\e\e\k W, Y'); return $date->format('\W\e\e\k W, Y');
break; break;
case 'quarter':
return $date->format('F Y');
break;
case 'monthly': case 'monthly':
case 'month': case 'month':
return $date->format('F Y'); return $date->format('F Y');
break; break;
case 'year':
case 'yearly': case 'yearly':
return $date->format('Y'); return $date->format('Y');
break; break;

View File

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

View File

@ -2,7 +2,12 @@
@section('content') @section('content')
<div class="row"> <div class="row">
<div class="col-lg-12 col-md-12 col-sm-12"> <div class="col-lg-12 col-md-12 col-sm-12">
@foreach($piggyBank->piggybankrepetitions as $rep) @foreach($repetitions as $rep)
<?php
$barSize = floor(12 / $rep->bars->count()) == 0 ? 1 : floor(12 / $rep->bars->count());
?>
<div class="panel <div class="panel
@if($today > $rep->startdate && $today < $rep->targetdate) @if($today > $rep->startdate && $today < $rep->targetdate)
panel-primary panel-primary
@ -14,10 +19,45 @@
Repetition from {{$rep->startdate->format('j F Y')}} to {{$rep->targetdate->format('j F Y')}} Repetition from {{$rep->startdate->format('j F Y')}} to {{$rep->targetdate->format('j F Y')}}
</div> </div>
<div class="panel-body"> <div class="panel-body">
{{$piggyBank->reminder}} <p>
Target amount: {{mf($piggyBank->targetamount)}}. Currently saved: {{mf($rep->currentamount)}}.
</p>
<div class="row">
@foreach($rep->bars as $bar)
<div class="col-lg-{{$barSize}} col-md-{{$barSize}} col-sm-{{$barSize}}">
<div class="progress">
<!-- currentAmount:{{$bar->getCurrentAmount()}} getAmount:{{$bar->getAmount()}} -->
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="{{$bar->percentage()}}" aria-valuemin="0" aria-valuemax="100" style="width: {{$bar->percentage()}}%;"></div>
<div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="{{100-$bar->percentage()}}" aria-valuemin="0" aria-valuemax="100" style="width: {{100-$bar->percentage()}}%;"></div>
</div> </div>
<p class="small">
<!-- {{mf($bar->getAmount())}} -->
{{--
@if($bar->hasReminder())
<a href="{{route('reminders.show',$bar->getReminder()->id)}}">
{{DateKit::periodShow($bar->getStartDate(),$piggyBank->reminder)}}
</a>
@else
@if(!is_null($piggyBank->reminder))
{{DateKit::periodShow($bar->getStartDate(),$piggyBank->reminder)}}
@endif
@endif
--}}
{{$bar->getStartDate()->format('d/m/y')}} &rarr; {{$bar->getTargetDate()->format('d/m/y')}}
@if($bar->hasReminder())
!
@endif
</p>
</div> </div>
@endforeach @endforeach
</div> </div>
</div> </div>
</div>
@endforeach
@foreach($piggyBank->reminders()->get() as $reminder)
Reminder: #{{$reminder->id}} [from: {{$reminder->startdate->format('d/m/y')}}, to: {{$reminder->enddate->format('d/m/y')}}]<br />
@endforeach
</div>
</div>
@stop @stop