Просмотр исходного кода

Stampe: comande cucina, check stampanti e biglietto ritiro.

Se lo scontrino e gia in corso le comande partono comunque; senza stampante si logga e non si crasha.

Co-authored-by: Cursor <cursoragent@cursor.com>
marcofalabretti 2 недель назад
Родитель
Сommit
56122fe46a

+ 9
- 5
app/DataTables/CucinaDataTable.php Просмотреть файл

@@ -79,11 +79,12 @@ class CucinaDataTable extends DataTable
79 79
      */
80 80
     public function query(Cucina $model): QueryBuilder
81 81
     {
82
-        if($this->attivita_id){
83
-            return $model->newQuery()->where('attivita_id', $this->attivita_id)->orderBy('nome', 'asc');
84
-        }else{
82
+        $attivitaId = (int) $this->attivita_id;
83
+        if ($attivitaId <= 0) {
85 84
             return $model->newQuery()->whereRaw('1 = 0');
86 85
         }
86
+
87
+        return $model->newQuery()->where('attivita_id', $attivitaId)->orderBy('nome', 'asc');
87 88
     }
88 89
 
89 90
     /**
@@ -117,13 +118,14 @@ class CucinaDataTable extends DataTable
117 118
                         Editor::make()
118 119
                         ->ajax(route('cucina.store'))
119 120
                         ->fields([
120
-                          Fields\Hidden::make('attivita_id')->label('Attività')->default($this->attivita_id),
121
+                          Fields\Hidden::make('attivita_id')->label('Attività')->default((int) $this->attivita_id),
121 122
                           Fields\Boolean::make('is_attiva')->label('Attiva')->default(true),  
122 123
                           Fields\Text::make('nome')->label('Nome'),
123 124
                           Fields\Text::make('descrizione')->label('Descrizione'),
124
-                          Fields\Select2::make('stampante_id')->label('Stampante')->options(Dispositivo::where('tipo', Dispositivo::STAMPANTE)->pluck('id', 'nome')),
125
+                          Fields\Select2::make('stampante_id')->label('Stampante')->options(Dispositivo::where('tipo', Dispositivo::STAMPANTE)->where('attivita_id', (int) $this->attivita_id)->pluck('id', 'nome')),
125 126
                           Fields\Text::make('colore')->label('Colore')->set('attr', ['type' => 'color']),
126 127
                           Fields\Boolean::make('coperto_come_piatto')->label('Ogni piatto ordinato corrisponde ad un coperto'),  
128
+                          Fields\Boolean::make('stampa_ritiro')->label('Stampa biglietto per ritiro ordine'),
127 129
                             // Fields\Image::make('immagine')->label('Immagine'),
128 130
                             // Fields\Select2::make('cucina_id')->label('Cucina')->options(Cucina::where('is_attiva', true)->pluck('id', 'nome')),
129 131
                         ])
@@ -153,6 +155,7 @@ class CucinaDataTable extends DataTable
153 155
         Column::make('badge_colore')->title('Cucina')->addClass('text-left')->responsivePriority(1),
154 156
         Column::computed('piatti_count')->title('Piatti')->addClass('text-center'),
155 157
         Column::make('coperto_come_piatto')->visible(false),
158
+        Column::make('stampa_ritiro')->visible(false),
156 159
         Column::computed('action')
157 160
         ->title('')
158 161
         ->exportable(false)
@@ -174,6 +177,7 @@ class CucinaDataTable extends DataTable
174 177
             Column::make('stampante_id')->data('stampante_display')->title('Stampante')->addClass('text-center'),
175 178
             // Column::make('cucina_id')->data('cucina_id_display')->title('Cucina')->addClass('text-center'),
176 179
             Column::make('coperto_come_piatto')->visible(false),
180
+            Column::make('stampa_ritiro')->visible(false),
177 181
             Column::make('action')
178 182
             ->title('')
179 183
             ->exportable(false)

+ 37
- 12
app/DataTables/CucinaDataTableEditor.php Просмотреть файл

@@ -3,13 +3,11 @@
3 3
 namespace App\DataTables;
4 4
 
5 5
 use App\Models\Cucina;
6
+use App\Models\Dispositivo;
7
+use App\Services\Attivita\AttivitaService;
6 8
 use Illuminate\Database\Eloquent\Model;
7
-use Illuminate\Validation\Rule;
8 9
 use Yajra\DataTables\DataTablesEditor;
9
-use Illuminate\Http\Request;
10
-use Illuminate\Validation\ValidationException;
11
-use Storage;
12
-use Illuminate\Support\Str;
10
+use Illuminate\Support\Facades\Session;
13 11
 
14 12
 class CucinaDataTableEditor extends DataTablesEditor
15 13
 {
@@ -77,26 +75,53 @@ class CucinaDataTableEditor extends DataTablesEditor
77 75
 
78 76
   public function creating(Model $model, array $data): array
79 77
   {
80
-    // $model->roles()->sync([$data['ruolo_id']]);
78
+    $data['attivita_id'] = Session::get('attivita_attuale');
79
+    AttivitaService::garantisciAttivita($data['attivita_id']);
80
+    $this->garantisciStampanteDiQuestaAttivita($data['stampante_id'] ?? null, $data['attivita_id']);
81
+
81 82
     $data['is_attiva'] = isset($data['is_attiva']) ? true : false;
82
-    
83 83
     $data['info']['coperto_come_piatto'] = filter_var($data['coperto_come_piatto'] ?? false, FILTER_VALIDATE_BOOLEAN);
84
-    $data['info']['colore'] = $data['colore'];
84
+    $data['info']['stampa_ritiro'] = filter_var($data['stampa_ritiro'] ?? false, FILTER_VALIDATE_BOOLEAN);
85
+    $data['info']['colore'] = $data['colore'] ?? null;
85 86
 
86
-    // $data['info']['coperto_come_piatto'] = isset($data['coperto_come_piatto']) ? true : false;
87 87
     return $data;
88 88
   }
89 89
 
90 90
   public function updating(Model $model, array $data): array
91 91
   {
92
-    // dd($data['ruolo']);
93
-    // $model->roles()->sync([$data['ruolo_id']]);
92
+    AttivitaService::garantisciAttivita($model->attivita_id);
93
+    unset($data['attivita_id']);
94
+    $this->garantisciStampanteDiQuestaAttivita($data['stampante_id'] ?? $model->stampante_id, $model->attivita_id);
95
+
94 96
     $data['is_attiva'] = isset($data['is_attiva']) ? true : false;
95
-    $data['info']['colore'] = $data['colore'];
97
+    $data['info']['colore'] = $data['colore'] ?? ($model->info['colore'] ?? null);
96 98
     $data['info']['coperto_come_piatto'] = filter_var($data['coperto_come_piatto'] ?? false, FILTER_VALIDATE_BOOLEAN);
99
+    $data['info']['stampa_ritiro'] = filter_var($data['stampa_ritiro'] ?? false, FILTER_VALIDATE_BOOLEAN);
100
+    return $data;
101
+  }
102
+
103
+  public function deleting(Model $model, array $data): array
104
+  {
105
+    AttivitaService::garantisciAttivita($model->attivita_id);
97 106
 
98 107
     return $data;
99 108
   }
109
+
110
+  private function garantisciStampanteDiQuestaAttivita($stampanteId, $attivitaId): void
111
+  {
112
+    if (! $stampanteId) {
113
+      return;
114
+    }
115
+
116
+    $stampante = Dispositivo::find($stampanteId);
117
+    if (
118
+      ! $stampante
119
+      || (int) $stampante->attivita_id !== (int) $attivitaId
120
+      || $stampante->tipo !== Dispositivo::STAMPANTE
121
+    ) {
122
+      abort(404);
123
+    }
124
+  }
100 125
   public function messages(): array
101 126
   {
102 127
     return $this->messages;

+ 16
- 1
app/DataTables/PrintJobDataTable.php Просмотреть файл

@@ -67,7 +67,22 @@ class PrintJobDataTable extends DataTable
67 67
      */
68 68
     public function query(PrintJob $model): QueryBuilder
69 69
     {
70
-        return $model->newQuery()->orderBy('created_at', 'desc');
70
+        $attivitaId = (int) session('attivita_attuale');
71
+        if ($attivitaId <= 0) {
72
+            return $model->newQuery()->whereRaw('1 = 0');
73
+        }
74
+
75
+        return $model->newQuery()
76
+            ->where(function ($q) use ($attivitaId) {
77
+                $q->whereHas('dispositivo', function ($d) use ($attivitaId) {
78
+                    $d->where('attivita_id', $attivitaId);
79
+                })->orWhereHas('stampante', function ($d) use ($attivitaId) {
80
+                    $d->where('attivita_id', $attivitaId);
81
+                })->orWhereHas('ordine', function ($o) use ($attivitaId) {
82
+                    $o->where('attivita_id', $attivitaId);
83
+                });
84
+            })
85
+            ->orderBy('created_at', 'desc');
71 86
     }
72 87
 
73 88
     /**

+ 3
- 1
app/Http/Controllers/CucinaController.php Просмотреть файл

@@ -2,6 +2,8 @@
2 2
 
3 3
 namespace App\Http\Controllers;
4 4
 
5
+
6
+use Illuminate\Routing\Controllers\Middleware;
5 7
 use Illuminate\Http\Request;
6 8
 use App\Models\Cucina;
7 9
 use App\DataTables\CucinaDataTable;
@@ -23,7 +25,7 @@ class CucinaController extends Controller
23 25
     public static function middleware(): array
24 26
     {
25 27
         return [
26
-            new Middleware('permission:view-cucina', only: ['index']),
28
+            new Middleware('permission:view-cucina', only: ['index', 'dashboard']),
27 29
             new Middleware('permission:create-cucina|edit-cucina|delete-cucina', only: ['store', 'update', 'destroy']),
28 30
         ];
29 31
     }

+ 12
- 2
app/Http/Controllers/PrintJobController.php Просмотреть файл

@@ -2,11 +2,14 @@
2 2
 
3 3
 namespace App\Http\Controllers;
4 4
 
5
+
6
+use Illuminate\Routing\Controllers\Middleware;
5 7
 use Illuminate\Http\Request;
6 8
 use App\Models\PrintJob;
7 9
 use App\DataTables\PrintJobDataTable;
8 10
 use App\DataTables\PrintJobDataTableEditor;
9 11
 use Illuminate\Support\Facades\Auth;
12
+use App\Services\Attivita\AttivitaService;
10 13
 
11 14
 class PrintJobController extends Controller
12 15
 {
@@ -21,13 +24,14 @@ class PrintJobController extends Controller
21 24
     public static function middleware(): array
22 25
     {
23 26
         return [
24
-            new Middleware('permission:view-print-job', only: ['index']),
27
+            new Middleware('permission:view-print-job', only: ['index', 'show']),
25 28
             new Middleware('permission:create-print-job|edit-print-job|delete-print-job', only: ['store', 'update', 'destroy']),
26 29
         ];
27 30
     }
28 31
     
29 32
     public function index(PrintJobDataTable $dataTable)
30 33
     {
34
+        $dataTable->attivita_id = session('attivita_attuale');
31 35
         return $dataTable->render('print_job.index');
32 36
     }
33 37
     public function store(PrintJobDataTableEditor $editor)
@@ -51,7 +55,13 @@ class PrintJobController extends Controller
51 55
     }
52 56
     public function show(Request $request)
53 57
     {
54
-        $print_job = PrintJob::find($request->get('print_job_id'));
58
+        $print_job = PrintJob::with(['dispositivo', 'stampante'])->find($request->get('print_job_id'));
59
+        if (! $print_job) {
60
+            abort(404);
61
+        }
62
+        AttivitaService::garantisciAttivita(
63
+            $print_job->dispositivo?->attivita_id ?? $print_job->stampante?->attivita_id
64
+        );
55 65
         return view('print_job.show', compact('print_job'));
56 66
     }
57 67
 }

+ 27
- 1
app/Jobs/ElaboraJobStampa.php Просмотреть файл

@@ -10,9 +10,12 @@ use Illuminate\Queue\InteractsWithQueue;
10 10
 use Illuminate\Queue\SerializesModels;
11 11
 use App\Services\Stampa\CreaScontrino;
12 12
 use App\Services\Stampa\CreaComandaCucina;
13
+use App\Services\Stampa\CreaCupon;
14
+use App\Models\Cupon;
13 15
 use Throwable;
14 16
 use Illuminate\Support\Facades\Log;
15 17
 
18
+
16 19
 class ElaboraJobStampa implements ShouldQueue
17 20
 {
18 21
     use Dispatchable;
@@ -63,7 +66,30 @@ class ElaboraJobStampa implements ShouldQueue
63 66
                     Log::error('Print job senza cucina associata.');
64 67
                     throw new \RuntimeException('Print job senza cucina associata.');
65 68
                 }
66
-                $payload = array_merge($payload, app(CreaComandaCucina::class)->perOrdine($job->ordine, $cucina_id));
69
+                $payload = array_merge($payload, app(CreaComandaCucina::class)->perOrdine($job->ordine, (int) $cucina_id));
70
+        } elseif (str_starts_with($job->tipo, PrintJob::TIPO_COMANDA_RITIRO.'_')) {
71
+            if ($job->ordine === null) {
72
+                Log::error('Print job senza ordine associato.');
73
+                throw new \RuntimeException('Print job senza ordine associato.');
74
+            }
75
+
76
+            $prefix = PrintJob::TIPO_COMANDA_RITIRO.'_';
77
+            $cucina_id = substr($job->tipo, strlen($prefix));
78
+
79
+            if ($cucina_id === '' || ! is_numeric($cucina_id)) {
80
+                Log::error('Print job ritiro senza cucina associata.');
81
+                throw new \RuntimeException('Print job ritiro senza cucina associata.');
82
+            }
83
+
84
+            $payload = array_merge($payload, app(CreaComandaCucina::class)->perOrdineRitiro($job->ordine, (int) $cucina_id));
85
+        } elseif ($job->tipo === PrintJob::TIPO_CUPON) {
86
+            $cupon = Cupon::find($job->printable_id);
87
+            if($cupon === null){
88
+                Log::error('Cupon non trovato.');
89
+                throw new \RuntimeException('Cupon non trovato.');
90
+            }
91
+            
92
+            $payload = array_merge($payload, app(CreaCupon::class)->perCupon($cupon));
67 93
         }else{
68 94
             Log::error('Tipo di stampa non supportato: '.$job->tipo);
69 95
             throw new \RuntimeException('Tipo di stampa non supportato: '.$job->tipo);

+ 4
- 2
app/Models/AbstractModels/AbstractPrintJob.php Просмотреть файл

@@ -48,7 +48,8 @@ abstract class AbstractPrintJob extends \Illuminate\Foundation\Auth\User
48 48
         'printed_at' => 'datetime',
49 49
         'failed_at' => 'datetime',
50 50
         'created_at' => 'datetime',
51
-        'updated_at' => 'datetime'
51
+        'updated_at' => 'datetime',
52
+        'printable_id' => 'integer',
52 53
     ];
53 54
     
54 55
     /**  
@@ -70,7 +71,8 @@ abstract class AbstractPrintJob extends \Illuminate\Foundation\Auth\User
70 71
         'printed_at',
71 72
         'failed_at',
72 73
         'created_at',
73
-        'updated_at'
74
+        'updated_at',
75
+        'printable_id',
74 76
     ];
75 77
     
76 78
     public function dispositivo()

+ 13
- 1
app/Models/Cucina.php Просмотреть файл

@@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model;
6 6
 
7 7
 class Cucina extends \App\Models\AbstractModels\AbstractCucina
8 8
 {
9
-    protected $appends = ['coperto_come_piatto', 'colore'];
9
+    protected $appends = ['coperto_come_piatto', 'colore', 'stampa_ritiro'];
10 10
 
11 11
     public function getCopertoComePiattoAttribute()
12 12
     {
@@ -33,4 +33,16 @@ class Cucina extends \App\Models\AbstractModels\AbstractCucina
33 33
     $this->attributes['info'] = json_encode($info);
34 34
     }
35 35
 
36
+    public function getStampaRitiroAttribute(): bool
37
+    {
38
+        return (bool) ($this->info['stampa_ritiro'] ?? false);
39
+    }
40
+
41
+    public function setStampaRitiroAttribute($value): void
42
+    {
43
+        $info = is_array($this->info) ? $this->info : [];
44
+        $info['stampa_ritiro'] = filter_var($value, FILTER_VALIDATE_BOOLEAN);
45
+        $this->attributes['info'] = json_encode($info);
46
+    }
47
+
36 48
 }

+ 25
- 9
app/Models/PrintJob.php Просмотреть файл

@@ -25,8 +25,13 @@ class PrintJob extends \App\Models\AbstractModels\AbstractPrintJob
25 25
     /** Comanda unica aggregata (una riga job per ordine). */
26 26
     public const TIPO_COMANDA_CUCINA = 'comanda_cucina';
27 27
 
28
+    /** Biglietto ritiro su stampante cassa (una riga job per ordine e cucina). */
29
+    public const TIPO_COMANDA_RITIRO = 'comanda_ritiro';
30
+
28 31
     public const TIPO_PRECONTO = 'preconto';
29 32
 
33
+    public const TIPO_CUPON = 'cupon';
34
+
30 35
     /**
31 36
      * Tipo per comande per cucina distinte (idempotenza: include id cucina).
32 37
      */
@@ -35,6 +40,11 @@ class PrintJob extends \App\Models\AbstractModels\AbstractPrintJob
35 40
         return 'comanda_cucina_'.$cucinaId;
36 41
     }
