diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.php index 3f36b31a09..cf9ea778c8 100644 --- a/app/controllers/AccountController.php +++ b/app/controllers/AccountController.php @@ -34,8 +34,9 @@ class AccountController extends BaseController $subTitleIcon = 'fa-download'; break; } + $subTitle = 'Create a new ' . $what . ' account'; - return View::make('accounts.create')->with('subTitle', 'Create a new ' . $what . ' account')->with('what', $what)->with(compact('subTitleIcon')); + return View::make('accounts.create', compact('subTitleIcon', 'what', 'subTitle')); } /** @@ -45,9 +46,9 @@ class AccountController extends BaseController */ public function delete(Account $account) { - return View::make('accounts.delete')->with('account', $account)->with( - 'subTitle', 'Delete ' . strtolower($account->accountType->type) . ' "' . $account->name . '"' - ); + $subTitle = 'Delete ' . strtolower($account->accountType->type) . ' "' . $account->name . '"'; + + return View::make('accounts.delete', compact('account', 'subTitle')); } /** @@ -98,29 +99,31 @@ class AccountController extends BaseController } /* - * Delete it + * Delete the initial balance as well. */ if ($initialBalance) { $acct->destroy($initialBalance); } + $name = $account->name; $acct->destroy($account); - Session::flash('success', 'The account was deleted.'); + + $return = 'asset'; switch ($type) { - case 'Asset account': - case 'Default account': - return Redirect::route('accounts.index', 'asset'); - break; case 'Expense account': case 'Beneficiary account': - return Redirect::route('accounts.index', 'expense'); + $return = 'expense'; break; case 'Revenue account': - return Redirect::route('accounts.index', 'revenue'); + $return = 'revenue'; break; } + Session::flash('success', 'The ' . $return . ' account "' . e($name) . '" was deleted.'); + + return Redirect::route('accounts.index', $return); + } diff --git a/app/controllers/ReportController.php b/app/controllers/ReportController.php index 48eef3a0b3..83b6dd83d6 100644 --- a/app/controllers/ReportController.php +++ b/app/controllers/ReportController.php @@ -221,23 +221,67 @@ class ReportController extends BaseController */ public function year($year) { + Config::set('app.debug',false); try { $date = new Carbon('01-01-' . $year); } catch (Exception $e) { App::abort(500); } $date = new Carbon('01-01-' . $year); + /** @var \FireflyIII\Database\TransactionJournal $tj */ $tj = App::make('FireflyIII\Database\TransactionJournal'); + /** @var \FireflyIII\Database\Account $accountRepository */ + $accountRepository = App::make('FireflyIII\Database\Account'); + + /** @var \FireflyIII\Database\Report $reportRepository */ + $reportRepository = App::make('FireflyIII\Database\Report'); + + $accounts = $accountRepository->getAssetAccounts(); + // get some sums going $summary = []; + /** @var \Account $account */ + $accounts->each( + function (\Account $account) { + if ($account->getMeta('accountRole') == 'sharedExpense') { + $account->sharedExpense = true; + } else { + $account->sharedExpense = false; + } + } + ); + $end = clone $date; $end->endOfYear(); while ($date < $end) { - $summary[] = ['month' => $date->format('F'), 'income' => $tj->getSumOfIncomesByMonth($date), 'expense' => $tj->getSumOfExpensesByMonth($date),]; + $month = $date->format('F'); + + $income = 0; + $incomeShared = 0; + $expense = 0; + $expenseShared = 0; + + foreach ($accounts as $account) { + if ($account->sharedExpense === true) { + $incomeShared += $reportRepository->getIncomeByMonth($account, $date); + $expenseShared += $reportRepository->getExpenseByMonth($account, $date); + } else { + $income += $reportRepository->getIncomeByMonth($account, $date); + $expense += $reportRepository->getExpenseByMonth($account, $date); + } + } + + $summary[] = [ + 'month' => $month, + 'income' => $income, + 'expense' => $expense, + 'incomeShared' => $incomeShared, + 'expenseShared' => $expenseShared, + ]; $date->addMonth(); } diff --git a/app/lib/FireflyIII/Database/Ifaces/ReportInterface.php b/app/lib/FireflyIII/Database/Ifaces/ReportInterface.php new file mode 100644 index 0000000000..192d388c90 --- /dev/null +++ b/app/lib/FireflyIII/Database/Ifaces/ReportInterface.php @@ -0,0 +1,27 @@ +sharedExpense) && $account->sharedExpense === true) { + $shared = true; + } else { + if (isset($account->sharedExpense) && $account->sharedExpense === false) { + $shared = false; + } else { + $shared = ($account->getMeta('accountRole') == 'sharedExpense'); + } + } + + $start = clone $month; + $end = clone $month; + $start->startOfMonth(); + $end->endOfMonth(); + $sum = 0; + + // get all journals. + $journals = \TransactionJournal::whereIn( + 'id', function ($query) use ($account, $start, $end) { + $query->select('transaction_journal_id') + ->from('transactions') + ->where('account_id', $account->id); + } + )->before($end)->after($start)->get(); + + + if ($shared) { + $expenses = $journals->filter( + function (\TransactionJournal $journal) use ($account) { + // any withdrawal is an expense: + if ($journal->transactionType->type == 'Withdrawal') { + return $journal; + } + // any transfer away from this account is an expense. + if ($journal->transactionType->type == 'Transfer') { + /** @var \Transaction $t */ + foreach ($journal->transactions as $t) { + if ($t->account_id == $account->id && floatval($t->amount) < 0) { + return $journal; + } + } + } + } + ); + } else { + $expenses = $journals->filter( + function (\TransactionJournal $journal) use ($account) { + // only withdrawals are expenses: + if ($journal->transactionType->type == 'Withdrawal') { + return $journal; + } + // transfers TO a shared account are also expenses. + if ($journal->transactionType->type == 'Transfer') { + /** @var \Transaction $t */ + foreach ($journal->transactions() as $t) { + if ($t->account->getMeta('accountRole') == 'sharedExpense') { + echo '#'.$journal->id.' is a shared expense!
'; + return $journal; + } + } + } + } + ); + } + /** @var \TransactionJournal $expense */ + foreach ($expenses as $expense) { + $sum += $expense->getAmount(); + } + + + return $sum; + } + + /** + * @param \Account $account + * @param Carbon $month + * + * @return float + */ + public function getIncomeByMonth(\Account $account, Carbon $month) + { + if (isset($account->sharedExpense) && $account->sharedExpense === true) { + $shared = true; + } else { + if (isset($account->sharedExpense) && $account->sharedExpense === false) { + $shared = false; + } else { + $shared = ($account->getMeta('accountRole') == 'sharedExpense'); + } + } + + $start = clone $month; + $end = clone $month; + $start->startOfMonth(); + $end->endOfMonth(); + $sum = 0; + + // get all journals. + $journals = \TransactionJournal::whereIn( + 'id', function ($query) use ($account, $start, $end) { + $query->select('transaction_journal_id') + ->from('transactions') + ->where('account_id', $account->id); + } + )->before($end)->after($start)->get(); + + + if ($shared) { + $incomes = $journals->filter( + function (\TransactionJournal $journal) use ($account) { + // any deposit is an income: + if ($journal->transactionType->type == 'Deposit') { + return $journal; + } + // any transfer TO this account is an income. + if ($journal->transactionType->type == 'Transfer') { + /** @var \Transaction $t */ + foreach ($journal->transactions as $t) { + if ($t->account_id == $account->id && floatval($t->amount) > 0) { + return $journal; + } + } + } + } + ); + } else { + $incomes = $journals->filter( + function (\TransactionJournal $journal) use ($account) { + // only deposits are incomes: + if ($journal->transactionType->type == 'Deposit') { + return $journal; + } + } + ); + } + /** @var \TransactionJournal $expense */ + foreach ($incomes as $income) { + $sum += $income->getAmount(); + } + + + return $sum; + } +} \ No newline at end of file diff --git a/app/models/Account.php b/app/models/Account.php index a6f34c971e..d2479f939c 100644 --- a/app/models/Account.php +++ b/app/models/Account.php @@ -110,14 +110,6 @@ class Account extends Ardent $query->with(['accountmeta']); } - /** - * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough - */ - public function transactionjournals() - { - return $this->hasManyThrough('TransactionJournal', 'Transaction', 'transaction_journal_id', 'id'); - } - /** * Transactions. * diff --git a/app/views/reports/year.blade.php b/app/views/reports/year.blade.php index f0afdb2b4a..7ceaf3eb52 100644 --- a/app/views/reports/year.blade.php +++ b/app/views/reports/year.blade.php @@ -28,7 +28,7 @@
- Summary + Summary (without shared accounts)
@@ -66,6 +66,48 @@ +
+
+
+
+ Summary (shared accounts only) +
+
+ + + @foreach($summary as $entry) + + @endforeach + + + + + + @foreach($summary as $entry) + + + @endforeach + + + + + @foreach($summary as $entry) + + + @endforeach + + + + @foreach($summary as $entry) + + @endforeach + + +
{{$entry['month']}}Sum
In{{mf($entry['incomeShared'])}}{{mf($inSum)}}
Out{{mf($entry['expenseShared']*-1)}}{{mf($outSum)}}
Difference{{mf($entry['incomeShared']- $entry['expenseShared'])}}{{mf($inSum + $outSum)}}
+
+
+ +