Skip to content

Firefly III: a personal finances manager

All kinds of code cleanup, mostly to get some mess detection fixed.

James Cole30ac62ffb7cb
diff --git a/app/controllers/BudgetController.php b/app/controllers/BudgetController.php
index a71c53c842..a06c89b76a 100644
--- a/app/controllers/BudgetController.php
+++ b/app/controllers/BudgetController.php
@@ -175,7 +175,7 @@ class BudgetController extends BaseController
Session::flash('successes', $messages['successes']);
Session::flash('errors', $messages['errors']);
if ($messages['errors']->count() > 0) {
- Session::flash('error', 'Could not store budget: ' . $messages['errors']->first());
+ Session::flash('error', 'Could not validate budget: ' . $messages['errors']->first());
}
// return to create screen:
diff --git a/app/controllers/GoogleChartController.php b/app/controllers/GoogleChartController.php
index 638da44256..659628e16d 100644
--- a/app/controllers/GoogleChartController.php
+++ b/app/controllers/GoogleChartController.php
@@ -299,64 +299,6 @@ class GoogleChartController extends BaseController
}
- /**
- * @param $year
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function budgetsReportChart($year)
- {
-
- try {
- $start = new Carbon('01-01-' . $year);
- } catch (Exception $e) {
- App::abort(500);
- }
-
- /** @var \Grumpydictator\Gchart\GChart $chart */
- $chart = App::make('gchart');
-
- /** @var \FireflyIII\Database\Budget\Budget $bdt */
- $bdt = App::make('FireflyIII\Database\Budget\Budget');
- $budgets = $bdt->get();
-
- $chart->addColumn('Month', 'date');
- /** @var \Budget $budget */
- foreach ($budgets as $budget) {
- $chart->addColumn($budget->name, 'number');
- }
- $chart->addColumn('No budget', 'number');
-
- /*
- * Loop budgets this year.
- */
- $end = clone $start;
- $end->endOfYear();
- while ($start <= $end) {
- $row = [clone $start];
-
- foreach ($budgets as $budget) {
- $row[] = $bdt->spentInMonth($budget, $start);
- }
-
- /*
- * Without a budget:
- */
- $endOfMonth = clone $start;
- $endOfMonth->endOfMonth();
- $set = $bdt->transactionsWithoutBudgetInDateRange($start, $endOfMonth);
- $row[] = floatval($set->sum('amount')) * -1;
-
- $chart->addRowArray($row);
- $start->addMonth();
- }
-
-
- $chart->generate();
-
- return Response::json($chart->getData());
- }
-
/**
* @param Category $component
* @param $year
diff --git a/app/controllers/HomeController.php b/app/controllers/HomeController.php
index aaf1a5e224..5e944f0386 100644
--- a/app/controllers/HomeController.php
+++ b/app/controllers/HomeController.php
@@ -80,22 +80,6 @@ class HomeController extends BaseController
return Redirect::back();
}
- public function repair()
- {
- BudgetLimit::get()->each(
- function (BudgetLimit $bl) {
- $component = Component::find($bl->component_id);
- if ($component) {
- $budget = Budget::whereName($component->name)->whereUserId($component->user_id)->first();
- if ($budget) {
- $bl->budget_id = $budget->id;
- $bl->save();
- }
- }
- }
- );
- }
-
/**
* @return \Illuminate\Http\RedirectResponse
*/
diff --git a/app/controllers/RepeatedExpenseController.php b/app/controllers/RepeatedExpenseController.php
index f212571e5a..71f821dfbc 100644
--- a/app/controllers/RepeatedExpenseController.php
+++ b/app/controllers/RepeatedExpenseController.php
@@ -1,21 +1,25 @@
<?php
use Carbon\Carbon;
+use FireflyIII\Database\PiggyBank\RepeatedExpense as Repository;
use FireflyIII\Exception\FireflyException;
-use Illuminate\Support\MessageBag;
/**
* Class RepeatedExpenseController
*/
class RepeatedExpenseController extends BaseController
{
+ /** @var Repository */
+ protected $_repository;
+
/**
- *
+ * @param Repository $repository
*/
- public function __construct()
+ public function __construct(Repository $repository)
{
View::share('title', 'Repeated expenses');
View::share('mainTitleIcon', 'fa-rotate-left');
+ $this->_repository = $repository;
}
/**
@@ -86,50 +90,36 @@ class RepeatedExpenseController extends BaseController
*/
public function store()
{
- $data = Input::all();
+ $data = Input::except('_token');
$data['repeats'] = 1;
- /** @var \FireflyIII\Database\PiggyBank\RepeatedExpense $repository */
- $repository = App::make('FireflyIII\Database\PiggyBank\RepeatedExpense');
- switch ($data['post_submit_action']) {
- default:
- throw new FireflyException('Cannot handle post_submit_action "' . e($data['post_submit_action']) . '"');
- break;
- case 'create_another':
- case 'store':
- $messages = $repository->validate($data);
- /** @var MessageBag $messages ['errors'] */
- if ($messages['errors']->count() > 0) {
- Session::flash('warnings', $messages['warnings']);
- Session::flash('successes', $messages['successes']);
- Session::flash('error', 'Could not save repeated expense: ' . $messages['errors']->first());
-
- return Redirect::route('repeated.create')->withInput()->withErrors($messages['errors']);
- }
- // store!
- $repeated = $repository->store($data);
-
- /*
- * Create the relevant repetition per Event.
- */
- Event::fire('piggybank.store', [$repeated]); // new and used.
-
- Session::flash('success', 'New repeated expense stored!');
-
- if ($data['post_submit_action'] == 'create_another') {
- return Redirect::route('repeated.create')->withInput();
- } else {
- return Redirect::route('repeated.index');
- }
- break;
- case 'validate_only':
- $messageBags = $repository->validate($data);
- Session::flash('warnings', $messageBags['warnings']);
- Session::flash('successes', $messageBags['successes']);
- Session::flash('errors', $messageBags['errors']);
-
- return Redirect::route('repeated.create')->withInput();
- break;
+ // always validate:
+ $messages = $this->_repository->validate($data);
+
+ // flash messages:
+ Session::flash('warnings', $messages['warnings']);
+ Session::flash('successes', $messages['successes']);
+ Session::flash('errors', $messages['errors']);
+ if ($messages['errors']->count() > 0) {
+ Session::flash('error', 'Could not validate repeated expense: ' . $messages['errors']->first());
+ }
+ // return to create screen:
+ if ($data['post_submit_action'] == 'validate_only' || $messages['errors']->count() > 0) {
+ return Redirect::route('repeated.create')->withInput();
}
+
+ // store:
+ $this->_repository->store($data);
+ Session::flash('success', 'Budget "' . e($data['name']) . '" stored.');
+ if ($data['post_submit_action'] == 'store') {
+ return Redirect::route('repeated.index');
+ }
+
+ // create another.
+ if ($data['post_submit_action'] == 'create_another') {
+ return Redirect::route('repeated.create')->withInput();
+ }
+
+ return Redirect::route('repeated.index');
}
}
\ No newline at end of file
diff --git a/app/database/migrations/2014_12_13_190730_changes_for_v321.php b/app/database/migrations/2014_12_13_190730_changes_for_v321.php
index ba9fd71fde..5b7f33e267 100644
--- a/app/database/migrations/2014_12_13_190730_changes_for_v321.php
+++ b/app/database/migrations/2014_12_13_190730_changes_for_v321.php
@@ -55,8 +55,8 @@ class ChangesForV321 extends Migration
$this->moveCategoriesBack(); // 2.
$this->updateComponentInBudgetLimits(); // 3.
$this->dropBudgetIdColumnInBudgetLimits(); // 4.
- $createJournalComponents = new CreateComponentTransactionJournalTable; // 5.
- $createJournalComponents->up();
+ $createComponents = new CreateComponentTransactionJournalTable; // 5.
+ $createComponents->up();
$this->moveBackEntriesForBudgetsInJoinedTable(); // 6.
$this->moveBackEntriesForCategoriesInJoinedTable(); // 7.
$this->dropBudgetJournalTable(); // 8.
diff --git a/app/database/seeds/TestContentSeeder.php b/app/database/seeds/TestContentSeeder.php
index 498e8f55f4..03c8f584a1 100644
--- a/app/database/seeds/TestContentSeeder.php
+++ b/app/database/seeds/TestContentSeeder.php
@@ -49,7 +49,7 @@ class TestContentSeeder extends Seeder
Component::create(['user_id' => $user->id, 'name' => 'Some Component 7', 'class' => 'Category']);
// create some expense accounts.
- $ah = Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Albert Heijn', 'active' => 1]);
+ $albert = Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Albert Heijn', 'active' => 1]);
$plus = Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'PLUS', 'active' => 1]);
$vitens = Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Vitens', 'active' => 1]);
$greenchoice = Account::create(['user_id' => $user->id, 'account_type_id' => $expenseType->id, 'name' => 'Greenchoice', 'active' => 1]);
@@ -59,7 +59,7 @@ class TestContentSeeder extends Seeder
// create three revenue accounts.
$employer = Account::create(['user_id' => $user->id, 'account_type_id' => $revenueType->id, 'name' => 'Employer', 'active' => 1]);
$taxes = Account::create(['user_id' => $user->id, 'account_type_id' => $revenueType->id, 'name' => 'IRS', 'active' => 1]);
- $job = Account::create(['user_id' => $user->id, 'account_type_id' => $revenueType->id, 'name' => 'Job', 'active' => 1]);
+ Account::create(['user_id' => $user->id, 'account_type_id' => $revenueType->id, 'name' => 'Job', 'active' => 1]);
// put money in the two accounts (initial balance)
$ibChecking = Account::create(
@@ -98,7 +98,7 @@ class TestContentSeeder extends Seeder
$groceriesStart->addDay();
if (intval($groceriesStart->format('d')) % 2 == 0) {
$this->createTransaction(
- $checking, $ah, $amt, $withdrawal, 'Groceries', $groceriesStart->format('Y-m-d'), $groceriesBudget, $dailyGroceries
+ $checking, $albert, $amt, $withdrawal, 'Groceries', $groceriesStart->format('Y-m-d'), $groceriesBudget, $dailyGroceries
);
}
$groceriesStart->addDay();
diff --git a/app/lib/FireflyIII/Database/Budget/Budget.php b/app/lib/FireflyIII/Database/Budget/Budget.php
index dce5882442..bb9677462b 100644
--- a/app/lib/FireflyIII/Database/Budget/Budget.php
+++ b/app/lib/FireflyIII/Database/Budget/Budget.php
@@ -235,11 +235,10 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
public function repetitionOnStartingOnDate(\Budget $budget, Carbon $date)
{
return \LimitRepetition::
- leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')->leftJoin(
- 'budgets', 'budget_limits.budget_id', '=', 'budgets.id'
- )->where('limit_repetitions.startdate', $date->format('Y-m-d'))->where(
- 'budgets.id', $budget->id
- )->first(['limit_repetitions.*']);
+ leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
+ ->where('limit_repetitions.startdate', $date->format('Y-m-d'))
+ ->where('budget_limits.budget_id', $budget->id)
+ ->first(['limit_repetitions.*']);
}
/**
@@ -251,15 +250,22 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
public function transactionsWithoutBudgetInDateRange(Carbon $start, Carbon $end)
{
// Add expenses that have no budget:
- return $this->getUser()->transactionjournals()->whereNotIn(
- 'transaction_journals.id', function ($query) use ($start, $end) {
- $query->select('transaction_journals.id')->from('transaction_journals')->leftJoin(
- 'budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id'
- )->leftJoin('budgets', 'budgets.id', '=', 'budget_transaction_journal.budget_id')->where(
- 'transaction_journals.date', '>=', $start->format('Y-m-d')
- )->where('transaction_journals.date', '<=', $end->format('Y-m-d'));
+ return $this->getUser()
+ ->transactionjournals()
+ ->whereNotIn('transaction_journals.id', function ($query) use ($start, $end) {
+ $query
+ ->select('transaction_journals.id')
+ ->from('transaction_journals')
+ ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
+ ->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
+ ->where('transaction_journals.date', '<=', $end->format('Y-m-d'));
}
- )->before($end)->after($start)->lessThan(0)->transactionTypes(['Withdrawal'])->get();
+ )
+ ->before($end)
+ ->after($start)
+ ->lessThan(0)
+ ->transactionTypes(['Withdrawal'])
+ ->get();
}
/**
diff --git a/app/lib/FireflyIII/Database/PiggyBank/PiggyBank.php b/app/lib/FireflyIII/Database/PiggyBank/PiggyBank.php
index 114b7646d3..7d9c2d4464 100644
--- a/app/lib/FireflyIII/Database/PiggyBank/PiggyBank.php
+++ b/app/lib/FireflyIII/Database/PiggyBank/PiggyBank.php
@@ -232,6 +232,7 @@ class PiggyBank implements CUD, CommonDatabaseCalls, PiggyBankInterface
*/
public function findRepetitionByDate(\Piggybank $piggybank, Carbon $date)
{
+ /** @var Collection $reps */
$reps = $piggybank->piggybankrepetitions()->get();
if ($reps->count() == 1) {
return $reps->first();
@@ -239,7 +240,19 @@ class PiggyBank implements CUD, CommonDatabaseCalls, PiggyBankInterface
if ($reps->count() == 0) {
throw new FireflyException('Should always find a piggy bank repetition.');
}
- throw new NotImplementedException;
+ // should filter the one we need:
+ $repetitions = $reps->filter(
+ function (\PiggybankRepetition $rep) use ($date) {
+ if ($date >= $rep->startdate && $date <= $rep->targetdate) {
+ return $rep;
+ }
+ }
+ );
+ if ($repetitions->count() == 0) {
+ return null;
+ }
+
+ return $repetitions->first();
}
/**
diff --git a/app/lib/FireflyIII/Database/PiggyBank/RepeatedExpense.php b/app/lib/FireflyIII/Database/PiggyBank/RepeatedExpense.php
index b6886deed2..9c7f774893 100644
--- a/app/lib/FireflyIII/Database/PiggyBank/RepeatedExpense.php
+++ b/app/lib/FireflyIII/Database/PiggyBank/RepeatedExpense.php
@@ -117,12 +117,10 @@ class RepeatedExpense implements CUD, CommonDatabaseCalls, PiggyBankInterface
public function store(array $data)
{
- $data['rep_every'] = isset($data['rep_every']) ? $data['rep_every'] : 0;
- $data['reminder_skip'] = isset($data['reminder_skip']) ? $data['reminder_skip'] : 0;
- $data['order'] = isset($data['order']) ? $data['order'] : 0;
- $data['remind_me'] = isset($data['remind_me']) ? intval($data['remind_me']) : 0;
- $data['startdate'] = isset($data['startdate']) ? $data['startdate'] : Carbon::now()->format('Y-m-d');
- $data['targetdate'] = isset($data['targetdate']) && $data['targetdate'] != '' ? $data['targetdate'] : null;
+ $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']);
@@ -130,12 +128,7 @@ class RepeatedExpense implements CUD, CommonDatabaseCalls, PiggyBankInterface
$data['reminder'] = null;
}
-
$repeated = new \Piggybank($data);
- if (!$repeated->isValid()) {
- var_dump($repeated->getErrors()->all());
- exit;
- }
$repeated->save();
return $repeated;
diff --git a/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php b/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php
index 36d6c53c1a..1953566272 100644
--- a/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php
+++ b/app/lib/FireflyIII/Database/TransactionJournal/TransactionJournal.php
@@ -92,78 +92,20 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
*/
public function update(\Eloquent $model, array $data)
{
- /** @var \FireflyIII\Database\TransactionType\TransactionType $typeRepository */
- $typeRepository = \App::make('FireflyIII\Database\TransactionType\TransactionType');
-
- /** @var \FireflyIII\Database\Account\Account $accountRepository */
- $accountRepository = \App::make('FireflyIII\Database\Account\Account');
-
- /** @var \FireflyIII\Database\TransactionCurrency\TransactionCurrency $currencyRepository */
- $currencyRepository = \App::make('FireflyIII\Database\TransactionCurrency\TransactionCurrency');
-
- $journalType = $typeRepository->findByWhat($data['what']);
- $currency = $currencyRepository->findByCode($data['currency']);
+ $journalType = $this->getJournalType($data['what']);
+ $currency = $this->getJournalCurrency($data['currency']);
+ $model->description = $data['description'];
+ $model->date = $data['date'];
$model->transactionType()->associate($journalType);
$model->transactionCurrency()->associate($currency);
$model->user()->associate($this->getUser());
- $model->description = $data['description'];
- $model->date = $data['date'];
-
- /*
- * This must be enough to store the journal:
- */
- if (!$model->isValid()) {
- \Log::error($model->getErrors()->all());
- throw new FireflyException('store() transaction journal failed, but it should not!');
- }
$model->save();
- /*
- * Still need to find the accounts related to the transactions.
- * This depends on the type of transaction.
- */
- switch ($data['what']) {
- case 'withdrawal':
- $data['from'] = $accountRepository->find($data['account_id']);
- $data['to'] = $accountRepository->firstExpenseAccountOrCreate($data['expense_account']);
- break;
- case 'opening':
- break;
- case 'deposit':
- $data['to'] = $accountRepository->find($data['account_id']);
- $data['from'] = $accountRepository->firstRevenueAccountOrCreate($data['revenue_account']);
- break;
- case 'transfer':
- $data['from'] = $accountRepository->find($data['account_from_id']);
- $data['to'] = $accountRepository->find($data['account_to_id']);
- break;
-
- default:
- throw new FireflyException('Cannot save transaction journal with accounts based on $what "' . $data['what'] . '".');
- break;
- }
-
- /*
- * Update the budget and the category.
- */
+ list($fromAccount, $toAccount) = $this->storeAccounts($data);
- if (isset($data['budget_id']) && intval($data['budget_id']) > 0) {
- /** @var \FireflyIII\Database\Budget\Budget $budgetRepository */
- $budgetRepository = \App::make('FireflyIII\Database\Budget\Budget');
- $budget = $budgetRepository->find(intval($data['budget_id']));
- if ($budget) {
- $model->budgets()->sync([$budget->id]);
- }
- }
- if (strlen($data['category']) > 0) {
- /** @var \FireflyIII\Database\Category\Category $categoryRepository */
- $categoryRepository = \App::make('FireflyIII\Database\Category\Category');
- $category = $categoryRepository->firstOrCreate($data['category']);
- if ($category) {
- $model->categories()->sync([$category->id]);
- }
- }
+ $this->storeBudget($data, $model);
+ $this->storeCategory($data, $model);
/*
* TODO move to transaction thing.
@@ -174,10 +116,10 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
foreach ($model->transactions()->get() as $transaction) {
if (floatval($transaction->amount) > 0) {
// the TO transaction.
- $transaction->account()->associate($data['to']);
+ $transaction->account()->associate($toAccount);
$transaction->amount = $amount;
} else {
- $transaction->account()->associate($data['from']);
+ $transaction->account()->associate($fromAccount);
$transaction->amount = $amount * -1;
}
if (!$transaction->isValid()) {
@@ -443,8 +385,8 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
}
/**
- * @param array $data
- * @param \TransactionJournal $journal
+ * @param array $data
+ * @param \TransactionJournal|\Eloquent $journal
*/
public function storeBudget($data, \TransactionJournal $journal)
{
@@ -453,14 +395,14 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
$budgetRepository = \App::make('FireflyIII\Database\Budget\Budget');
$budget = $budgetRepository->find(intval($data['budget_id']));
if ($budget) {
- $journal->budgets()->save($budget);
+ $journal->budgets()->sync([$budget->id]);
}
}
}
/**
- * @param array $data
- * @param \TransactionJournal $journal
+ * @param array $data
+ * @param \TransactionJournal|\Eloquent $journal
*/
public function storeCategory(array $data, \TransactionJournal $journal)
{
diff --git a/app/lib/FireflyIII/Event/Piggybank.php b/app/lib/FireflyIII/Event/Piggybank.php
index bdc067259f..0190368234 100644
--- a/app/lib/FireflyIII/Event/Piggybank.php
+++ b/app/lib/FireflyIII/Event/Piggybank.php
@@ -87,10 +87,6 @@ class Piggybank
$event->piggybank()->associate($piggybank);
$event->amount = floatval($amount);
$event->date = new Carbon;
- if (!$event->isValid()) {
- var_dump($event->getErrors());
- exit();
- }
$event->save();
}
}
@@ -120,7 +116,7 @@ class Piggybank
*/
public function storeTransfer(\TransactionJournal $journal, $piggybankId = 0)
{
- if ($piggybankId == 0 || is_null($piggybankId)) {
+ if (intval($piggybankId) == 0) {
return;
}
/** @var \FireflyIII\Database\PiggyBank\PiggyBank $repository */
@@ -129,58 +125,32 @@ class Piggybank
/** @var \Piggybank $piggyBank */
$piggyBank = $repository->find($piggybankId);
- /** @var \PiggybankRepetition $repetition */
- $repetition = $repository->findRepetitionByDate($piggyBank, $journal->date);
-
- \Log::debug(
- 'Connecting transfer "' . $journal->description . '" (#' . $journal->id . ') to piggy bank "' . $piggyBank->name . '" (#' . $piggyBank->id . ').'
- );
-
- // some variables to double check the connection.
- $start = $piggyBank->startdate;
- $end = $piggyBank->targetdate;
- $amount = floatval($piggyBank->targetamount);
- $leftToSave = $amount - floatval($repetition->currentamount);
- $relevantTransaction = null;
- /** @var \Transaction $transaction */
- foreach ($journal->transactions as $transaction) {
- if ($transaction->account_id == $piggyBank->account_id) {
- $relevantTransaction = $transaction;
- }
- }
- if (is_null($relevantTransaction)) {
+ if ($journal->transactions()->where('account_id', $piggyBank->account_id)->count() == 0) {
return;
}
- \Log::debug('Relevant transaction is #' . $relevantTransaction->id . ' with amount ' . $relevantTransaction->amount);
-
- // if FF3 should save this connection depends on some variables:
- if ($start && $end && $journal->date >= $start && $journal->date <= $end) {
- if ($relevantTransaction->amount < 0) { // amount removed from account, so removed from piggy bank.
- \Log::debug('Remove from piggy bank.');
- $continue = ($relevantTransaction->amount * -1 <= floatval($repetition->currentamount));
- \Log::debug(
- 'relevantTransaction.amount *-1 = ' . ($relevantTransaction->amount * -1) . ' >= current = ' . floatval($repetition->currentamount)
- );
- } else { // amount added
- \Log::debug('Add from piggy bank.');
- $continue = $relevantTransaction->amount <= $leftToSave;
- }
- if ($continue) {
- \Log::debug('Update repetition.');
- $repetition->currentamount += floatval($relevantTransaction->amount);
- $repetition->save();
-
- $event = new \PiggyBankEvent;
- $event->piggybank()->associate($piggyBank);
- $event->transactionjournal()->associate($journal);
- $event->amount = floatval($relevantTransaction->amount);
- $event->date = new Carbon;
- if (!$event->isValid()) {
- var_dump($event->getErrors());
- exit();
- }
- $event->save();
- }
+ /** @var \PiggybankRepetition $repetition */
+ $repetition = $repository->findRepetitionByDate($piggyBank, $journal->date);
+ $amount = floatval($piggyBank->targetamount);
+ $leftToSave = $amount - floatval($repetition->currentamount);
+ $transaction = $journal->transactions()->where('account_id', $piggyBank->account_id)->first();
+ // must be in range of journal. Continue determines if we can move it.
+ if (floatval($transaction->amount < 0)) {
+ // amount removed from account, so removed from piggy bank.
+ $continue = ($transaction->amount * -1 <= floatval($repetition->currentamount));
+ } else {
+ // amount added
+ $continue = $transaction->amount <= $leftToSave;
+ }
+ if ($continue) {
+ \Log::debug('Update repetition.');
+ $repetition->currentamount += floatval($transaction->amount);
+ $repetition->save();
+ $event = new \PiggyBankEvent;
+ $event->piggybank()->associate($piggyBank);
+ $event->transactionjournal()->associate($journal);
+ $event->amount = floatval($transaction->amount);
+ $event->date = new Carbon;
+ $event->save();
}
}
diff --git a/app/lib/FireflyIII/Shared/Toolkit/Reminders.php b/app/lib/FireflyIII/Shared/Toolkit/Reminders.php
index 35b4f42504..c70ea542e6 100644
--- a/app/lib/FireflyIII/Shared/Toolkit/Reminders.php
+++ b/app/lib/FireflyIII/Shared/Toolkit/Reminders.php
@@ -71,15 +71,9 @@ class Reminders
$today = Carbon::now();
- //$today = new Carbon('14-12-2014');
/** @var \Piggybank $piggybank */
foreach ($set as $piggyBank) {
- /*
- * Try to find a reminder that is valid in the current [period]
- * aka between [start of period] and [end of period] as denoted
- * by the piggy's repeat_freq.
- */
/** @var \PiggybankRepetition $repetition */
$repetition = $piggyBank->currentRelevantRep();
$start = \DateKit::startOfPeriod($today, $piggyBank->reminder);
diff --git a/app/models/LimitRepetition.php b/app/models/LimitRepetition.php
index 17754d4e34..eb8091be1b 100644
--- a/app/models/LimitRepetition.php
+++ b/app/models/LimitRepetition.php
@@ -51,15 +51,16 @@ class LimitRepetition extends Eloquent
*/
public function spentInRepetition()
{
- $sum = \DB::table('transactions')->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->leftJoin(
- 'component_transaction_journal', 'component_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id'
- )->leftJoin('components', 'components.id', '=', 'component_transaction_journal.component_id')->leftJoin(
- 'budget_limits', 'budget_limits.component_id', '=', 'components.id'
- )->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')->where(
- 'transaction_journals.date', '>=', $this->startdate->format('Y-m-d')
- )->where('transaction_journals.date', '<=', $this->enddate->format('Y-m-d'))->where('transactions.amount', '>', 0)->where(
- 'limit_repetitions.id', '=', $this->id
- )->sum('transactions.amount');
+ $sum = \DB::table('transactions')
+ ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
+ ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
+ ->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budget_transaction_journal.budget_id')
+ ->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
+ ->where('transaction_journals.date', '>=', $this->startdate->format('Y-m-d'))
+ ->where('transaction_journals.date', '<=', $this->enddate->format('Y-m-d'))
+ ->where('transactions.amount', '>', 0)
+ ->where('limit_repetitions.id', '=', $this->id)
+ ->sum('transactions.amount');
return floatval($sum);
}
diff --git a/app/routes.php b/app/routes.php
index 117d272f52..4f16877a22 100644
--- a/app/routes.php
+++ b/app/routes.php
@@ -98,10 +98,11 @@ Route::bind(
Route::bind(
'limitrepetition', function ($value, $route) {
if (Auth::check()) {
- return LimitRepetition::
- where('limit_repetitions.id', $value)->leftjoin('budget_limits', 'budget_limits.id', '=', 'limit_repetitions.budget_limit_id')->leftJoin(
- 'budgets', 'budgets.id', '=', 'budget_limits.budget_id'
- )->where('budgets.user_id', Auth::user()->id)->first(['limit_repetitions.*']);
+ return LimitRepetition::where('limit_repetitions.id', $value)
+ ->leftjoin('budget_limits', 'budget_limits.id', '=', 'limit_repetitions.budget_limit_id')
+ ->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
+ ->where('budgets.user_id', Auth::user()->id)
+ ->first(['limit_repetitions.*']);
}
return null;
@@ -180,7 +181,6 @@ Route::group(
Route::get('/chart/reports/income-expenses/{year}', ['uses' => 'GoogleChartController@yearInExp']);
Route::get('/chart/reports/income-expenses-sum/{year}', ['uses' => 'GoogleChartController@yearInExpSum']);
Route::get('/chart/recurring/{recurring}', ['uses' => 'GoogleChartController@recurringOverview']);
- Route::get('/chart/reports/budgets/{year}', ['uses' => 'GoogleChartController@budgetsReportChart']);
Route::get('/chart/budget/{budget}/{limitrepetition}', ['uses' => 'GoogleChartController@budgetLimitSpending']);
Route::get('/chart/piggyhistory/{piggybank}', ['uses' => 'GoogleChartController@piggyBankHistory']);
diff --git a/public/assets/javascript/firefly/reports.js b/public/assets/javascript/firefly/reports.js
index 6f64d40b14..3ef9f9326d 100644
--- a/public/assets/javascript/firefly/reports.js
+++ b/public/assets/javascript/firefly/reports.js
@@ -3,6 +3,5 @@ if (typeof(google) != 'undefined') {
function drawChart() {
googleColumnChart('chart/reports/income-expenses/' + year, 'income-expenses-chart');
googleColumnChart('chart/reports/income-expenses-sum/' + year, 'income-expenses-sum-chart')
- googleStackedColumnChart('chart/reports/budgets/' + year, 'budgets');
}
}
\ No newline at end of file