37 42
 
43
+    public static function tipoComandaRitiro(int $cucinaId): string
44
+    {
45
+        return 'comanda_ritiro_'.$cucinaId;
46
+    }
47
+
38 48
     public static function getStatoLabel($stato)
39 49
     {
40 50
         return match($stato) {
@@ -47,16 +57,22 @@ class PrintJob extends \App\Models\AbstractModels\AbstractPrintJob
47 57
 
48 58
     public static function getTipoLabel($tipo)
49 59
     {
50
-        if(str_starts_with($tipo, self::TIPO_COMANDA_CUCINA.'_')){
51
-            return 'Comanda cucina';//self::TIPO_COMANDA_CUCINA;
52
-        }else{
53
-            return match($tipo) {
54
-                self::TIPO_SCONTRINO => 'Scontrino',
55
-                self::TIPO_DUPLICATO_SCONTRINO => 'Duplicato scontrino',
56
-                self::TIPO_COMANDA_CUCINA => 'Comanda cucina',
57
-                self::TIPO_PRECONTO => 'Preconto',
58
-            };
60
+        if (str_starts_with($tipo, self::TIPO_COMANDA_CUCINA.'_')) {
61
+            return 'Comanda cucina';
59 62
         }
63
+
64
+        if (str_starts_with($tipo, self::TIPO_COMANDA_RITIRO.'_')) {
65
+            return 'Biglietto ritiro';
66
+        }
67
+
68
+        return match ($tipo) {
69
+            self::TIPO_SCONTRINO => 'Scontrino',
70
+            self::TIPO_DUPLICATO_SCONTRINO => 'Duplicato scontrino',
71
+            self::TIPO_COMANDA_CUCINA => 'Comanda cucina',
72
+            self::TIPO_PRECONTO => 'Preconto',
73
+            self::TIPO_CUPON => 'Cupon',
74
+            default => $tipo,
75
+        };
60 76
     }
61 77
 
62 78
     public function endpoint()

+ 126
- 53
app/Observers/OrdineObserver.php Просмотреть файл

@@ -8,6 +8,7 @@ use App\Models\PrintJob;
8 8
 use App\Models\Cucina;
9 9
 use Illuminate\Support\Facades\Log;
10 10
 use App\Models\SaltacodaOrdine;
11
+use Illuminate\Support\Str;
11 12
 
12 13
 class OrdineObserver
13 14
 {
@@ -16,7 +17,14 @@ class OrdineObserver
16 17
      */
17 18
     public function created(Ordine $ordine): void
18 19
     {
19
-        //
20
+        if (filled($ordine->codice)) {
21
+            return;
22
+        }
23
+        do {
24
+            $codice = Str::random(32);
25
+        } while (Ordine::where('codice', $codice)->exists());
26
+        $ordine->codice = $codice;
27
+        $ordine->saveQuietly();
20 28
     }
21 29
 
22 30
     /**
@@ -29,13 +37,14 @@ class OrdineObserver
29 37
             return;
30 38
         }
31 39
 
32
-        //Aggiorna lo stato del pre_ordine
40
+        //Aggiorna lo stato del pre_ordine (solo stessa attività dell'ordine)
33 41
         $preOrdineId = data_get($ordine->info, 'pre_ordine_id');
34 42
         $preOrdineCodice = data_get($ordine->info, 'pre_ordine_codice');
35 43
 
36
-        if ($preOrdineId || $preOrdineCodice) {
44
+        if (($preOrdineId || $preOrdineCodice) && $ordine->attivita_id) {
37 45
             $preOrdineQuery = SaltacodaOrdine::query()
38
-                ->where('stato', SaltacodaOrdine::PRE_CARRELLO);
46
+                ->where('stato', SaltacodaOrdine::PRE_CARRELLO)
47
+                ->where('attivita_id', $ordine->attivita_id);
39 48
 
40 49
             if ($preOrdineId) {
41 50
                 $preOrdineQuery->where('id', $preOrdineId);
@@ -43,9 +52,41 @@ class OrdineObserver
43 52
                 $preOrdineQuery->where('codice', $preOrdineCodice);
44 53
             }
45 54
 
46
-            $preOrdineQuery->update(['stato' => SaltacodaOrdine::IMPORTATO]);
55
+            $saltacodaOrdine = $preOrdineQuery->first();
56
+            if ($saltacodaOrdine) {
57
+                $saltacodaOrdine->stato = SaltacodaOrdine::IMPORTATO;
58
+                $saltacodaOrdine->save();
59
+            } else {
60
+                Log::warning('Pre-ordine saltacoda non valido per ordine '.$ordine->id.' (attività/mismatch o stato errato)');
61
+            }
47 62
         }
48
-        
63
+
64
+        //Aggiorna utilizzo dispositivo solo se appartiene alla stessa attività
65
+        if ($ordine->dispositivo_id) {
66
+            $dispositivoOk = $ordine->dispositivo
67
+                && (int) $ordine->dispositivo->attivita_id === (int) $ordine->attivita_id;
68
+
69
+            if ($dispositivoOk) {
70
+                app(\App\Services\Fatturazione\RigaFatturazioneService::class)
71
+                    ->aggiornaUtilizzoDispositivo((int) $ordine->dispositivo_id, (int) $ordine->id);
72
+            } else {
73
+                Log::warning('Utilizzo dispositivo saltato: dispositivo '.$ordine->dispositivo_id.' non allineato all\'attività ordine '.$ordine->id);
74
+            }
75
+        }
76
+
77
+        //Crea l'asporto se l'ordine è asporto
78
+        if (isset($ordine->info['asporto']) && $ordine->info['asporto'] == true) {
79
+            app('\App\Services\Asporto\AsportoService')->creaAsporto($ordine);
80
+        }
81
+
82
+        $statiStampaGiaPresi = [
83
+            PrintJob::STATO_IN_CODA,
84
+            PrintJob::STATO_PRESO,
85
+            PrintJob::STATO_COMPLETATO,
86
+        ];
87
+
88
+        $stampanteCassaId = $ordine->dispositivo?->stampante_dispositivo()?->id;
89
+
49 90
         //Crea il print job per lo scontrino
50 91
         $printJob = PrintJob::query()->firstOrNew([
51 92
             'ordine_id' => $ordine->id,
@@ -53,69 +94,101 @@ class OrdineObserver
53 94
         ]);
54 95
 
55 96
         if (
56
-            $printJob->exists
57
-            && in_array($printJob->stato, [
58
-                PrintJob::STATO_IN_CODA,
59
-                PrintJob::STATO_PRESO,
60
-                PrintJob::STATO_COMPLETATO,
61
-            ], true)
97
+            ! (
98
+                $printJob->exists
99
+                && in_array($printJob->stato, $statiStampaGiaPresi, true)
100
+            )
62 101
         ) {
63
-            return;
102
+            if (! $stampanteCassaId) {
103
+                Log::warning('Stampante cassa non configurata, scontrino saltato per ordine '.$ordine->id, [
104
+                    'dispositivo_id' => $ordine->dispositivo_id,
105
+                    'url_stampante' => $ordine->dispositivo?->url_stampante,
106
+                ]);
107
+            } else {
108
+                $printJob->fill([
109
+                    'dispositivo_id' => $ordine->dispositivo_id,
110
+                    'stampante_id' => $stampanteCassaId,
111
+                    'stato' => PrintJob::STATO_IN_ATTESA,
112
+                    'payload' => null,
113
+                ]);
114
+                $printJob->save();
115
+
116
+                ElaboraJobStampa::dispatch($printJob->id);
117
+            }
64 118
         }
65 119
 
66
-        $printJob->fill([
67
-            'dispositivo_id' => $ordine->dispositivo_id,
68
-            'stampante_id' => $ordine->dispositivo->url_stampante,
69
-            'stato' => PrintJob::STATO_IN_ATTESA,
70
-            'payload' => null,
71
-        ]);
72
-        $printJob->save();
73
-
74
-        ElaboraJobStampa::dispatch($printJob->id);
75
-
76 120
         //Crea comande cucine
77 121
         $cucine = $ordine->cucine();
78
-        foreach($cucine as $cucina){
122
+        foreach ($cucine as $cucina) {
123
+            $cucinaModel = Cucina::find($cucina);
124
+            if ($cucinaModel === null) {
125
+                Log::error('Cucina non trovata, comanda saltata', [
126
+                    'cucina_id' => $cucina,
127
+                    'ordine_id' => $ordine->id,
128
+                ]);
129
+                continue;
130
+            }
131
+
79 132
             $printJob = PrintJob::query()->firstOrNew([
80 133
                 'ordine_id' => $ordine->id,
81 134
                 'tipo' => PrintJob::tipoComandaCucina($cucina),
82 135
             ]);
83 136
             if (
84
-                $printJob->exists
85
-                && in_array($printJob->stato, [
86
-                    PrintJob::STATO_IN_CODA,
87
-                    PrintJob::STATO_PRESO,
88
-                    PrintJob::STATO_COMPLETATO,
89
-                ], true)
137
+                ! (
138
+                    $printJob->exists
139
+                    && in_array($printJob->stato, $statiStampaGiaPresi, true)
140
+                )
90 141
             ) {
91
-                continue;
92
-            }
93
-            $cucinaModel = Cucina::find($cucina);
94
-            if ($cucinaModel === null) {
95
-                Log::error('Cucina non trovata id: '.$cucina);
96
-                throw new \RuntimeException('Cucina non trovata id: '.$cucina);
97
-            }elseif($cucinaModel->stampante_id === null){
98
-                Log::warning('Stampante non trovata per la cucina '.$cucina.' ordine: '.$ordine->id);
99
-                // throw new \RuntimeException('Stampante non trovata per la cucina '.$cucina);
142
+                if ($cucinaModel->stampante_id === null) {
143
+                    Log::warning('Stampante non configurata per la cucina, comanda saltata', [
144
+                        'cucina_id' => $cucina,
145
+                        'ordine_id' => $ordine->id,
146
+                    ]);
147
+                } else {
148
+                    $printJob->fill([
149
+                        'dispositivo_id' => $ordine->dispositivo_id,
150
+                        'stampante_id' => $cucinaModel->stampante_id,
151
+                        'stato' => PrintJob::STATO_IN_ATTESA,
152
+                        'payload' => null,
153
+                    ]);
154
+                    $printJob->save();
155
+                    ElaboraJobStampa::dispatch($printJob->id);
156
+                }
100 157
             }
101 158
 
102
-            $printJob->fill([
103
-                'dispositivo_id' => $ordine->dispositivo_id,
104
-                'stampante_id' => $cucinaModel->stampante_id,
105
-                'stato' => PrintJob::STATO_IN_ATTESA,
106
-                'payload' => null,
107
-            ]);
108
-            $printJob->save();
109
-            ElaboraJobStampa::dispatch($printJob->id);
159
+            if ($cucinaModel->stampa_ritiro) {
160
+                if (! $stampanteCassaId) {
161
+                    Log::warning('Stampante cassa non configurata, biglietto ritiro saltato per ordine '.$ordine->id, [
162
+                        'cucina_id' => $cucina,
163
+                        'dispositivo_id' => $ordine->dispositivo_id,
164
+                    ]);
165
+                } else {
166
+                    $printJobRitiro = PrintJob::query()->firstOrNew([
167
+                        'ordine_id' => $ordine->id,
168
+                        'tipo' => PrintJob::tipoComandaRitiro((int) $cucina),
169
+                    ]);
170
+
171
+                    if (
172
+                        ! (
173
+                            $printJobRitiro->exists
174
+                            && in_array($printJobRitiro->stato, $statiStampaGiaPresi, true)
175
+                        )
176
+                    ) {
177
+                        $printJobRitiro->fill([
178
+                            'dispositivo_id' => $ordine->dispositivo_id,
179
+                            'stampante_id' => $stampanteCassaId,
180
+                            'stato' => PrintJob::STATO_IN_ATTESA,
181
+                            'payload' => null,
182
+                        ]);
183
+                        $printJobRitiro->save();
184
+                        ElaboraJobStampa::dispatch($printJobRitiro->id);
185
+                    }
186
+                }
187
+            }
110 188
         }
111 189
 
112
-        //Aggiorna il utilizzo del dispositivo
113
-        app('\App\Services\Fatturazione\RigaFatturazioneService')->aggiornaUtilizzoDispositivo($ordine->dispositivo_id);
114 190
 
115
-        //Crea l'asporto se l'ordine è asporto
116
-        if (isset($ordine->info['asporto']) && $ordine->info['asporto'] == true) {
117
-            app('\App\Services\Asporto\AsportoService')->creaAsporto($ordine);
118
-        }
191
+
119 192
    
120 193
     }
121 194
 

+ 54
- 0
app/Services/Stampa/CreaComandaCucina.php Просмотреть файл

@@ -58,6 +58,60 @@ class CreaComandaCucina
58 58
         ];
59 59
     }
60 60
 
61
+    /**
62
+     * Biglietto ritiro su stampante cassa: ID ordine, cucina e piatti (senza dati operativi della comanda).
63
+     */
64
+    public function perOrdineRitiro(Ordine $ordine, int $cucina_id): array
65
+    {
66
+        $ordine->loadMissing(['righe_ordine.piatto', 'righe_ordine.has_variante.variante_piatto']);
67
+        $cucina = Cucina::find($cucina_id);
68
+        if ($cucina === null) {
69
+            Log::error('--- CreaComandaCucina --- Cucina non trovata id: '.$cucina_id);
70
+            throw new \RuntimeException('--- CreaComandaCucina --- Cucina non trovata id: '.$cucina_id);
71
+        }
72
+
73
+        $dataOra = $ordine->created_at?->format('d/m/Y H:i') ?? now()->format('d/m/Y H:i');
74
+
75
+        $connector = new DummyPrintConnector;
76
+        $printer = new Printer($connector);
77
+
78
+        $this->printHeaderRitiro($printer, $ordine, $cucina->nome ?? null);
79
+
80
+        $printer->text("\n");
81
+        $printer->text($this->separator());
82
+        $this->printItemsTable($printer, $ordine, $cucina_id);
83
+
84
+        $printer->feed(2);
85
+        $printer->cut();
86
+
87
+        $bytes = $connector->getData();
88
+        $printer->close();
89
+
90
+        return [
91
+            'formato' => 'escpos_base64',
92
+            'dati' => base64_encode($bytes),
93
+            'ordine_id' => $ordine->id,
94
+            'ordine_numero' => $ordine->id,
95
+            'ordine_data' => $dataOra,
96
+        ];
97
+    }
98
+
99
+    private function printHeaderRitiro(Printer $printer, Ordine $ordine, ?string $nomeCucina): void
100
+    {
101
+        $printer->setJustification(Printer::JUSTIFY_CENTER);
102
+        $printer->setEmphasis(true);
103
+        $printer->setTextSize(2, 2);
104
+        $printer->text('#'.$ordine->id."\n");
105
+
106
+        if (! empty($nomeCucina)) {
107
+            $printer->text('RITIRO - '.$this->sanitizeLine((string) $nomeCucina)."\n");
108
+        }
109
+
110
+        $printer->setTextSize(1, 1);
111
+        $printer->setEmphasis(false);
112
+        $printer->text("\n");
113
+    }
114
+
61 115
     private function printHeader(Printer $printer, Ordine $ordine, ?string $nomeCucina, array $info): void
62 116
     {
63 117
         $printer->setJustification(Printer::JUSTIFY_CENTER);

+ 1
- 1
app/Services/Stampa/CreaScontrino.php Просмотреть файл

@@ -182,7 +182,7 @@ class CreaScontrino
182 182
         $printer->text($this->separator());
183 183
         $printer->setJustification(Printer::JUSTIFY_CENTER);
184 184
         if(Impostazioni::getImpostazione('stampa_qr_code_chiamata')){
185
-        $printer->qrCode(route('consulta.scontrino.show', ['ordine_id' => $ordine->id]) , Printer::QR_ECLEVEL_L, 6, Printer::QR_MODEL_2);
185
+        $printer->qrCode(route('consulta.scontrino.show', ['ordine_codice' => $ordine->codice]) , Printer::QR_ECLEVEL_L, 6, Printer::QR_MODEL_2);
186 186
         // $this->printQrCode($printer, route('consulta.scontrino.show', ['ordine_id' => $ordine->id]));
187 187
         }
188 188
         $printer->text("\n");

Загрузка…
Отмена
Сохранить