|
|
@@ -2,50 +2,45 @@
|
|
2
|
2
|
|
|
3
|
3
|
namespace App\Services\Stampa;
|
|
4
|
4
|
|
|
5
|
|
-use App\Models\Ordine;
|
|
6
|
5
|
use App\Models\Cucina;
|
|
|
6
|
+use App\Models\Ordine;
|
|
|
7
|
+use Illuminate\Support\Facades\Log;
|
|
7
|
8
|
use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
|
|
8
|
9
|
use Mike42\Escpos\Printer;
|
|
9
|
|
-use Illuminate\Support\Facades\Log;
|
|
10
|
10
|
|
|
11
|
11
|
class CreaComandaCucina
|
|
12
|
12
|
{
|
|
13
|
|
- private const LINE_WIDTH = 42;
|
|
|
13
|
+ /** Larghezza nominale per stampante 78 mm (~48 caratteri font normale). */
|
|
|
14
|
+ private const LINE_WIDTH = 48;
|
|
|
15
|
+
|
|
14
|
16
|
private const COL_QTA_WIDTH = 4;
|
|
15
|
|
- private const COL_TOTAL_WIDTH = 10;
|
|
16
|
|
- private const LARGE_ITEM_WIDTH = 20;
|
|
17
|
17
|
|
|
18
|
18
|
/**
|
|
19
|
19
|
* Genera il buffer ESC/POS e restituisce il payload per print_job (agent: base64).
|
|
20
|
20
|
*/
|
|
21
|
21
|
public function perOrdine(Ordine $ordine, int $cucina_id): array
|
|
22
|
22
|
{
|
|
23
|
|
- $ordine->loadMissing(['righe_ordine.piatto', 'attivita', 'metodo_pagamento']);
|
|
|
23
|
+ $ordine->loadMissing(['righe_ordine.piatto', 'dispositivo']);
|
|
24
|
24
|
$cucina = Cucina::find($cucina_id);
|
|
25
|
25
|
if ($cucina === null) {
|
|
26
|
26
|
Log::error('--- CreaComandaCucina --- Cucina non trovata id: '.$cucina_id);
|
|
27
|
27
|
throw new \RuntimeException('--- CreaComandaCucina --- Cucina non trovata id: '.$cucina_id);
|
|
28
|
28
|
}
|
|
29
|
|
- $connector = new DummyPrintConnector;
|
|
30
|
|
- $printer = new Printer($connector);
|
|
31
|
29
|
|
|
32
|
|
- $nomeAttivita = $ordine->attivita?->nome ?? 'Ordine';
|
|
33
|
30
|
$info = is_array($ordine->info) ? $ordine->info : [];
|
|
34
|
|
- $cliente = $info['cliente'] ?? null;
|
|
35
|
|
- $tavolo = $info['tavolo'] ?? null;
|
|
36
|
|
- $this->printHeader($printer, $ordine, $nomeAttivita, $cliente, $tavolo, $cucina->nome ?? null);
|
|
|
31
|
+ $dataOra = $ordine->created_at?->format('d/m/Y H:i') ?? now()->format('d/m/Y H:i');
|
|
37
|
32
|
|
|
38
|
|
- if (! empty($ordine->note)) {
|
|
39
|
|
- $printer->setTextSize(1, 2);
|
|
40
|
|
- $printer->text('-> '.$this->sanitizeLine((string) $ordine->note)."\n");
|
|
41
|
|
- $printer->setTextSize(1, 1);
|
|
42
|
|
- }
|
|
|
33
|
+ $connector = new DummyPrintConnector;
|
|
|
34
|
+ $printer = new Printer($connector);
|
|
|
35
|
+
|
|
|
36
|
+ $this->printHeader($printer, $ordine, $cucina->nome ?? null, $info);
|
|
43
|
37
|
|
|
|
38
|
+ $printer->text("\n");
|
|
44
|
39
|
$printer->text($this->separator());
|
|
45
|
40
|
$this->printItemsTable($printer, $ordine, $cucina_id);
|
|
46
|
41
|
|
|
47
|
|
- $this->printQrCode($printer, route('consulta.comanda.show', ['ordine_id' => $ordine->id]));
|
|
48
|
|
-
|
|
|
42
|
+ $printer->text("\n");
|
|
|
43
|
+ $this->printFooter($printer, $ordine, $info, $dataOra);
|
|
49
|
44
|
|
|
50
|
45
|
$printer->feed(2);
|
|
51
|
46
|
$printer->cut();
|
|
|
@@ -59,25 +54,16 @@ class CreaComandaCucina
|
|
59
|
54
|
'dati' => base64_encode($bytes),
|
|
60
|
55
|
'ordine_id' => $ordine->id,
|
|
61
|
56
|
'ordine_numero' => $ordine->id,
|
|
62
|
|
- 'ordine_data' => $ordine->created_at?->format('d/m/Y H:i') ?? now()->format('d/m/Y H:i'),
|
|
|
57
|
+ 'ordine_data' => $dataOra,
|
|
63
|
58
|
];
|
|
64
|
59
|
}
|
|
65
|
60
|
|
|
66
|
|
- private function printHeader(Printer $printer, Ordine $ordine, string $nomeAttivita, ?string $cliente, ?string $tavolo, ?string $nomeCucina): void
|
|
|
61
|
+ private function printHeader(Printer $printer, Ordine $ordine, ?string $nomeCucina, array $info): void
|
|
67
|
62
|
{
|
|
68
|
|
- $nomeAttivitaHeader = mb_strtoupper($this->sanitizeLine($nomeAttivita));
|
|
69
|
|
-
|
|
70
|
63
|
$printer->setJustification(Printer::JUSTIFY_CENTER);
|
|
71
|
64
|
$printer->setEmphasis(true);
|
|
72
|
65
|
$printer->setTextSize(2, 2);
|
|
73
|
|
- $printer->text($nomeAttivitaHeader."\n");
|
|
74
|
|
- $printer->setTextSize(1, 1);
|
|
75
|
|
- $printer->setEmphasis(false);
|
|
76
|
|
- $printer->text("\n");
|
|
77
|
|
- $printer->setJustification(Printer::JUSTIFY_CENTER);
|
|
78
|
|
- $printer->setEmphasis(true);
|
|
79
|
|
- $printer->setTextSize(2, 2);
|
|
80
|
|
- $printer->text("#".$ordine->id."\n");
|
|
|
66
|
+ $printer->text('#'.$ordine->id."\n");
|
|
81
|
67
|
$printer->setTextSize(1, 1);
|
|
82
|
68
|
$printer->setEmphasis(false);
|
|
83
|
69
|
|
|
|
@@ -87,21 +73,70 @@ class CreaComandaCucina
|
|
87
|
73
|
$printer->setEmphasis(false);
|
|
88
|
74
|
}
|
|
89
|
75
|
|
|
|
76
|
+ $printer->text("\n");
|
|
90
|
77
|
$printer->setJustification(Printer::JUSTIFY_LEFT);
|
|
91
|
78
|
|
|
92
|
|
- if ($tavolo) {
|
|
93
|
|
- $printer->text("Tavolo:\n");
|
|
94
|
|
- $printer->setTextSize(2, 2);
|
|
95
|
|
- $printer->text(mb_strtoupper($this->sanitizeLine((string) $tavolo))."\n");
|
|
96
|
|
- $printer->setTextSize(1, 1);
|
|
|
79
|
+ $this->printTavoloClienteRow(
|
|
|
80
|
+ $printer,
|
|
|
81
|
+ $info['tavolo'] ?? null,
|
|
|
82
|
+ $info['cliente'] ?? null
|
|
|
83
|
+ );
|
|
|
84
|
+
|
|
|
85
|
+ $this->printExtraInfoFields($printer, $info, $ordine);
|
|
|
86
|
+ }
|
|
|
87
|
+
|
|
|
88
|
+ /**
|
|
|
89
|
+ * Tavolo (col-4) e cliente (col-8) sulla stessa riga, testo grande.
|
|
|
90
|
+ */
|
|
|
91
|
+ private function printTavoloClienteRow(Printer $printer, ?string $tavolo, ?string $cliente): void
|
|
|
92
|
+ {
|
|
|
93
|
+ $effectiveWidth = (int) (self::LINE_WIDTH / 2);
|
|
|
94
|
+ $tavoloWidth = (int) floor($effectiveWidth * 4 / 12);
|
|
|
95
|
+ $clienteWidth = $effectiveWidth - $tavoloWidth;
|
|
|
96
|
+
|
|
|
97
|
+ $tavoloText = mb_strtoupper($this->sanitizeLine((string) ($tavolo ?? '')));
|
|
|
98
|
+ $clienteText = mb_strtoupper($this->sanitizeLine((string) ($cliente ?? '')));
|
|
|
99
|
+
|
|
|
100
|
+ if ($tavoloText === '' && $clienteText === '') {
|
|
|
101
|
+ return;
|
|
97
|
102
|
}
|
|
98
|
|
- if ($cliente) {
|
|
99
|
|
- $printer->text("Cliente:\n");
|
|
100
|
|
- $printer->setTextSize(2, 2);
|
|
101
|
|
- $printer->text(mb_strtoupper($this->sanitizeLine((string) $cliente))."\n");
|
|
102
|
|
- $printer->setTextSize(1, 1);
|
|
|
103
|
+
|
|
|
104
|
+ $tavoloText = mb_substr($tavoloText, 0, $tavoloWidth);
|
|
|
105
|
+ $clienteText = mb_substr($clienteText, 0, $clienteWidth);
|
|
|
106
|
+
|
|
|
107
|
+ $printer->setEmphasis(true);
|
|
|
108
|
+ $printer->setTextSize(2, 2);
|
|
|
109
|
+ $printer->text(str_pad($tavoloText, $tavoloWidth).str_pad($clienteText, $clienteWidth)."\n");
|
|
|
110
|
+ $printer->setTextSize(1, 1);
|
|
|
111
|
+ $printer->setEmphasis(false);
|
|
|
112
|
+ }
|
|
|
113
|
+
|
|
|
114
|
+ private function printExtraInfoFields(Printer $printer, array $info, Ordine $ordine): void
|
|
|
115
|
+ {
|
|
|
116
|
+ $skip = ['cliente', 'tavolo', 'coperti'];
|
|
|
117
|
+
|
|
|
118
|
+ foreach ($info as $key => $value) {
|
|
|
119
|
+ if (in_array($key, $skip, true) || $value === null || $value === '') {
|
|
|
120
|
+ continue;
|
|
|
121
|
+ }
|
|
|
122
|
+
|
|
|
123
|
+ $label = mb_strtoupper(str_replace('_', ' ', (string) $key));
|
|
|
124
|
+ $printer->text($label.': '.$this->sanitizeLine((string) $value)."\n");
|
|
103
|
125
|
}
|
|
104
|
|
- $printer->setJustification(Printer::JUSTIFY_LEFT);
|
|
|
126
|
+
|
|
|
127
|
+ if (! empty($ordine->note) && empty($info['note_ordine'])) {
|
|
|
128
|
+ $printer->text('NOTE: '.$this->sanitizeLine((string) $ordine->note)."\n");
|
|
|
129
|
+ }
|
|
|
130
|
+ }
|
|
|
131
|
+
|
|
|
132
|
+ private function printFooter(Printer $printer, Ordine $ordine, array $info, string $dataOra): void
|
|
|
133
|
+ {
|
|
|
134
|
+ $coperti = $info['coperti'] ?? null;
|
|
|
135
|
+ $copertiDisplay = ($coperti !== null && $coperti !== '') ? (string) $coperti : 'ND';
|
|
|
136
|
+
|
|
|
137
|
+ $printer->text('Totale coperti: '.$this->sanitizeLine($copertiDisplay)."\n");
|
|
|
138
|
+ $printer->text($this->sanitizeLine($dataOra)."\n");
|
|
|
139
|
+ $printer->text($this->sanitizeLine((string) ($ordine->dispositivo?->nome ?? 'ND'))."\n");
|
|
105
|
140
|
}
|
|
106
|
141
|
|
|
107
|
142
|
private function printItemsTable(Printer $printer, Ordine $ordine, int $cucinaId): void
|
|
|
@@ -115,12 +150,14 @@ class CreaComandaCucina
|
|
115
|
150
|
throw new \RuntimeException('Nessuna riga ordine trovata per la cucina '.$cucinaId);
|
|
116
|
151
|
}
|
|
117
|
152
|
|
|
|
153
|
+ $itemWidth = (int) (self::LINE_WIDTH / 2);
|
|
|
154
|
+
|
|
118
|
155
|
$printer->setJustification(Printer::JUSTIFY_LEFT);
|
|
119
|
156
|
foreach ($righeCucina as $riga) {
|
|
120
|
157
|
$nome = $riga->piatto?->nome ?? 'Voce';
|
|
121
|
158
|
$q = max(1, (int) ($riga->quantita ?? 1));
|
|
122
|
159
|
$dishLabel = sprintf('%dx %s', $q, $this->sanitizeLine($nome));
|
|
123
|
|
- $nameLines = $this->wrapText($dishLabel, self::LARGE_ITEM_WIDTH);
|
|
|
160
|
+ $nameLines = $this->wrapText($dishLabel, $itemWidth);
|
|
124
|
161
|
|
|
125
|
162
|
$printer->setEmphasis(true);
|
|
126
|
163
|
$printer->setTextSize(2, 2);
|
|
|
@@ -138,6 +175,7 @@ class CreaComandaCucina
|
|
138
|
175
|
}
|
|
139
|
176
|
$printer->setTextSize(1, 1);
|
|
140
|
177
|
}
|
|
|
178
|
+
|
|
141
|
179
|
$printer->text("\n");
|
|
142
|
180
|
}
|
|
143
|
181
|
}
|
|
|
@@ -174,14 +212,4 @@ class CreaComandaCucina
|
|
174
|
212
|
|
|
175
|
213
|
return mb_substr($line, 0, 120);
|
|
176
|
214
|
}
|
|
177
|
|
-
|
|
178
|
|
-
|
|
179
|
|
- private function printQrCode(Printer $printer, string $value): void
|
|
180
|
|
- {
|
|
181
|
|
- try {
|
|
182
|
|
- $printer->qrCode($this->sanitizeLine($value), Printer::QR_ECLEVEL_L, 6, Printer::QR_MODEL_2);
|
|
183
|
|
- } catch (\Throwable $e) {
|
|
184
|
|
- $printer->text('[QR: '.$this->sanitizeLine($value)."]\n");
|
|
185
|
|
- }
|
|
186
|
|
- }
|
|
187
|
215
|
}
|