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;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class ReportController.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2015-04-01 21:57:02 +02:00
|
|
|
class ReportController extends BaseController
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2016-07-03 18:11:58 +02: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
|
|
|
|
2016-04-19 04:35:18 +02: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();
|
|
|
|
$clients = $account->clients->toJson();
|
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);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2015-04-30 19:54:19 +02:00
|
|
|
public function showReports()
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2017-02-07 16:23:55 +01:00
|
|
|
if (! Auth::user()->hasPermission('view_all')) {
|
|
|
|
return redirect('/');
|
|
|
|
}
|
|
|
|
|
2015-04-30 19:54:19 +02:00
|
|
|
$action = Input::get('action');
|
|
|
|
|
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-02-07 13:10:37 +01:00
|
|
|
'aging',
|
2017-01-22 11:09:29 +01:00
|
|
|
'client',
|
2017-02-07 13:10:37 +01:00
|
|
|
'expense',
|
2017-01-22 11:09:29 +01:00
|
|
|
'invoice',
|
|
|
|
'payment',
|
2017-02-07 13:10:37 +01:00
|
|
|
'product',
|
|
|
|
'profit_and_loss',
|
2017-01-22 11:09:29 +01:00
|
|
|
'task',
|
|
|
|
'tax_rate',
|
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
|
|
|
];
|
|
|
|
|
2016-04-19 04:35:18 +02: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,
|
|
|
|
];
|
|
|
|
$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) {
|
|
|
|
self::export($reportType, $params['displayData'], $params['columns'], $params['reportTotals']);
|
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);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $reportType
|
|
|
|
* @param $data
|
|
|
|
* @param $columns
|
|
|
|
* @param $totals
|
|
|
|
*/
|
2016-04-17 12:08:58 +02:00
|
|
|
private function export($reportType, $data, $columns, $totals)
|
2015-04-30 19:54:19 +02:00
|
|
|
{
|
2017-02-07 16:23:55 +01:00
|
|
|
if (! Auth::user()->hasPermission('view_all')) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2015-04-30 19:54:19 +02:00
|
|
|
$output = fopen('php://output', 'w') or Utils::fatalError();
|
2016-06-07 20:50:09 +02:00
|
|
|
$reportType = trans("texts.{$reportType}s");
|
2016-04-17 12:08:58 +02:00
|
|
|
$date = date('Y-m-d');
|
2016-06-07 20:50:09 +02:00
|
|
|
|
2015-04-30 19:54:19 +02:00
|
|
|
header('Content-Type:application/csv');
|
2016-04-17 12:08:58 +02:00
|
|
|
header("Content-Disposition:attachment;filename={$date}_Ninja_{$reportType}.csv");
|
2015-04-30 19:54:19 +02:00
|
|
|
|
2016-02-23 22:32:39 +01:00
|
|
|
Utils::exportData($output, $data, Utils::trans($columns));
|
2016-06-07 20:50:09 +02:00
|
|
|
|
2017-01-22 11:09:29 +01:00
|
|
|
/*
|
2016-02-23 22:32:39 +01:00
|
|
|
fwrite($output, trans('texts.totals'));
|
|
|
|
foreach ($totals as $currencyId => $fields) {
|
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
fwrite($output, ',' . trans("texts.{$key}"));
|
|
|
|
}
|
|
|
|
fwrite($output, "\n");
|
|
|
|
break;
|
|
|
|
}
|
2015-04-30 19:54:19 +02:00
|
|
|
|
2016-02-23 22:32:39 +01:00
|
|
|
foreach ($totals as $currencyId => $fields) {
|
|
|
|
$csv = Utils::getFromCache($currencyId, 'currencies')->name . ',';
|
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
$csv .= '"' . Utils::formatMoney($value, $currencyId).'",';
|
2015-04-30 19:54:19 +02:00
|
|
|
}
|
2015-11-10 23:07:05 +01:00
|
|
|
fwrite($output, $csv."\n");
|
2015-04-30 19:54:19 +02:00
|
|
|
}
|
2017-01-22 11:09:29 +01:00
|
|
|
*/
|
2015-04-30 19:54:19 +02:00
|
|
|
|
|
|
|
fclose($output);
|
|
|
|
exit;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|