# Invoice Number Settings Module - Installation Guide

## Quick Installation

### Step 1: Install via Module Manager

1. Login to UltimatePOS admin panel
2. Navigate to **Settings → Module Manager**
3. Find **InvoiceNumberSettings** in the module list
4. Click **Install** button
5. Wait for installation to complete

### Step 2: Verify Installation

1. Go to **Settings → Business Settings**
2. You should see a new tab: **Invoice Settings**
3. The module is ready to use!

## Manual Installation (If Module Manager is not available)

### Step 1: Copy Module Files

Ensure the module is in the correct location:
```
Modules/InvoiceNumberSettings/
├── Database/
│   └── Migrations/
│       ├── 2026_01_19_110049_add_invoice_number_type_to_business_table.php
│       └── 2026_01_19_110053_add_unique_index_to_transactions_invoice_no.php
├── Providers/
│   └── InvoiceNumberSettingsServiceProvider.php
├── Resources/
│   ├── lang/
│   │   └── en/
│   │       └── invoice.php
│   └── views/
│       └── business/
│           └── partials/
│               └── settings_invoice.blade.php
├── module.json
└── README.md
```

### Step 2: Register Module

The module should be automatically detected if it's in the `Modules` directory and has a valid `module.json` file.

### Step 3: Run Migrations

```bash
php artisan migrate
```

This will:
- Add `invoice_number_type` column to `business` table
- Add UNIQUE index to `transactions.invoice_no` column

### Step 4: Clear Cache

```bash
php artisan cache:clear
php artisan config:clear
php artisan view:clear
```

### Step 5: Verify Core Integration

**Important**: This module requires core modifications to work properly. Ensure the following files are updated:

1. **app/Utils/TransactionUtil.php**
   - Should have `generateInvoiceNumber()` method
   - `getInvoiceNumber()` should use `generateInvoiceNumber()` for final invoices

2. **app/Http/Controllers/BusinessController.php**
   - Should include `invoice_number_type` in the `$business_details` array in `postBusinessSettings()` method

3. **resources/views/business/settings.blade.php**
   - Should have "Invoice Settings" tab in the menu
   - Should include the invoice settings partial

If these modifications are not present, the module will not function correctly.

## Post-Installation

### Configure Invoice Number Type

1. Go to **Settings → Business Settings**
2. Click on **Invoice Settings** tab
3. Select your preferred option:
   - **Sequential**: Auto-incrementing numbers (INV-000001, INV-000002...)
   - **Random**: Random alphanumeric strings (INV-A1B2C3D4...)
4. Click **Update Settings**

### Test the Module

1. Create a new POS sale or Sales invoice
2. Check the invoice number format:
   - Sequential: Should follow pattern like INV-000001, INV-000002
   - Random: Should be 8-character alphanumeric after prefix

## Troubleshooting

### Module Not Appearing in Module Manager

- Check that `module.json` is valid JSON
- Verify the module is in `Modules/InvoiceNumberSettings/` directory
- Clear cache: `php artisan cache:clear`

### Invoice Settings Tab Not Showing

- Verify the view file exists: `Modules/InvoiceNumberSettings/Resources/views/business/partials/settings_invoice.blade.php`
- Check that `business.settings` view includes the partial
- Clear view cache: `php artisan view:clear`

### Invoice Numbers Not Generating Correctly

- Verify `TransactionUtil::generateInvoiceNumber()` method exists
- Check that `getInvoiceNumber()` uses `generateInvoiceNumber()` for final invoices
- Ensure database migrations ran successfully

### Database Errors

- Check that migrations completed successfully
- Verify `business` table has `invoice_number_type` column
- Verify `transactions` table has UNIQUE index on `invoice_no`

## Uninstallation

To uninstall the module:

1. Go to **Settings → Module Manager**
2. Find **InvoiceNumberSettings**
3. Click **Uninstall**

**Note**: Uninstalling will NOT remove:
- The `invoice_number_type` column (to preserve data)
- The UNIQUE index on `transactions.invoice_no` (for data integrity)

To fully remove, you would need to manually:
1. Drop the `invoice_number_type` column (if desired)
2. Remove the UNIQUE index (if desired)
3. Remove core code modifications

## Support

For issues or questions:
1. Check the README.md file
2. Review the installation logs
3. Contact UltimatePOS support
