/** * @param $value * @param $route * * @return mixed */ public static function routeBinder($value, $route) { if (Auth::check()) { $ids = explode(',', $value); /** @var \Illuminate\Support\Collection $object */ $object = Account::leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')->where('account_types.editable', 1)->whereIn('accounts.id', $ids)->where('user_id', Auth::user()->id)->get(['accounts.*']); if ($object->count() > 0) { return $object; } } throw new NotFoundHttpException(); }
/** * @param $value * @param $route * * @return Collection */ public static function routeBinder($value, $route) : Collection { if (auth()->check()) { $ids = explode(',', $value); // filter ids: $ids = self::filterIds($ids); /** @var \Illuminate\Support\Collection $object */ $object = Account::leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')->whereIn('accounts.id', $ids)->where('user_id', auth()->user()->id)->get(['accounts.*']); if ($object->count() > 0) { return $object; } } throw new NotFoundHttpException(); }
/** * This method generates a full report for the given period on all * given accounts * * @param Carbon $start * @param Carbon $end * @param Collection $accounts * * @return AccountCollection */ public function getAccountReport(Carbon $start, Carbon $end, Collection $accounts) { $startAmount = '0'; $endAmount = '0'; $diff = '0'; $ids = $accounts->pluck('id')->toArray(); $yesterday = clone $start; $yesterday->subDay(); bcscale(2); // get balances for start. $startSet = Account::leftJoin('transactions', 'transactions.account_id', '=', 'accounts.id')->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->whereIn('accounts.id', $ids)->whereNull('transaction_journals.deleted_at')->whereNull('transactions.deleted_at')->where('transaction_journals.date', '<=', $yesterday->format('Y-m-d'))->groupBy('accounts.id')->get(['accounts.id', DB::Raw('SUM(`transactions`.`amount`) as `balance`')]); // and end: $endSet = Account::leftJoin('transactions', 'transactions.account_id', '=', 'accounts.id')->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->whereIn('accounts.id', $ids)->whereNull('transaction_journals.deleted_at')->whereNull('transactions.deleted_at')->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->groupBy('accounts.id')->get(['accounts.id', DB::Raw('SUM(`transactions`.`amount`) as `balance`')]); $accounts->each(function (Account $account) use($startSet, $endSet) { /** * The balance for today always incorporates transactions * made on today. So to get todays "start" balance, we sub one * day. */ // $currentStart = $startSet->filter(function (Account $entry) use($account) { return $account->id == $entry->id; }); if ($currentStart->first()) { $account->startBalance = $currentStart->first()->balance; } $currentEnd = $endSet->filter(function (Account $entry) use($account) { return $account->id == $entry->id; }); if ($currentEnd->first()) { $account->endBalance = $currentEnd->first()->balance; } }); // summarize: foreach ($accounts as $account) { $startAmount = bcadd($startAmount, $account->startBalance); $endAmount = bcadd($endAmount, $account->endBalance); $diff = bcadd($diff, bcsub($account->endBalance, $account->startBalance)); } $object = new AccountCollection(); $object->setStart($startAmount); $object->setEnd($endAmount); $object->setDifference($diff); $object->setAccounts($accounts); return $object; }
/** * @param array $ids * @param Carbon $date * * @return Collection */ private function getSet(array $ids, Carbon $date) : Collection { return Account::leftJoin('transactions', 'transactions.account_id', '=', 'accounts.id')->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->whereIn('accounts.id', $ids)->whereNull('transaction_journals.deleted_at')->whereNull('transactions.deleted_at')->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->groupBy('accounts.id')->get(['accounts.id', DB::raw('SUM(`transactions`.`amount`) as `balance`')]); }
use FireflyIII\Models\Account; use FireflyIII\Models\Attachment; use FireflyIII\Models\Bill; use FireflyIII\Models\Budget; use FireflyIII\Models\Category; use FireflyIII\Models\LimitRepetition; use FireflyIII\Models\PiggyBank; use FireflyIII\Models\Tag; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionJournal; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; // models Route::bind('account', function ($value) { if (Auth::check()) { $object = Account::leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')->where('account_types.editable', 1)->where('accounts.id', $value)->where('user_id', Auth::user()->id)->first(['accounts.*']); if ($object) { return $object; } } throw new NotFoundHttpException(); }); Route::bind('tj', function ($value) { if (Auth::check()) { $object = TransactionJournal::where('id', $value)->where('user_id', Auth::user()->id)->first(); if ($object) { return $object; } } throw new NotFoundHttpException(); });
/** * Reports on deleted accounts that still have not deleted transactions or journals attached to them. */ private function reportDeletedAccounts() { $set = Account::leftJoin('transactions', 'transactions.account_id', '=', 'accounts.id')->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')->whereNotNull('accounts.deleted_at')->whereNotNull('transactions.id')->where(function (Builder $q) { $q->whereNull('transactions.deleted_at'); $q->orWhereNull('transaction_journals.deleted_at'); })->get(['accounts.id as account_id', 'accounts.deleted_at as account_deleted_at', 'transactions.id as transaction_id', 'transactions.deleted_at as transaction_deleted_at', 'transaction_journals.id as journal_id', 'transaction_journals.deleted_at as journal_deleted_at']); /** @var stdClass $entry */ foreach ($set as $entry) { $date = is_null($entry->transaction_deleted_at) ? $entry->journal_deleted_at : $entry->transaction_deleted_at; $this->error('Error: Account #' . $entry->account_id . ' should have been deleted, but has not.' . ' Find it in the table called `accounts` and change the `deleted_at` field to: "' . $date . '"'); } }