2016-01-27 20:45:05 +01:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* AccountReportHelper.php
|
2016-04-01 16:44:46 +02:00
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
2016-01-27 20:45:05 +01:00
|
|
|
*
|
2016-10-05 06:52:15 +02:00
|
|
|
* 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.
|
2016-01-27 20:45:05 +01:00
|
|
|
*/
|
|
|
|
|
|
2016-05-20 12:27:31 +02:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
2016-01-27 20:45:05 +01:00
|
|
|
namespace FireflyIII\Helpers\Report;
|
|
|
|
|
|
2016-01-27 20:48:35 +01:00
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use FireflyIII\Helpers\Collection\Account as AccountCollection;
|
|
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
|
use Illuminate\Support\Collection;
|
2016-10-08 15:59:58 +02:00
|
|
|
use Log;
|
|
|
|
|
use Steam;
|
2016-01-27 20:48:35 +01:00
|
|
|
|
2016-01-27 20:45:05 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class AccountReportHelper
|
|
|
|
|
*
|
|
|
|
|
* @package FireflyIII\Helpers\Report
|
|
|
|
|
*/
|
|
|
|
|
class AccountReportHelper implements AccountReportHelperInterface
|
|
|
|
|
{
|
2016-04-28 10:59:36 +02:00
|
|
|
|
2016-01-27 20:48:35 +01:00
|
|
|
/**
|
|
|
|
|
* @param Carbon $start
|
|
|
|
|
* @param Carbon $end
|
|
|
|
|
* @param Collection $accounts
|
|
|
|
|
*
|
|
|
|
|
* @return AccountCollection
|
|
|
|
|
*/
|
2016-04-06 16:37:28 +02:00
|
|
|
public function getAccountReport(Carbon $start, Carbon $end, Collection $accounts): AccountCollection
|
2016-01-27 20:48:35 +01:00
|
|
|
{
|
|
|
|
|
$startAmount = '0';
|
|
|
|
|
$endAmount = '0';
|
|
|
|
|
$diff = '0';
|
|
|
|
|
$ids = $accounts->pluck('id')->toArray();
|
2016-04-27 10:38:51 +02:00
|
|
|
$yesterday = clone $start;
|
2016-01-27 20:48:35 +01:00
|
|
|
$yesterday->subDay();
|
2016-10-08 15:59:58 +02:00
|
|
|
$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()
|
|
|
|
|
)
|
|
|
|
|
);
|
2016-01-27 20:48:35 +01:00
|
|
|
$accounts->each(
|
2016-02-08 20:59:09 +01:00
|
|
|
function (Account $account) use ($startSet, $endSet, $backupSet) {
|
2016-10-08 15:59:58 +02:00
|
|
|
$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];
|
|
|
|
|
}
|
2016-01-27 20:48:35 +01:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 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);
|
2016-01-27 20:45:05 +01:00
|
|
|
|
2016-04-27 10:38:51 +02:00
|
|
|
|
2016-10-08 15:59:58 +02:00
|
|
|
return $object;
|
2016-04-27 10:38:51 +02:00
|
|
|
}
|
2016-01-28 21:50:20 +01:00
|
|
|
}
|