diff --git a/db/migrations/20260813114707_convert_quantityclass_to_int.php b/db/migrations/20260813114707_convert_quantityclass_to_int.php new file mode 100644 index 00000000..42ea9e7d --- /dev/null +++ b/db/migrations/20260813114707_convert_quantityclass_to_int.php @@ -0,0 +1,49 @@ +execute("UPDATE orderbook SET quantityclass = TRIM(quantityclass) WHERE quantityclass IS NOT NULL"); + + // 2) Bonifica: i valori non interamente numerici o vuoti diventano 0 + // (REGEXP '^[0-9]+$' = solo cifre). Rivedi manualmente eventuali 0 + // risultanti dopo la migration se non te li aspetti. + $this->execute("UPDATE orderbook SET quantityclass = '0' WHERE quantityclass IS NULL OR quantityclass = '' OR quantityclass NOT REGEXP '^[0-9]+$'"); + + // 3) Cambia il tipo di colonna a INT + $table = $this->table('orderbook'); + $table->changeColumn('quantityclass', 'integer', [ + 'null' => true, + 'default' => null, + 'signed' => true, + ])->update(); + } + + public function down(): void + { + // Ripristina varchar(20). I valori numerici restano validi come stringa. + $table = $this->table('orderbook'); + $table->changeColumn('quantityclass', 'string', [ + 'limit' => 20, + 'null' => true, + 'default' => null, + ])->update(); + } +} diff --git a/db/migrations/20260813114708_add_is_openflexy_to_orderbook.php b/db/migrations/20260813114708_add_is_openflexy_to_orderbook.php new file mode 100644 index 00000000..637ee261 --- /dev/null +++ b/db/migrations/20260813114708_add_is_openflexy_to_orderbook.php @@ -0,0 +1,47 @@ +table('orderbook'); + $table->addColumn('is_openflexy', 'char', [ + 'limit' => 1, + 'null' => false, + 'default' => 'N', + 'after' => 'idservice', + ])->update(); + + // Popola il flag sugli ordini già collegati alla classe OpenFlexy + // esistente (riconosciuta per nome, una tantum). Usa lower() per + // sicurezza sul confronto. + $this->execute( + "UPDATE orderbook o + JOIN service s ON s.idservice = o.idservice + SET o.is_openflexy = 'Y' + WHERE LOWER(TRIM(s.servicename)) = 'openflexy'" + ); + } + + public function down(): void + { + $table = $this->table('orderbook'); + $table->removeColumn('is_openflexy')->update(); + } +} diff --git a/db/migrations/20260813114709_add_foreign_key_bookingclass_order.php b/db/migrations/20260813114709_add_foreign_key_bookingclass_order.php new file mode 100644 index 00000000..74eab60d --- /dev/null +++ b/db/migrations/20260813114709_add_foreign_key_bookingclass_order.php @@ -0,0 +1,44 @@ + orderbook.idorderbook. + * + * Con ON DELETE SET NULL: cancellare un ordine NON cancella le prenotazioni + * collegate, ma ne slega il riferimento (idorder = NULL). Questo preserva la + * storia delle prenotazioni ed evita errori di vincolo alla cancellazione. + * + * IMPORTANTE: se esistono righe in bookingclass con un idorder che non + * corrisponde a nessun orderbook (dati orfani), la creazione della FK + * fallisce. Il metodo up() bonifica prima questi orfani portandoli a NULL. + */ +final class AddForeignKeyBookingclassOrder extends AbstractMigration +{ + public function up(): void + { + // 1) Bonifica orfani: idorder che non esiste in orderbook -> NULL + $this->execute( + "UPDATE bookingclass b + LEFT JOIN orderbook o ON o.idorderbook = b.idorder + SET b.idorder = NULL + WHERE b.idorder IS NOT NULL AND o.idorderbook IS NULL" + ); + + // 2) Crea la foreign key + $table = $this->table('bookingclass'); + $table->addForeignKey('idorder', 'orderbook', 'idorderbook', [ + 'delete' => 'SET_NULL', + 'update' => 'NO_ACTION', + 'constraint' => 'fk_bookingclass_orderbook', + ])->update(); + } + + public function down(): void + { + $table = $this->table('bookingclass'); + $table->dropForeignKey('idorder', null, ['constraint' => 'fk_bookingclass_orderbook'])->update(); + } +} diff --git a/public/api/my_lessons.php b/public/api/my_lessons.php index fad4592d..c481c59e 100644 --- a/public/api/my_lessons.php +++ b/public/api/my_lessons.php @@ -199,6 +199,7 @@ foreach ($rows as $r) { $lessons[] = [ 'booking_id' => (int) $r['idbookingclass'], + 'service_id' => (int) ($r['idservice'] ?? 0), 'datetime' => $classDt->format('Y-m-d H:i:s'), 'date' => $classDt->format('Y-m-d'), 'time' => $classDt->format('H:i'),