diff --git a/app/Events/JournalCreated.php b/app/Events/JournalCreated.php new file mode 100644 index 0000000000..d7af994329 --- /dev/null +++ b/app/Events/JournalCreated.php @@ -0,0 +1,31 @@ +journal = $journal; + $this->piggyBankId = $piggyBankId; + + + + + } + +} diff --git a/app/Handlers/Events/ConnectJournalToPiggyBank.php b/app/Handlers/Events/ConnectJournalToPiggyBank.php new file mode 100644 index 0000000000..5747aeba49 --- /dev/null +++ b/app/Handlers/Events/ConnectJournalToPiggyBank.php @@ -0,0 +1,85 @@ +journal; + $piggyBankId = $event->piggyBankId; + + Log::debug('JournalCreated event: ' . $journal->id . ', ' . $piggyBankId); + + /** @var PiggyBank $piggyBank */ + $piggyBank = Auth::user()->piggybanks()->where('piggy_banks.id', $piggyBankId)->first(['piggy_banks.*']); + + if (is_null($piggyBank) || $journal->transactionType->type != 'Transfer') { + return; + } + Log::debug('Found a piggy bank'); + $amount = 0; + /** @var Transaction $transaction */ + foreach ($journal->transactions()->get() as $transaction) { + if ($transaction->account_id === $piggyBank->account_id) { + // this transaction is the relevant one. + $amount = floatval($transaction->amount); + } + } + Log::debug('Amount: ' . $amount); + if ($amount == 0) { + return; + } + // update piggy bank rep for date of transaction journal. + $repetition = $piggyBank->piggyBankRepetitions()->relevantOnDate($journal->date)->first(); + if (is_null($repetition)) { + return; + } + + Log::debug('Found rep! ' . $repetition->id); + $repetition->currentamount += $amount; + $repetition->save(); + + PiggyBankEvent::create( + [ + 'piggy_bank_id' => $piggyBank->id, + 'transaction_journal_id' => $journal->id, + 'date' => $journal->date, + 'amount' => $amount + ] + ); + + } + + +} diff --git a/app/Handlers/Events/UpdateJournalConnection.php b/app/Handlers/Events/UpdateJournalConnection.php new file mode 100644 index 0000000000..beeb58e42f --- /dev/null +++ b/app/Handlers/Events/UpdateJournalConnection.php @@ -0,0 +1,64 @@ +journal; + + // get the event connected to this journal: + /** @var PiggyBankEvent $event */ + $event = PiggyBankEvent::where('transaction_journal_id', $journal->id)->first(); + $piggyBank = $event->piggyBank()->first(); + $repetition = $piggyBank->piggyBankRepetitions()->relevantOnDate($journal->date)->first(); + + if (is_null($repetition)) { + return; + } + $amount = 0; + /** @var Transaction $transaction */ + foreach ($journal->transactions()->get() as $transaction) { + if ($transaction->account_id === $piggyBank->account_id) { + // this transaction is the relevant one. + $amount = floatval($transaction->amount); + } + } + + // update current repetition: + $diff = $amount - $event->amount; + + $repetition->currentamount += $diff; + if($repetition->currentamount < 0) { + $repetition->currentamount = 0; + } + $repetition->save(); + + + $event->amount = $amount; + $event->save(); + } + +} diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 212b62f36a..0e05b3c65f 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -1,6 +1,5 @@ function ($query) { $query->where('name', 'accountRole'); }] - )->accountTypeIn($types)->take($size)->offset($offset)->orderBy('accounts.name','ASC')->get(['accounts.*']); + )->accountTypeIn($types)->take($size)->offset($offset)->orderBy('accounts.name', 'ASC')->get(['accounts.*']); $total = Auth::user()->accounts()->accountTypeIn($types)->count(); // last activity: @@ -148,7 +147,7 @@ class AccountController extends Controller ); $accounts = new LengthAwarePaginator($set, $total, $size, $page); - $accounts->setPath(route('accounts.index',$what)); + $accounts->setPath(route('accounts.index', $what)); return view('accounts.index', compact('what', 'subTitleIcon', 'subTitle', 'accounts')); @@ -156,22 +155,19 @@ class AccountController extends Controller /** * @param Account $account - * @param string $range * @param AccountRepositoryInterface $repository * - * @return \Illuminate\View\View + * @return View */ - public function show(Account $account, $range = 'session') + public function show(Account $account, AccountRepositoryInterface $repository) { - /** @var \FireflyIII\Repositories\Account\AccountRepositoryInterface $repository */ - $repository = App::make('FireflyIII\Repositories\Account\AccountRepositoryInterface'); $page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page')); $subTitleIcon = Config::get('firefly.subTitlesByIdentifier.' . $account->accountType->type); $what = Config::get('firefly.shortNamesByFullName.' . $account->accountType->type); - $journals = $repository->getJournals($account, $page, $range); + $journals = $repository->getJournals($account, $page); $subTitle = 'Details for ' . strtolower(e($account->accountType->type)) . ' "' . e($account->name) . '"'; - return view('accounts.show', compact('account', 'what', 'range', 'subTitleIcon', 'journals', 'subTitle')); + return view('accounts.show', compact('account', 'what', 'subTitleIcon', 'journals', 'subTitle')); } /** @@ -197,6 +193,10 @@ class AccountController extends Controller Session::flash('success', 'New account "' . $account->name . '" stored!'); + if (intval(Input::get('create_another')) === 1) { + return Redirect::route('accounts.create', $request->input('what')); + } + return Redirect::route('accounts.index', $request->input('what')); } @@ -225,6 +225,10 @@ class AccountController extends Controller Session::flash('success', 'Account "' . $account->name . '" updated.'); + if (intval(Input::get('return_to_edit')) === 1) { + return Redirect::route('accounts.edit', $account->id); + } + return Redirect::route('accounts.index', $what); } diff --git a/app/Http/Controllers/CurrencyController.php b/app/Http/Controllers/CurrencyController.php index 318e2736d3..87baafd9d2 100644 --- a/app/Http/Controllers/CurrencyController.php +++ b/app/Http/Controllers/CurrencyController.php @@ -9,6 +9,8 @@ use Redirect; use Session; use View; use Cache; +use Input; + /** * Class CurrencyController @@ -145,6 +147,10 @@ class CurrencyController extends Controller Session::flash('success', 'Currency "' . $currency->name . '" created'); + if (intval(Input::get('create_another')) === 1) { + return Redirect::route('currency.create'); + } + return Redirect::route('currency.index'); @@ -163,7 +169,12 @@ class CurrencyController extends Controller $currency->name = $request->get('name'); $currency->save(); - Session::flash('success', 'Currency "' . e($currency->namename) . '" updated.'); + Session::flash('success', 'Currency "' . e($currency->name) . '" updated.'); + + + if (intval(Input::get('return_to_edit')) === 1) { + return Redirect::route('currency.edit', $currency->id); + } return Redirect::route('currency.index'); diff --git a/app/Http/Controllers/GoogleChartController.php b/app/Http/Controllers/GoogleChartController.php index 82297f3b4d..e4ef277a58 100644 --- a/app/Http/Controllers/GoogleChartController.php +++ b/app/Http/Controllers/GoogleChartController.php @@ -42,27 +42,14 @@ class GoogleChartController extends Controller * * @return \Illuminate\Http\JsonResponse */ - public function accountBalanceChart(Account $account, $view = 'session', GChart $chart) + public function accountBalanceChart(Account $account, GChart $chart) { $chart->addColumn('Day of month', 'date'); $chart->addColumn('Balance for ' . $account->name, 'number'); $chart->addCertainty(1); - $start = Session::get('start', Carbon::now()->startOfMonth()); - $end = Session::get('end', Carbon::now()->endOfMonth()); - $count = $account->transactions()->count(); - - if ($view == 'all' && $count > 0) { - $first = $account->transactions()->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->orderBy( - 'date', 'ASC' - )->first(['transaction_journals.date']); - $last = $account->transactions()->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->orderBy( - 'date', 'DESC' - )->first(['transaction_journals.date']); - $start = new Carbon($first->date); - $end = new Carbon($last->date); - } - + $start = Session::get('start', Carbon::now()->startOfMonth()); + $end = Session::get('end', Carbon::now()->endOfMonth()); $current = clone $start; while ($end >= $current) { diff --git a/app/Http/Controllers/PiggyBankController.php b/app/Http/Controllers/PiggyBankController.php index 651a55be89..3042134a18 100644 --- a/app/Http/Controllers/PiggyBankController.php +++ b/app/Http/Controllers/PiggyBankController.php @@ -273,7 +273,7 @@ class PiggyBankController extends Controller $piggyBankData = [ 'repeats' => false, 'name' => $request->get('name'), - 'startdate' => new Carbon, + 'startdate' => null, 'account_id' => intval($request->get('account_id')), 'targetamount' => floatval($request->get('targetamount')), 'targetdate' => strlen($request->get('targetdate')) > 0 ? new Carbon($request->get('targetdate')) : null, diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index 11ccb60bfd..1954c77230 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -3,6 +3,7 @@ use Auth; use Carbon\Carbon; use ExpandedForm; +use FireflyIII\Events\JournalCreated; use FireflyIII\Events\JournalSaved; use FireflyIII\Http\Requests; use FireflyIII\Http\Requests\JournalFormRequest; @@ -144,7 +145,7 @@ class TransactionController extends Controller } if ($journal->piggyBankEvents()->count() > 0) { - $preFilled['piggy_bank_id'] = $journal->piggyBankEvents()->first()->piggy_bank_id; + $preFilled['piggy_bank_id'] = $journal->piggyBankEvents()->orderBy('date', 'DESC')->first()->piggy_bank_id; } $preFilled['amount'] = 0; @@ -266,8 +267,10 @@ class TransactionController extends Controller $journal = $repository->store($journalData); event(new JournalSaved($journal)); + event(new JournalCreated($journal, intval($request->get('piggy_bank_id')))); Session::flash('success', 'New transaction "' . $journal->description . '" stored!'); + if (intval(Input::get('create_another')) === 1) { return Redirect::route('transactions.create', $request->input('what')); } @@ -307,6 +310,7 @@ class TransactionController extends Controller $repository->update($journal, $journalData); event(new JournalSaved($journal)); + // update, get events by date and sort DESC Session::flash('success', 'Transaction "' . e($journalData['description']) . '" updated.'); diff --git a/app/Models/PiggyBankEvent.php b/app/Models/PiggyBankEvent.php index f9a94a0286..775a4e310d 100644 --- a/app/Models/PiggyBankEvent.php +++ b/app/Models/PiggyBankEvent.php @@ -10,6 +10,8 @@ use Illuminate\Database\Eloquent\Model; class PiggyBankEvent extends Model { + protected $fillable = ['piggy_bank_id', 'transaction_journal_id', 'date', 'amount']; + /** * @return array */ diff --git a/app/Models/PiggyBankRepetition.php b/app/Models/PiggyBankRepetition.php index fe4d72a6f2..6bb9d2d6e2 100644 --- a/app/Models/PiggyBankRepetition.php +++ b/app/Models/PiggyBankRepetition.php @@ -1,7 +1,8 @@ belongsTo('FireflyIII\Models\PiggyBank'); } + /** + * @param EloquentBuilder $query + * @param Carbon $date + * + * @return mixed + */ + public function scopeRelevantOnDate(EloquentBuilder $query, Carbon $date) + { + return $query->where( + function($q) use ($date) { + $q->where('startdate', '>=', $date->format('Y-m-d 00:00:00')); + $q->orWhereNull('startdate'); + }) + + ->where(function($q) use ($date) { + + $q->where('targetdate', '<=', $date->format('Y-m-d 00:00:00')); + $q->orWhereNull('targetdate'); + }); + } + } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index b12e29e483..043e9ad7e1 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -32,7 +32,12 @@ class EventServiceProvider extends ServiceProvider = [ 'FireflyIII\Events\JournalSaved' => [ 'FireflyIII\Handlers\Events\RescanJournal', + 'FireflyIII\Handlers\Events\UpdateJournalConnection', + ], + 'FireflyIII\Events\JournalCreated' => [ + 'FireflyIII\Handlers\Events\ConnectJournalToPiggyBank', + ] ]; /** @@ -73,8 +78,8 @@ class EventServiceProvider extends ServiceProvider function (PiggyBank $piggyBank) { $repetition = new PiggyBankRepetition; $repetition->piggyBank()->associate($piggyBank); - $repetition->startdate = $piggyBank->startdate; - $repetition->targetdate = $piggyBank->targetdate; + $repetition->startdate = is_null($piggyBank->startdate) ? null : $piggyBank->startdate; + $repetition->targetdate = is_null($piggyBank->targetdate) ? null : $piggyBank->targetdate; $repetition->currentamount = 0; $repetition->save(); } diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 2faee4c964..7cb8e23608 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -40,11 +40,10 @@ class AccountRepository implements AccountRepositoryInterface /** * @param Account $account * @param int $page - * @param string $range * * @return mixed */ - public function getJournals(Account $account, $page, $range = 'session') + public function getJournals(Account $account, $page) { $offset = ($page - 1) * 50; $query = Auth::user() @@ -54,10 +53,8 @@ class AccountRepository implements AccountRepositoryInterface ->where('transactions.account_id', $account->id) ->orderBy('date', 'DESC'); - if ($range == 'session') { - $query->before(Session::get('end', Carbon::now()->endOfMonth())); - $query->after(Session::get('start', Carbon::now()->startOfMonth())); - } + $query->before(Session::get('end', Carbon::now()->endOfMonth())); + $query->after(Session::get('start', Carbon::now()->startOfMonth())); $count = $query->count(); $set = $query->take(50)->offset($offset)->get(['transaction_journals.*']); $paginator = new LengthAwarePaginator($set, $count, 50, $page); @@ -296,32 +293,6 @@ class AccountRepository implements AccountRepositoryInterface } - /** - * @param Account $account - * @param TransactionJournal $journal - * @param array $data - * - * @return TransactionJournal - */ - protected function _updateInitialBalance(Account $account, TransactionJournal $journal, array $data) - { - $journal->date = $data['openingBalanceDate']; - - /** @var Transaction $transaction */ - foreach ($journal->transactions()->get() as $transaction) { - if ($account->id == $transaction->account_id) { - $transaction->amount = $data['openingBalance']; - $transaction->save(); - } - if ($account->id != $transaction->account_id) { - $transaction->amount = $data['openingBalance'] * -1; - $transaction->save(); - } - } - - return $journal; - } - /** * @param Account $account * @param array $data @@ -355,4 +326,30 @@ class AccountRepository implements AccountRepositoryInterface } } + + /** + * @param Account $account + * @param TransactionJournal $journal + * @param array $data + * + * @return TransactionJournal + */ + protected function _updateInitialBalance(Account $account, TransactionJournal $journal, array $data) + { + $journal->date = $data['openingBalanceDate']; + + /** @var Transaction $transaction */ + foreach ($journal->transactions()->get() as $transaction) { + if ($account->id == $transaction->account_id) { + $transaction->amount = $data['openingBalance']; + $transaction->save(); + } + if ($account->id != $transaction->account_id) { + $transaction->amount = $data['openingBalance'] * -1; + $transaction->save(); + } + } + + return $journal; + } } \ No newline at end of file diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php index 619309d762..644f67744d 100644 --- a/app/Repositories/Account/AccountRepositoryInterface.php +++ b/app/Repositories/Account/AccountRepositoryInterface.php @@ -21,12 +21,11 @@ interface AccountRepositoryInterface /** * @param Account $account - * @param int $page * @param string $range * * @return mixed */ - public function getJournals(Account $account, $page, $range = 'session'); + public function getJournals(Account $account, $page); /** * @param Account $account diff --git a/app/Support/ExpandedForm.php b/app/Support/ExpandedForm.php index 94f462e6fe..168357d439 100644 --- a/app/Support/ExpandedForm.php +++ b/app/Support/ExpandedForm.php @@ -8,6 +8,7 @@ use Illuminate\Support\MessageBag; use Input; use Session; use View; +use Amount; /** * Class ExpandedForm @@ -17,46 +18,6 @@ use View; class ExpandedForm { - /** - * @param $name - * @param null $value - * @param array $options - * - * @return string - */ - public function integer($name, $value = null, array $options = []) - { - $label = $this->label($name, $options); - $options = $this->expandOptionArray($name, $label, $options); - $classes = $this->getHolderClasses($name); - $value = $this->fillFieldValue($name, $value); - $options['step'] = '1'; - $html = \View::make('form.integer', compact('classes', 'name', 'label', 'value', 'options'))->render(); - - return $html; - - } - - - /** - * @param $name - * @param null $value - * @param array $options - * - * @return string - */ - public function tags($name, $value = null, array $options = []) - { - $label = $this->label($name, $options); - $options = $this->expandOptionArray($name, $label, $options); - $classes = $this->getHolderClasses($name); - $value = $this->fillFieldValue($name, $value); - $options['data-role'] = 'tagsinput'; - $html = \View::make('form.tags', compact('classes', 'name', 'label', 'value', 'options'))->render(); - - return $html; - } - /** * @param $name * @param null $value @@ -91,9 +52,19 @@ class ExpandedForm if (isset($options['label'])) { return $options['label']; } - $labels = ['amount_min' => 'Amount (min)', 'amount_max' => 'Amount (max)', 'match' => 'Matches on', 'repeat_freq' => 'Repetition', - 'account_from_id' => 'Account from', 'account_to_id' => 'Account to', 'account_id' => 'Asset account', 'budget_id' => 'Budget' - , 'piggy_bank_id' => 'Piggy bank']; + $labels = [ + 'amount_min' => 'Amount (min)', + 'amount_max' => 'Amount (max)', + 'match' => 'Matches on', + 'repeat_freq' => 'Repetition', + 'account_from_id' => 'Account from', + 'account_to_id' => 'Account to', + 'account_id' => 'Asset account', + 'budget_id' => 'Budget', + 'openingBalance' => 'Opening balance', + 'accountRole' => 'Account role', + 'openingBalanceDate' => 'Opening balance date', + 'piggy_bank_id' => 'Piggy bank']; return isset($labels[$name]) ? $labels[$name] : str_replace('_', ' ', ucfirst($name)); @@ -229,6 +200,26 @@ class ExpandedForm return $html; } + /** + * @param $name + * @param null $value + * @param array $options + * + * @return string + */ + public function integer($name, $value = null, array $options = []) + { + $label = $this->label($name, $options); + $options = $this->expandOptionArray($name, $label, $options); + $classes = $this->getHolderClasses($name); + $value = $this->fillFieldValue($name, $value); + $options['step'] = '1'; + $html = \View::make('form.integer', compact('classes', 'name', 'label', 'value', 'options'))->render(); + + return $html; + + } + /** * @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind. * @@ -296,6 +287,25 @@ class ExpandedForm return $html; } + /** + * @param $name + * @param null $value + * @param array $options + * + * @return string + */ + public function tags($name, $value = null, array $options = []) + { + $label = $this->label($name, $options); + $options = $this->expandOptionArray($name, $label, $options); + $classes = $this->getHolderClasses($name); + $value = $this->fillFieldValue($name, $value); + $options['data-role'] = 'tagsinput'; + $html = \View::make('form.tags', compact('classes', 'name', 'label', 'value', 'options'))->render(); + + return $html; + } + /** * @param $name * @param null $value diff --git a/public/js/accounts.js b/public/js/accounts.js index d0830eecef..9301f828e6 100644 --- a/public/js/accounts.js +++ b/public/js/accounts.js @@ -1,7 +1,7 @@ $(function () { - if (typeof(googleLineChart) === "function" && typeof accountID !== 'undefined' && typeof view !== 'undefined') { - googleLineChart('chart/account/' + accountID + '/' + view, 'overview-chart'); + if (typeof(googleLineChart) === "function" && typeof accountID !== 'undefined') { + googleLineChart('chart/account/' + accountID, 'overview-chart'); } }); \ No newline at end of file diff --git a/resources/views/accounts/show.blade.php b/resources/views/accounts/show.blade.php index 2fbd79614d..462cbe5a9e 100644 --- a/resources/views/accounts/show.blade.php +++ b/resources/views/accounts/show.blade.php @@ -2,7 +2,7 @@ @section('content') {!! Breadcrumbs::renderIfExists(Route::getCurrentRoute()->getName(), $account) !!}
- @if($range == 'all') - Stick to date-range - @else - Show all transactions - @endif -
-+ | Currency | +|
---|---|---|
+ + | +{{{$currency->name}}} ({{{$currency->code}}}) ({{{$currency->symbol}}}) | +@if($currency->id == $defaultCurrency->id) default @else - make default - + make default @endif - + | +