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

228 lines
7.4 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-03-17 02:30:56 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Http\Controllers;
use App\Models\Account;
2015-03-17 02:30:56 +01:00
use Auth;
use Input;
2017-01-22 11:09:29 +01:00
use Str;
2017-01-30 20:40:43 +01:00
use Utils;
2015-04-01 21:57:02 +02:00
use View;
use Excel;
2015-04-01 21:57:02 +02:00
/**
2017-01-30 20:40:43 +01:00
* Class ReportController.
*/
2015-04-01 21:57:02 +02:00
class ReportController extends BaseController
2015-03-16 22:45:25 +01:00
{
/**
* @return \Illuminate\Contracts\View\View
*/
2015-03-16 22:45:25 +01:00
public function d3()
{
$message = '';
2015-11-10 23:07:05 +01:00
$fileName = storage_path().'/dataviz_sample.txt';
2015-03-16 22:45:25 +01:00
if (Auth::user()->account->hasFeature(FEATURE_REPORTS)) {
2015-08-11 16:38:36 +02:00
$account = Account::where('id', '=', Auth::user()->account->id)
->with(['clients.invoices.invoice_items', 'clients.contacts'])
->first();
2015-03-16 22:45:25 +01:00
$account = $account->hideFieldsForViz();
2017-04-30 11:22:31 +02:00
$clients = $account->clients;
2015-05-19 21:14:00 +02:00
} elseif (file_exists($fileName)) {
$clients = file_get_contents($fileName);
2015-03-16 22:45:25 +01:00
$message = trans('texts.sample_data');
} else {
$clients = '[]';
}
$data = [
'clients' => $clients,
'message' => $message,
];
return View::make('reports.d3', $data);
}
/**
* @return \Illuminate\Contracts\View\View
*/
2015-04-30 19:54:19 +02:00
public function showReports()
2015-03-16 22:45:25 +01:00
{
if (! Auth::user()->hasPermission('view_all')) {
return redirect('/');
}
2015-04-30 19:54:19 +02:00
$action = Input::get('action');
$format = Input::get('format');
2015-04-30 19:54:19 +02:00
2016-10-13 11:46:29 +02:00
if (Input::get('report_type')) {
2015-04-30 19:54:19 +02:00
$reportType = Input::get('report_type');
2016-02-24 21:58:42 +01:00
$dateField = Input::get('date_field');
2016-09-18 16:06:56 +02:00
$startDate = date_create(Input::get('start_date'));
$endDate = date_create(Input::get('end_date'));
2015-03-16 22:45:25 +01:00
} else {
2015-11-10 23:07:05 +01:00
$reportType = ENTITY_INVOICE;
2016-02-24 21:58:42 +01:00
$dateField = FILTER_INVOICE_DATE;
2015-03-16 22:45:25 +01:00
$startDate = Utils::today(false)->modify('-3 month');
$endDate = Utils::today(false);
}
2015-04-30 19:54:19 +02:00
$reportTypes = [
2017-03-23 16:37:22 +01:00
'activity',
'aging',
2017-01-22 11:09:29 +01:00
'client',
'document',
'expense',
2017-01-22 11:09:29 +01:00
'invoice',
'payment',
'product',
'profit_and_loss',
2017-01-22 11:09:29 +01:00
'task',
'tax_rate',
2017-03-29 20:17:05 +02:00
'quote',
2015-04-30 19:54:19 +02:00
];
2015-03-16 22:45:25 +01:00
$params = [
2016-09-18 16:06:56 +02:00
'startDate' => $startDate->format('Y-m-d'),
'endDate' => $endDate->format('Y-m-d'),
2017-01-22 11:09:29 +01:00
'reportTypes' => array_combine($reportTypes, Utils::trans($reportTypes)),
2015-04-30 19:54:19 +02:00
'reportType' => $reportType,
2015-07-07 22:08:16 +02:00
'title' => trans('texts.charts_and_reports'),
2016-09-18 16:06:56 +02:00
'account' => Auth::user()->account,
2015-03-16 22:45:25 +01:00
];
if (Auth::user()->account->hasFeature(FEATURE_REPORTS)) {
2016-09-11 16:30:23 +02:00
$isExport = $action == 'export';
2017-01-22 11:09:29 +01:00
$reportClass = '\\App\\Ninja\\Reports\\' . Str::studly($reportType) . 'Report';
$options = [
'date_field' => $dateField,
'invoice_status' => request()->invoice_status,
'group_dates_by' => request()->group_dates_by,
'document_filter' => request()->document_filter,
'export_format' => $format,
2017-01-22 11:09:29 +01:00
];
$report = new $reportClass($startDate, $endDate, $isExport, $options);
if (Input::get('report_type')) {
$report->run();
}
$params['report'] = $report;
$params = array_merge($params, $report->results());
2016-09-11 16:30:23 +02:00
if ($isExport) {
return self::export($format, $reportType, $params);
2015-11-10 23:07:05 +01:00
}
2015-11-15 10:50:21 +01:00
} else {
$params['columns'] = [];
$params['displayData'] = [];
2016-02-27 22:00:27 +01:00
$params['reportTotals'] = [];
2017-01-22 11:09:29 +01:00
$params['report'] = false;
2015-11-10 23:07:05 +01:00
}
2015-04-30 19:54:19 +02:00
return View::make('reports.chart_builder', $params);
}
/**
* @param $format
* @param $reportType
* @param $params
* @todo: Add summary to export
*/
private function export($format, $reportType, $params)
{
if (! Auth::user()->hasPermission('view_all')) {
exit;
}
$format = strtolower($format);
$data = $params['displayData'];
$columns = $params['columns'];
$totals = $params['reportTotals'];
$report = $params['report'];
$filename = "{$params['startDate']}-{$params['endDate']}_invoiceninja-".strtolower(Utils::normalizeChars(trans("texts.$reportType")))."-report";
$formats = ['csv', 'pdf', 'xlsx', 'zip'];
if (! in_array($format, $formats)) {
throw new \Exception("Invalid format request to export report");
}
2015-04-30 19:54:19 +02:00
//Get labeled header
2017-10-22 10:27:42 +02:00
$data = array_merge(
[
array_map(function($col) {
return $col['label'];
}, $report->tableHeaderArray())
],
$data
);
2016-06-07 20:50:09 +02:00
$summary = [];
if (count(array_values($totals))) {
$summary[] = array_merge([
trans("texts.totals")
], array_map(function ($key) {
return trans("texts.{$key}");
}, array_keys(array_values(array_values($totals)[0])[0])));
2016-02-23 22:32:39 +01:00
}
2015-04-30 19:54:19 +02:00
foreach ($totals as $currencyId => $each) {
foreach ($each as $dimension => $val) {
$tmp = [];
$tmp[] = Utils::getFromCache($currencyId, 'currencies')->name . (($dimension) ? ' - ' . $dimension : '');
foreach ($val as $id => $field) {
$tmp[] = Utils::formatMoney($field, $currencyId);
}
$summary[] = $tmp;
2015-04-30 19:54:19 +02:00
}
}
2017-07-30 19:18:04 +02:00
2017-10-22 10:27:42 +02:00
return Excel::create($filename, function($excel) use($report, $data, $reportType, $format, $summary) {
2017-10-22 10:27:42 +02:00
$excel->sheet(trans("texts.$reportType"), function($sheet) use($report, $data, $format, $summary) {
$sheet->setOrientation('landscape');
$sheet->freezeFirstRow();
if ($format == 'pdf') {
$sheet->setAllBorders('thin');
}
2017-10-22 10:27:42 +02:00
if ($format == 'csv') {
$sheet->rows(array_merge($data, [[]], $summary));
} else {
$sheet->rows($data);
}
// Styling header
2017-10-22 10:27:42 +02:00
$sheet->cells('A1:'.Utils::num2alpha(count($data[0])-1).'1', function($cells) {
$cells->setBackground('#777777');
$cells->setFontColor('#FFFFFF');
2017-08-03 11:18:36 +02:00
$cells->setFontSize(13);
$cells->setFontFamily('Calibri');
$cells->setFontWeight('bold');
});
2017-10-22 10:27:42 +02:00
$sheet->setAutoSize(true);
});
$excel->sheet(trans("texts.totals"), function($sheet) use($report, $summary, $format) {
$sheet->setOrientation('landscape');
$sheet->freezeFirstRow();
if ($format == 'pdf') {
$sheet->setAllBorders('thin');
}
$sheet->rows($summary);
2017-10-22 10:27:42 +02:00
// Styling header
$sheet->cells('A1:'.Utils::num2alpha(count($summary[0])-1).'1', function($cells) {
$cells->setBackground('#777777');
$cells->setFontColor('#FFFFFF');
$cells->setFontSize(13);
$cells->setFontFamily('Calibri');
$cells->setFontWeight('bold');
});
$sheet->setAutoSize(true);
});
2017-10-22 10:27:42 +02:00
})->export($format);
2015-03-16 22:45:25 +01:00
}
}