1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-18 09:04:35 +01:00
invoiceninja/app/controllers/ReportController.php

100 lines
2.7 KiB
PHP
Raw Normal View History

2013-12-15 13:55:50 +01:00
<?php
class ReportController extends \BaseController {
2013-12-16 22:27:32 +01:00
public function report()
2013-12-15 13:55:50 +01:00
{
2013-12-16 22:27:32 +01:00
if (Input::all())
{
$groupBy = Input::get('group_by');
$chartType = Input::get('chart_type');
2014-01-01 00:50:13 +01:00
$startDate = Utils::toSqlDate(Input::get('start_date'), false);
$endDate = Utils::toSqlDate(Input::get('end_date'), false);
2013-12-16 22:27:32 +01:00
}
else
{
$groupBy = 'MONTH';
$chartType = 'Bar';
2014-01-01 00:50:13 +01:00
$startDate = Utils::today(false)->modify('-3 month');
$endDate = Utils::today(false);
2013-12-16 22:27:32 +01:00
}
2013-12-15 13:55:50 +01:00
2013-12-16 22:27:32 +01:00
$padding = $groupBy == 'DAYOFYEAR' ? 'day' : ($groupBy == 'WEEK' ? 'week' : 'month');
$endDate->modify('+1 '.$padding);
$datasets = [];
$labels = [];
$maxTotals = 0;
2013-12-15 13:55:50 +01:00
2013-12-16 22:27:32 +01:00
foreach ([ENTITY_INVOICE, ENTITY_PAYMENT, ENTITY_CREDIT] as $entityType)
2013-12-15 13:55:50 +01:00
{
2013-12-16 22:27:32 +01:00
$records = DB::table($entityType.'s')
->select(DB::raw('sum(amount) as total, '.$groupBy.'('.$entityType.'_date) as '.$groupBy))
->where($entityType.'s.deleted_at', '=', null)
->where($entityType.'s.'.$entityType.'_date', '>=', $startDate->format('Y-m-d'))
->where($entityType.'s.'.$entityType.'_date', '<=', $endDate->format('Y-m-d'))
->groupBy($groupBy);
$totals = $records->lists('total');
$dates = $records->lists($groupBy);
$data = array_combine($dates, $totals);
$interval = new DateInterval('P1'.substr($groupBy, 0, 1));
$period = new DatePeriod($startDate, $interval, $endDate);
$totals = [];
foreach ($period as $d)
{
$dateFormat = $groupBy == 'DAYOFYEAR' ? 'z' : ($groupBy == 'WEEK' ? 'W' : 'n');
$date = $d->format($dateFormat);
$totals[] = isset($data[$date]) ? $data[$date] : 0;
if ($entityType == ENTITY_INVOICE)
{
$labelFormat = $groupBy == 'DAYOFYEAR' ? 'j' : ($groupBy == 'WEEK' ? 'W' : 'F');
$label = $d->format($labelFormat);
$labels[] = $label;
}
}
2013-12-15 13:55:50 +01:00
2013-12-16 22:27:32 +01:00
$max = max($totals);
if ($max > 0)
{
$datasets[] = [
'totals' => $totals,
'colors' => $entityType == ENTITY_INVOICE ? '78,205,196' : ($entityType == ENTITY_CREDIT ? '199,244,100' : '255,107,107')
];
$maxTotals = max($max, $maxTotals);
}
2013-12-15 13:55:50 +01:00
}
2013-12-16 22:27:32 +01:00
$width = (ceil( $maxTotals / 100 ) * 100) / 10;
$width = max($width, 10);
$dateTypes = [
'DAYOFYEAR' => 'Daily',
'WEEK' => 'Weekly',
'MONTH' => 'Monthly'
];
$chartTypes = [
'Bar' => 'Bar',
'Line' => 'Line'
];
2013-12-15 13:55:50 +01:00
$params = [
2013-12-16 22:27:32 +01:00
'labels' => $labels,
'datasets' => $datasets,
2013-12-15 13:55:50 +01:00
'scaleStepWidth' => $width,
2013-12-16 22:27:32 +01:00
'dateTypes' => $dateTypes,
'chartTypes' => $chartTypes,
'chartType' => $chartType,
2014-01-01 00:50:13 +01:00
'startDate' => $startDate->format(Session::get(SESSION_DATE_FORMAT)),
'endDate' => $endDate->modify('-1'.$padding)->format(Session::get(SESSION_DATE_FORMAT)),
2013-12-16 22:27:32 +01:00
'groupBy' => $groupBy
2013-12-15 13:55:50 +01:00
];
2013-12-16 22:27:32 +01:00
return View::make('reports.report_builder', $params);
2013-12-15 13:55:50 +01:00
}
}