2015-06-27 09:01:06 -05:00
|
|
|
<?php
|
2016-02-05 05:08:25 -06:00
|
|
|
declare(strict_types = 1);
|
2015-06-27 09:01:06 -05:00
|
|
|
namespace FireflyIII\Generator\Chart\Account;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Steam;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ChartJsAccountChartGenerator
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Generator\Chart\Account
|
|
|
|
*/
|
2016-01-28 14:50:20 -06:00
|
|
|
class ChartJsAccountChartGenerator implements AccountChartGeneratorInterface
|
2015-06-27 09:01:06 -05:00
|
|
|
{
|
|
|
|
|
2015-08-01 00:04:41 -05:00
|
|
|
/**
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function expenseAccounts(Collection $accounts, Carbon $start, Carbon $end)
|
|
|
|
{
|
|
|
|
$data = [
|
2015-12-18 09:38:50 -06:00
|
|
|
'count' => 1,
|
|
|
|
'labels' => [], 'datasets' => [[
|
|
|
|
'label' => trans('firefly.spent'),
|
|
|
|
'data' => []]]];
|
2015-08-01 00:04:41 -05:00
|
|
|
|
2015-08-16 13:26:11 -05:00
|
|
|
bcscale(2);
|
2015-08-01 00:12:03 -05:00
|
|
|
$start->subDay();
|
2015-08-16 13:26:11 -05:00
|
|
|
$ids = $this->getIdsFromCollection($accounts);
|
2015-08-01 00:04:41 -05:00
|
|
|
$startBalances = Steam::balancesById($ids, $start);
|
|
|
|
$endBalances = Steam::balancesById($ids, $end);
|
|
|
|
|
2015-08-02 00:41:47 -05:00
|
|
|
$accounts->each(
|
|
|
|
function (Account $account) use ($startBalances, $endBalances) {
|
|
|
|
$id = $account->id;
|
2015-08-16 13:26:11 -05:00
|
|
|
$startBalance = $this->isInArray($startBalances, $id);
|
|
|
|
$endBalance = $this->isInArray($endBalances, $id);
|
|
|
|
$diff = bcsub($endBalance, $startBalance);
|
2015-08-02 00:41:47 -05:00
|
|
|
$account->difference = round($diff, 2);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$accounts = $accounts->sortByDesc(
|
|
|
|
function (Account $account) {
|
|
|
|
return $account->difference;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-08-01 00:04:41 -05:00
|
|
|
foreach ($accounts as $account) {
|
2015-08-02 00:41:47 -05:00
|
|
|
if ($account->difference > 0) {
|
2015-08-01 00:04:41 -05:00
|
|
|
$data['labels'][] = $account->name;
|
2015-08-02 00:41:47 -05:00
|
|
|
$data['datasets'][0]['data'][] = $account->difference;
|
2015-08-01 00:04:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2015-06-27 09:01:06 -05:00
|
|
|
/**
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function frontpage(Collection $accounts, Carbon $start, Carbon $end)
|
|
|
|
{
|
|
|
|
// language:
|
2016-01-27 11:31:44 -06:00
|
|
|
$format = (string)trans('config.month_and_day');
|
2016-01-15 10:53:54 -06:00
|
|
|
$data = ['count' => 0, 'labels' => [], 'datasets' => [],];
|
2015-12-27 01:39:29 -06:00
|
|
|
$current = clone $start;
|
2015-06-27 09:01:06 -05:00
|
|
|
while ($current <= $end) {
|
|
|
|
$data['labels'][] = $current->formatLocalized($format);
|
|
|
|
$current->addDay();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($accounts as $account) {
|
2015-12-27 01:39:29 -06:00
|
|
|
$set = [
|
2015-06-27 09:01:06 -05:00
|
|
|
'label' => $account->name,
|
|
|
|
'fillColor' => 'rgba(220,220,220,0.2)',
|
|
|
|
'strokeColor' => 'rgba(220,220,220,1)',
|
|
|
|
'pointColor' => 'rgba(220,220,220,1)',
|
|
|
|
'pointStrokeColor' => '#fff',
|
|
|
|
'pointHighlightFill' => '#fff',
|
|
|
|
'pointHighlightStroke' => 'rgba(220,220,220,1)',
|
|
|
|
'data' => [],
|
|
|
|
];
|
2015-12-27 01:39:29 -06:00
|
|
|
$current = clone $start;
|
2015-12-27 02:40:28 -06:00
|
|
|
$range = Steam::balanceInRange($account, $start, clone $end);
|
2016-01-08 13:43:46 -06:00
|
|
|
$previous = round(array_values($range)[0], 2);
|
2015-06-27 09:01:06 -05:00
|
|
|
while ($current <= $end) {
|
2015-12-27 01:39:29 -06:00
|
|
|
$format = $current->format('Y-m-d');
|
2016-01-08 13:43:46 -06:00
|
|
|
$balance = isset($range[$format]) ? round($range[$format], 2) : $previous;
|
2015-12-27 01:39:29 -06:00
|
|
|
|
|
|
|
$set['data'][] = $balance;
|
|
|
|
$previous = $balance;
|
2015-06-27 09:01:06 -05:00
|
|
|
$current->addDay();
|
|
|
|
}
|
|
|
|
$data['datasets'][] = $set;
|
|
|
|
}
|
2015-06-28 09:20:14 -05:00
|
|
|
$data['count'] = count($data['datasets']);
|
2015-06-27 09:01:06 -05:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function single(Account $account, Carbon $start, Carbon $end)
|
|
|
|
{
|
2015-06-27 10:38:16 -05:00
|
|
|
// language:
|
2016-01-27 12:35:00 -06:00
|
|
|
$format = (string)trans('config.month_and_day');
|
2015-06-27 10:38:16 -05:00
|
|
|
|
2015-12-27 02:44:12 -06:00
|
|
|
$data = [
|
2015-06-27 10:38:16 -05:00
|
|
|
'count' => 1,
|
|
|
|
'labels' => [],
|
|
|
|
'datasets' => [
|
|
|
|
[
|
|
|
|
'label' => $account->name,
|
2016-01-15 10:53:54 -06:00
|
|
|
'data' => [],
|
|
|
|
],
|
2015-06-27 10:38:16 -05:00
|
|
|
],
|
|
|
|
];
|
2015-12-27 02:44:12 -06:00
|
|
|
$range = Steam::balanceInRange($account, $start, $end);
|
|
|
|
$current = clone $start;
|
|
|
|
$previous = array_values($range)[0];
|
2015-06-27 10:38:16 -05:00
|
|
|
|
|
|
|
while ($end >= $current) {
|
2015-12-28 00:58:40 -06:00
|
|
|
$theDate = $current->format('Y-m-d');
|
2016-02-05 01:03:26 -06:00
|
|
|
$balance = $range[$theDate] ?? $previous;
|
2015-12-27 02:44:12 -06:00
|
|
|
|
2015-06-27 10:38:16 -05:00
|
|
|
$data['labels'][] = $current->formatLocalized($format);
|
2015-12-27 02:44:12 -06:00
|
|
|
$data['datasets'][0]['data'][] = $balance;
|
|
|
|
$previous = $balance;
|
2015-06-27 10:38:16 -05:00
|
|
|
$current->addDay();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
2015-06-27 09:01:06 -05:00
|
|
|
}
|
2015-08-16 13:26:11 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Collection $collection
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getIdsFromCollection(Collection $collection)
|
|
|
|
{
|
|
|
|
$ids = [];
|
|
|
|
foreach ($collection as $entry) {
|
|
|
|
$ids[] = $entry->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique($ids);
|
|
|
|
|
|
|
|
}
|
2016-01-19 06:59:54 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $array
|
|
|
|
* @param $entryId
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function isInArray($array, $entryId)
|
|
|
|
{
|
|
|
|
if (isset($array[$entryId])) {
|
|
|
|
return $array[$entryId];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '0';
|
|
|
|
}
|
2015-06-28 01:24:12 -05:00
|
|
|
}
|