2014-10-31 07:32:43 +01:00
|
|
|
<?php
|
2014-12-13 22:11:51 +01:00
|
|
|
namespace FireflyIII\Database\PiggyBank;
|
2014-10-31 07:32:43 +01:00
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2015-01-02 06:05:40 +01:00
|
|
|
use FireflyIII\Database\CommonDatabaseCallsInterface;
|
|
|
|
|
use FireflyIII\Database\CUDInterface;
|
2014-12-06 12:12:55 +01:00
|
|
|
use FireflyIII\Exception\FireflyException;
|
2014-11-12 22:21:48 +01:00
|
|
|
use FireflyIII\Exception\NotImplementedException;
|
|
|
|
|
use Illuminate\Support\Collection;
|
2014-12-06 12:12:55 +01:00
|
|
|
|
2014-10-31 07:32:43 +01:00
|
|
|
/**
|
2014-12-24 20:55:42 +01:00
|
|
|
* Class PiggyBank
|
2014-10-31 07:32:43 +01:00
|
|
|
*
|
|
|
|
|
* @package FireflyIII\Database
|
|
|
|
|
*/
|
2015-01-02 06:05:40 +01:00
|
|
|
class PiggyBank extends PiggyBankShared implements CUDInterface, CommonDatabaseCallsInterface, PiggyBankInterface
|
2014-10-31 07:32:43 +01:00
|
|
|
{
|
2014-12-13 21:59:02 +01:00
|
|
|
/**
|
2014-12-24 20:55:42 +01:00
|
|
|
* @param \PiggyBank $piggyBank
|
2014-12-13 21:59:02 +01:00
|
|
|
* @param Carbon $date
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws FireflyException
|
|
|
|
|
* @throws NotImplementedException
|
|
|
|
|
*/
|
2014-12-24 20:55:42 +01:00
|
|
|
public function findRepetitionByDate(\PiggyBank $piggyBank, Carbon $date)
|
2014-11-16 10:31:19 +01:00
|
|
|
{
|
2014-12-19 20:47:33 +01:00
|
|
|
/** @var Collection $reps */
|
2014-12-24 20:55:42 +01:00
|
|
|
$reps = $piggyBank->piggyBankRepetitions()->get();
|
2014-11-16 10:31:19 +01:00
|
|
|
if ($reps->count() == 1) {
|
|
|
|
|
return $reps->first();
|
|
|
|
|
}
|
2014-12-19 20:47:33 +01:00
|
|
|
// should filter the one we need:
|
|
|
|
|
$repetitions = $reps->filter(
|
2014-12-24 20:55:42 +01:00
|
|
|
function (\PiggyBankRepetition $rep) use ($date) {
|
2015-01-17 08:58:49 +01:00
|
|
|
if ($date->between($rep->startdate, $rep->targetdate)) {
|
2014-12-19 20:47:33 +01:00
|
|
|
return $rep;
|
|
|
|
|
}
|
2014-12-20 15:00:53 +01:00
|
|
|
|
2014-12-19 21:18:42 +01:00
|
|
|
return null;
|
2014-12-19 20:47:33 +01:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
if ($repetitions->count() == 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $repetitions->first();
|
2014-11-16 10:31:19 +01:00
|
|
|
}
|
|
|
|
|
|
2014-10-31 07:32:43 +01:00
|
|
|
/**
|
2014-12-30 15:17:01 +01:00
|
|
|
* Returns all objects.
|
2014-10-31 07:32:43 +01:00
|
|
|
*
|
2014-12-30 15:17:01 +01:00
|
|
|
* @return Collection
|
2014-10-31 07:32:43 +01:00
|
|
|
*/
|
2014-12-30 15:17:01 +01:00
|
|
|
public function get()
|
2014-10-31 07:32:43 +01:00
|
|
|
{
|
2014-12-30 15:17:01 +01:00
|
|
|
return $this->getUser()->piggyBanks()->where('repeats', 0)->orderBy('name')->get();
|
2014-11-02 14:59:09 +01:00
|
|
|
}
|
2015-01-02 06:16:49 +01:00
|
|
|
}
|