<?php
use Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;
return new class extends Migration{public function up():void{Schema::create('accounts',function(Blueprint $t){$t->id();$t->ulid('public_id')->unique();$t->foreignId('company_id')->constrained()->cascadeOnDelete();$t->foreignId('parent_id')->nullable()->constrained('accounts')->nullOnDelete();$t->string('code',30);$t->string('name');$t->string('type',30);$t->string('normal_balance',10);$t->boolean('is_control_account')->default(false);$t->boolean('is_active')->default(true);$t->string('system_key')->nullable();$t->timestamps();$t->unique(['company_id','code']);$t->unique(['company_id','system_key']);});Schema::create('fiscal_years',function(Blueprint $t){$t->id();$t->ulid('public_id')->unique();$t->foreignId('company_id')->constrained()->cascadeOnDelete();$t->string('name');$t->date('starts_on');$t->date('ends_on');$t->string('status')->default('open');$t->timestamps();$t->unique(['company_id','starts_on','ends_on']);});Schema::create('accounting_periods',function(Blueprint $t){$t->id();$t->ulid('public_id')->unique();$t->foreignId('fiscal_year_id')->constrained()->cascadeOnDelete();$t->string('name');$t->date('starts_on');$t->date('ends_on');$t->boolean('is_locked')->default(false);$t->timestamp('locked_at')->nullable();$t->foreignId('locked_by')->nullable()->constrained('users')->nullOnDelete();$t->text('lock_reason')->nullable();$t->timestamps();$t->unique(['fiscal_year_id','starts_on','ends_on']);});Schema::create('tax_codes',function(Blueprint $t){$t->id();$t->ulid('public_id')->unique();$t->foreignId('company_id')->constrained()->cascadeOnDelete();$t->string('code',30);$t->string('name');$t->string('category',30);$t->decimal('rate',9,4)->default(0);$t->decimal('recoverable_percentage',9,4)->default(100);$t->foreignId('output_account_id')->nullable()->constrained('accounts')->restrictOnDelete();$t->foreignId('input_account_id')->nullable()->constrained('accounts')->restrictOnDelete();$t->foreignId('reverse_charge_output_account_id')->nullable()->constrained('accounts')->restrictOnDelete();$t->foreignId('reverse_charge_input_account_id')->nullable()->constrained('accounts')->restrictOnDelete();$t->boolean('is_active')->default(true);$t->boolean('requires_trn')->default(false);$t->boolean('requires_evidence')->default(false);$t->timestamps();$t->unique(['company_id','code']);});}public function down():void{Schema::dropIfExists('tax_codes');Schema::dropIfExists('accounting_periods');Schema::dropIfExists('fiscal_years');Schema::dropIfExists('accounts');}};
