Basic testing, not full coverage.

This commit is contained in:
James Cole
2014-08-19 13:24:11 +02:00
parent f5330728d4
commit f00dfd2859
17 changed files with 325 additions and 162 deletions

View File

@@ -121,6 +121,7 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
$limit->repeat_freq = $data['repeat_freq'];
if ($limit->validate()) {
$limit->save();
\Event::fire('limits.store',[$limit]);
}
}
if ($budget->validate()) {

View File

@@ -26,6 +26,22 @@ class EloquentLimitRepository implements LimitRepositoryInterface
return true;
}
/**
* @param \Limit $limit
* @param $data
* @return mixed|void
*/
public function update(\Limit $limit, $data)
{
$limit->startdate = new Carbon($data['startdate']);
$limit->repeat_freq = $data['period'];
$limit->repeats = isset($data['repeats']) && $data['repeats'] == '1' ? 1 : 0;
$limit->amount = floatval($data['amount']);
$limit->save();
return $limit;
}
/**
* @param $limitId
*
@@ -41,8 +57,8 @@ class EloquentLimitRepository implements LimitRepositoryInterface
/**
* @param \Budget $budget
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return mixed
*/
@@ -97,11 +113,11 @@ class EloquentLimitRepository implements LimitRepositoryInterface
}
// find existing:
$count = \Limit::
leftJoin('components', 'components.id', '=', 'limits.component_id')->where(
'components.user_id', \Auth::user()->id
)->where('startdate', $date->format('Y-m-d'))->where('component_id', $data['budget_id'])->where(
'repeat_freq', $data['period']
)->count();
leftJoin('components', 'components.id', '=', 'limits.component_id')->where(
'components.user_id', \Auth::user()->id
)->where('startdate', $date->format('Y-m-d'))->where('component_id', $data['budget_id'])->where(
'repeat_freq', $data['period']
)->count();
if ($count > 0) {
\Session::flash('error', 'There already is an entry for these parameters.');

View File

@@ -19,6 +19,13 @@ interface LimitRepositoryInterface
*/
public function store($data);
/**
* @param \Limit $limit
* @param $data
* @return mixed
*/
public function update(\Limit $limit, $data);
/**
* @param \Budget $budget
* @param Carbon $start

View File

@@ -121,8 +121,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$fromTransaction->description = null;
$fromTransaction->amount = $amountFrom;
if (!$fromTransaction->validate()) {
throw new FireflyException('Cannot create valid transaction (from): ' . $fromTransaction->errors()->first(
));
throw new FireflyException('Cannot create valid transaction (from): ' . $fromTransaction->errors()->first());
}
$fromTransaction->save();
@@ -151,9 +150,9 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
{
return \Auth::user()->transactionjournals()->with(
['transactions' => function ($q) {
return $q->orderBy('amount', 'ASC');
}, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account',
'transactions.account.accounttype']
return $q->orderBy('amount', 'ASC');
}, 'transactioncurrency', 'transactiontype', 'components', 'transactions.account',
'transactions.account.accounttype']
)
->where('id', $journalId)->first();
}
@@ -168,7 +167,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
/**
* @param \Account $account
* @param Carbon $date
* @param Carbon $date
*
* @return mixed
*/
@@ -197,9 +196,9 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
/**
* @param \Account $account
* @param int $count
* @param Carbon $start
* @param Carbon $end
* @param int $count
* @param Carbon $start
* @param Carbon $end
*
* @return mixed
*/
@@ -236,8 +235,8 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$query = \Auth::user()->transactionjournals()->with(
[
'transactions' => function ($q) {
return $q->orderBy('amount', 'ASC');
},
return $q->orderBy('amount', 'ASC');
},
'transactions.account',
'transactions.account.accounttype',
'transactioncurrency',
@@ -300,8 +299,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
// find budget:
$budget = isset($data['budget_id']) ? $budRepository->find(intval($data['budget_id'])) : null;
//
// // find amount & description:
// find amount & description:
$description = trim($data['description']);
$amount = floatval($data['amount']);
$date = new Carbon($data['date']);
@@ -323,25 +321,19 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$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
= $transactionJournal->transactions()->where('account_id', $toAccount->id)->first();
// connect the piggy to it:
$transaction->piggybank()->associate($piggyBank);
$transaction->save();
\Event::fire(
'piggybanks.createRelatedTransfer', [$piggyBank, $transactionJournal, $transaction]
);
} 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.'
);
// one of the two transactions may be connected to this piggy bank.
$connected = false;
foreach ($transactionJournal->transactions()->get() as $transaction) {
if ($transaction->account_id == $piggyBank->account_id) {
$connected = true;
$transaction->piggybank()->associate($piggyBank);
$transaction->save();
\Event::fire('piggybanks.createRelatedTransfer', [$piggyBank, $transactionJournal, $transaction]);
break;
}
}
if ($connected === false) {
\Session::flash('warning', 'Piggy bank "' . e($piggyBank->name) . '" is not set to draw money from any of the accounts in this transfer');
}
}
}
@@ -405,7 +397,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
/** @var \Transaction $transaction */
foreach ($transactions as $transaction) {
if (!is_null($transaction->piggybank()->first())) {
$transaction->piggybank()->detach($transaction->piggybank()->first()->first());
$transaction->piggybank_id = null;
$transaction->save();
}
}
@@ -415,6 +407,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$transactions[1]->amount = $amount;
// switch on type to properly change things:
$fireEvent = false;
switch ($journal->transactiontype->type) {
case 'Withdrawal':
// means transaction[0] is the users account.
@@ -454,24 +447,22 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
/** @var \Piggybank $piggyBank */
$piggyBank = $piggyRepository->find(intval($data['piggybank_id']));
if ($piggyBank) {
if ($toAccount->id == $piggyBank->account_id) {
// loop transactions and re-attach the piggy bank:
// 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.'
);
if ($piggyBank) {
$connected = false;
foreach ($journal->transactions()->get() as $transaction) {
if ($transaction->account_id == $piggyBank->account_id) {
$connected = true;
$transaction->piggybank()->associate($piggyBank);
$transaction->save();
$fireEvent = true;
break;
}
}
if ($connected === false) {
\Session::flash('warning', 'Piggy bank "' . e($piggyBank->name) . '" is not set to draw money from any of the accounts in this transfer');
}
}
}
@@ -488,6 +479,9 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
if ($journal->validate()) {
$journal->save();
}
if($fireEvent) {
\Event::fire('piggybanks.updateRelatedTransfer', [$piggyBank]);
}
return $journal;

View File

@@ -0,0 +1,62 @@
<?php
namespace Firefly\Trigger\Budgets;
use Illuminate\Events\Dispatcher;
/**
* Class EloquentLimitTrigger
*
* @package Firefly\Trigger\Budgets
*/
class EloquentBudgetTrigger
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
//$events->listen('budgets.change', 'Firefly\Trigger\Limits\EloquentLimitTrigger@updateLimitRepetitions');
$events->listen('budgets.destroy', 'Firefly\Trigger\Budgets\EloquentBudgetTrigger@destroy');
$events->listen('budgets.store', 'Firefly\Trigger\Budgets\EloquentBudgetTrigger@store');
$events->listen('budgets.update', 'Firefly\Trigger\Budgets\EloquentBudgetTrigger@update');
}
/**
* Destroying a budget doesn't do much either.
*
* @param \Budget $budget
* @return bool
*/
public function destroy(\Budget $budget)
{
return true;
}
/**
* A new budget is just there, there is nothing to trigger.
*
* @param \Budget $budget
* @return bool
*/
public function store(\Budget $budget)
{
return true;
}
/**
* Same. Doesn't do much.
*
* @param \Budget $budget
* @return bool
*/
public function update(\Budget $budget)
{
return true;
}
}

