1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/controllers/ReportController.php

137 lines
3.7 KiB
PHP
Raw Normal View History

2013-12-15 13:55:50 +01:00
<?php
class ReportController extends \BaseController {
2014-10-03 01:51:21 +02:00
public function d3()
{
2014-10-05 11:35:51 +02:00
$message = '';
2014-10-03 01:51:21 +02:00
2014-10-05 11:35:51 +02:00
if (Auth::user()->account->isPro()) {
$account = Auth::user()->account;
$account = $account->with(['clients.invoices.invoice_items', 'clients.contacts'])->first();
$account = $account->hideFieldsForViz();
$clients = $account->clients->toJson();
} else if (isset($_ENV['DATA_VIZ_SAMPLE'])) {
$clients = $_ENV['DATA_VIZ_SAMPLE'];
$message = trans('texts.sample_data');
} else {
$clients = '[]';
}
2014-10-03 01:51:21 +02:00
$data = [
2014-10-05 11:35:51 +02:00
'feature' => ACCOUNT_DATA_VISUALIZATIONS,
'clients' => $clients,
'message' => $message
2014-10-03 01:51:21 +02:00
];
return View::make('reports.d3', $data);
}
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
2014-05-05 00:00:33 +02:00
$padding = $groupBy == 'DAYOFYEAR' ? 'day' : ($groupBy == 'WEEK' ? 'week' : 'month');
2013-12-16 22:27:32 +01:00
$endDate->modify('+1 '.$padding);
$datasets = [];
$labels = [];
$maxTotals = 0;
2014-05-08 21:19:33 +02:00
$width = 10;
if (Auth::user()->account->isPro())
2013-12-15 13:55:50 +01:00
{
2014-05-08 21:19:33 +02:00
foreach ([ENTITY_INVOICE, ENTITY_PAYMENT, ENTITY_CREDIT] as $entityType)
2013-12-16 22:27:32 +01:00
{
2014-05-08 21:19:33 +02:00
$records = DB::table($entityType.'s')
->select(DB::raw('sum(amount) as total, '.$groupBy.'('.$entityType.'_date) as '.$groupBy))
2014-05-20 23:40:09 +02:00
->where('account_id', '=', Auth::user()->account_id)
2014-05-08 21:19:33 +02:00
->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);
2014-05-20 23:40:09 +02:00
if ($entityType == ENTITY_INVOICE)
{
$records->where('is_quote', '=', false)
->where('is_recurring', '=', false);
}
2014-05-08 21:19:33 +02:00
$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 = [];
2013-12-16 22:27:32 +01:00
2014-05-08 21:19:33 +02:00
foreach ($period as $d)
2013-12-16 22:27:32 +01:00
{
2014-05-08 21:19:33 +02:00
$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-16 22:27:32 +01:00
}
2013-12-15 13:55:50 +01:00
2014-05-08 21:19:33 +02:00
$max = max($totals);
2013-12-16 22:27:32 +01:00
2014-05-08 21:19:33 +02:00
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-16 22:27:32 +01:00
}
2014-05-08 21:19:33 +02:00
$width = (ceil( $maxTotals / 100 ) * 100) / 10;
$width = max($width, 10);
}
2013-12-16 22:27:32 +01:00
$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)),
2014-05-09 15:28:20 +02:00
'groupBy' => $groupBy,
'feature' => ACCOUNT_CHART_BUILDER,
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
}
}