Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

daily help requests and offers report #273

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions app/Http/Controllers/Admin/ReportsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace App\Http\Controllers\Admin;

use App\AllocationHistory;
use App\HelpRequest;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Reports\OffersRequest;
use Carbon\Carbon;
use Carbon\CarbonInterval;
use Carbon\CarbonPeriod;
use Doctrine\DBAL\Schema\Schema;
use Faker\Provider\DateTime;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;

class ReportsController extends Controller
{
public function index()
{
return view('admin.reports');
}

/**
* @param OffersRequest $request
*
* @return \Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\StreamedResponse
*/
public function offers(OffersRequest $request) {
if (!$request->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"
]);
}
}
32 changes: 32 additions & 0 deletions app/Http/Requests/Admin/Reports/OffersRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Requests\Admin\Reports;

use Illuminate\Foundation\Http\FormRequest;

class OffersRequest extends FormRequest
{

/**
* @property string startDate
* @property string endDate
*/

/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

public function rules()
{
return [
'startDate' => 'date|required',
'endDate' => 'date|required|after:startDate|before_or_equal:today'
];
}
}
5 changes: 4 additions & 1 deletion resources/lang/ro.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
67 changes: 67 additions & 0 deletions resources/views/admin/reports.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
@extends('layouts.admin')

@section('content')
<section class="mb-5">

<h6 class="page-title font-weight-600">{{ __("Generate reports") }}</h6>
<div class="card p-3 mt-4 shadow-sm">
<span><b>{{ __("Daily created help requests and offers report") }}</b></span>
<br>
<div class="row">
<div class="mb-4 mx-sm-3">
<form class="form" id="offers-report" method="POST" action="{{ route('admin.reports.offers') }}">
@csrf <!-- {{ csrf_field() }} -->
<div class="form-group">
<label class="" for="startDate">{{ __('Starting with') }}</label>

<input
class="flatpickr flatpickr-input form-control @error('startDate') is-invalid @enderror"
type="text" id="startDate" name="startDate" value="{{ old('startDate') }}" />
@error('startDate') <span class="invalid-feedback" role="alert">{{ $message }}</span> @enderror
</div>
<div class="form-group">
<label class="" for="endDate">{{ __('Until') }}</label>
<input
class="flatpickr flatpickr-input form-control @error('endDate') is-invalid @enderror"
type="text" id="endDate" name="endDate" value="{{ old('endDate') }}" />
@error('endDate') <span class="invalid-feedback" role="alert">{{ $message }}</span> @enderror

</div>
<button type="submit" class="btn btn-primary mb-2">{{ __("Generate") }}</button>
</form>
</div>
</div>
<div class="row">
<dl class="row mx-sm-3">
<dt class="col-md-2">Zi</dt>
<dd class="col-md-9">Ziua pentru care s-au calculat informatiile</dd>
<dt class="col-md-2">Nr. Cereri</dt>
<dd class="col-md-9">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.</dd>
<dt class="col-md-2">Nr. Persoane</dt>
<dd class="col-md-9">Numarul de persoane inregistrate pe cererile de cazare din ziua curenta.</dd>
<dt class="col-md-2">Nevoie de masina</dt>
<dd class="col-md-9">Numarul de cereri de cazare in care s-a solicitat deplasare cu masina din ziua curenta.</dd>
<dt class="col-md-2">Nevoie de transport special</dt>
<dd class="col-md-9">Numarul de cereri de cazare in care s-a solicitat deplasare cu transport spcecial din ziua curenta.</dd>
<dt class="col-md-2">Nevoi speciale</dt>
<dd class="col-md-9">Numarul de cereri de cazare in care s-au mentionat nevoi speciale din ziua curenta.</dd>
<dt class="col-md-2">Oferte cazare</dt>
<dd class="col-md-9">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.</dd>
</dl>
</div>
</div>
</section>
@endsection

@section('scripts')
@parent
<script type="application/javascript">
$("#offers-report #startDate").flatpickr({
defaultDate: new Date().fp_incr(-30)
});
$("#offers-report #endDate").flatpickr({
defaultDate: "today",
maxDate: "today"
})
</script>
@endsection
3 changes: 3 additions & 0 deletions resources/views/layouts/partials/sidebars/admin.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@
<a href="{{ route('admin.user-list') }}" class="list-group-item list-group-item-action ">
<i class="fa fa-users mr-3"></i>Utilizatori
</a>
<a href="{{ route('admin.reports.index') }}" class="list-group-item list-group-item-action ">
<i class="fa fa-table mr-3"></i>Rapoarte
</a>
<!-- New buttons end -->
3 changes: 2 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');



Expand Down