From 21a0a5d573ee2a134241a23cd3095da18c24cfe8 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 13 Dec 2014 22:11:51 +0100 Subject: [PATCH] Remapped all library classes. --- .../Database/{ => Account}/Account.php | 20 +- .../{Ifaces => Account}/AccountInterface.php | 2 +- .../{ => AccountType}/AccountType.php | 7 +- .../Database/{ => Budget}/Budget.php | 8 +- .../{Ifaces => Budget}/BudgetInterface.php | 2 +- .../FireflyIII/Database/{Ifaces => }/CUD.php | 2 +- .../Database/{ => Category}/Category.php | 8 +- .../{Ifaces => }/CommonDatabaseCalls.php | 2 +- .../Database/Ifaces/ReportInterface.php | 32 --- .../PiggyBank.php} | 10 +- .../PiggybankInterface.php | 2 +- .../{ => PiggyBank}/RepeatedExpense.php | 8 +- .../RecurringTransaction.php} | 14 +- .../RecurringTransactionInterface.php} | 4 +- app/lib/FireflyIII/Database/Report.php | 180 ---------------- .../{ => Transaction}/Transaction.php | 8 +- .../TransactionCurrency.php | 4 +- .../TransactionCurrencyInterface.php | 2 +- .../TransactionJournal.php | 8 +- .../TransactionJournalInterface.php | 2 +- .../{ => TransactionType}/TransactionType.php | 7 +- .../FireflyIII/Database/{ => User}/User.php | 2 +- app/lib/FireflyIII/Report/Report.php | 200 ++++++++++++++++-- 23 files changed, 241 insertions(+), 293 deletions(-) rename app/lib/FireflyIII/Database/{ => Account}/Account.php (97%) rename app/lib/FireflyIII/Database/{Ifaces => Account}/AccountInterface.php (98%) rename app/lib/FireflyIII/Database/{ => AccountType}/AccountType.php (95%) rename app/lib/FireflyIII/Database/{ => Budget}/Budget.php (98%) rename app/lib/FireflyIII/Database/{Ifaces => Budget}/BudgetInterface.php (93%) rename app/lib/FireflyIII/Database/{Ifaces => }/CUD.php (94%) rename app/lib/FireflyIII/Database/{ => Category}/Category.php (96%) rename app/lib/FireflyIII/Database/{Ifaces => }/CommonDatabaseCalls.php (95%) delete mode 100644 app/lib/FireflyIII/Database/Ifaces/ReportInterface.php rename app/lib/FireflyIII/Database/{Piggybank.php => PiggyBank/PiggyBank.php} (97%) rename app/lib/FireflyIII/Database/{Ifaces => PiggyBank}/PiggybankInterface.php (86%) rename app/lib/FireflyIII/Database/{ => PiggyBank}/RepeatedExpense.php (99%) rename app/lib/FireflyIII/Database/{Recurring.php => RecurringTransaction/RecurringTransaction.php} (97%) rename app/lib/FireflyIII/Database/{Ifaces/RecurringInterface.php => RecurringTransaction/RecurringTransactionInterface.php} (90%) delete mode 100644 app/lib/FireflyIII/Database/Report.php rename app/lib/FireflyIII/Database/{ => Transaction}/Transaction.php (96%) rename app/lib/FireflyIII/Database/{ => TransactionCurrency}/TransactionCurrency.php (79%) rename app/lib/FireflyIII/Database/{Ifaces => TransactionCurrency}/TransactionCurrencyInterface.php (83%) rename app/lib/FireflyIII/Database/{ => TransactionJournal}/TransactionJournal.php (99%) rename app/lib/FireflyIII/Database/{Ifaces => TransactionJournal}/TransactionJournalInterface.php (93%) rename app/lib/FireflyIII/Database/{ => TransactionType}/TransactionType.php (94%) rename app/lib/FireflyIII/Database/{ => User}/User.php (97%) 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