<?php
// Nowills SQC ERP — B5: monthly fixed-asset depreciation runs + per-asset lines
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('depreciation_runs', function (Blueprint $table) {
            $table->id();
            $table->char('period', 7)->unique(); // YYYY-MM
            $table->date('run_date');
            $table->unsignedInteger('assets_count')->default(0);
            $table->decimal('total_amount', 15, 2)->default(0);
            $table->foreignId('journal_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('run_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
        });
        Schema::create('depreciation_run_lines', function (Blueprint $table) {
            $table->id();
            $table->foreignId('depreciation_run_id')->constrained()->cascadeOnDelete();
            $table->foreignId('fixed_asset_id')->constrained()->cascadeOnDelete();
            $table->decimal('monthly_charge', 15, 2);
            $table->decimal('acc_before', 15, 2);
            $table->decimal('acc_after', 15, 2);
            $table->timestamps();
        });
    }
    public function down(): void {
        Schema::dropIfExists('depreciation_run_lines');
        Schema::dropIfExists('depreciation_runs');
    }
};
