65 lines
2.6 KiB
PHP
Executable File
65 lines
2.6 KiB
PHP
Executable File
<?php
|
|
|
|
use Illuminate\Database\Migrations\Migration;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
return new class extends Migration
|
|
{
|
|
/**
|
|
* Tabella casse (Anag_casse legacy) per gestire codici cassa/banche locali non IBAN (es. cassa contanti, POS).
|
|
*/
|
|
public function up(): void
|
|
{
|
|
if (!Schema::hasTable('casse')) {
|
|
Schema::create('casse', function (Blueprint $table) {
|
|
$table->id();
|
|
if (Schema::hasTable('tenants')) {
|
|
$table->foreignId('tenant_id')->nullable()->constrained('tenants');
|
|
} else {
|
|
$table->unsignedBigInteger('tenant_id')->nullable()->index();
|
|
}
|
|
if (Schema::hasTable('stabili')) {
|
|
$table->foreignId('stabile_id')->nullable()->constrained('stabili');
|
|
} else {
|
|
$table->unsignedBigInteger('stabile_id')->nullable()->index();
|
|
}
|
|
$table->string('cod_cassa', 30)->index(); // cod_cassa legacy
|
|
$table->string('descrizione');
|
|
$table->enum('tipo', ['cassa_contanti', 'conto_bancario_locale', 'pos', 'altro'])->default('cassa_contanti');
|
|
$table->string('iban', 34)->nullable();
|
|
$table->string('intestazione_conto')->nullable();
|
|
$table->boolean('attiva')->default(true);
|
|
$table->json('meta')->nullable();
|
|
$table->timestamps();
|
|
$table->unique(['stabile_id', 'cod_cassa']);
|
|
});
|
|
}
|
|
|
|
// Aggiunge FK opzionale a incassi se non presente
|
|
if (Schema::hasTable('incassi') && !Schema::hasColumn('incassi', 'cassa_id')) {
|
|
Schema::table('incassi', function (Blueprint $table) {
|
|
if (Schema::hasTable('casse')) {
|
|
$table->foreignId('cassa_id')->nullable()->after('conto_bancario_id')->constrained('casse');
|
|
} else {
|
|
$table->unsignedBigInteger('cassa_id')->nullable()->after('conto_bancario_id')->index();
|
|
}
|
|
$table->string('cod_cassa_legacy', 30)->nullable()->after('cassa_id')->index();
|
|
});
|
|
}
|
|
}
|
|
|
|
public function down(): void
|
|
{
|
|
if (Schema::hasTable('incassi')) {
|
|
Schema::table('incassi', function (Blueprint $table) {
|
|
if (Schema::hasColumn('incassi', 'cassa_id')) {
|
|
$table->dropForeign(['cassa_id']);
|
|
$table->dropColumn(['cassa_id', 'cod_cassa_legacy']);
|
|
}
|
|
});
|
|
}
|
|
Schema::dropIfExists('casse');
|
|
}
|
|
};
|