# Task 02 — Setup Tables (Migrations Only)

## Goal
Create migrations for all new setup/lookup tables.
Stop after migrations. Do NOT generate models yet.

---

## Migration 1: `item_categories`

```php
Schema::create('item_categories', function (Blueprint $table) {
    $table->id();
    $table->string('code')->unique();
    $table->json('name');  // translatable {ar, en}
    $table->unsignedBigInteger('inventory_account_id')->nullable();
    $table->unsignedBigInteger('cogs_account_id')->nullable();
    $table->unsignedBigInteger('revenue_account_id')->nullable();
    $table->boolean('is_active')->default(true);
    $table->unsignedBigInteger('created_by')->nullable();
    $table->unsignedBigInteger('updated_by')->nullable();
    $table->timestamps();

    $table->foreign('inventory_account_id')->references('id')->on('accounts')->nullOnDelete();
    $table->foreign('cogs_account_id')->references('id')->on('accounts')->nullOnDelete();
    $table->foreign('revenue_account_id')->references('id')->on('accounts')->nullOnDelete();
    $table->foreign('created_by')->references('id')->on('admins')->nullOnDelete();
    $table->foreign('updated_by')->references('id')->on('admins')->nullOnDelete();
    $table->index('is_active');
});
```

---

## Migration 2: `units_of_measure`

```php
Schema::create('units_of_measure', function (Blueprint $table) {
    $table->id();
    $table->string('code')->unique();
    $table->json('name');  // translatable {ar, en}
    $table->boolean('is_active')->default(true);
    $table->unsignedBigInteger('created_by')->nullable();
    $table->unsignedBigInteger('updated_by')->nullable();
    $table->timestamps();

    $table->foreign('created_by')->references('id')->on('admins')->nullOnDelete();
    $table->foreign('updated_by')->references('id')->on('admins')->nullOnDelete();
    $table->index('is_active');
});
```

---

## Migration 3: `sales_tax_rates`

```php
Schema::create('sales_tax_rates', function (Blueprint $table) {
    $table->id();
    $table->string('code')->unique();
    $table->json('name');  // translatable {ar, en}
    $table->decimal('rate', 8, 4);         // e.g. 14.0000 for 14%
    $table->unsignedBigInteger('tax_payable_account_id')->nullable();
    $table->boolean('is_active')->default(true);
    $table->boolean('is_default')->default(false);
    $table->unsignedBigInteger('created_by')->nullable();
    $table->unsignedBigInteger('updated_by')->nullable();
    $table->timestamps();

    $table->foreign('tax_payable_account_id')->references('id')->on('accounts')->nullOnDelete();
    $table->foreign('created_by')->references('id')->on('admins')->nullOnDelete();
    $table->foreign('updated_by')->references('id')->on('admins')->nullOnDelete();
    $table->index('is_active');
    $table->index('is_default');
});
```

---

## Migration 4: `suppliers`

```php
Schema::create('suppliers', function (Blueprint $table) {
    $table->id();
    $table->string('code')->unique();
    $table->json('name');  // translatable
    $table->string('phone')->nullable();
    $table->string('email')->nullable();
    $table->json('address')->nullable();
    $table->string('tax_number')->nullable();
    $table->decimal('credit_limit', 18, 2)->default(0);
    $table->unsignedInteger('payment_terms_days')->default(30);
    $table->unsignedBigInteger('ap_account_id')->nullable();
    $table->unsignedBigInteger('branch_id')->nullable();
    $table->decimal('opening_balance', 18, 2)->default(0);
    $table->date('opening_balance_date')->nullable();
    $table->boolean('is_active')->default(true);
    $table->unsignedBigInteger('created_by')->nullable();
    $table->unsignedBigInteger('updated_by')->nullable();
    $table->timestamps();

    $table->foreign('ap_account_id')->references('id')->on('accounts')->nullOnDelete();
    $table->foreign('branch_id')->references('id')->on('branches')->nullOnDelete();
    $table->foreign('created_by')->references('id')->on('admins')->nullOnDelete();
    $table->foreign('updated_by')->references('id')->on('admins')->nullOnDelete();
    $table->index(['branch_id', 'is_active']);
});
```

---

## Alter Migration 5: Add columns to `customers`

```php
Schema::table('customers', function (Blueprint $table) {
    $table->decimal('credit_limit', 18, 2)->default(0)->after('tax_number');
    $table->unsignedInteger('payment_terms_days')->default(30)->after('credit_limit');
    $table->unsignedBigInteger('ar_account_id')->nullable()->after('payment_terms_days');
    $table->decimal('opening_balance', 18, 2)->default(0)->after('ar_account_id');
    $table->date('opening_balance_date')->nullable()->after('opening_balance');
    $table->string('currency_code', 3)->default('USD')->after('opening_balance_date');

    $table->foreign('ar_account_id')->references('id')->on('accounts')->nullOnDelete();
});
```

---

## Notes

- Run `php artisan make:migration` for each table with a descriptive name
- Use timestamps like `2026_05_03_000001_create_item_categories_table.php`
- All FK columns must have an index — foreign keys create them automatically in MySQL
- Do NOT generate models or services yet
