mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 23:45:10 +00:00
Moved a method around.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user