|
|
@@ -5,6 +5,8 @@ namespace App\Http\Controllers;
|
|
5
|
5
|
use Illuminate\Http\Request;
|
|
6
|
6
|
use App\Models\Attivita;
|
|
7
|
7
|
use App\Models\ChiusuraServizio;
|
|
|
8
|
+use App\Models\SaltacodaOrdine;
|
|
|
9
|
+use Barryvdh\DomPDF\Facade\Pdf;
|
|
8
|
10
|
use App\DataTables\ChiusuraServizioDataTable;
|
|
9
|
11
|
use App\DataTables\ChiusuraServizioDataTableEditor;
|
|
10
|
12
|
use App\Services\Bilancio\BilancioServizioService;
|
|
|
@@ -12,6 +14,7 @@ use Carbon\Carbon;
|
|
12
|
14
|
use Illuminate\Routing\Controllers\Middleware;
|
|
13
|
15
|
use Illuminate\Support\Facades\Auth;
|
|
14
|
16
|
use Illuminate\Support\Facades\Session;
|
|
|
17
|
+use Illuminate\Support\Facades\Log;
|
|
15
|
18
|
use App\Services\Attivita\AttivitaService;
|
|
16
|
19
|
|
|
17
|
20
|
class ChiusuraServizioController extends Controller
|
|
|
@@ -27,7 +30,7 @@ class ChiusuraServizioController extends Controller
|
|
27
|
30
|
public static function middleware(): array
|
|
28
|
31
|
{
|
|
29
|
32
|
return [
|
|
30
|
|
- new Middleware('permission:view-chiusura-servizio', only: ['index', 'show']),
|
|
|
33
|
+ new Middleware('permission:view-chiusura-servizio', only: ['index', 'show', 'exportPdf']),
|
|
31
|
34
|
new Middleware('permission:create-chiusura-servizio', only: ['chiudi_servizio']),
|
|
32
|
35
|
new Middleware('permission:create-chiusura-servizio|edit-chiusura-servizio|delete-chiusura-servizio', only: ['store', 'update', 'destroy']),
|
|
33
|
36
|
];
|
|
|
@@ -75,6 +78,23 @@ class ChiusuraServizioController extends Controller
|
|
75
|
78
|
? Carbon::parse($snapshot['periodo']['a'])
|
|
76
|
79
|
: $chiusura->chiusura_at;
|
|
77
|
80
|
|
|
|
81
|
+ // Backfill lazy dei coperti: mantiene gli snapshot storici compatibili.
|
|
|
82
|
+ // La metrica usa forPeriod() che passa da scope perIncasso (pagamenti STAFF esclusi).
|
|
|
83
|
+ if (!array_key_exists('coperti', $snapshot) && $periodoDa && $periodoA) {
|
|
|
84
|
+ try {
|
|
|
85
|
+ $report = $bilancioServizio->forPeriod((int) $chiusura->attivita_id, $periodoDa, $periodoA);
|
|
|
86
|
+ $snapshot['coperti'] = (int) ($report['totCoperti'] ?? 0);
|
|
|
87
|
+ $chiusura->info = $snapshot;
|
|
|
88
|
+ $chiusura->save();
|
|
|
89
|
+ } catch (\Throwable $e) {
|
|
|
90
|
+ Log::warning('Impossibile backfill coperti su chiusura servizio', [
|
|
|
91
|
+ 'chiusura_servizio_id' => $chiusura->id,
|
|
|
92
|
+ 'attivita_id' => $chiusura->attivita_id,
|
|
|
93
|
+ 'errore' => $e->getMessage(),
|
|
|
94
|
+ ]);
|
|
|
95
|
+ }
|
|
|
96
|
+ }
|
|
|
97
|
+
|
|
78
|
98
|
$heatmap = $bilancioServizio->heatmapFromSnapshot(
|
|
79
|
99
|
(int) $chiusura->attivita_id,
|
|
80
|
100
|
$snapshot,
|
|
|
@@ -82,6 +102,120 @@ class ChiusuraServizioController extends Controller
|
|
82
|
102
|
$periodoA
|
|
83
|
103
|
);
|
|
84
|
104
|
|
|
|
105
|
+ $staffSummary = collect();
|
|
|
106
|
+ $staffTotale = 0.0;
|
|
|
107
|
+ $staffTransazioni = 0;
|
|
|
108
|
+ $staffCoperti = 0;
|
|
|
109
|
+ $staffTotPiattiOrdinati = 0;
|
|
|
110
|
+ $staffCucineRows = collect();
|
|
|
111
|
+ $staffHeatmapHourLabels = collect();
|
|
|
112
|
+ $saltacodaFunnel = [
|
|
|
113
|
+ 'totale' => 0,
|
|
|
114
|
+ 'pre_ordine' => 0,
|
|
|
115
|
+ 'pre_carrello' => 0,
|
|
|
116
|
+ 'pre_promosso' => 0,
|
|
|
117
|
+ 'importato' => 0,
|
|
|
118
|
+ 'tasso_to_carrello' => 0.0,
|
|
|
119
|
+ 'tasso_to_importato' => 0.0,
|
|
|
120
|
+ 'carico_labels' => collect(range(0, 23))->map(fn ($h) => str_pad((string) $h, 2, '0', STR_PAD_LEFT))->values()->all(),
|
|
|
121
|
+ 'carico_creati' => array_fill(0, 24, 0),
|
|
|
122
|
+ 'carico_importati' => array_fill(0, 24, 0),
|
|
|
123
|
+ 'carico_totale_creati' => 0,
|
|
|
124
|
+ 'carico_totale_importati' => 0,
|
|
|
125
|
+ 'tempo_import_medio_min' => 0.0,
|
|
|
126
|
+ 'tempo_import_mediana_min' => 0.0,
|
|
|
127
|
+ 'tempo_import_min_min' => 0,
|
|
|
128
|
+ 'tempo_import_max_min' => 0,
|
|
|
129
|
+ 'tempo_import_sample' => 0,
|
|
|
130
|
+ ];
|
|
|
131
|
+
|
|
|
132
|
+ if ($periodoDa && $periodoA) {
|
|
|
133
|
+ $saltacodaByStato = SaltacodaOrdine::query()
|
|
|
134
|
+ ->where('attivita_id', (int) $chiusura->attivita_id)
|
|
|
135
|
+ ->whereBetween('created_at', [$periodoDa, $periodoA])
|
|
|
136
|
+ ->selectRaw('stato, COUNT(*) as totale')
|
|
|
137
|
+ ->groupBy('stato')
|
|
|
138
|
+ ->pluck('totale', 'stato');
|
|
|
139
|
+
|
|
|
140
|
+ $saltacodaTotale = (int) $saltacodaByStato->sum();
|
|
|
141
|
+ $saltacodaPreOrdine = (int) ($saltacodaByStato[SaltacodaOrdine::PRE_ORDINE] ?? 0);
|
|
|
142
|
+ $saltacodaPreCarrello = (int) ($saltacodaByStato[SaltacodaOrdine::PRE_CARRELLO] ?? 0);
|
|
|
143
|
+ $saltacodaPrePromosso = (int) ($saltacodaByStato[SaltacodaOrdine::PRE_PROMOSSO] ?? 0);
|
|
|
144
|
+ $saltacodaImportato = (int) ($saltacodaByStato[SaltacodaOrdine::IMPORTATO] ?? 0);
|
|
|
145
|
+ $saltacodaFunnel = [
|
|
|
146
|
+ 'totale' => $saltacodaTotale,
|
|
|
147
|
+ 'pre_ordine' => $saltacodaPreOrdine,
|
|
|
148
|
+ 'pre_carrello' => $saltacodaPreCarrello,
|
|
|
149
|
+ 'pre_promosso' => $saltacodaPrePromosso,
|
|
|
150
|
+ 'importato' => $saltacodaImportato,
|
|
|
151
|
+ 'tasso_to_carrello' => $saltacodaTotale > 0 ? round(($saltacodaPreCarrello / $saltacodaTotale) * 100, 1) : 0.0,
|
|
|
152
|
+ 'tasso_to_importato' => $saltacodaTotale > 0 ? round(($saltacodaImportato / $saltacodaTotale) * 100, 1) : 0.0,
|
|
|
153
|
+ ];
|
|
|
154
|
+
|
|
|
155
|
+ $saltacodaCreatiByHour = SaltacodaOrdine::query()
|
|
|
156
|
+ ->where('attivita_id', (int) $chiusura->attivita_id)
|
|
|
157
|
+ ->whereBetween('created_at', [$periodoDa, $periodoA])
|
|
|
158
|
+ ->selectRaw('HOUR(created_at) as ora, COUNT(*) as totale')
|
|
|
159
|
+ ->groupBy('ora')
|
|
|
160
|
+ ->pluck('totale', 'ora');
|
|
|
161
|
+
|
|
|
162
|
+ $saltacodaImportatiByHour = SaltacodaOrdine::query()
|
|
|
163
|
+ ->where('attivita_id', (int) $chiusura->attivita_id)
|
|
|
164
|
+ ->where('stato', SaltacodaOrdine::IMPORTATO)
|
|
|
165
|
+ ->whereBetween('updated_at', [$periodoDa, $periodoA])
|
|
|
166
|
+ ->selectRaw('HOUR(updated_at) as ora, COUNT(*) as totale')
|
|
|
167
|
+ ->groupBy('ora')
|
|
|
168
|
+ ->pluck('totale', 'ora');
|
|
|
169
|
+
|
|
|
170
|
+ $durateImport = SaltacodaOrdine::query()
|
|
|
171
|
+ ->where('attivita_id', (int) $chiusura->attivita_id)
|
|
|
172
|
+ ->where('stato', SaltacodaOrdine::IMPORTATO)
|
|
|
173
|
+ ->whereBetween('created_at', [$periodoDa, $periodoA])
|
|
|
174
|
+ ->whereBetween('updated_at', [$periodoDa, $periodoA])
|
|
|
175
|
+ ->selectRaw('TIMESTAMPDIFF(MINUTE, created_at, updated_at) as minuti')
|
|
|
176
|
+ ->pluck('minuti')
|
|
|
177
|
+ ->filter(fn ($minuti) => is_numeric($minuti) && (int) $minuti >= 0)
|
|
|
178
|
+ ->map(fn ($minuti) => (int) $minuti)
|
|
|
179
|
+ ->values();
|
|
|
180
|
+
|
|
|
181
|
+ $durateArray = $durateImport->all();
|
|
|
182
|
+
|
|
|
183
|
+ $saltacodaFunnel['carico_creati'] = collect(range(0, 23))
|
|
|
184
|
+ ->map(fn ($h) => (int) ($saltacodaCreatiByHour[(string) $h] ?? $saltacodaCreatiByHour[$h] ?? 0))
|
|
|
185
|
+ ->values()
|
|
|
186
|
+ ->all();
|
|
|
187
|
+ $saltacodaFunnel['carico_importati'] = collect(range(0, 23))
|
|
|
188
|
+ ->map(fn ($h) => (int) ($saltacodaImportatiByHour[(string) $h] ?? $saltacodaImportatiByHour[$h] ?? 0))
|
|
|
189
|
+ ->values()
|
|
|
190
|
+ ->all();
|
|
|
191
|
+ $saltacodaFunnel['carico_totale_creati'] = array_sum($saltacodaFunnel['carico_creati']);
|
|
|
192
|
+ $saltacodaFunnel['carico_totale_importati'] = array_sum($saltacodaFunnel['carico_importati']);
|
|
|
193
|
+ $saltacodaFunnel['tempo_import_sample'] = $durateImport->count();
|
|
|
194
|
+ $saltacodaFunnel['tempo_import_medio_min'] = $durateImport->isNotEmpty() ? round((float) $durateImport->avg(), 1) : 0.0;
|
|
|
195
|
+ $saltacodaFunnel['tempo_import_mediana_min'] = $durateImport->isNotEmpty() ? $this->median($durateArray) : 0.0;
|
|
|
196
|
+ $saltacodaFunnel['tempo_import_min_min'] = $durateImport->isNotEmpty() ? (int) $durateImport->min() : 0;
|
|
|
197
|
+ $saltacodaFunnel['tempo_import_max_min'] = $durateImport->isNotEmpty() ? (int) $durateImport->max() : 0;
|
|
|
198
|
+
|
|
|
199
|
+ $staffReport = $bilancioServizio->forPeriodStaff((int) $chiusura->attivita_id, $periodoDa, $periodoA);
|
|
|
200
|
+ $staffSummary = $staffReport['paymentSummary'] ?? collect();
|
|
|
201
|
+ $staffTotale = (float) ($staffReport['totaleIncasso'] ?? 0);
|
|
|
202
|
+ $staffTransazioni = (int) ($staffReport['totaleTransazioni'] ?? 0);
|
|
|
203
|
+ $staffCoperti = (int) ($staffReport['totCoperti'] ?? 0);
|
|
|
204
|
+ $staffTotPiattiOrdinati = (int) ($staffReport['totPiattiOrdinati'] ?? 0);
|
|
|
205
|
+ $staffHeatmapSeries = collect($staffReport['heatmapSeries'] ?? []);
|
|
|
206
|
+ $staffCucineRows = collect($staffReport['cucineRows'] ?? [])->values()->map(function ($cucina, $index) use ($staffHeatmapSeries) {
|
|
|
207
|
+ if (!empty($cucina['ordini_per_ora'])) {
|
|
|
208
|
+ return $cucina;
|
|
|
209
|
+ }
|
|
|
210
|
+
|
|
|
211
|
+ $orario = $staffHeatmapSeries->get($index) ?? $staffHeatmapSeries->firstWhere('name', $cucina['nome'] ?? '');
|
|
|
212
|
+ $cucina['ordini_per_ora'] = $orario['data'] ?? [];
|
|
|
213
|
+
|
|
|
214
|
+ return $cucina;
|
|
|
215
|
+ });
|
|
|
216
|
+ $staffHeatmapHourLabels = $staffReport['heatmapHourLabels'] ?? collect();
|
|
|
217
|
+ }
|
|
|
218
|
+
|
|
85
|
219
|
return view('chiusura_servizio.show', [
|
|
86
|
220
|
'chiusura' => $chiusura,
|
|
87
|
221
|
'snapshot' => $snapshot,
|
|
|
@@ -92,7 +226,16 @@ class ChiusuraServizioController extends Controller
|
|
92
|
226
|
'totaleIncasso' => (float) ($snapshot['incasso'] ?? 0),
|
|
93
|
227
|
'totaleTransazioni' => (int) ($snapshot['transazioni'] ?? 0),
|
|
94
|
228
|
'totPiattiOrdinati' => (int) ($snapshot['piatti_ordinati'] ?? 0),
|
|
|
229
|
+ 'totCoperti' => (int) ($snapshot['coperti'] ?? 0),
|
|
95
|
230
|
'heatmapHourLabels' => $heatmap['heatmapHourLabels'],
|
|
|
231
|
+ 'staffSummary' => $staffSummary,
|
|
|
232
|
+ 'staffTotale' => $staffTotale,
|
|
|
233
|
+ 'staffTransazioni' => $staffTransazioni,
|
|
|
234
|
+ 'staffCoperti' => $staffCoperti,
|
|
|
235
|
+ 'staffTotPiattiOrdinati' => $staffTotPiattiOrdinati,
|
|
|
236
|
+ 'staffCucineRows' => $staffCucineRows,
|
|
|
237
|
+ 'staffHeatmapHourLabels' => $staffHeatmapHourLabels,
|
|
|
238
|
+ 'saltacodaFunnel' => $saltacodaFunnel,
|
|
96
|
239
|
]);
|
|
97
|
240
|
}
|
|
98
|
241
|
|
|
|
@@ -122,4 +265,235 @@ class ChiusuraServizioController extends Controller
|
|
122
|
265
|
|
|
123
|
266
|
return redirect()->back()->with('success', 'Servizio chiuso con successo');
|
|
124
|
267
|
}
|
|
|
268
|
+
|
|
|
269
|
+ public function exportPdf(Request $request, BilancioServizioService $bilancioServizio)
|
|
|
270
|
+ {
|
|
|
271
|
+ $chiusura = ChiusuraServizio::query()
|
|
|
272
|
+ ->with(['user', 'attivita'])
|
|
|
273
|
+ ->where('attivita_id', Session::get('attivita_attuale'))
|
|
|
274
|
+ ->find($request->get('chiusura_servizio_id'));
|
|
|
275
|
+
|
|
|
276
|
+ if (!$chiusura) {
|
|
|
277
|
+ return redirect()->route('chiusura-servizio.index')->with('error', 'Chiusura non trovata');
|
|
|
278
|
+ }
|
|
|
279
|
+
|
|
|
280
|
+ $snapshot = is_array($chiusura->info) ? $chiusura->info : (json_decode($chiusura->info ?? '[]', true) ?: []);
|
|
|
281
|
+ $periodoDa = !empty($snapshot['periodo']['da']) ? Carbon::parse($snapshot['periodo']['da']) : null;
|
|
|
282
|
+ $periodoA = !empty($snapshot['periodo']['a'])
|
|
|
283
|
+ ? Carbon::parse($snapshot['periodo']['a'])
|
|
|
284
|
+ : $chiusura->chiusura_at;
|
|
|
285
|
+
|
|
|
286
|
+ if (!array_key_exists('coperti', $snapshot) && $periodoDa && $periodoA) {
|
|
|
287
|
+ try {
|
|
|
288
|
+ $report = $bilancioServizio->forPeriod((int) $chiusura->attivita_id, $periodoDa, $periodoA);
|
|
|
289
|
+ $snapshot['coperti'] = (int) ($report['totCoperti'] ?? 0);
|
|
|
290
|
+ $chiusura->info = $snapshot;
|
|
|
291
|
+ $chiusura->save();
|
|
|
292
|
+ } catch (\Throwable $e) {
|
|
|
293
|
+ Log::warning('Impossibile backfill coperti su export PDF chiusura servizio', [
|
|
|
294
|
+ 'chiusura_servizio_id' => $chiusura->id,
|
|
|
295
|
+ 'attivita_id' => $chiusura->attivita_id,
|
|
|
296
|
+ 'errore' => $e->getMessage(),
|
|
|
297
|
+ ]);
|
|
|
298
|
+ }
|
|
|
299
|
+ }
|
|
|
300
|
+
|
|
|
301
|
+ $heatmap = $bilancioServizio->heatmapFromSnapshot(
|
|
|
302
|
+ (int) $chiusura->attivita_id,
|
|
|
303
|
+ $snapshot,
|
|
|
304
|
+ $periodoDa,
|
|
|
305
|
+ $periodoA
|
|
|
306
|
+ );
|
|
|
307
|
+
|
|
|
308
|
+ $staffSummary = collect();
|
|
|
309
|
+ $staffTotale = 0.0;
|
|
|
310
|
+ $staffTransazioni = 0;
|
|
|
311
|
+ $staffCoperti = 0;
|
|
|
312
|
+ $staffTotPiattiOrdinati = 0;
|
|
|
313
|
+ $staffCucineRows = collect();
|
|
|
314
|
+ $staffHeatmapHourLabels = collect();
|
|
|
315
|
+ $saltacodaFunnel = [
|
|
|
316
|
+ 'totale' => 0,
|
|
|
317
|
+ 'pre_ordine' => 0,
|
|
|
318
|
+ 'pre_carrello' => 0,
|
|
|
319
|
+ 'pre_promosso' => 0,
|
|
|
320
|
+ 'importato' => 0,
|
|
|
321
|
+ 'tasso_to_carrello' => 0.0,
|
|
|
322
|
+ 'tasso_to_importato' => 0.0,
|
|
|
323
|
+ 'carico_labels' => collect(range(0, 23))->map(fn ($h) => str_pad((string) $h, 2, '0', STR_PAD_LEFT))->values()->all(),
|
|
|
324
|
+ 'carico_creati' => array_fill(0, 24, 0),
|
|
|
325
|
+ 'carico_importati' => array_fill(0, 24, 0),
|
|
|
326
|
+ 'carico_totale_creati' => 0,
|
|
|
327
|
+ 'carico_totale_importati' => 0,
|
|
|
328
|
+ 'tempo_import_medio_min' => 0.0,
|
|
|
329
|
+ 'tempo_import_mediana_min' => 0.0,
|
|
|
330
|
+ 'tempo_import_min_min' => 0,
|
|
|
331
|
+ 'tempo_import_max_min' => 0,
|
|
|
332
|
+ 'tempo_import_sample' => 0,
|
|
|
333
|
+ ];
|
|
|
334
|
+
|
|
|
335
|
+ if ($periodoDa && $periodoA) {
|
|
|
336
|
+ $saltacodaByStato = SaltacodaOrdine::query()
|
|
|
337
|
+ ->where('attivita_id', (int) $chiusura->attivita_id)
|
|
|
338
|
+ ->whereBetween('created_at', [$periodoDa, $periodoA])
|
|
|
339
|
+ ->selectRaw('stato, COUNT(*) as totale')
|
|
|
340
|
+ ->groupBy('stato')
|
|
|
341
|
+ ->pluck('totale', 'stato');
|
|
|
342
|
+
|
|
|
343
|
+ $saltacodaTotale = (int) $saltacodaByStato->sum();
|
|
|
344
|
+ $saltacodaPreOrdine = (int) ($saltacodaByStato[SaltacodaOrdine::PRE_ORDINE] ?? 0);
|
|
|
345
|
+ $saltacodaPreCarrello = (int) ($saltacodaByStato[SaltacodaOrdine::PRE_CARRELLO] ?? 0);
|
|
|
346
|
+ $saltacodaPrePromosso = (int) ($saltacodaByStato[SaltacodaOrdine::PRE_PROMOSSO] ?? 0);
|
|
|
347
|
+ $saltacodaImportato = (int) ($saltacodaByStato[SaltacodaOrdine::IMPORTATO] ?? 0);
|
|
|
348
|
+ $saltacodaFunnel = [
|
|
|
349
|
+ 'totale' => $saltacodaTotale,
|
|
|
350
|
+ 'pre_ordine' => $saltacodaPreOrdine,
|
|
|
351
|
+ 'pre_carrello' => $saltacodaPreCarrello,
|
|
|
352
|
+ 'pre_promosso' => $saltacodaPrePromosso,
|
|
|
353
|
+ 'importato' => $saltacodaImportato,
|
|
|
354
|
+ 'tasso_to_carrello' => $saltacodaTotale > 0 ? round(($saltacodaPreCarrello / $saltacodaTotale) * 100, 1) : 0.0,
|
|
|
355
|
+ 'tasso_to_importato' => $saltacodaTotale > 0 ? round(($saltacodaImportato / $saltacodaTotale) * 100, 1) : 0.0,
|
|
|
356
|
+ ];
|
|
|
357
|
+
|
|
|
358
|
+ $saltacodaCreatiByHour = SaltacodaOrdine::query()
|
|
|
359
|
+ ->where('attivita_id', (int) $chiusura->attivita_id)
|
|
|
360
|
+ ->whereBetween('created_at', [$periodoDa, $periodoA])
|
|
|
361
|
+ ->selectRaw('HOUR(created_at) as ora, COUNT(*) as totale')
|
|
|
362
|
+ ->groupBy('ora')
|
|
|
363
|
+ ->pluck('totale', 'ora');
|
|
|
364
|
+
|
|
|
365
|
+ $saltacodaImportatiByHour = SaltacodaOrdine::query()
|
|
|
366
|
+ ->where('attivita_id', (int) $chiusura->attivita_id)
|
|
|
367
|
+ ->where('stato', SaltacodaOrdine::IMPORTATO)
|
|
|
368
|
+ ->whereBetween('updated_at', [$periodoDa, $periodoA])
|
|
|
369
|
+ ->selectRaw('HOUR(updated_at) as ora, COUNT(*) as totale')
|
|
|
370
|
+ ->groupBy('ora')
|
|
|
371
|
+ ->pluck('totale', 'ora');
|
|
|
372
|
+
|
|
|
373
|
+ $durateImport = SaltacodaOrdine::query()
|
|
|
374
|
+ ->where('attivita_id', (int) $chiusura->attivita_id)
|
|
|
375
|
+ ->where('stato', SaltacodaOrdine::IMPORTATO)
|
|
|
376
|
+ ->whereBetween('created_at', [$periodoDa, $periodoA])
|
|
|
377
|
+ ->whereBetween('updated_at', [$periodoDa, $periodoA])
|
|
|
378
|
+ ->selectRaw('TIMESTAMPDIFF(MINUTE, created_at, updated_at) as minuti')
|
|
|
379
|
+ ->pluck('minuti')
|
|
|
380
|
+ ->filter(fn ($minuti) => is_numeric($minuti) && (int) $minuti >= 0)
|
|
|
381
|
+ ->map(fn ($minuti) => (int) $minuti)
|
|
|
382
|
+ ->values();
|
|
|
383
|
+
|
|
|
384
|
+ $durateArray = $durateImport->all();
|
|
|
385
|
+
|
|
|
386
|
+ $saltacodaFunnel['carico_creati'] = collect(range(0, 23))
|
|
|
387
|
+ ->map(fn ($h) => (int) ($saltacodaCreatiByHour[(string) $h] ?? $saltacodaCreatiByHour[$h] ?? 0))
|
|
|
388
|
+ ->values()
|
|
|
389
|
+ ->all();
|
|
|
390
|
+ $saltacodaFunnel['carico_importati'] = collect(range(0, 23))
|
|
|
391
|
+ ->map(fn ($h) => (int) ($saltacodaImportatiByHour[(string) $h] ?? $saltacodaImportatiByHour[$h] ?? 0))
|
|
|
392
|
+ ->values()
|
|
|
393
|
+ ->all();
|
|
|
394
|
+ $saltacodaFunnel['carico_totale_creati'] = array_sum($saltacodaFunnel['carico_creati']);
|
|
|
395
|
+ $saltacodaFunnel['carico_totale_importati'] = array_sum($saltacodaFunnel['carico_importati']);
|
|
|
396
|
+ $saltacodaFunnel['tempo_import_sample'] = $durateImport->count();
|
|
|
397
|
+ $saltacodaFunnel['tempo_import_medio_min'] = $durateImport->isNotEmpty() ? round((float) $durateImport->avg(), 1) : 0.0;
|
|
|
398
|
+ $saltacodaFunnel['tempo_import_mediana_min'] = $durateImport->isNotEmpty() ? $this->median($durateArray) : 0.0;
|
|
|
399
|
+ $saltacodaFunnel['tempo_import_min_min'] = $durateImport->isNotEmpty() ? (int) $durateImport->min() : 0;
|
|
|
400
|
+ $saltacodaFunnel['tempo_import_max_min'] = $durateImport->isNotEmpty() ? (int) $durateImport->max() : 0;
|
|
|
401
|
+
|
|
|
402
|
+ $staffReport = $bilancioServizio->forPeriodStaff((int) $chiusura->attivita_id, $periodoDa, $periodoA);
|
|
|
403
|
+ $staffSummary = $staffReport['paymentSummary'] ?? collect();
|
|
|
404
|
+ $staffTotale = (float) ($staffReport['totaleIncasso'] ?? 0);
|
|
|
405
|
+ $staffTransazioni = (int) ($staffReport['totaleTransazioni'] ?? 0);
|
|
|
406
|
+ $staffCoperti = (int) ($staffReport['totCoperti'] ?? 0);
|
|
|
407
|
+ $staffTotPiattiOrdinati = (int) ($staffReport['totPiattiOrdinati'] ?? 0);
|
|
|
408
|
+ $staffHeatmapSeries = collect($staffReport['heatmapSeries'] ?? []);
|
|
|
409
|
+ $staffCucineRows = collect($staffReport['cucineRows'] ?? [])->values()->map(function ($cucina, $index) use ($staffHeatmapSeries) {
|
|
|
410
|
+ if (!empty($cucina['ordini_per_ora'])) {
|
|
|
411
|
+ return $cucina;
|
|
|
412
|
+ }
|
|
|
413
|
+
|
|
|
414
|
+ $orario = $staffHeatmapSeries->get($index) ?? $staffHeatmapSeries->firstWhere('name', $cucina['nome'] ?? '');
|
|
|
415
|
+ $cucina['ordini_per_ora'] = $orario['data'] ?? [];
|
|
|
416
|
+
|
|
|
417
|
+ return $cucina;
|
|
|
418
|
+ });
|
|
|
419
|
+ $staffHeatmapHourLabels = $staffReport['heatmapHourLabels'] ?? collect();
|
|
|
420
|
+ }
|
|
|
421
|
+
|
|
|
422
|
+ $payload = [
|
|
|
423
|
+ 'chiusura' => $chiusura,
|
|
|
424
|
+ 'snapshot' => $snapshot,
|
|
|
425
|
+ 'periodoDa' => $periodoDa,
|
|
|
426
|
+ 'periodoA' => $periodoA,
|
|
|
427
|
+ 'paymentSummary' => collect($snapshot['pagamenti_per_metodo'] ?? []),
|
|
|
428
|
+ 'cucineRows' => $bilancioServizio->cucineWithOrario($snapshot, $heatmap['heatmapSeries']),
|
|
|
429
|
+ 'totaleIncasso' => (float) ($snapshot['incasso'] ?? 0),
|
|
|
430
|
+ 'totaleTransazioni' => (int) ($snapshot['transazioni'] ?? 0),
|
|
|
431
|
+ 'totPiattiOrdinati' => (int) ($snapshot['piatti_ordinati'] ?? 0),
|
|
|
432
|
+ 'totCoperti' => (int) ($snapshot['coperti'] ?? 0),
|
|
|
433
|
+ 'staffSummary' => $staffSummary,
|
|
|
434
|
+ 'staffTotale' => $staffTotale,
|
|
|
435
|
+ 'staffTransazioni' => $staffTransazioni,
|
|
|
436
|
+ 'staffCoperti' => $staffCoperti,
|
|
|
437
|
+ 'staffTotPiattiOrdinati' => $staffTotPiattiOrdinati,
|
|
|
438
|
+ 'staffCucineRows' => $staffCucineRows,
|
|
|
439
|
+ 'staffHeatmapHourLabels' => $staffHeatmapHourLabels,
|
|
|
440
|
+ 'saltacodaFunnel' => $saltacodaFunnel,
|
|
|
441
|
+ 'generatoIl' => now()->format('d/m/Y H:i'),
|
|
|
442
|
+ ];
|
|
|
443
|
+
|
|
|
444
|
+ if ($request->boolean('preview')) {
|
|
|
445
|
+ $payload['previewMode'] = true;
|
|
|
446
|
+ return view('chiusura_servizio.pdf', $payload);
|
|
|
447
|
+ }
|
|
|
448
|
+
|
|
|
449
|
+ $pdf = Pdf::loadView('chiusura_servizio.pdf', $payload)->setPaper('a4', 'portrait');
|
|
|
450
|
+ $dompdf = $pdf->getDomPDF();
|
|
|
451
|
+ $dompdf->render();
|
|
|
452
|
+
|
|
|
453
|
+ $canvas = $dompdf->getCanvas();
|
|
|
454
|
+ $fontMetrics = $dompdf->getFontMetrics();
|
|
|
455
|
+ $fontRegular = $fontMetrics->getFont('DejaVu Sans', 'normal');
|
|
|
456
|
+ $footerLeft = 'Generato: ' . now()->format('d/m/Y H:i');
|
|
|
457
|
+ $headerRight = 'Chiusura servizio #' . $chiusura->id;
|
|
|
458
|
+
|
|
|
459
|
+ $canvas->page_script(function ($pageNumber, $pageCount, $canvas, $fontMetrics) use ($fontRegular, $footerLeft, $headerRight) {
|
|
|
460
|
+ $width = $canvas->get_width();
|
|
|
461
|
+ $height = $canvas->get_height();
|
|
|
462
|
+
|
|
|
463
|
+ $canvas->text(28, $height - 24, $footerLeft, $fontRegular, 8, [0.4, 0.44, 0.5]);
|
|
|
464
|
+ $pageText = sprintf('%d / %d', $pageNumber, $pageCount);
|
|
|
465
|
+ $pageWidth = $fontMetrics->getTextWidth($pageText, $fontRegular, 8);
|
|
|
466
|
+ $canvas->text($width - 28 - $pageWidth, $height - 24, $pageText, $fontRegular, 8, [0.4, 0.44, 0.5]);
|
|
|
467
|
+
|
|
|
468
|
+ if ($pageNumber > 1) {
|
|
|
469
|
+ $headerWidth = $fontMetrics->getTextWidth($headerRight, $fontRegular, 8);
|
|
|
470
|
+ $canvas->text($width - 28 - $headerWidth, 22, $headerRight, $fontRegular, 8, [0.2, 0.24, 0.3]);
|
|
|
471
|
+ }
|
|
|
472
|
+ });
|
|
|
473
|
+
|
|
|
474
|
+ $filename = sprintf('chiusura-servizio-%d-%s.pdf', $chiusura->id, now()->format('Ymd-His'));
|
|
|
475
|
+
|
|
|
476
|
+ return response($dompdf->output(), 200, [
|
|
|
477
|
+ 'Content-Type' => 'application/pdf',
|
|
|
478
|
+ 'Content-Disposition' => 'attachment; filename="' . $filename . '"',
|
|
|
479
|
+ ]);
|
|
|
480
|
+ }
|
|
|
481
|
+
|
|
|
482
|
+ private function median(array $values): float
|
|
|
483
|
+ {
|
|
|
484
|
+ if (empty($values)) {
|
|
|
485
|
+ return 0.0;
|
|
|
486
|
+ }
|
|
|
487
|
+
|
|
|
488
|
+ sort($values);
|
|
|
489
|
+ $count = count($values);
|
|
|
490
|
+ $middle = intdiv($count, 2);
|
|
|
491
|
+
|
|
|
492
|
+ if ($count % 2 === 0) {
|
|
|
493
|
+ return round((($values[$middle - 1] + $values[$middle]) / 2), 1);
|
|
|
494
|
+ }
|
|
|
495
|
+
|
|
|
496
|
+ return round((float) $values[$middle], 1);
|
|
|
497
|
+ }
|
|
|
498
|
+
|
|
125
|
499
|
}
|