Files
firefly-iii/app/lib/FireflyIII/Database/PiggyBank/RepeatedExpense.php

125 lines
4.0 KiB
PHP
Raw Normal View History

<?php
2014-12-13 22:11:51 +01:00
namespace FireflyIII\Database\PiggyBank;
use FireflyIII\Collection\PiggyBankPart;
2015-01-02 06:05:40 +01:00
use FireflyIII\Database\CommonDatabaseCallsInterface;
use FireflyIII\Database\CUDInterface;
use Illuminate\Support\Collection;
2014-12-06 12:12:55 +01:00
2014-12-13 21:59:02 +01:00
/**
* Class RepeatedExpense
*
* @package FireflyIII\Database
*/
2015-01-02 06:05:40 +01:00
class RepeatedExpense extends PiggyBankShared implements CUDInterface, CommonDatabaseCallsInterface, PiggyBankInterface
{
2014-11-24 10:12:34 +01:00
/**
2015-01-17 10:06:12 +01:00
* @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind.
*
2014-11-24 10:12:34 +01:00
* Based on the piggy bank, the reminder-setting and
* other variables this method tries to divide the piggy bank into equal parts. Each is
* accommodated by a reminder (if everything goes to plan).
*
* @param \PiggyBankRepetition $repetition
2014-12-13 21:59:02 +01:00
*
2014-12-14 20:40:02 +01:00
* @return Collection
2014-11-24 10:12:34 +01:00
*/
public function calculateParts(\PiggyBankRepetition $repetition)
2014-11-24 10:12:34 +01:00
{
/** @var \PiggyBank $piggyBank */
$piggyBank = $repetition->piggyBank()->first();
2014-12-14 20:40:02 +01:00
$bars = new Collection;
$currentStart = clone $repetition->startdate;
2014-11-24 10:12:34 +01:00
if (is_null($piggyBank->reminder)) {
2014-12-14 20:40:02 +01:00
$entry = ['repetition' => $repetition, 'amountPerBar' => floatval($piggyBank->targetamount),
'currentAmount' => floatval($repetition->currentamount), 'cumulativeAmount' => floatval($piggyBank->targetamount),
'startDate' => clone $repetition->startdate, 'targetDate' => clone $repetition->targetdate];
$bars->push($this->createPiggyBankPart($entry));
2014-11-24 10:12:34 +01:00
2014-12-14 20:40:02 +01:00
return $bars;
2014-11-24 10:12:34 +01:00
}
2014-11-24 17:01:37 +01:00
2014-12-14 20:40:02 +01:00
while ($currentStart < $repetition->targetdate) {
$currentTarget = \DateKit::endOfX($currentStart, $piggyBank->reminder, $repetition->targetdate);
$entry = ['repetition' => $repetition, 'amountPerBar' => null, 'currentAmount' => floatval($repetition->currentamount),
'cumulativeAmount' => null, 'startDate' => $currentStart, 'targetDate' => $currentTarget];
$bars->push($this->createPiggyBankPart($entry));
2014-11-24 18:06:21 +01:00
$currentStart = clone $currentTarget;
2014-12-14 20:40:02 +01:00
$currentStart->addDay();
2014-11-24 10:12:34 +01:00
}
2014-12-14 20:40:02 +01:00
$amountPerBar = floatval($piggyBank->targetamount) / $bars->count();
2014-11-24 18:06:21 +01:00
$cumulative = $amountPerBar;
/** @var PiggyBankPart $bar */
foreach ($bars as $index => $bar) {
2014-11-24 18:06:21 +01:00
$bar->setAmountPerBar($amountPerBar);
$bar->setCumulativeAmount($cumulative);
2014-12-14 20:40:02 +01:00
if ($bars->count() - 1 == $index) {
$bar->setCumulativeAmount($piggyBank->targetamount);
}
2014-11-24 18:06:21 +01:00
$cumulative += $amountPerBar;
2014-11-24 10:12:34 +01:00
}
2014-12-14 20:40:02 +01:00
return $bars;
}
2014-11-24 10:12:34 +01:00
2014-12-14 20:40:02 +01:00
/**
* @param array $data
*
* @return PiggyBankPart
2014-12-14 20:40:02 +01:00
*/
public function createPiggyBankPart(array $data)
{
$part = new PiggyBankPart;
2014-12-14 20:40:02 +01:00
$part->setRepetition($data['repetition']);
$part->setAmountPerBar($data['amountPerBar']);
$part->setCurrentamount($data['currentAmount']);
$part->setCumulativeAmount($data['cumulativeAmount']);
$part->setStartdate($data['startDate']);
$part->setTargetdate($data['targetDate']);
return $part;
2014-11-24 10:12:34 +01:00
}
/**
* @param array $data
*
2014-12-06 12:12:55 +01:00
* @return \Eloquent
*/
public function store(array $data)
{
$data['rep_every'] = intval($data['rep_every']);
$data['reminder_skip'] = intval($data['reminder_skip']);
$data['order'] = intval($data['order']);
$data['remind_me'] = intval($data['remind_me']);
$data['account_id'] = intval($data['account_id']);
if ($data['remind_me'] == 0) {
$data['reminder'] = null;
}
$repeated = new \PiggyBank($data);
$repeated->save();
return $repeated;
}
/**
* Returns all objects.
*
* @return Collection
*/
public function get()
{
return $this->getUser()->piggyBanks()->where('repeats', 1)->get();
}
2015-01-02 06:16:49 +01:00
}