From e3bf902cb60737b45c52a9264f8dccae38250d10 Mon Sep 17 00:00:00 2001 From: "beniamin.calota" Date: Wed, 3 Aug 2022 22:01:03 +0300 Subject: [PATCH] add new admin section for reports and add a new report for daily help requests and offers --- .../Controllers/Admin/ReportsController.php | 93 +++++++++++++++++++ .../Requests/Admin/Reports/OffersRequest.php | 32 +++++++ resources/lang/ro.json | 5 +- resources/views/admin/reports.blade.php | 67 +++++++++++++ .../layouts/partials/sidebars/admin.blade.php | 3 + routes/web.php | 3 +- 6 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 app/Http/Controllers/Admin/ReportsController.php create mode 100644 app/Http/Requests/Admin/Reports/OffersRequest.php create mode 100644 resources/views/admin/reports.blade.php diff --git a/app/Http/Controllers/Admin/ReportsController.php b/app/Http/Controllers/Admin/ReportsController.php new file mode 100644 index 00000000..2bcae2c1 --- /dev/null +++ b/app/Http/Controllers/Admin/ReportsController.php @@ -0,0 +1,93 @@ +validated()) { + return redirect()->back(); + } + + $start = Carbon::parse($request->startDate); + $end = Carbon::parse($request->endDate); + $interval = CarbonInterval::day(); + + $dateRange = CarbonPeriod::between($start, $end); + $dateRange->setDateInterval($interval); + + DB::unprepared(DB::raw(" + DROP TABLE IF EXISTS tmp_reports_offers_days; + CREATE TEMPORARY TABLE tmp_reports_offers_days( + day DATE, + PRIMARY KEY i_day(day) + ); + ")); + + foreach($dateRange as $day) { + DB::insert("INSERT INTO tmp_reports_offers_days VALUES (?)", [$day->format("Y-m-d")]); + } + + $dbRows = DB::select(" + SELECT + rd.day, + count(id) as requests, + IFNULL(sum(guests_number), 0) as people, + IFNULL(sum(need_car), 0) as need_car, + IFNULL(sum(need_special_transport), 0) as need_special_transport, + sum(IF(special_needs = '' OR special_needs IS NULL, 0, 1)) as special_needs, + (SELECT COUNT(*) FROM accommodations acc WHERE rd.day = DATE(acc.created_at) AND acc.deleted_at IS NULL) as accommodation_offers + FROM tmp_reports_offers_days rd + LEFT JOIN help_requests hr ON rd.day = DATE(hr.created_at) AND hr.deleted_at IS NULL + GROUP BY rd.day + "); + + $columns = ["zi", "Nr. Cereri", "Nr. Persoane", "Nevoie de masina", "Nevoie de transport special", "Nevoi speciale", "Oferte cazare"]; + + $callback = function() use ($dbRows, $columns) { + $file = fopen("php://output", "w"); + fputcsv($file, $columns); + + foreach($dbRows as $row) { + fputcsv($file, (array)$row); + } + fclose($file); + + DB::unprepared(DB::raw(" + DROP TABLE IF EXISTS tmp_reports_offers_days; + ")); + + }; + + return response()->stream($callback, 200, [ + "Content-type" => "text/csv", + "Content-Disposition" => "attachment; filename=offers_report_". Carbon::today()->format("YmdHis") .".csv", + "Pragma" => "no-cache", + "Cache-Control" => "must-revalidate, post-check=0, pre-check=0", + "Expires" => "0" + ]); + } +} diff --git a/app/Http/Requests/Admin/Reports/OffersRequest.php b/app/Http/Requests/Admin/Reports/OffersRequest.php new file mode 100644 index 00000000..f6aab96c --- /dev/null +++ b/app/Http/Requests/Admin/Reports/OffersRequest.php @@ -0,0 +1,32 @@ + 'date|required', + 'endDate' => 'date|required|after:startDate|before_or_equal:today' + ]; + } +} diff --git a/resources/lang/ro.json b/resources/lang/ro.json index 8a930e09..00ca90a0 100644 --- a/resources/lang/ro.json +++ b/resources/lang/ro.json @@ -542,5 +542,8 @@ "Provider type": "Tip proprietar", "Select provider type": "Alege tipul de proprietar", "Company": "Persoană juridică", - "Person": "Persoană fizică" + "Person": "Persoană fizică", + "Daily created help requests and offers report": "Raport cu cereri si oferte de cazare create zilnic", + "Generate reports": "Genereaza rapoarte", + "Generate": "Genereaza" } diff --git a/resources/views/admin/reports.blade.php b/resources/views/admin/reports.blade.php new file mode 100644 index 00000000..34c470bf --- /dev/null +++ b/resources/views/admin/reports.blade.php @@ -0,0 +1,67 @@ +@extends('layouts.admin') + +@section('content') +
+ +
{{ __("Generate reports") }}
+
+ {{ __("Daily created help requests and offers report") }} +
+
+
+
+ @csrf +
+ + + + @error('startDate') {{ $message }} @enderror +
+
+ + + @error('endDate') {{ $message }} @enderror + +
+ +
+
+
+
+
+
Zi
+
Ziua pentru care s-au calculat informatiile
+
Nr. Cereri
+
Numarul de cereri de cazare create in ziua curenta, mai putin cele care au fost sterse, indiferent de statusul in care se afla la momentul actual.
+
Nr. Persoane
+
Numarul de persoane inregistrate pe cererile de cazare din ziua curenta.
+
Nevoie de masina
+
Numarul de cereri de cazare in care s-a solicitat deplasare cu masina din ziua curenta.
+
Nevoie de transport special
+
Numarul de cereri de cazare in care s-a solicitat deplasare cu transport spcecial din ziua curenta.
+
Nevoi speciale
+
Numarul de cereri de cazare in care s-au mentionat nevoi speciale din ziua curenta.
+
Oferte cazare
+
Numarul de oferte de cazare adaugate in ziua curenta, mai putin cele care au fost sterse, indiferent de statusl in care se afla la momentul actual.
+
+
+
+
+@endsection + +@section('scripts') + @parent + +@endsection diff --git a/resources/views/layouts/partials/sidebars/admin.blade.php b/resources/views/layouts/partials/sidebars/admin.blade.php index fb8ea949..f0dcad14 100644 --- a/resources/views/layouts/partials/sidebars/admin.blade.php +++ b/resources/views/layouts/partials/sidebars/admin.blade.php @@ -28,4 +28,7 @@ Utilizatori + + Rapoarte + diff --git a/routes/web.php b/routes/web.php index e0e2fe19..e7e7be4a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -136,7 +136,8 @@ Route::get('/audit-logs', 'Admin\AuditLogController@index')->name('admin.auditLogs.index'); Route::get('/audit-logs/{log}', 'Admin\AuditLogController@show')->name('admin.auditLogs.show'); Route::get('/audit-logs-search', 'Admin\AuditLogController@search')->name('admin.auditLogs.search'); - + Route::get('/reports', 'Admin\ReportsController@index')->name('admin.reports.index'); + Route::post('/reports/offers', 'Admin\ReportsController@offers')->name('admin.reports.offers');