# Task 12 — Customer Expansion

## Goal
Expand customer with credit limits, payment terms, AR account, opening balance.
Add customer balance calculation.

---

## Migration (already defined in Task 02, Migration 5)

Columns already specified. Run the migration from Task 02.

---

## Update Model: `app/Models/Sales/Customer.php`

Add to `$fillable`:
```php
'credit_limit', 'payment_terms_days', 'ar_account_id',
'opening_balance', 'opening_balance_date', 'currency_code',
```

Add to `$casts`:
```php
'credit_limit'          => 'decimal:2',
'opening_balance'       => 'decimal:2',
'opening_balance_date'  => 'date',
```

Add relationships:
```php
public function arAccount(): BelongsTo { return $this->belongsTo(Account::class, 'ar_account_id'); }
public function invoices(): HasMany { return $this->hasMany(Invoice::class); }
public function payments(): HasMany { return $this->hasMany(SalesPayment::class); }
public function quotations(): HasMany { return $this->hasMany(SalesQuotation::class); }
```

---

## Update Service: `app/Services/Sales/CustomerService.php`

Add method `getBalance(Customer $customer): array`:
```php
public function getBalance(Customer $customer): array
{
    $totalInvoiced = $customer->invoices()
        ->whereIn('status', ['confirmed', 'posted', 'partially_paid'])
        ->sum('total_amount');

    $totalPaid = $customer->payments()
        ->where('status', 'posted')
        ->sum('amount');

    $balance = $customer->opening_balance + $totalInvoiced - $totalPaid;

    return [
        'opening_balance' => (float) $customer->opening_balance,
        'total_invoiced'  => (float) $totalInvoiced,
        'total_paid'      => (float) $totalPaid,
        'balance'         => (float) $balance,
        'credit_limit'    => (float) $customer->credit_limit,
        'available_credit'=> $customer->credit_limit > 0
                             ? max(0, (float)$customer->credit_limit - $balance)
                             : null,
    ];
}
```

Add method `checkCreditLimit(Customer $customer, float $newInvoiceAmount): bool`:
```php
public function checkCreditLimit(Customer $customer, float $newInvoiceAmount): bool
{
    if ($customer->credit_limit <= 0) return true; // 0 = unlimited
    $balance = $this->getBalance($customer);
    return ($balance['balance'] + $newInvoiceAmount) <= $customer->credit_limit;
}
```

---

## Update FormRequest: `StoreCustomerRequest`

Add:
```php
'credit_limit'          => 'nullable|numeric|min:0',
'payment_terms_days'    => 'nullable|integer|min:0',
'ar_account_id'         => 'nullable|exists:accounts,id',
'opening_balance'       => 'nullable|numeric',
'opening_balance_date'  => 'nullable|date',
'currency_code'         => 'nullable|string|size:3',
```

---

## Update Views

Add to customer create/edit form:
- Credit Limit field (numeric, 0 = unlimited, show hint)
- Payment Terms Days field (default 30)
- AR Account dropdown (accounts, select2)
- Opening Balance (numeric)
- Opening Balance Date (date picker)
- Currency Code (text, default USD)

Update customer `show.blade.php` to display:
- Balance card: Opening Balance | Total Invoiced | Total Paid | **Current Balance**
- Credit status: Available Credit / Limit Exceeded warning badge
- Recent invoices list (last 5)
- Recent payments list (last 5)

---

## Test This Task

1. Create customer with credit_limit = 5000, payment_terms_days = 30
2. Call getBalance() → should return opening_balance as full balance
3. Verify available_credit = 5000 - balance
