mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-19 02:52:44 +00:00
New chart called "net worth".
This commit is contained in:
@@ -10,6 +10,7 @@ use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
use Illuminate\Support\Collection;
|
||||
use Response;
|
||||
use Steam;
|
||||
|
||||
/**
|
||||
* Class ReportController
|
||||
@@ -32,6 +33,53 @@ class ReportController extends Controller
|
||||
$this->generator = app('FireflyIII\Generator\Chart\Report\ReportChartGeneratorInterface');
|
||||
}
|
||||
|
||||
/**
|
||||
* This chart, by default, is shown on the multi-year and year report pages,
|
||||
* which means that giving it a 2 week "period" should be enough granularity.
|
||||
*
|
||||
* @param ReportQueryInterface $query
|
||||
* @param string $reportType
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Collection $accounts
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function netWorth(ReportQueryInterface $query, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
|
||||
{
|
||||
bcscale(2);
|
||||
// chart properties for cache:
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty('netWorth');
|
||||
$cache->addProperty($start);
|
||||
$cache->addProperty($reportType);
|
||||
$cache->addProperty($accounts);
|
||||
$cache->addProperty($end);
|
||||
if ($cache->has()) {
|
||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
||||
}
|
||||
$ids = $accounts->pluck('id')->toArray();
|
||||
$current = clone $start;
|
||||
$entries = new Collection;
|
||||
while ($current < $end) {
|
||||
$balances = Steam::balancesById($ids, $current);
|
||||
$sum = $this->array_sum($balances);
|
||||
$entries->push(
|
||||
[
|
||||
'date' => clone $current,
|
||||
'net-worth' => $sum,
|
||||
]
|
||||
);
|
||||
|
||||
$current->addDays(7);
|
||||
}
|
||||
$data = $this->generator->netWorth($entries);
|
||||
|
||||
//$cache->store($data);
|
||||
|
||||
return Response::json($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Summarizes all income and expenses, per month, for a given year.
|
||||
@@ -250,4 +298,20 @@ class ReportController extends Controller
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function array_sum($array) : string
|
||||
{
|
||||
bcscale(2);
|
||||
$sum = '0';
|
||||
foreach ($array as $entry) {
|
||||
$sum = bcadd($sum, $entry);
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user