App\Ninja\Repositories\DashboardRepository::chartData PHP Method

chartData() public method

public chartData ( $account, $groupBy, $startDate, $endDate, $currencyId, $includeExpenses ) : array
$groupBy
$startDate
$endDate
return array
    public function chartData($account, $groupBy, $startDate, $endDate, $currencyId, $includeExpenses)
    {
        $accountId = $account->id;
        $startDate = date_create($startDate);
        $endDate = date_create($endDate);
        $groupBy = strtoupper($groupBy);
        if ($groupBy == 'DAY') {
            $groupBy = 'DAYOFYEAR';
        }
        $datasets = [];
        $labels = [];
        $totals = new stdClass();
        $entitTypes = [ENTITY_INVOICE, ENTITY_PAYMENT];
        if ($includeExpenses) {
            $entitTypes[] = ENTITY_EXPENSE;
        }
        foreach ($entitTypes as $entityType) {
            $data = [];
            $count = 0;
            $balance = 0;
            $records = $this->rawChartData($entityType, $account, $groupBy, $startDate, $endDate, $currencyId);
            array_map(function ($item) use(&$data, &$count, &$balance, $groupBy) {
                $data[$item->{$groupBy}] = $item->total;
                $count += $item->count;
                $balance += isset($item->balance) ? $item->balance : 0;
            }, $records);
            $padding = $groupBy == 'DAYOFYEAR' ? 'day' : ($groupBy == 'WEEK' ? 'week' : 'month');
            $endDate->modify('+1 ' . $padding);
            $interval = new DateInterval('P1' . substr($groupBy, 0, 1));
            $period = new DatePeriod($startDate, $interval, $endDate);
            $endDate->modify('-1 ' . $padding);
            $records = [];
            foreach ($period as $d) {
                $dateFormat = $groupBy == 'DAYOFYEAR' ? 'z' : ($groupBy == 'WEEK' ? 'W' : 'n');
                // MySQL returns 1-366 for DAYOFYEAR, whereas PHP returns 0-365
                $date = $groupBy == 'DAYOFYEAR' ? $d->format('Y') . ($d->format($dateFormat) + 1) : $d->format('Y' . $dateFormat);
                $records[] = isset($data[$date]) ? $data[$date] : 0;
                if ($entityType == ENTITY_INVOICE) {
                    $labels[] = $d->format('r');
                }
            }
            if ($entityType == ENTITY_INVOICE) {
                $color = '51,122,183';
            } elseif ($entityType == ENTITY_PAYMENT) {
                $color = '54,193,87';
            } elseif ($entityType == ENTITY_EXPENSE) {
                $color = '128,128,128';
            }
            $record = new stdClass();
            $record->data = $records;
            $record->label = trans("texts.{$entityType}s");
            $record->lineTension = 0;
            $record->borderWidth = 4;
            $record->borderColor = "rgba({$color}, 1)";
            $record->backgroundColor = "rgba({$color}, 0.05)";
            $datasets[] = $record;
            if ($entityType == ENTITY_INVOICE) {
                $totals->invoices = array_sum($data);
                $totals->average = $count ? round($totals->invoices / $count, 2) : 0;
                $totals->balance = $balance;
            } elseif ($entityType == ENTITY_PAYMENT) {
                $totals->revenue = array_sum($data);
            } elseif ($entityType == ENTITY_EXPENSE) {
                //$totals->profit = $totals->revenue - array_sum($data);
                $totals->expenses = array_sum($data);
            }
        }
        $data = new stdClass();
        $data->labels = $labels;
        $data->datasets = $datasets;
        $response = new stdClass();
        $response->data = $data;
        $response->totals = $totals;
        return $response;
    }