From 5c59c819b62166b4a472812b03303ee4dda2f087 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 27 Jan 2016 20:48:35 +0100 Subject: [PATCH] Moved method to new helper. --- app/Helpers/Report/AccountReportHelper.php | 92 ++++++++++++++++ .../Report/AccountReportHelperInterface.php | 16 +++ app/Helpers/Report/ReportHelper.php | 101 +----------------- app/Helpers/Report/ReportHelperInterface.php | 14 --- 4 files changed, 113 insertions(+), 110 deletions(-) diff --git a/app/Helpers/Report/AccountReportHelper.php b/app/Helpers/Report/AccountReportHelper.php index e982fcc35a..9d0dae3a91 100644 --- a/app/Helpers/Report/AccountReportHelper.php +++ b/app/Helpers/Report/AccountReportHelper.php @@ -9,6 +9,12 @@ namespace FireflyIII\Helpers\Report; +use Carbon\Carbon; +use DB; +use FireflyIII\Helpers\Collection\Account as AccountCollection; +use FireflyIII\Models\Account; +use Illuminate\Support\Collection; + /** * Class AccountReportHelper @@ -17,5 +23,91 @@ namespace FireflyIII\Helpers\Report; */ class AccountReportHelper implements AccountReportHelperInterface { + /** + * 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; + } } \ No newline at end of file diff --git a/app/Helpers/Report/AccountReportHelperInterface.php b/app/Helpers/Report/AccountReportHelperInterface.php index a118995345..b80052e1a2 100644 --- a/app/Helpers/Report/AccountReportHelperInterface.php +++ b/app/Helpers/Report/AccountReportHelperInterface.php @@ -9,6 +9,11 @@ namespace FireflyIII\Helpers\Report; +use Carbon\Carbon; +use FireflyIII\Helpers\Collection\Account as AccountCollection; +use Illuminate\Support\Collection; + + /** * Interface AccountReportHelperInterface * @@ -16,5 +21,16 @@ namespace FireflyIII\Helpers\Report; */ interface AccountReportHelperInterface { + /** + * 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); } \ No newline at end of file diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php index e34284444f..d66e8daa73 100644 --- a/app/Helpers/Report/ReportHelper.php +++ b/app/Helpers/Report/ReportHelper.php @@ -3,9 +3,6 @@ namespace FireflyIII\Helpers\Report; use Carbon\Carbon; -use DB; -use FireflyIII\Helpers\FiscalHelper; -use FireflyIII\Helpers\Collection\Account as AccountCollection; use FireflyIII\Helpers\Collection\Balance; use FireflyIII\Helpers\Collection\BalanceEntry; use FireflyIII\Helpers\Collection\BalanceHeader; @@ -17,10 +14,10 @@ use FireflyIII\Helpers\Collection\BudgetLine; use FireflyIII\Helpers\Collection\Category as CategoryCollection; use FireflyIII\Helpers\Collection\Expense; use FireflyIII\Helpers\Collection\Income; -use FireflyIII\Models\Account; +use FireflyIII\Helpers\FiscalHelper; use FireflyIII\Models\Bill; -use FireflyIII\Models\Budget as BudgetModel; use FireflyIII\Models\Budget; +use FireflyIII\Models\Budget as BudgetModel; use FireflyIII\Models\LimitRepetition; use FireflyIII\Models\Tag; use FireflyIII\Models\TransactionJournal; @@ -59,94 +56,6 @@ class ReportHelper implements ReportHelperInterface $this->tagRepository = $tagRepository; } - /** - * 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 Carbon $start * @param Carbon $end @@ -386,9 +295,9 @@ class ReportHelper implements ReportHelperInterface public function listOfMonths(Carbon $date) { - $start = clone $date; - $end = Carbon::now(); - $months = []; + $start = clone $date; + $end = Carbon::now(); + $months = []; $fiscalHelper = new FiscalHelper; while ($start <= $end) { $year = $fiscalHelper->endOfFiscalYear($start)->year; diff --git a/app/Helpers/Report/ReportHelperInterface.php b/app/Helpers/Report/ReportHelperInterface.php index 9759d755d7..55d95cde75 100644 --- a/app/Helpers/Report/ReportHelperInterface.php +++ b/app/Helpers/Report/ReportHelperInterface.php @@ -3,7 +3,6 @@ namespace FireflyIII\Helpers\Report; use Carbon\Carbon; -use FireflyIII\Helpers\Collection\Account as AccountCollection; use FireflyIII\Helpers\Collection\Balance; use FireflyIII\Helpers\Collection\Bill as BillCollection; use FireflyIII\Helpers\Collection\Budget as BudgetCollection; @@ -19,19 +18,6 @@ use Illuminate\Support\Collection; */ interface ReportHelperInterface { - - /** - * 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); - /** * @param Carbon $start * @param Carbon $end