View File

@@ -2,7 +2,6 @@
namespace Firefly\Trigger\Limits;
use Carbon\Carbon;
use Illuminate\Events\Dispatcher;
/**
@@ -18,71 +17,102 @@ class EloquentLimitTrigger
*/
public function subscribe(Dispatcher $events)
{
$events->listen('budgets.change', 'Firefly\Trigger\Limits\EloquentLimitTrigger@updateLimitRepetitions');
//$events->listen('budgets.change', 'Firefly\Trigger\Limits\EloquentLimitTrigger@updateLimitRepetitions');
$events->listen('limits.destroy', 'Firefly\Trigger\Limits\EloquentLimitTrigger@destroy');
$events->listen('limits.store', 'Firefly\Trigger\Limits\EloquentLimitTrigger@store');
$events->listen('limits.update', 'Firefly\Trigger\Limits\EloquentLimitTrigger@update');
}
/**
*
*/
public function updateLimitRepetitions()
public function destroy(\Limit $limit)
{
if (!\Auth::check() || is_null(\Auth::user())) {
\Log::debug('No user for updateLimitRepetitions.');
return;
}
return true;
// get budgets with limits:
$budgets = \Auth::user()->budgets()
->with(
['limits', 'limits.limitrepetitions']
)
->where('components.class', 'Budget')
->get(['components.*']);
// double check the non-repeating budgetlimits first.
foreach ($budgets as $budget) {
\Log::debug('Budgetstart: ' . $budget->name);
foreach ($budget->limits as $limit) {
if ($limit->repeats == 0) {
$limit->createRepetition($limit->startdate);
}
if ($limit->repeats == 1) {
$start = $limit->startdate;
$end = new Carbon;
// repeat for period:
$current = clone $start;
\Log::debug('Create repeating limit for #' . $limit->id . ' starting on ' . $current);
while ($current <= $end) {
\Log::debug('Current is now: ' . $current);
$limit->createRepetition(clone $current);
// switch period, add time:
switch ($limit->repeat_freq) {
case 'daily':
$current->addDay();
break;
case 'weekly':
$current->addWeek();
break;
case 'monthly':
$current->addMonth();
break;
case 'quarterly':
$current->addMonths(3);
break;
case 'half-year':
$current->addMonths(6);
break;
case 'yearly':
$current->addYear();
break;
}
}
}
}
}
}
public function store(\Limit $limit)
{
// create a repetition (repetitions) for this limit (we ignore "repeats"):
$limit->createRepetition($limit->startdate);
// we may want to build a routine that does this for repeating limits.
// TODO.
return true;
}
public function update(\Limit $limit)
{
// remove and recreate limit repetitions.
// if limit is not repeating, simply update the repetition to match the limit,
// even though deleting everything is easier.
foreach($limit->limitrepetitions()->get() as $l) {
$l->delete();
}
$limit->createRepetition($limit->startdate);
return true;
}
// /**
// *
// */
// public function updateLimitRepetitions()
// {
// if (!\Auth::check() || is_null(\Auth::user())) {
// \Log::debug('No user for updateLimitRepetitions.');
// return;
// }
//
// // get budgets with limits:
// $budgets = \Auth::user()->budgets()
// ->with(
// ['limits', 'limits.limitrepetitions']
// )
// ->where('components.class', 'Budget')
// ->get(['components.*']);
//
// // double check the non-repeating budgetlimits first.
// foreach ($budgets as $budget) {
// \Log::debug('Budgetstart: ' . $budget->name);
// foreach ($budget->limits as $limit) {
// if ($limit->repeats == 0) {
// $limit->createRepetition($limit->startdate);
// }
// if ($limit->repeats == 1) {
// $start = $limit->startdate;
// $end = new Carbon;
//
// // repeat for period:
// $current = clone $start;
// \Log::debug('Create repeating limit for #' . $limit->id . ' starting on ' . $current);
// while ($current <= $end) {
// \Log::debug('Current is now: ' . $current);
// $limit->createRepetition(clone $current);
// // switch period, add time:
// switch ($limit->repeat_freq) {
// case 'daily':
// $current->addDay();
// break;
// case 'weekly':
// $current->addWeek();
// break;
// case 'monthly':
// $current->addMonth();
// break;
// case 'quarterly':
// $current->addMonths(3);
// break;
// case 'half-year':
// $current->addMonths(6);
// break;
// case 'yearly':
// $current->addYear();
// break;
// }
//
// }
// }
// }
//
// }
// }
}

View File

@@ -13,12 +13,13 @@ use Illuminate\Events\Dispatcher;
class EloquentPiggybankTrigger
{
/**
* @param \Piggybank $piggyBank
* @param \Piggybank $piggyBank
* @param \TransactionJournal $journal
*/
public function createRelatedTransfer(
\Piggybank $piggyBank, \TransactionJournal $journal, \Transaction $transaction
) {
)
{
$repetition = $piggyBank->repetitionForDate($journal->date);
if (!is_null($repetition)) {
// get the amount transferred TO this
@@ -226,15 +227,17 @@ class EloquentPiggybankTrigger
}
}
public function updateRelatedTransfer(\Piggybank $piggyBank, \TransactionJournal $journal)
public function updateRelatedTransfer(\Piggybank $piggyBank)
{
die('no impl updateRelatedTransfer');
// fire the "update" trigger which should handle things just fine:
\Event::fire('piggybanks.update', [$piggyBank]);
}
//
// /**
// *