mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Some cleaning up in the whole piggy bank section. More narrow events and stricter code. Still not catching some events, so it'll eventually all break down. [skip ci]
This commit is contained in:
parent
87bddce1d3
commit
f5330728d4
@ -77,8 +77,9 @@ class PiggybankController extends BaseController
|
||||
*/
|
||||
public function destroy(Piggybank $piggyBank)
|
||||
{
|
||||
Event::fire('piggybanks.destroy', [$piggyBank]);
|
||||
$this->_repository->destroy($piggyBank);
|
||||
Event::fire('piggybanks.change');
|
||||
|
||||
Session::flash('success', 'Piggy bank deleted.');
|
||||
|
||||
return Redirect::route('piggybanks.index');
|
||||
@ -109,7 +110,6 @@ class PiggybankController extends BaseController
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
Event::fire('piggybanks.change');
|
||||
$countRepeating = $this->_repository->countRepeating();
|
||||
$countNonRepeating = $this->_repository->countNonrepeating();
|
||||
|
||||
@ -120,10 +120,10 @@ class PiggybankController extends BaseController
|
||||
|
||||
$accounts = [];
|
||||
/** @var \Piggybank $piggybank */
|
||||
foreach($piggybanks as $piggybank) {
|
||||
foreach ($piggybanks as $piggybank) {
|
||||
$account = $piggybank->account;
|
||||
$id = $account->id;
|
||||
if(!isset($accounts[$id])) {
|
||||
if (!isset($accounts[$id])) {
|
||||
$accounts[$id] = ['account' => $account, 'left' => $this->_repository->leftOnAccount($account)];
|
||||
}
|
||||
}
|
||||
@ -131,7 +131,7 @@ class PiggybankController extends BaseController
|
||||
return View::make('piggybanks.index')->with('piggybanks', $piggybanks)
|
||||
->with('countRepeating', $countRepeating)
|
||||
->with('countNonRepeating', $countNonRepeating)
|
||||
->with('accounts',$accounts);
|
||||
->with('accounts', $accounts);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -152,6 +152,7 @@ class PiggybankController extends BaseController
|
||||
if (round($amount, 2) <= round(min($maxAdd, $piggyBank->targetamount), 2)) {
|
||||
Session::flash('success', 'Amount updated!');
|
||||
$this->_repository->modifyAmount($piggyBank, $amount);
|
||||
Event::fire('piggybanks.modifyAmountAdd', [$piggyBank, $amount]);
|
||||
} else {
|
||||
Session::flash('warning', 'Could not!');
|
||||
}
|
||||
@ -162,6 +163,7 @@ class PiggybankController extends BaseController
|
||||
if (round($amount, 2) <= round($maxRemove, 2)) {
|
||||
Session::flash('success', 'Amount updated!');
|
||||
$this->_repository->modifyAmount($piggyBank, ($amount * -1));
|
||||
Event::fire('piggybanks.modifyAmountRemove', [$piggyBank, ($amount * -1)]);
|
||||
} else {
|
||||
Session::flash('warning', 'Could not!');
|
||||
}
|
||||
@ -210,7 +212,7 @@ class PiggybankController extends BaseController
|
||||
$piggyBank = $this->_repository->store($data);
|
||||
if (!is_null($piggyBank->id)) {
|
||||
Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!');
|
||||
Event::fire('piggybanks.change');
|
||||
Event::fire('piggybanks.storePiggy',[$piggyBank]);
|
||||
|
||||
return Redirect::route('piggybanks.index');
|
||||
|
||||
@ -239,7 +241,7 @@ class PiggybankController extends BaseController
|
||||
$piggyBank = $this->_repository->store($data);
|
||||
if ($piggyBank->id) {
|
||||
Session::flash('success', 'New piggy bank "' . $piggyBank->name . '" created!');
|
||||
Event::fire('piggybanks.change');
|
||||
Event::fire('piggybanks.storeRepeated',[$piggyBank]);
|
||||
|
||||
return Redirect::route('piggybanks.index');
|
||||
|
||||
@ -259,7 +261,7 @@ class PiggybankController extends BaseController
|
||||
$piggyBank = $this->_repository->update($piggyBank, Input::all());
|
||||
if ($piggyBank->validate()) {
|
||||
Session::flash('success', 'Piggy bank "' . $piggyBank->name . '" updated.');
|
||||
Event::fire('piggybanks.change');
|
||||
Event::fire('piggybanks.update',[$piggyBank]);
|
||||
|
||||
return Redirect::route('piggybanks.index');
|
||||
} else {
|
||||
|
@ -107,13 +107,6 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
||||
if (!is_null($rep)) {
|
||||
$rep->currentamount += $amount;
|
||||
$rep->save();
|
||||
|
||||
// create event:
|
||||
$event = new \PiggybankEvent;
|
||||
$event->date = new Carbon;
|
||||
$event->amount = $amount;
|
||||
$event->piggybank()->associate($piggyBank);
|
||||
$event->save();
|
||||
}
|
||||
|
||||
|
||||
|
@ -332,6 +332,9 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
// connect the piggy to it:
|
||||
$transaction->piggybank()->associate($piggyBank);
|
||||
$transaction->save();
|
||||
\Event::fire(
|
||||
'piggybanks.createRelatedTransfer', [$piggyBank, $transactionJournal, $transaction]
|
||||
);
|
||||
} else {
|
||||
\Session::flash(
|
||||
'warning',
|
||||
@ -388,6 +391,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
if (!is_null($journal->budgets()->first())) {
|
||||
$journal->budgets()->detach($journal->budgets()->first()->id);
|
||||
}
|
||||
// remove previous piggy bank, if any:
|
||||
|
||||
|
||||
$category = isset($data['category']) ? $catRepository->findByName($data['category']) : null;
|
||||
@ -395,8 +399,18 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
$journal->categories()->attach($category);
|
||||
}
|
||||
// update the amounts:
|
||||
/** @var \Transaction $transaction */
|
||||
$transactions = $journal->transactions()->orderBy('amount', 'ASC')->get();
|
||||
|
||||
// remove previous piggy bank, if any:
|
||||
/** @var \Transaction $transaction */
|
||||
foreach ($transactions as $transaction) {
|
||||
if (!is_null($transaction->piggybank()->first())) {
|
||||
$transaction->piggybank()->detach($transaction->piggybank()->first()->first());
|
||||
$transaction->save();
|
||||
}
|
||||
}
|
||||
unset($transaction);
|
||||
|
||||
$transactions[0]->amount = $amount * -1;
|
||||
$transactions[1]->amount = $amount;
|
||||
|
||||
@ -432,6 +446,37 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
$journal->transactions[0]->account()->associate($fromAccount);
|
||||
$journal->transactions[1]->account()->associate($toAccount);
|
||||
|
||||
// attach the new piggy bank, if valid:
|
||||
/** @var \Firefly\Storage\Piggybank\PiggybankRepositoryInterface $piggyRepository */
|
||||
$piggyRepository = \App::make('Firefly\Storage\Piggybank\PiggybankRepositoryInterface');
|
||||
|
||||
if (isset($data['piggybank_id'])) {
|
||||
/** @var \Piggybank $piggyBank */
|
||||
$piggyBank = $piggyRepository->find(intval($data['piggybank_id']));
|
||||
|
||||
if ($piggyBank) {
|
||||
if ($toAccount->id == $piggyBank->account_id) {
|
||||
|
||||
// find the transaction connected to the $toAccount:
|
||||
/** @var \Transaction $transaction */
|
||||
$transaction
|
||||
= $journal->transactions()->where('account_id', $toAccount->id)->first();
|
||||
// connect the piggy to it:
|
||||
$transaction->piggybank()->associate($piggyBank);
|
||||
$transaction->save();
|
||||
\Event::fire('piggybanks.updateRelatedTransfer', [$piggyBank, $journal]);
|
||||
} else {
|
||||
\Session::flash(
|
||||
'warning',
|
||||
'Piggy bank "' . e($piggyBank->name) . '" is not set to draw money from account "' . e(
|
||||
$toAccount->name
|
||||
) . '", so the money isn\'t added to the piggy bank.'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new FireflyException('Cannot edit this!');
|
||||
|
@ -3,7 +3,6 @@
|
||||
namespace Firefly\Trigger\Piggybanks;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Events\Dispatcher;
|
||||
|
||||
/**
|
||||
@ -14,149 +13,362 @@ use Illuminate\Events\Dispatcher;
|
||||
class EloquentPiggybankTrigger
|
||||
{
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
* @param \Piggybank $piggyBank
|
||||
* @param \TransactionJournal $journal
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
public function createRelatedTransfer(
|
||||
\Piggybank $piggyBank, \TransactionJournal $journal, \Transaction $transaction
|
||||
) {
|
||||
$repetition = $piggyBank->repetitionForDate($journal->date);
|
||||
if (!is_null($repetition)) {
|
||||
// get the amount transferred TO this
|
||||
$amount = floatval($transaction->amount);
|
||||
$repetition->currentamount += $amount;
|
||||
$repetition->save();
|
||||
} else {
|
||||
\Session::flash('warning', 'Cannot add transfer to piggy, outside of scope.');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Piggybank $piggyBank
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy(\Piggybank $piggyBank)
|
||||
{
|
||||
$events->listen(
|
||||
'piggybanks.change', 'Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger@updatePiggybankRepetitions'
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Piggybank $piggyBank
|
||||
* @param $amount
|
||||
*/
|
||||
public function modifyAmountAdd(\Piggybank $piggyBank, $amount)
|
||||
{
|
||||
$rep = $piggyBank->currentRelevantRep();
|
||||
$today = new Carbon;
|
||||
|
||||
// create event:
|
||||
$event = new \PiggybankEvent;
|
||||
$event->date = new Carbon;
|
||||
$event->amount = $amount;
|
||||
$event->piggybank()->associate($piggyBank);
|
||||
|
||||
// for future / past repetitions.
|
||||
if (!($rep->startdate >= $today && $rep->targetdate <= $today)) {
|
||||
$event->date = $rep->startdate;
|
||||
}
|
||||
|
||||
|
||||
$event->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Piggybank $piggyBank
|
||||
* @param $amount
|
||||
*/
|
||||
public function modifyAmountRemove(\Piggybank $piggyBank, $amount)
|
||||
{
|
||||
// create event:
|
||||
$event = new \PiggybankEvent;
|
||||
$event->date = new Carbon;
|
||||
$event->amount = $amount;
|
||||
$event->piggybank()->associate($piggyBank);
|
||||
$event->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Piggybank $piggyBank
|
||||
*/
|
||||
public function storePiggy(\Piggybank $piggyBank)
|
||||
{
|
||||
$rep = new \PiggybankRepetition;
|
||||
$rep->piggybank()->associate($piggyBank);
|
||||
$rep->targetdate = $piggyBank->targetdate;
|
||||
$rep->startdate = $piggyBank->startdate;
|
||||
$rep->currentamount = 0;
|
||||
$rep->save();
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Piggybank $piggyBank
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function updatePiggybankRepetitions()
|
||||
public function storeRepeated(\Piggybank $piggyBank)
|
||||
{
|
||||
// grab all piggy banks.
|
||||
if (\Auth::check()) {
|
||||
$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) {
|
||||
}
|
||||
// loop from start to today or something
|
||||
$rep = new \PiggybankRepetition;
|
||||
$rep->piggybank()->associate($piggyBank);
|
||||
$rep->startdate = $piggyBank->startdate;
|
||||
$rep->targetdate = $piggyBank->targetdate;
|
||||
$rep->currentamount = 0;
|
||||
$rep->save();
|
||||
unset($rep);
|
||||
$today = new Carbon;
|
||||
|
||||
if ($piggyBank->targetdate <= $today) {
|
||||
// add 1 month to startdate, or maybe X period, like 3 weeks.
|
||||
$startTarget = clone $piggyBank->targetdate;
|
||||
while ($startTarget <= $today) {
|
||||
$startCurrent = clone $startTarget;
|
||||
|
||||
// add some kind of period to start current making $endCurrent.
|
||||
$endCurrent = clone $startCurrent;
|
||||
switch ($piggyBank->rep_length) {
|
||||
default:
|
||||
return true;
|
||||
break;
|
||||
case 'day':
|
||||
$endCurrent->addDays($piggyBank->rep_every);
|
||||
break;
|
||||
case 'week':
|
||||
$endCurrent->addWeeks($piggyBank->rep_every);
|
||||
break;
|
||||
case 'month':
|
||||
$endCurrent->addMonths($piggyBank->rep_every);
|
||||
break;
|
||||
case 'year':
|
||||
$endCurrent->addYears($piggyBank->rep_every);
|
||||
break;
|
||||
}
|
||||
|
||||
// whatever we did here, we now have all repetitions for this
|
||||
// piggy bank, and we can find transactions that fall within
|
||||
// that repetition (to fix the "saved amount".
|
||||
$reps = $piggy->piggybankrepetitions()->get();
|
||||
|
||||
/** @var \PiggybankRepetition $rep */
|
||||
foreach ($reps as $rep) {
|
||||
$query = \Transaction::where('piggybank_id', $piggy->id)->leftJoin(
|
||||
'transaction_journals', 'transaction_journals.id', '=',
|
||||
'transactions.transaction_journal_id'
|
||||
);
|
||||
if (!is_null($rep->startdate)) {
|
||||
$query->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'));
|
||||
}
|
||||
if (!is_null($rep->targetdate)) {
|
||||
$query->where(
|
||||
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
||||
);
|
||||
}
|
||||
|
||||
// get events for piggy bank, save those as well:
|
||||
$eventSumQuery = $piggy->piggybankevents();
|
||||
if(!is_null($rep->startdate)) {
|
||||
$eventSumQuery->where('date','>=',$rep->startdate->format('Y-m-d'));
|
||||
}
|
||||
if(!is_null($rep->targetdate)) {
|
||||
$eventSumQuery->where('date','<=',$rep->targetdate->format('Y-m-d'));
|
||||
}
|
||||
$eventSum = floatval($eventSumQuery->sum('amount'));
|
||||
|
||||
|
||||
$sum = $query->sum('transactions.amount');
|
||||
$rep->currentamount = floatval($sum) + $eventSum;
|
||||
$rep->save();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
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->piggybank()->associate($piggyBank);
|
||||
$rep->startdate = $startCurrent;
|
||||
$rep->targetdate = $endCurrent;
|
||||
$rep->currentamount = 0;
|
||||
try {
|
||||
$rep->save();
|
||||
} catch (QueryException $e) {
|
||||
}
|
||||
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) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
$reps = $repeated->piggybankrepetitions()->get();
|
||||
/** @var \PiggybankRepetition $rep */
|
||||
foreach ($reps as $rep) {
|
||||
$sum = \Transaction::where('piggybank_id', $repeated->id)->leftJoin(
|
||||
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
||||
)->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'))->where(
|
||||
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
||||
)->sum('transactions.amount');
|
||||
$rep->currentamount = floatval($sum);
|
||||
$rep->save();
|
||||
|
||||
|
||||
}
|
||||
$startTarget = $endCurrent;
|
||||
$rep->save();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen('piggybanks.destroy', 'Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger@destroy');
|
||||
|
||||
$events->listen(
|
||||
'piggybanks.modifyAmountAdd', 'Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger@modifyAmountAdd'
|
||||
);
|
||||
$events->listen(
|
||||
'piggybanks.modifyAmountRemove', 'Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger@modifyAmountRemove'
|
||||
);
|
||||
$events->listen('piggybanks.storePiggy', 'Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger@storePiggy');
|
||||
$events->listen(
|
||||
'piggybanks.storeRepeated', 'Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger@storeRepeated'
|
||||
);
|
||||
$events->listen('piggybanks.update', 'Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger@update');
|
||||
$events->listen(
|
||||
'piggybanks.createRelatedTransfer',
|
||||
'Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger@createRelatedTransfer'
|
||||
);
|
||||
$events->listen(
|
||||
'piggybanks.updateRelatedTransfer',
|
||||
'Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger@updateRelatedTransfer'
|
||||
);
|
||||
}
|
||||
|
||||
public function update(\Piggybank $piggyBank)
|
||||
{
|
||||
// delete all repetitions:
|
||||
foreach ($piggyBank->piggybankrepetitions()->get() as $rep) {
|
||||
$rep->delete();
|
||||
}
|
||||
unset($rep);
|
||||
|
||||
// trigger "new" piggy bank to recreate them.
|
||||
if ($piggyBank->repeats == 1) {
|
||||
\Event::fire('piggybanks.storeRepeated', [$piggyBank]);
|
||||
} else {
|
||||
\Event::fire('piggybanks.storePiggy', [$piggyBank]);
|
||||
}
|
||||
// loop the repetitions and update them according to the events and the transactions:
|
||||
foreach ($piggyBank->piggybankrepetitions()->get() as $rep) {
|
||||
// SUM for transactions
|
||||
$query = \Transaction::where('piggybank_id', $piggyBank->id)->leftJoin(
|
||||
'transaction_journals', 'transaction_journals.id', '=',
|
||||
'transactions.transaction_journal_id'
|
||||
);
|
||||
if (!is_null($rep->startdate)) {
|
||||
$query->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'));
|
||||
}
|
||||
if (!is_null($rep->targetdate)) {
|
||||
$query->where(
|
||||
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
||||
);
|
||||
}
|
||||
$sum = $query->sum('transactions.amount');
|
||||
|
||||
// get events for piggy bank, save those as well:
|
||||
$eventSumQuery = $piggyBank->piggybankevents();
|
||||
if (!is_null($rep->startdate)) {
|
||||
$eventSumQuery->where('date', '>=', $rep->startdate->format('Y-m-d'));
|
||||
}
|
||||
if (!is_null($rep->targetdate)) {
|
||||
$eventSumQuery->where('date', '<=', $rep->targetdate->format('Y-m-d'));
|
||||
}
|
||||
$eventSum = floatval($eventSumQuery->sum('amount'));
|
||||
$rep->currentamount = floatval($sum) + $eventSum;
|
||||
$rep->save();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function updateRelatedTransfer(\Piggybank $piggyBank, \TransactionJournal $journal)
|
||||
{
|
||||
die('no impl updateRelatedTransfer');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
// /**
|
||||
// *
|
||||
// */
|
||||
// public function updatePiggybankRepetitions()
|
||||
// {
|
||||
// // grab all piggy banks.
|
||||
// if (\Auth::check()) {
|
||||
// $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) {
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // whatever we did here, we now have all repetitions for this
|
||||
// // piggy bank, and we can find transactions that fall within
|
||||
// // that repetition (to fix the "saved amount".
|
||||
// $reps = $piggy->piggybankrepetitions()->get();
|
||||
//
|
||||
// /** @var \PiggybankRepetition $rep */
|
||||
// foreach ($reps as $rep) {
|
||||
// $query = \Transaction::where('piggybank_id', $piggy->id)->leftJoin(
|
||||
// 'transaction_journals', 'transaction_journals.id', '=',
|
||||
// 'transactions.transaction_journal_id'
|
||||
// );
|
||||
// if (!is_null($rep->startdate)) {
|
||||
// $query->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'));
|
||||
// }
|
||||
// if (!is_null($rep->targetdate)) {
|
||||
// $query->where(
|
||||
// 'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// // get events for piggy bank, save those as well:
|
||||
// $eventSumQuery = $piggy->piggybankevents();
|
||||
// if(!is_null($rep->startdate)) {
|
||||
// $eventSumQuery->where('date','>=',$rep->startdate->format('Y-m-d'));
|
||||
// }
|
||||
// if(!is_null($rep->targetdate)) {
|
||||
// $eventSumQuery->where('date','<=',$rep->targetdate->format('Y-m-d'));
|
||||
// }
|
||||
// $eventSum = floatval($eventSumQuery->sum('amount'));
|
||||
//
|
||||
//
|
||||
// $sum = $query->sum('transactions.amount');
|
||||
// $rep->currentamount = floatval($sum) + $eventSum;
|
||||
// $rep->save();
|
||||
//
|
||||
//
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// 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 {
|
||||
// $rep->save();
|
||||
// } catch (QueryException $e) {
|
||||
// }
|
||||
// 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) {
|
||||
//
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// $reps = $repeated->piggybankrepetitions()->get();
|
||||
// /** @var \PiggybankRepetition $rep */
|
||||
// foreach ($reps as $rep) {
|
||||
// $sum = \Transaction::where('piggybank_id', $repeated->id)->leftJoin(
|
||||
// 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
||||
// )->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'))->where(
|
||||
// 'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
||||
// )->sum('transactions.amount');
|
||||
// $rep->currentamount = floatval($sum);
|
||||
// $rep->save();
|
||||
//
|
||||
//
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
@ -42,18 +42,18 @@ class Piggybank extends Ardent
|
||||
{
|
||||
public static $rules
|
||||
= [
|
||||
'account_id' => 'required|exists:accounts,id', // link to Account
|
||||
'name' => 'required|between:1,255', // name
|
||||
'targetamount' => 'required|min:0', // amount you want to save
|
||||
'startdate' => 'date', // when you started
|
||||
'targetdate' => 'date', // when its due
|
||||
'repeats' => 'required|between:0,1', // does it repeat?
|
||||
'rep_length' => 'in:day,week,month,year', // how long is the period?
|
||||
'rep_every' => 'required|min:1|max:100', // how often does it repeat? every 3 years.
|
||||
'rep_times' => 'min:1|max:100', // how many times do you want to save this amount? eg. 3 times
|
||||
'reminder' => 'in:day,week,month,year', // want a reminder to put money in this?
|
||||
'account_id' => 'required|exists:accounts,id', // link to Account
|
||||
'name' => 'required|between:1,255', // name
|
||||
'targetamount' => 'required|min:0', // amount you want to save
|
||||
'startdate' => 'date', // when you started
|
||||
'targetdate' => 'date', // when its due
|
||||
'repeats' => 'required|between:0,1', // does it repeat?
|
||||
'rep_length' => 'in:day,week,month,year', // how long is the period?
|
||||
'rep_every' => 'required|min:1|max:100', // how often does it repeat? every 3 years.
|
||||
'rep_times' => 'min:1|max:100', // how many times do you want to save this amount? eg. 3 times
|
||||
'reminder' => 'in:day,week,month,year', // want a reminder to put money in this?
|
||||
'reminder_skip' => 'required|min:0|max:100', // every week? every 2 months?
|
||||
'order' => 'required:min:1', // not yet used.
|
||||
'order' => 'required:min:1', // not yet used.
|
||||
];
|
||||
public $fillable
|
||||
= [
|
||||
@ -82,18 +82,18 @@ class Piggybank extends Ardent
|
||||
$end->endOfMonth();
|
||||
|
||||
return [
|
||||
'account_id' => 'factory|Account',
|
||||
'name' => 'string',
|
||||
'targetamount' => 'integer',
|
||||
'startdate' => $start->format('Y-m-d'),
|
||||
'targetdate' => $end->format('Y-m-d'),
|
||||
'repeats' => 0,
|
||||
'rep_length' => null,
|
||||
'rep_times' => 0,
|
||||
'rep_every' => 0,
|
||||
'reminder' => null,
|
||||
'account_id' => 'factory|Account',
|
||||
'name' => 'string',
|
||||
'targetamount' => 'integer',
|
||||
'startdate' => $start->format('Y-m-d'),
|
||||
'targetdate' => $end->format('Y-m-d'),
|
||||
'repeats' => 0,
|
||||
'rep_length' => null,
|
||||
'rep_times' => 0,
|
||||
'rep_every' => 0,
|
||||
'reminder' => null,
|
||||
'reminder_skip' => 0,
|
||||
'order' => 1,
|
||||
'order' => 1,
|
||||
];
|
||||
}
|
||||
|
||||
@ -185,18 +185,29 @@ class Piggybank extends Ardent
|
||||
$query = $this->piggybankrepetitions()
|
||||
->where(
|
||||
function ($q) {
|
||||
$today = new Carbon;
|
||||
$q->whereNull('startdate');
|
||||
$q->orWhere('startdate', '<=', $today->format('Y-m-d'));
|
||||
|
||||
$q->where(
|
||||
function ($q) {
|
||||
$today = new Carbon;
|
||||
$q->whereNull('startdate');
|
||||
$q->orWhere('startdate', '<=', $today->format('Y-m-d'));
|
||||
}
|
||||
)
|
||||
->where(
|
||||
function ($q) {
|
||||
$today = new Carbon;
|
||||
$q->whereNull('targetdate');
|
||||
$q->orWhere('targetdate', '>=', $today->format('Y-m-d'));
|
||||
}
|
||||
);
|
||||
}
|
||||
)
|
||||
->where(
|
||||
)->orWhere(
|
||||
function ($q) {
|
||||
$today = new Carbon;
|
||||
$q->whereNull('targetdate');
|
||||
$q->orWhere('targetdate', '>=', $today->format('Y-m-d'));
|
||||
$q->where('startdate', '>=', $today->format('Y-m-d'));
|
||||
$q->where('targetdate', '>=', $today->format('Y-m-d'));
|
||||
}
|
||||
);
|
||||
)->orderBy('startdate', 'ASC');
|
||||
$result = $query->first();
|
||||
|
||||
return $result;
|
||||
@ -220,4 +231,36 @@ class Piggybank extends Ardent
|
||||
return $this->hasMany('PiggybankEvent');
|
||||
}
|
||||
|
||||
/**
|
||||
* Same but for specific date.
|
||||
*
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @returns \PiggybankRepetition
|
||||
*/
|
||||
public function repetitionForDate(Carbon $date)
|
||||
{
|
||||
$query = $this->piggybankrepetitions()
|
||||
->where(
|
||||
function ($q) use ($date) {
|
||||
$q->whereNull('startdate');
|
||||
$q->orWhere('startdate', '<=', $date->format('Y-m-d'));
|
||||
}
|
||||
)
|
||||
->where(
|
||||
function ($q) use ($date) {
|
||||
$q->whereNull('targetdate');
|
||||
$q->orWhere('targetdate', '>=', $date->format('Y-m-d'));
|
||||
}
|
||||
);
|
||||
$result = $query->first();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function transactions()
|
||||
{
|
||||
return $this->hasMany('Transaction');
|
||||
}
|
||||
|
||||
}
|
@ -34,6 +34,7 @@ class Transaction extends Ardent
|
||||
public static $rules
|
||||
= [
|
||||
'account_id' => 'numeric|required|exists:accounts,id',
|
||||
'piggybank_id' => 'numeric|exists:piggybanks,id',
|
||||
'transaction_journal_id' => 'numeric|required|exists:transaction_journals,id',
|
||||
'description' => 'between:1,255',
|
||||
'amount' => 'required|between:-65536,65536|not_in:0,0.00',
|
||||
@ -42,9 +43,11 @@ class Transaction extends Ardent
|
||||
public static $factory
|
||||
= [
|
||||
'account_id' => 'factory|Account',
|
||||
'piggybank_id' => null,
|
||||
'transaction_journal_id' => 'factory|TransactionJournal',
|
||||
'description' => 'string',
|
||||
'amount' => 'integer:5'
|
||||
'amount' => 'integer:5',
|
||||
|
||||
];
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user