diff --git a/app/lib/FireflyIII/Database/Account.php b/app/lib/FireflyIII/Database/Account/Account.php
similarity index 97%
rename from app/lib/FireflyIII/Database/Account.php
rename to app/lib/FireflyIII/Database/Account/Account.php
index d1d91bce21..0e1f08df2f 100644
--- a/app/lib/FireflyIII/Database/Account.php
+++ b/app/lib/FireflyIII/Database/Account/Account.php
@@ -1,11 +1,11 @@
findByWhat('initial');
@@ -182,8 +182,8 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
*/
$balance = floatval($data['openingbalance']);
$date = new Carbon($data['openingbalancedate']);
- /** @var \FireflyIII\Database\TransactionJournal $tj */
- $tj = \App::make('FireflyIII\Database\TransactionJournal');
+ /** @var \FireflyIII\Database\TransactionJournal\TransactionJournal $tj */
+ $tj = \App::make('FireflyIII\Database\TransactionJournal\TransactionJournal');
if ($balance < 0) {
// first transaction draws money from the new account to the opposing
$from = $account;
@@ -282,8 +282,8 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
/*
* Find account type.
*/
- /** @var \FireflyIII\Database\AccountType $acctType */
- $acctType = \App::make('FireflyIII\Database\AccountType');
+ /** @var \FireflyIII\Database\AccountType\AccountType $acctType */
+ $acctType = \App::make('FireflyIII\Database\AccountType\AccountType');
$accountType = $acctType->findByWhat($data['what']);
diff --git a/app/lib/FireflyIII/Database/Ifaces/AccountInterface.php b/app/lib/FireflyIII/Database/Account/AccountInterface.php
similarity index 98%
rename from app/lib/FireflyIII/Database/Ifaces/AccountInterface.php
rename to app/lib/FireflyIII/Database/Account/AccountInterface.php
index 614c476556..55c7883865 100644
--- a/app/lib/FireflyIII/Database/Ifaces/AccountInterface.php
+++ b/app/lib/FireflyIII/Database/Account/AccountInterface.php
@@ -1,6 +1,6 @@
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::with(['transactionType','transactions'])->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;
- }
- }
- }
- return null;
- }
- );
- } 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;
- }
- }
- }
- return null;
- }
- );
- }
- /** @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::with(['transactionType','transactions'])->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;
- }
- }
- }
- return null;
- }
- );
- } else {
- $incomes = $journals->filter(
- function (\TransactionJournal $journal) use ($account) {
- // only deposits are incomes:
- if ($journal->transactionType->type == 'Deposit') {
- return $journal;
- }
- return null;
- }
- );
- }
- /** @var \TransactionJournal $expense */
- foreach ($incomes as $income) {
- $sum += $income->getAmount();
- }
-
-
- return $sum;
- }
-}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Database/Transaction.php b/app/lib/FireflyIII/Database/Transaction/Transaction.php
similarity index 96%
rename from app/lib/FireflyIII/Database/Transaction.php
rename to app/lib/FireflyIII/Database/Transaction/Transaction.php
index 566c41c9de..d400caea6e 100644
--- a/app/lib/FireflyIII/Database/Transaction.php
+++ b/app/lib/FireflyIII/Database/Transaction/Transaction.php
@@ -1,11 +1,11 @@
on('t_from.transaction_journal_id', '=', 'transaction_journals.id')->where('t_from.amount', '<', 0);
- }
+ $join->on('t_from.transaction_journal_id', '=', 'transaction_journals.id')->where('t_from.amount', '<', 0);
+ }
)
->leftJoin('accounts as ac_from', 't_from.account_id', '=', 'ac_from.id')
->leftJoin(
'account_meta as acm_from', function ($join) {
- $join->on('ac_from.id', '=', 'acm_from.account_id')->where('acm_from.name', '=', 'accountRole');
- }
+ $join->on('ac_from.id', '=', 'acm_from.account_id')->where('acm_from.name', '=', 'accountRole');
+ }
)
->leftJoin(
'transactions as t_to', function ($join) {
- $join->on('t_to.transaction_journal_id', '=', 'transaction_journals.id')->where('t_to.amount', '>', 0);
- }
+ $join->on('t_to.transaction_journal_id', '=', 'transaction_journals.id')->where('t_to.amount', '>', 0);
+ }
)
->leftJoin('accounts as ac_to', 't_to.account_id', '=', 'ac_to.id')
->leftJoin(
'account_meta as acm_to', function ($join) {
- $join->on('ac_to.id', '=', 'acm_to.account_id')->where('acm_to.name', '=', 'accountRole');
- }
+ $join->on('ac_to.id', '=', 'acm_to.account_id')->where('acm_to.name', '=', 'accountRole');
+ }
)
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->where('transaction_types.type', 'Withdrawal')
@@ -120,25 +122,25 @@ class Report implements ReportInterface
return \TransactionJournal::
leftJoin(
'transactions as t_from', function ($join) {
- $join->on('t_from.transaction_journal_id', '=', 'transaction_journals.id')->where('t_from.amount', '<', 0);
- }
+ $join->on('t_from.transaction_journal_id', '=', 'transaction_journals.id')->where('t_from.amount', '<', 0);
+ }
)
->leftJoin('accounts as ac_from', 't_from.account_id', '=', 'ac_from.id')
->leftJoin(
'account_meta as acm_from', function ($join) {
- $join->on('ac_from.id', '=', 'acm_from.account_id')->where('acm_from.name', '=', 'accountRole');
- }
+ $join->on('ac_from.id', '=', 'acm_from.account_id')->where('acm_from.name', '=', 'accountRole');
+ }
)
->leftJoin(
'transactions as t_to', function ($join) {
- $join->on('t_to.transaction_journal_id', '=', 'transaction_journals.id')->where('t_to.amount', '>', 0);
- }
+ $join->on('t_to.transaction_journal_id', '=', 'transaction_journals.id')->where('t_to.amount', '>', 0);
+ }
)
->leftJoin('accounts as ac_to', 't_to.account_id', '=', 'ac_to.id')
->leftJoin(
'account_meta as acm_to', function ($join) {
- $join->on('ac_to.id', '=', 'acm_to.account_id')->where('acm_to.name', '=', 'accountRole');
- }
+ $join->on('ac_to.id', '=', 'acm_to.account_id')->where('acm_to.name', '=', 'accountRole');
+ }
)
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->where('transaction_types.type', 'Deposit')
@@ -196,4 +198,168 @@ class Report implements ReportInterface
return $report;
}
+ use SwitchUser;
+
+
+ /**
+ * @param \Account $account
+ * @param Carbon $month
+ *
+ * @return float
+ */
+ public function getExpenseByMonth(\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::with(['transactionType', 'transactions'])->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;
+ }
+ }
+ }
+
+ return null;
+ }
+ );
+ } 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;
+ }
+ }
+ }
+
+ return null;
+ }
+ );
+ }
+ /** @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::with(['transactionType', 'transactions'])->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;
+ }
+ }
+ }
+
+ return null;
+ }
+ );
+ } else {
+ $incomes = $journals->filter(
+ function (\TransactionJournal $journal) use ($account) {
+ // only deposits are incomes:
+ if ($journal->transactionType->type == 'Deposit') {
+ return $journal;
+ }
+
+ return null;
+ }
+ );
+ }
+ /** @var \TransactionJournal $expense */
+ foreach ($incomes as $income) {
+ $sum += $income->getAmount();
+ }
+
+
+ return $sum;
+ }
+
}
\ No newline at end of file