Moved a method around.

This commit is contained in:
James Cole 2016-10-08 16:04:05 +02:00
parent d0c92a2244
commit a1f797c4d1
6 changed files with 96 additions and 133 deletions

View File

@ -1,87 +0,0 @@
<?php
/**
* AccountReportHelper.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Helpers\Report;
use Carbon\Carbon;
use FireflyIII\Helpers\Collection\Account as AccountCollection;
use FireflyIII\Models\Account;
use Illuminate\Support\Collection;
use Log;
use Steam;
/**
* Class AccountReportHelper
*
* @package FireflyIII\Helpers\Report
*/
class AccountReportHelper implements AccountReportHelperInterface
{
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return AccountCollection
*/
public function getAccountReport(Carbon $start, Carbon $end, Collection $accounts): AccountCollection
{
$startAmount = '0';
$endAmount = '0';
$diff = '0';
$ids = $accounts->pluck('id')->toArray();
$yesterday = clone $start;
$yesterday->subDay();
$startSet = Steam::balancesById($ids, $yesterday);
$backupSet = Steam::balancesById($ids, $start);
$endSet = Steam::balancesById($ids, $end);
Log::debug(
sprintf(
'getAccountReport from %s to %s for %d accounts.',
$start->format('Y-m-d'),
$end->format('Y-m-d'),
$accounts->count()
)
);
$accounts->each(
function (Account $account) use ($startSet, $endSet, $backupSet) {
$account->startBalance = $startSet[$account->id] ?? '0';
$account->endBalance = $endSet[$account->id] ?? '0';
// check backup set just in case:
if ($account->startBalance === '0' && isset($backupSet[$account->id])) {
$account->startBalance = $backupSet[$account->id];
}
}
);
// 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;
}
}

View File

@ -1,40 +0,0 @@
<?php
/**
* AccountReportHelperInterface.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Helpers\Report;
use Carbon\Carbon;
use FireflyIII\Helpers\Collection\Account as AccountCollection;
use Illuminate\Support\Collection;
/**
* Interface AccountReportHelperInterface
*
* @package 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): AccountCollection;
}

View File

@ -24,6 +24,7 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
use FireflyIII\Repositories\Account\AccountTaskerInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use Illuminate\Support\Collection;
@ -40,8 +41,8 @@ use View;
class ReportController extends Controller
{
/** @var AccountReportHelperInterface */
protected $accountHelper;
/** @var AccountTaskerInterface */
protected $accountTasker;
/** @var BalanceReportHelperInterface */
protected $balanceHelper;
@ -234,7 +235,7 @@ class ReportController extends Controller
private function createRepositories()
{
$this->helper = app(ReportHelperInterface::class);
$this->accountHelper = app(AccountReportHelperInterface::class);
$this->accountTasker = app(AccountTaskerInterface::class);
$this->budgetHelper = app(BudgetReportHelperInterface::class);
$this->balanceHelper = app(BalanceReportHelperInterface::class);
}
@ -253,7 +254,7 @@ class ReportController extends Controller
$expenseTopLength = 8;
// get report stuff!
$accountReport = $this->accountHelper->getAccountReport($start, $end, $accounts);
$accountReport = $this->accountTasker->getAccountReport($start, $end, $accounts);
$incomes = $this->helper->getIncomeReport($start, $end, $accounts);
$expenses = $this->helper->getExpenseReport($start, $end, $accounts);
$budgets = $this->budgetHelper->getBudgetReport($start, $end, $accounts);
@ -297,7 +298,7 @@ class ReportController extends Controller
// list of users stuff:
$budgets = app(BudgetRepositoryInterface::class)->getActiveBudgets();
$categories = app(CategoryRepositoryInterface::class)->getCategories();
$accountReport = $this->accountHelper->getAccountReport($start, $end, $accounts);
$accountReport = $this->accountTasker->getAccountReport($start, $end, $accounts);
$incomes = $this->helper->getIncomeReport($start, $end, $accounts);
$expenses = $this->helper->getExpenseReport($start, $end, $accounts);
$tags = $this->helper->tagReport($start, $end, $accounts);
@ -332,7 +333,7 @@ class ReportController extends Controller
$incomeTopLength = 8;
$expenseTopLength = 8;
$accountReport = $this->accountHelper->getAccountReport($start, $end, $accounts);
$accountReport = $this->accountTasker->getAccountReport($start, $end, $accounts);
$incomes = $this->helper->getIncomeReport($start, $end, $accounts);
$expenses = $this->helper->getExpenseReport($start, $end, $accounts);
$tags = $this->helper->tagReport($start, $end, $accounts);

View File

@ -55,5 +55,19 @@ class AccountServiceProvider extends ServiceProvider
return app('FireflyIII\Repositories\Account\AccountRepository', $arguments);
}
);
$this->app->bind(
'FireflyIII\Repositories\Account\AccountTaskerInterface',
function (Application $app, array $arguments) {
if (!isset($arguments[0]) && $app->auth->check()) {
return app('FireflyIII\Repositories\Account\AccountTasker', [auth()->user()]);
}
if (!isset($arguments[0]) && !$app->auth->check()) {
throw new FireflyException('There is no user present.');
}
return app('FireflyIII\Repositories\Account\AccountTasker', $arguments);
}
);
}
}

View File

@ -13,7 +13,13 @@ declare(strict_types = 1);
namespace FireflyIII\Repositories\Account;
use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\User;
use Illuminate\Support\Collection;
use FireflyIII\Helpers\Collection\Account as AccountCollection;
use Log;
use Steam;
/**
* Class AccountTasker
@ -34,4 +40,60 @@ class AccountTasker
{
$this->user = $user;
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return AccountCollection
*/
public function getAccountReport(Carbon $start, Carbon $end, Collection $accounts): AccountCollection
{
$startAmount = '0';
$endAmount = '0';
$diff = '0';
$ids = $accounts->pluck('id')->toArray();
$yesterday = clone $start;
$yesterday->subDay();
$startSet = Steam::balancesById($ids, $yesterday);
$backupSet = Steam::balancesById($ids, $start);
$endSet = Steam::balancesById($ids, $end);
Log::debug(
sprintf(
'getAccountReport from %s to %s for %d accounts.',
$start->format('Y-m-d'),
$end->format('Y-m-d'),
$accounts->count()
)
);
$accounts->each(
function (Account $account) use ($startSet, $endSet, $backupSet) {
$account->startBalance = $startSet[$account->id] ?? '0';
$account->endBalance = $endSet[$account->id] ?? '0';
// check backup set just in case:
if ($account->startBalance === '0' && isset($backupSet[$account->id])) {
$account->startBalance = $backupSet[$account->id];
}
}
);
// 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;
}
}

View File

@ -13,6 +13,10 @@ declare(strict_types = 1);
namespace FireflyIII\Repositories\Account;
use Carbon\Carbon;
use FireflyIII\Helpers\Collection\Account as AccountCollection;
use Illuminate\Support\Collection;
/**
* Interface AccountTaskerInterface
*
@ -21,4 +25,13 @@ namespace FireflyIII\Repositories\Account;
interface AccountTaskerInterface
{
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return AccountCollection
*/
public function getAccountReport(Carbon $start, Carbon $end, Collection $accounts): AccountCollection;
}