# Task 05 — Item Expansion

## Goal
Add pricing, cost, reorder, account mapping, and category fields to the existing `items` table and update its model and service.

---

## Migration: Alter `items` Table

```php
Schema::table('items', function (Blueprint $table) {
    // Category & UOM
    $table->unsignedBigInteger('category_id')->nullable()->after('branch_id');
    $table->unsignedBigInteger('uom_id')->nullable()->after('category_id');

    // Pricing
    $table->decimal('purchase_price', 18, 2)->default(0)->after('uom_id');
    $table->decimal('sale_price', 18, 2)->default(0)->after('purchase_price');

    // Reorder
    $table->decimal('reorder_level', 18, 2)->default(0)->after('sale_price');
    $table->decimal('reorder_qty', 18, 2)->default(0)->after('reorder_level');

    // Valuation
    $table->enum('cost_method', ['average', 'fifo'])->default('average')->after('reorder_qty');
    $table->decimal('average_cost', 18, 6)->default(0)->after('cost_method');

    // Account mapping
    $table->unsignedBigInteger('inventory_account_id')->nullable()->after('average_cost');
    $table->unsignedBigInteger('cogs_account_id')->nullable()->after('inventory_account_id');
    $table->unsignedBigInteger('revenue_account_id')->nullable()->after('cogs_account_id');

    // Safety flag
    $table->boolean('allow_negative_stock')->default(false)->after('revenue_account_id');

    // Foreign keys
    $table->foreign('category_id')->references('id')->on('item_categories')->nullOnDelete();
    $table->foreign('uom_id')->references('id')->on('units_of_measure')->nullOnDelete();
    $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();

    // Indexes
    $table->index('category_id');
    $table->index('cost_method');
    $table->index('reorder_level');
});
```

---

## Update Model: `app/Models/Inventory/Item.php`

Add to `$fillable`:
```php
'category_id', 'uom_id', 'purchase_price', 'sale_price',
'reorder_level', 'reorder_qty', 'cost_method', 'average_cost',
'inventory_account_id', 'cogs_account_id', 'revenue_account_id', 'allow_negative_stock',
```

Add to `$casts`:
```php
'purchase_price'  => 'decimal:2',
'sale_price'      => 'decimal:2',
'reorder_level'   => 'decimal:2',
'reorder_qty'     => 'decimal:2',
'average_cost'    => 'decimal:6',
'allow_negative_stock' => 'boolean',
```

Add relationships:
```php
use App\Models\Inventory\Setup\ItemCategory;
use App\Models\Inventory\Setup\UnitOfMeasure;
use App\Models\Finance\Account;

public function category(): BelongsTo { return $this->belongsTo(ItemCategory::class, 'category_id'); }
public function uom(): BelongsTo { return $this->belongsTo(UnitOfMeasure::class, 'uom_id'); }
public function inventoryAccount(): BelongsTo { return $this->belongsTo(Account::class, 'inventory_account_id'); }
public function cogsAccount(): BelongsTo { return $this->belongsTo(Account::class, 'cogs_account_id'); }
public function revenueAccount(): BelongsTo { return $this->belongsTo(Account::class, 'revenue_account_id'); }
public function stockBalance(): HasOne { return $this->hasOne(StockBalance::class); }
```

---

## Update Service: `app/Services/Inventory/ItemService.php`

Add to `getFormOptions()`:
```php
'categories' => ItemCategory::active()->orderBy('name->en')->get(),
'uoms'       => UnitOfMeasure::active()->orderBy('name->en')->get(),
'accounts'   => Account::active()->get(),
```

Add method `getItemsNeedingReorder()`:
```php
public function getItemsNeedingReorder(): Collection
{
    return Item::query()
        ->where('type', 'stock')
        ->where('reorder_level', '>', 0)
        ->whereHas('stockBalance', function ($q) {
            $q->whereColumn('quantity', '<=', 'items.reorder_level');
        })
        ->with(['category', 'stockBalance'])
        ->get();
}
```

---

## Update FormRequests

### `StoreItemRequest` — add new validation rules:
```php
'category_id'         => 'nullable|exists:item_categories,id',
'uom_id'              => 'nullable|exists:units_of_measure,id',
'purchase_price'      => 'nullable|numeric|min:0',
'sale_price'          => 'nullable|numeric|min:0',
'reorder_level'       => 'nullable|numeric|min:0',
'reorder_qty'         => 'nullable|numeric|min:0',
'cost_method'         => 'in:average,fifo',
'inventory_account_id'=> 'nullable|exists:accounts,id',
'cogs_account_id'     => 'nullable|exists:accounts,id',
'revenue_account_id'  => 'nullable|exists:accounts,id',
'allow_negative_stock'=> 'boolean',
```

---

## Update Views

Update `resources/views/dashboard/admin/inventory/items/create.blade.php` and `edit.blade.php` to show:
- Category dropdown (select2)
- Unit of Measure dropdown (select2)
- Purchase Price and Sale Price fields
- Reorder Level and Reorder Quantity fields
- Account mapping dropdowns (Inventory, COGS, Revenue accounts)
- Allow Negative Stock toggle

---

## Test This Task

1. Open item create form → confirm new fields appear
2. Create an item with category, UOM, prices, accounts
3. Confirm it saves correctly
4. Confirm category/UOM dropdowns populate from Task 04 data
