# Phase 8 Cleanup - Credit Notes Separation

## Overview
Phase 8 split credit notes into a separate `portalpeople/credit-notes` package to enable reuse across domains. The following files in the invoicing module are now obsolete and should be removed.

## Files to Delete

### 1. Old CreditNote Model
**File**: `src/models/CreditNote.php`
**Reason**: Replaced by `portalpeople\creditnotes\models\CreditNote` with polymorphic entity support
**Status**: Superseded ✅

### 2. Old CreditNoteTrait  
**File**: `src/traits/CreditNoteTrait.php`
**Reason**: Replaced by `portalpeople\creditnotes\traits\HasCreditNotesTrait` with generic entity support
**Status**: Superseded ✅

## What Changed

### Invoicing Module (`portalpeople/invoicing`)
- **Removed**: Credit note model and trait (now in separate package)
- **Updated**: `composer.json` to require `portalpeople/credit-notes`
- **Updated**: `Invoicesbe.php` to use `HasCreditNotesTrait` from credit-notes
- **Updated**: `InvoiceController.php` to work with polymorphic credit notes
- **Updated**: Migration to remove credit_notes table creation (handled by credit-notes package)

### Credit Notes Module (`portalpeople/credit-notes`)
- **New**: Standalone module with polymorphic entity support
- **Added**: `CreditNote.php` with `entity_id` + `entity_type` fields
- **Added**: `HasCreditNotesTrait.php` for any model to support credits
- **Added**: Separate migration for `credit_notes` table
- **Added**: Complete composer.json and README

## Integration Points

### Updated References
✅ `Invoicesbe.php`
- Now: `use portalpeople\creditnotes\traits\HasCreditNotesTrait;`
- Before: `use portalpeople\invoicing\traits\CreditNoteTrait;`

✅ `InvoiceController.php`
- Now queries: `CreditNote::find()->where(['entity_id' => ..., 'entity_type' => Invoicesbe::class])`
- Before: `CreditNote::find()->where(['invoice_id' => ...])`

## Execution Steps

### 1. Remove Old Files
```bash
# From workspace root
rm NGauge_modules/surf-invoicing/src/models/CreditNote.php
rm NGauge_modules/surf-invoicing/src/traits/CreditNoteTrait.php
```

### 2. Verify Removed
```bash
ls -la NGauge_modules/surf-invoicing/src/models/
ls -la NGauge_modules/surf-invoicing/src/traits/
```

### 3. Run Migrations
```bash
# Credit notes must run first (referenced by invoicing migration comment)
php yii migrate/up --interactive=0 --migrationPath=NGauge_modules/credit-notes/migrations
php yii migrate/up --interactive=0 --migrationPath=NGauge_modules/surf-invoicing/migrations
```

### 4. Test Credit Notes Work
```bash
# Via controller
GET /invoice/credit-notes?invoice_id=123

# Via model
$creditNotes = CreditNote::find()
    ->where(['entity_id' => 123, 'entity_type' => Invoicesbe::class])
    ->all();
```

## Verification Checklist

- [ ] Old CreditNote.php deleted
- [ ] Old CreditNoteTrait.php deleted
- [ ] No remaining references to `portalpeople\invoicing\CreditNote` in codebase
- [ ] No remaining references to `portalpeople\invoicing\traits\CreditNoteTrait` in codebase
- [ ] Migrations execute successfully
- [ ] Credit notes queries work with entity_id/entity_type
- [ ] Cart UI shows invoice status via CartInvoiceHelper
- [ ] Invoice links appear in cart list/view
- [ ] Credit note creation works through InvoiceController

## Files Already Updated (Phase 8)

✅ `NGauge_modules/surf-invoicing/composer.json`
- Added: `"portalpeople/credit-notes: *"` under requires
- Removed: `"provide"` section

✅ `NGauge_modules/surf-invoicing/src/models/Invoicesbe.php`
- Updated trait import to `portalpeople\creditnotes\traits\HasCreditNotesTrait`

✅ `NGauge_modules/surf-invoicing/src/controllers/InvoiceController.php`
- Updated CreditNote imports and queries
- Changed `invoice_id` → `entity_id`, `entity_type`

✅ `NGauge_modules/surf-invoicing/migrations/m260311_000001_create_invoicing_tables.php`
- Removed credit_notes table creation
- Removed credit_notes foreign key
- Added comment: "credit_notes table created by portalpeople/credit-notes package"

✅ `NGauge_modules/credit-notes/` (NEW)
- Complete module with polymorphic credit note support
- Ready for production use
