<?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('customer_invoices',function(Blueprint $t){$t->id();$t->ulid('public_id')->unique();$t->foreignId('company_id')->constrained()->restrictOnDelete();$t->foreignId('branch_id')->constrained()->restrictOnDelete();$t->foreignId('party_id')->constrained()->restrictOnDelete();$t->string('invoice_number')->unique();$t->string('invoice_type')->default('tax_invoice');$t->date('issue_date');$t->date('supply_date')->nullable();$t->date('due_date')->nullable();$t->char('currency_code',3)->default('AED');$t->decimal('exchange_rate',20,8)->default(1);$t->decimal('subtotal',20,4);$t->decimal('discount_total',20,4)->default(0);$t->decimal('taxable_total',20,4);$t->decimal('vat_total',20,4)->default(0);$t->decimal('grand_total',20,4);$t->decimal('paid_total',20,4)->default(0);$t->decimal('allocated_total',20,4)->default(0);$t->foreignId('receivable_account_id')->constrained('accounts')->restrictOnDelete();$t->foreignId('income_account_id')->constrained('accounts')->restrictOnDelete();$t->foreignId('output_vat_account_id')->nullable()->constrained('accounts')->restrictOnDelete();$t->foreignId('tax_code_id')->nullable()->constrained()->restrictOnDelete();$t->string('status',30)->index();$t->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();$t->timestamp('approved_at')->nullable();$t->timestamp('posted_at')->nullable();$t->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();$t->text('notes')->nullable();$t->timestamps();$t->index(['party_id','status','due_date']);});
Schema::create('customer_invoice_items',function(Blueprint $t){$t->id();$t->foreignId('customer_invoice_id')->constrained()->cascadeOnDelete();$t->unsignedInteger('line_number');$t->text('description');$t->decimal('quantity',20,4)->default(1);$t->string('unit',20)->nullable();$t->decimal('unit_rate',20,4);$t->decimal('discount_amount',20,4)->default(0);$t->decimal('taxable_amount',20,4);$t->decimal('vat_rate',9,4)->default(0);$t->decimal('vat_amount',20,4)->default(0);$t->decimal('line_total',20,4);$t->foreignId('income_account_id')->constrained('accounts')->restrictOnDelete();$t->foreignId('output_vat_account_id')->nullable()->constrained('accounts')->restrictOnDelete();$t->foreignId('tax_code_id')->nullable()->constrained()->restrictOnDelete();$t->timestamps();$t->unique(['customer_invoice_id','line_number']);});
Schema::create('vendor_bills',function(Blueprint $t){$t->id();$t->ulid('public_id')->unique();$t->foreignId('company_id')->constrained()->restrictOnDelete();$t->foreignId('branch_id')->constrained()->restrictOnDelete();$t->foreignId('party_id')->constrained()->restrictOnDelete();$t->string('bill_number')->unique();$t->string('vendor_invoice_number');$t->date('invoice_date');$t->date('supply_date')->nullable();$t->date('due_date')->nullable();$t->char('currency_code',3)->default('AED');$t->decimal('exchange_rate',20,8)->default(1);$t->decimal('taxable_total',20,4);$t->decimal('recoverable_vat_total',20,4)->default(0);$t->decimal('nonrecoverable_vat_total',20,4)->default(0);$t->decimal('grand_total',20,4);$t->decimal('paid_total',20,4)->default(0);$t->decimal('allocated_total',20,4)->default(0);$t->foreignId('payable_account_id')->constrained('accounts')->restrictOnDelete();$t->foreignId('expense_account_id')->constrained('accounts')->restrictOnDelete();$t->foreignId('input_vat_account_id')->nullable()->constrained('accounts')->restrictOnDelete();$t->foreignId('tax_code_id')->nullable()->constrained()->restrictOnDelete();$t->string('status',30)->index();$t->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();$t->timestamp('approved_at')->nullable();$t->timestamp('posted_at')->nullable();$t->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();$t->text('notes')->nullable();$t->timestamps();$t->unique(['party_id','vendor_invoice_number','invoice_date','grand_total'],'vendor_bill_duplicate_guard');$t->index(['party_id','status','due_date']);});
Schema::create('vendor_bill_items',function(Blueprint $t){$t->id();$t->foreignId('vendor_bill_id')->constrained()->cascadeOnDelete();$t->unsignedInteger('line_number');$t->text('description');$t->decimal('quantity',20,4)->default(1);$t->string('unit',20)->nullable();$t->decimal('unit_rate',20,4);$t->decimal('discount_amount',20,4)->default(0);$t->decimal('taxable_amount',20,4);$t->decimal('recoverable_vat_amount',20,4)->default(0);$t->decimal('nonrecoverable_vat_amount',20,4)->default(0);$t->decimal('line_total',20,4);$t->foreignId('expense_account_id')->constrained('accounts')->restrictOnDelete();$t->foreignId('input_vat_account_id')->nullable()->constrained('accounts')->restrictOnDelete();$t->foreignId('tax_code_id')->nullable()->constrained()->restrictOnDelete();$t->timestamps();$t->unique(['vendor_bill_id','line_number']);});
Schema::create('receipts',function(Blueprint $t){$t->id();$t->ulid('public_id')->unique();$t->foreignId('company_id')->constrained()->restrictOnDelete();$t->foreignId('branch_id')->constrained()->restrictOnDelete();$t->foreignId('party_id')->constrained()->restrictOnDelete();$t->string('receipt_number')->unique();$t->date('receipt_date');$t->decimal('amount',20,4);$t->char('currency_code',3)->default('AED');$t->decimal('exchange_rate',20,8)->default(1);$t->string('payment_method',30);$t->foreignId('cash_bank_account_id')->constrained('accounts')->restrictOnDelete();$t->foreignId('receivable_account_id')->constrained('accounts')->restrictOnDelete();$t->foreignId('customer_advance_account_id')->nullable()->constrained('accounts')->restrictOnDelete();$t->string('reference_number')->nullable();$t->text('description')->nullable();$t->decimal('allocated_total',20,4)->default(0);$t->string('status',30)->index();$t->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();$t->timestamp('approved_at')->nullable();$t->timestamp('posted_at')->nullable();$t->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();$t->timestamps();});
Schema::create('receipt_allocations',function(Blueprint $t){$t->id();$t->foreignId('receipt_id')->constrained()->cascadeOnDelete();$t->foreignId('customer_invoice_id')->constrained()->restrictOnDelete();$t->decimal('amount',20,4);$t->timestamps();$t->unique(['receipt_id','customer_invoice_id']);});
Schema::create('payments',function(Blueprint $t){$t->id();$t->ulid('public_id')->unique();$t->foreignId('company_id')->constrained()->restrictOnDelete();$t->foreignId('branch_id')->constrained()->restrictOnDelete();$t->foreignId('party_id')->constrained()->restrictOnDelete();$t->string('payment_number')->unique();$t->date('payment_date');$t->decimal('amount',20,4);$t->char('currency_code',3)->default('AED');$t->decimal('exchange_rate',20,8)->default(1);$t->string('payment_method',30);$t->foreignId('cash_bank_account_id')->constrained('accounts')->restrictOnDelete();$t->foreignId('payable_account_id')->constrained('accounts')->restrictOnDelete();$t->foreignId('vendor_advance_account_id')->nullable()->constrained('accounts')->restrictOnDelete();$t->string('reference_number')->nullable();$t->text('description')->nullable();$t->decimal('allocated_total',20,4)->default(0);$t->string('status',30)->index();$t->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();$t->timestamp('approved_at')->nullable();$t->timestamp('posted_at')->nullable();$t->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();$t->timestamps();});
Schema::create('payment_allocations',function(Blueprint $t){$t->id();$t->foreignId('payment_id')->constrained()->cascadeOnDelete();$t->foreignId('vendor_bill_id')->constrained()->restrictOnDelete();$t->decimal('amount',20,4);$t->timestamps();$t->unique(['payment_id','vendor_bill_id']);});
Schema::create('contra_settlements',function(Blueprint $t){$t->id();$t->ulid('public_id')->unique();$t->foreignId('company_id')->constrained()->restrictOnDelete();$t->foreignId('branch_id')->constrained()->restrictOnDelete();$t->foreignId('party_id')->constrained()->restrictOnDelete();$t->string('contra_number')->unique();$t->date('settlement_date');$t->decimal('amount',20,4);$t->foreignId('customer_invoice_id')->constrained()->restrictOnDelete();$t->foreignId('vendor_bill_id')->constrained()->restrictOnDelete();$t->foreignId('receivable_account_id')->constrained('accounts')->restrictOnDelete();$t->foreignId('payable_account_id')->constrained('accounts')->restrictOnDelete();$t->text('reason');$t->string('status',30)->index();$t->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();$t->timestamp('approved_at')->nullable();$t->timestamp('posted_at')->nullable();$t->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();$t->timestamps();});
}public function down():void{Schema::dropIfExists('contra_settlements');Schema::dropIfExists('payment_allocations');Schema::dropIfExists('payments');Schema::dropIfExists('receipt_allocations');Schema::dropIfExists('receipts');Schema::dropIfExists('vendor_bill_items');Schema::dropIfExists('vendor_bills');Schema::dropIfExists('customer_invoice_items');Schema::dropIfExists('customer_invoices');}};
