# GenerateBill

```lua
exports['kartik-banking']:GenerateBill(data)
```

#### 🧾 Description

Generates an invoice entry in the `kartik_banking_invoices` table. Supports `one-time`, `subscription`, and `emi` (installment) billing types. It validates the account, calculates required fields, and stores metadata for recurring or installment-based billing.

***

#### 🔧 Parameters

**`data: table`**

A Lua table containing all relevant invoice information.

<table><thead><tr><th width="211">Field</th><th>Type</th><th>Required</th><th>Description</th></tr></thead><tbody><tr><td><code>owner</code></td><td><code>string</code></td><td>✅</td><td>Account number to which this bill applies.</td></tr><tr><td><code>sender_name</code></td><td><code>string</code></td><td>❌</td><td>Sender/Issuer of the invoice (default: <code>"System"</code>).</td></tr><tr><td><code>issuer_account_number</code></td><td><code>string</code></td><td>❌</td><td>Sender/Issuer of the invoice ( If this is sent, then the paid amount will directly be transfered to that account).</td></tr><tr><td><code>title</code></td><td><code>string</code></td><td>❌</td><td>Title of the invoice (default: <code>"Untitled Invoice"</code>).</td></tr><tr><td><code>description</code></td><td><code>string</code></td><td>❌</td><td>Description of the invoice or charge.</td></tr><tr><td><code>frequency</code></td><td><code>string</code></td><td>❌</td><td><code>"daily"</code>, <code>"monthly"</code>, <code>"weekly"</code>, or <code>"custom"</code>.</td></tr><tr><td><code>custom_frequency_days</code></td><td><code>number</code></td><td>❌</td><td>Used when frequency = <code>"custom"</code>. <code>0</code> = one-time.</td></tr><tr><td><code>next_due</code></td><td><code>string</code></td><td>❌</td><td>Date/time when the bill is due (format: <code>"YYYY-MM-DD HH:MM:SS"</code>).</td></tr><tr><td><code>auto_pay</code></td><td><code>boolean</code></td><td>❌</td><td>Enables automatic payment deduction if true.</td></tr><tr><td><code>amount</code></td><td><code>number</code></td><td>✅/❌</td><td>Required unless <code>metadata.type</code> is <code>"emi"</code>, in which case it's calculated.</td></tr><tr><td><code>metadata</code></td><td><code>table</code></td><td>✅</td><td>Details based on bill type. Also includes <code>accountType</code> automatically.</td></tr></tbody></table>

***

#### 📦 `metadata` Content

| Field                | Required      | Used For | Description                                         |
| -------------------- | ------------- | -------- | --------------------------------------------------- |
| `type`               | ✅             | All      | `"one-time" \| "emi", "subscription"`               |
| `accountType`        | Auto          | All      | Fetched from `GetAccountDataByAccountNumber(owner)` |
| `original_amount`    | ✅ for EMI     | EMI      | Full loan amount                                    |
| `installments_total` | ✅ for EMI     | EMI      | Number of EMIs                                      |
| `installment_amount` | Auto/Optional | EMI      | Amount per EMI (calculated if not provided)         |
| `installments_paid`  | Auto          | EMI      | Set to 0 initially                                  |

***

#### 💸 Behavior Based on Type

* **EMI**:
  * Calculates installment if not provided.
  * Sets initial `installments_paid` to 0.
  * Stores full EMI metadata.
  * Disables (`active = 0`) if all paid.
* **One-Time**:
  * Requires `custom_frequency_days = 0`.
  * Can be used for things like tow fees, medical charges, etc.
* **Subscription**:
  * Typical recurring bills (electricity, water, internet, etc).
  * Can be paired with `auto_pay`.

***

#### ✅ Example Usage

**1. One-Time Bill**

```lua
exports['kartik-banking']:GenerateBill({
    owner = "1223443",
    title = "Tow Service Fee",
    issuer_account_number= 17273738, -- optional
    frequency = "custom",
    custom_frequency_days = 0,
    amount = 300,
    metadata = {
        type = "one-time",

    }
})
```

***

**2. Subscription Bill**

```lua
exports['kartik-banking']:exports['kartik-banking']:GenerateBill({
    owner = "234324",
    title = "Electricity Bill",
    frequency = "monthly",
    issuer_account_number= 17273738, -- optional
    auto_pay = true,
    amount = 250,
    metadata = {
        type = "subscription",

    }
})
```

***

**3. EMI-based Loan Bill**

```lua
exports['kartik-banking']:GenerateBill({
    owner = "432444",
    title = "Vehicle Loan EMI",
    frequency = "monthly",
    issuer_account_number= 17273738, -- optional
    auto_pay = true,
    metadata = {
        type = "emi",
        original_amount = 1200,
        installments_total = 12
        -- installment_amount will be calculated automatically if not given
    }
})
```

***

#### 🔁 Return Value

| Type    | Description                                       |
| ------- | ------------------------------------------------- |
| `true`  | Successfully created the invoice.                 |
| `false` | Error occurred (account not found, invalid data). |

***

#### 🧠 Notes

* `accountType` is auto-filled inside metadata.
* Will **fail silently** if account is invalid.
* Ideal for personal, companies, and shared account invoicing.
