# Bill Generation

## How Bills Work

Bills are generated monthly for each **active** customer profile. A bill:
- Has a `billing_month` and `billing_year`
- Contains line items (package fee, taxes, discounts, arrears, misc charges)
- Has a `total_amount` computed from line items
- Has a `status`: `not-paid`, `partial-paid`, `paid`, `installment`, `waived`

---

## Bill Line Items (BillItem)

Every amount on a bill is a `BillItem`. Types:

| Type | Description |
|---|---|
| `package` | Monthly subscription fee |
| `tax` | Applicable tax (e.g. GST 16%) |
| `discount` | Applied discount (e.g. early payment 15%) |
| `arrears` | Outstanding from previous months |
| `late_fee` | Late payment charge |
| `misc` | Any other charge (e.g. device rental) |
| `credit` | Credit applied to reduce amount |

### Recurring Items
A BillItem with `is_recurring = true` and `billing_months = N` is automatically
added to the next N bills. `billed_count` tracks how many times it has been billed.

### Queued Items
A BillItem with `bill_id = null` is pending and will be included in the next
bill generation run.

---

## Generating Bills

### Auto-generate (Monthly Run)
1. Go to **Bills → Generate Bills**
2. Select the billing month and year
3. Click **Generate** — the system creates bills for all active customer profiles
4. Queued BillItems are attached to the new bills
5. Taxes and discounts are applied according to configuration

### Manual Bill
1. Go to **Bills → Create Manual Bill**
2. Select a customer
3. Add line items manually

---

## Bill Statuses

| Status | Meaning |
|---|---|
| `not-paid` | No payment received |
| `partial-paid` | Some payment received, balance remaining |
| `paid` | Fully paid |
| `installment` | On an installment plan |
| `waived` | Waived by admin (no payment required) |

---

## Overdue Bills

A bill is overdue if its `due_date` has passed and status is not `paid` or
`waived`. Overdue bills can trigger late fee BillItems.

---

## A4 Bill Print

Each bill can be printed as a full A4 PDF:
1. Open the bill detail page
2. Click **Print / Download PDF**
3. The PDF is generated using the bill Blade template via dompdf

The PDF header shows:
- Business name, logo, proprietor names and phones
- Bill number, billing month, due date
- Customer name, CNIC, internet ID, address
- Itemized line items
- Total, paid, and balance amounts
