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 ;
2017-11-23 09:10:14 +01:00
use App\Jobs\ExportReportResults ;
2017-11-23 11:23:19 +01:00
use App\Jobs\RunReport ;
2017-01-30 20:40:43 +01:00
use App\Models\Account ;
2017-11-21 22:51:59 +01:00
use App\Models\ScheduledReport ;
2015-03-17 02:30:56 +01:00
use Auth ;
use Input ;
2017-01-30 20:40:43 +01:00
use Utils ;
2015-04-01 21:57:02 +02:00
use View ;
2017-11-23 09:10:14 +01:00
use Carbon ;
2017-11-23 12:15:50 +01:00
use Validator ;
2015-04-01 21:57:02 +02:00
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 )
2017-12-30 22:29:44 +01:00
-> with ([ 'clients.invoices.invoice_items' , 'clients.contacts' , 'clients.currency' ])
2015-08-11 16:38:36 +02:00
-> 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 );
}
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' );
2017-08-01 06:44:12 +02:00
$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' ,
2017-02-07 13:10:37 +01:00
'aging' ,
2017-01-22 11:09:29 +01:00
'client' ,
2018-01-24 19:26:26 +01:00
'credit' ,
2017-10-19 10:18:32 +02:00
'document' ,
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' ,
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
];
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-11-23 11:23:19 +01:00
$config = [
2017-01-22 11:09:29 +01:00
'date_field' => $dateField ,
2017-11-30 11:49:54 +01:00
'status_ids' => request () -> status_ids ,
2017-01-22 11:09:29 +01:00
'group_dates_by' => request () -> group_dates_by ,
2017-10-19 10:18:32 +02:00
'document_filter' => request () -> document_filter ,
2017-11-19 10:55:01 +01:00
'currency_type' => request () -> currency_type ,
2017-10-19 10:18:32 +02:00
'export_format' => $format ,
2017-11-23 11:23:19 +01:00
'start_date' => $params [ 'startDate' ],
'end_date' => $params [ 'endDate' ],
2017-01-22 11:09:29 +01:00
];
2017-11-23 11:23:19 +01:00
$report = dispatch ( new RunReport ( auth () -> user (), $reportType , $config , $isExport ));
$params = array_merge ( $params , $report -> exportParams );
2017-11-21 22:51:59 +01:00
switch ( $action ) {
case 'export' :
2017-11-23 09:10:14 +01:00
return dispatch ( new ExportReportResults ( auth () -> user (), $format , $reportType , $params )) -> export ( $format );
2017-11-21 22:51:59 +01:00
break ;
case 'schedule' :
2017-11-23 11:23:19 +01:00
self :: schedule ( $params , $config );
2017-11-23 12:50:11 +01:00
return redirect ( '/reports' );
2017-11-21 22:51:59 +01:00
break ;
case 'cancel_schedule' :
self :: cancelSchdule ();
2017-11-23 12:50:11 +01:00
return redirect ( '/reports' );
2017-11-21 22:51:59 +01:00
break ;
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
}
2017-11-21 22:51:59 +01:00
$params [ 'scheduledReports' ] = ScheduledReport :: scope () -> whereUserId ( auth () -> user () -> id ) -> get ();
return View :: make ( 'reports.report_builder' , $params );
}
private function schedule ( $params , $options )
{
2017-11-23 12:15:50 +01:00
$validator = Validator :: make ( request () -> all (), [
'frequency' => 'required|in:daily,weekly,biweekly,monthly' ,
'send_date' => 'required' ,
]);
if ( $validator -> fails ()) {
session () -> now ( 'message' , trans ( 'texts.scheduled_report_error' ));
} else {
$options [ 'report_type' ] = $params [ 'reportType' ];
$options [ 'range' ] = request ( 'range' );
$options [ 'start_date_offset' ] = $options [ 'range' ] ? '' : Carbon :: parse ( $params [ 'startDate' ]) -> diffInDays ( null , false ); // null,false to get the relative/non-absolute diff
$options [ 'end_date_offset' ] = $options [ 'range' ] ? '' : Carbon :: parse ( $params [ 'endDate' ]) -> diffInDays ( null , false );
unset ( $options [ 'start_date' ]);
unset ( $options [ 'end_date' ]);
unset ( $options [ 'group_dates_by' ]);
$schedule = ScheduledReport :: createNew ();
$schedule -> config = json_encode ( $options );
$schedule -> frequency = request ( 'frequency' );
$schedule -> send_date = Utils :: toSqlDate ( request ( 'send_date' ));
$schedule -> save ();
2017-11-23 12:50:11 +01:00
session () -> flash ( 'message' , trans ( 'texts.created_scheduled_report' ));
2017-11-23 12:15:50 +01:00
}
2017-11-21 22:51:59 +01:00
}
private function cancelSchdule ()
{
ScheduledReport :: scope ()
-> whereUserId ( auth () -> user () -> id )
-> wherePublicId ( request ( 'scheduled_report_id' ))
-> delete ();
2017-10-22 10:27:42 +02:00
2017-11-23 12:50:11 +01:00
session () -> flash ( 'message' , trans ( 'texts.deleted_scheduled_report' ));
2015-03-16 22:45:25 +01:00
}
}