mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-02 12:15:55 +00:00
Alright for issue #6 a lot of things have been updated. Piggy banks can now be made "repeated" in a different form, viewed and edited; Firefly will properly trigger to make the "instances". Also the show-view is finished which doesn't do much but helps in seeing how it works. [skip ci]
This commit is contained in:
@@ -40,6 +40,18 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
||||
)->where('repeats', 1)->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Piggybank $piggyBank
|
||||
*
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function destroy(\Piggybank $piggyBank)
|
||||
{
|
||||
$piggyBank->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $piggyBankId
|
||||
*
|
||||
@@ -57,9 +69,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
||||
*/
|
||||
public function get()
|
||||
{
|
||||
return \Piggybank::with('account')->leftJoin('accounts', 'accounts.id', '=', 'piggybanks.account_id')->where(
|
||||
'accounts.user_id', \Auth::user()->id
|
||||
)->get(['piggybanks.*']);
|
||||
return \Auth::user()->piggybanks()->with(['account', 'piggybankrepetitions'])->get();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,12 +100,12 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
||||
if ($piggyBank->validate()) {
|
||||
if (!is_null($piggyBank->targetdate) && $piggyBank->targetdate < $today) {
|
||||
$piggyBank->errors()->add('targetdate', 'Target date cannot be in the past.');
|
||||
|
||||
return $piggyBank;
|
||||
}
|
||||
|
||||
if (!is_null($piggyBank->reminder) && !is_null($piggyBank->targetdate)) {
|
||||
// first period for reminder is AFTER target date.
|
||||
// just flash a warning
|
||||
$reminderSkip = $piggyBank->reminder_skip < 1 ? 1 : intval($piggyBank->reminder_skip);
|
||||
$firstReminder = new Carbon;
|
||||
switch ($piggyBank->reminder) {
|
||||
@@ -116,7 +126,10 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
||||
break;
|
||||
}
|
||||
if ($firstReminder > $piggyBank->targetdate) {
|
||||
$piggyBank->errors()->add('reminder', 'The reminder has been set to remind you after the piggy bank will expire.');
|
||||
$piggyBank->errors()->add(
|
||||
'reminder', 'The reminder has been set to remind you after the piggy bank will expire.'
|
||||
);
|
||||
|
||||
return $piggyBank;
|
||||
}
|
||||
}
|
||||
@@ -127,26 +140,51 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @param \Piggybank $piggy
|
||||
* @param $data
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function update($data)
|
||||
public function update(\Piggybank $piggy, $data)
|
||||
{
|
||||
$piggyBank = $this->find($data['id']);
|
||||
if ($piggyBank) {
|
||||
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
|
||||
$account = $accounts->find($data['account_id']);
|
||||
// update piggybank accordingly:
|
||||
$piggyBank->name = $data['name'];
|
||||
$piggyBank->target = floatval($data['target']);
|
||||
$piggyBank->account()->associate($account);
|
||||
if ($piggyBank->validate()) {
|
||||
$piggyBank->save();
|
||||
}
|
||||
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
|
||||
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
|
||||
$account = isset($data['account_id']) ? $accounts->find($data['account_id']) : null;
|
||||
|
||||
if (!is_null($account)) {
|
||||
$piggy->account()->associate($account);
|
||||
}
|
||||
|
||||
return $piggyBank;
|
||||
$piggy->name = $data['name'];
|
||||
$piggy->targetamount = floatval($data['targetamount']);
|
||||
$piggy->reminder = isset($data['reminder']) && $data['reminder'] != 'none' ? $data['reminder'] : null;
|
||||
$piggy->reminder_skip = $data['reminder_skip'];
|
||||
$piggy->targetdate = strlen($data['targetdate']) > 0 ? new Carbon($data['targetdate']) : null;
|
||||
$piggy->startdate
|
||||
= isset($data['startdate']) && strlen($data['startdate']) > 0 ? new Carbon($data['startdate']) : null;
|
||||
|
||||
// everything we can update for NON repeating piggy banks:
|
||||
if ($piggy->repeats == 0) {
|
||||
// if non-repeating there is only one PiggyBank instance and we can delete it safely.
|
||||
// it will be recreated.
|
||||
$piggy->piggybankrepetitions()->first()->delete();
|
||||
} else {
|
||||
// we can delete all of them, because reasons
|
||||
foreach ($piggy->piggybankrepetitions()->get() as $rep) {
|
||||
$rep->delete();
|
||||
}
|
||||
|
||||
$piggy->rep_every = intval($data['rep_every']);
|
||||
$piggy->rep_length = $data['rep_length'];
|
||||
}
|
||||
if ($piggy->validate()) {
|
||||
// check the things we check for new piggies
|
||||
$piggy->save();
|
||||
}
|
||||
|
||||
|
||||
return $piggy;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,6 +11,28 @@ namespace Firefly\Storage\Piggybank;
|
||||
interface PiggybankRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function count();
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function countNonrepeating();
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function countRepeating();
|
||||
|
||||
/**
|
||||
* @param \Piggybank $piggyBank
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function destroy(\Piggybank $piggyBank);
|
||||
|
||||
/**
|
||||
* @param $piggyBankId
|
||||
*
|
||||
@@ -21,17 +43,7 @@ interface PiggybankRepositoryInterface
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function count();
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function countRepeating();
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function countNonrepeating();
|
||||
public function get();
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
@@ -41,9 +53,12 @@ interface PiggybankRepositoryInterface
|
||||
public function store($data);
|
||||
|
||||
/**
|
||||
* @param \Piggybank $piggy
|
||||
* @param $data
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get();
|
||||
public function update(\Piggybank $piggy, $data);
|
||||
|
||||
/**
|
||||
* @param \Piggybank $piggyBank
|
||||
@@ -53,11 +68,4 @@ interface PiggybankRepositoryInterface
|
||||
*/
|
||||
public function updateAmount(\Piggybank $piggyBank, $amount);
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function update($data);
|
||||
|
||||
}
|
||||
114
app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php
Normal file
114
app/lib/Firefly/Trigger/Piggybanks/EloquentPiggybankTrigger.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Firefly\Trigger\Piggybanks;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Events\Dispatcher;
|
||||
|
||||
/**
|
||||
* Class EloquentPiggybankTrigger
|
||||
*
|
||||
* @package Firefly\Trigger\Piggybanks
|
||||
*/
|
||||
class EloquentPiggybankTrigger
|
||||
{
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(
|
||||
'piggybanks.change', 'Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger@updatePiggybankRepetitions'
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function updatePiggybankRepetitions()
|
||||
{
|
||||
// grab all piggy banks.
|
||||
$piggybanks = \Auth::user()->piggybanks()->with(['piggybankrepetitions'])->where('repeats', 0)->get();
|
||||
$today = new Carbon;
|
||||
/** @var \Piggybank $piggy */
|
||||
foreach ($piggybanks as $piggy) {
|
||||
if (count($piggy->piggybankrepetitions) == 0) {
|
||||
$rep = new \PiggybankRepetition;
|
||||
$rep->piggybank()->associate($piggy);
|
||||
$rep->targetdate = $piggy->targetdate;
|
||||
$rep->startdate = $piggy->startdate;
|
||||
$rep->currentamount = 0;
|
||||
try {
|
||||
$rep->save();
|
||||
} catch (QueryException $e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
unset($piggy, $piggybanks, $rep);
|
||||
|
||||
// grab all repeated transactions.
|
||||
$repeatedExpenses = \Auth::user()->piggybanks()->with(['piggybankrepetitions'])->where('repeats', 1)->get();
|
||||
/** @var \Piggybank $repeated */
|
||||
foreach ($repeatedExpenses as $repeated) {
|
||||
// loop from start to today or something
|
||||
$rep = new \PiggybankRepetition;
|
||||
$rep->piggybank()->associate($repeated);
|
||||
$rep->startdate = $repeated->startdate;
|
||||
$rep->targetdate = $repeated->targetdate;
|
||||
$rep->currentamount = 0;
|
||||
try {
|
||||
\Log::debug(
|
||||
'Creating initial rep ('.$repeated->name.') (from ' . ($rep->startdate ? $rep->startdate->format('d-m-Y')
|
||||
: 'NULL') . ' to '
|
||||
. ($rep->targetdate ? $rep->targetdate->format('d-m-Y') : 'NULL') . ')'
|
||||
);
|
||||
$rep->save();
|
||||
} catch (QueryException $e) {
|
||||
\Log::error('FAILED initital repetition.');
|
||||
}
|
||||
unset($rep);
|
||||
|
||||
if ($repeated->targetdate <= $today) {
|
||||
// add 1 month to startdate, or maybe X period, like 3 weeks.
|
||||
$startTarget = clone $repeated->targetdate;
|
||||
while ($startTarget <= $today) {
|
||||
$startCurrent = clone $startTarget;
|
||||
|
||||
// add some kind of period to start current making $endCurrent.
|
||||
$endCurrent = clone $startCurrent;
|
||||
switch ($repeated->rep_length) {
|
||||
default:
|
||||
die('No rep lengt!');
|
||||
break;
|
||||
case 'day':
|
||||
$endCurrent->addDays($repeated->rep_every);
|
||||
break;
|
||||
case 'week':
|
||||
$endCurrent->addWeeks($repeated->rep_every);
|
||||
break;
|
||||
case 'month':
|
||||
$endCurrent->addMonths($repeated->rep_every);
|
||||
break;
|
||||
case 'year':
|
||||
$endCurrent->addYears($repeated->rep_every);
|
||||
break;
|
||||
}
|
||||
|
||||
$rep = new \PiggybankRepetition;
|
||||
$rep->piggybank()->associate($repeated);
|
||||
$rep->startdate = $startCurrent;
|
||||
$rep->targetdate = $endCurrent;
|
||||
$rep->currentamount = 0;
|
||||
$startTarget = $endCurrent;
|
||||
try {
|
||||
$rep->save();
|
||||
} catch (QueryException $e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user