<?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('journal_entries',function(Blueprint $t){$t->id();$t->ulid('public_id')->unique();$t->foreignId('company_id')->constrained()->restrictOnDelete();$t->foreignId('branch_id')->constrained()->restrictOnDelete();$t->foreignId('accounting_period_id')->constrained()->restrictOnDelete();$t->string('journal_number')->unique();$t->date('posting_date')->index();$t->date('document_date');$t->string('reference_number')->nullable()->index();$t->string('source_module',60)->index();$t->nullableMorphs('source');$t->unique(['source_type','source_id'],'journal_source_unique');$t->text('description');$t->char('currency_code',3)->default('AED');$t->decimal('exchange_rate',20,8)->default(1);$t->decimal('debit_total',20,4);$t->decimal('credit_total',20,4);$t->string('status',20)->index();$t->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();$t->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();$t->foreignId('posted_by')->nullable()->constrained('users')->nullOnDelete();$t->timestamp('posted_at')->nullable();$t->foreignId('reversal_of_id')->nullable()->constrained('journal_entries')->restrictOnDelete();$t->timestamps();});Schema::create('journal_lines',function(Blueprint $t){$t->id();$t->foreignId('journal_entry_id')->constrained()->cascadeOnDelete();$t->unsignedInteger('line_number');$t->foreignId('account_id')->constrained()->restrictOnDelete();$t->foreignId('party_id')->nullable()->constrained()->restrictOnDelete();$t->string('party_ledger',20)->nullable();$t->foreignId('branch_id')->nullable()->constrained()->restrictOnDelete();$t->foreignId('department_id')->nullable()->constrained()->restrictOnDelete();$t->foreignId('cost_center_id')->nullable()->constrained()->restrictOnDelete();$t->text('description')->nullable();$t->decimal('debit',20,4)->default(0);$t->decimal('credit',20,4)->default(0);$t->char('currency_code',3)->default('AED');$t->decimal('exchange_rate',20,8)->default(1);$t->unique(['journal_entry_id','line_number']);$t->index(['account_id','party_id','party_ledger']);});}public function down():void{Schema::dropIfExists('journal_lines');Schema::dropIfExists('journal_entries');}};
