# Order Invoice

{% hint style="info" %}
Read [Customer Deals](https://app.gitbook.com/o/rAHTXDKVzIOr4Jqk2SBF/s/bgpTyn5ehDgNS21U6ST7/~/changes/33/customer/customer-deal) to fully understand this process.
{% endhint %}

### Components

#### 1. Order Shop Product&#x20;

Defines the structure of the product involved in the transaction, including details like SKU, pricing, quantity, and any customer-specific deals.

**Attributes:**

* **SKU**: Unique identifier for the product.
* **Quote:** The requestd quote of the product.
* **Quantity**: The requested quanity for the product.

#### 2. Exchange Rates

Stores information about currency exchange rates, including the base currency, target currency, rate, and the date when the rate was last modified.

{% hint style="info" %}
To read the details of how the exchanges work in Gifthub ecosystem, read [Exchange Service](https://app.gitbook.com/o/rAHTXDKVzIOr4Jqk2SBF/s/bgpTyn5ehDgNS21U6ST7/~/changes/33/general/exchange-service)&#x20;
{% endhint %}

#### 3. Invoice Items

Generated for each line item in the invoice, including these types:

* `main-product`
* `discounts`
* `fee`
* `exchange-base-currency`
* `exchange-target-currency`
* `order-commission`
* `item-total`

#### 4. Invoice Records

Aggregates invoice items by SKU, facilitating group-specific operations like total calculation.

#### 5. Order Invoice

The final structure representing the complete invoice, including status, payment method, grouped items, and the total amount.

We use the structure above in **two invoices** per order.

1. **Wallet Invoice**: is the breakdown of the amount that is to be reduced from customer wallet in **the system**. (`invoice` field)
2. **Retail Invoice**: is the breakdown of the amount that is used when calculating the retail invoice to be paid by the customer. (`retailInvoice` field)

### Invoices Shared Structures

**InvoiceRecord**

| Field   | Type                        |
| ------- | --------------------------- |
| `sku`   | string                      |
| `items` | `InvoiceItem[]`             |
| `total` | `Map<CurrencyCode, Number>` |

**InvoiceItem**

<table><thead><tr><th>Field</th><th>Type</th></tr></thead><tbody><tr><td><code>description</code></td><td><code>string</code></td></tr><tr><td><code>metaData</code></td><td><code>object</code></td></tr><tr><td><code>type</code></td><td><ul><li><code>main-product</code></li><li><code>discounts</code></li><li><code>fee</code></li><li><code>exchange-base-currency</code></li><li><code>exchange-target-currency</code></li><li><code>order-commission</code></li><li><code>item-total</code></li></ul></td></tr><tr><td><code>effect</code></td><td><p><code>object</code></p><pre><code>{
  currency: CurrencyCodes,
  amount: number,
}
</code></pre></td></tr></tbody></table>

### Wallet Invoice

Everything in the wallet invoice is exchanged to **wallet currency**. Here is the **workflow** of generating the wallet invoice per order:

1. **Main product**: For each product in the order, we generate invoice item for the main product.
2. **Generate Exchange Invoices**: If the **wallet currency** differs from the **product base currency**, generate additional invoice items to account for currency exchange.
3. **Apply Customer Wallet Deal** : We add all the related adjustments based on the customer’s wallet deal for each specific product to the invoice.
4. **Group products:** We then group all added items to the invoice per specific product.
5. **Generate Invoice**: Compile the final invoice, summarizing the totals of all groups (products).

If there is a **percentage** mode in the adjustment amounts it’ll get applied to the **Main product after exchange**

#### Wallet Invoice Structure

| Field           | Type                                                                      |
| --------------- | ------------------------------------------------------------------------- |
| `records`       | `InvoiceRecord[]`                                                         |
| `status`        | <ul><li><code>pending</code></li><li><code>paid</code></li></ul>          |
| `paymentMethod` | <ul><li><code>bank-transfer</code></li><li><code>balance</code></li></ul> |
| `wallet`        | `CurrencyCode`                                                            |
| `total`         | number                                                                    |

### Retail Invoice

Everything in the retail invoice is exchanged to **retail currency**. Here is the **workflow** of generating the retail invoice per order:

1. **Product Total**: For each product in the order, we generate invoice item for the total amount of that product calculated in the **wallet invoice** in **wallet currency**
2. **Generate Exchange Invoices**: If the **retail currency** differs from the **wallet currency**, generate additional invoice items to account for currency exchange.
3. **Apply Customer Retail Deal** : We add all the related adjustments based on the customer’s retail deal for each specific product to the invoice.
4. **Group products:** We then group all added items to the invoice per specific product.
5. **Generate Invoice**: Compile the final invoice, summarizing the totals of all groups (products).

If there is a **percentage** mode in the adjustment amounts it’ll get applied to the **product total after exchange**

| Field           | Type                                                                      |
| --------------- | ------------------------------------------------------------------------- |
| `records`       | `InvoiceRecord[]`                                                         |
| `paymentMethod` | <ul><li><code>bank-transfer</code></li><li><code>balance</code></li></ul> |
| `currency`      | `CurrencyCode`                                                            |
| `total`         | number                                                                    |

### Example

Given a single product order:

**CustomerA** has the below table as its deal with **Gifthub** for **Product1.**

* retailCurrency → IRT
* walletCurrency → EUR

| SKU           | Quantity | Quote | Currency |
| ------------- | -------- | ----- | -------- |
| 039-208-range | 2        | 50    | DKK      |

**Wallet Deal**

| **Type**   | **Mode**       | **Amount** |
| ---------- | -------------- | ---------- |
| discount   | **percentage** | -2         |
| fee        | **fixed**      | 1          |
| commission | **percentage** | 6          |

**Retail Deal - deal items**

| Type       | Mode           | Amount |
| ---------- | -------------- | ------ |
| fee        | **fixed**      | 45,000 |
| commission | **percentage** | 6      |

#### Generated Wallet Invoice

| Description         | Type                       | Currency | Amount    |
| ------------------- | -------------------------- | -------- | --------- |
| Product1            | main-product               | DKK      | 100       |
| Exchange DKK to EUR | EXCHANGE\_TARGET\_CURRENCY | DKK      | -100      |
| Exchange DKK to EUR | EXCHANGE\_BASE\_CURRENCY   | EUR      | 13.39     |
| Discount (2%)       | DISCOUNT                   | EUR      | -0.26     |
| Fee                 | FEE                        | EUR      | 2         |
| Commission          | COMMISSION                 | EUR      | 0.8       |
|                     |                            |          |           |
|                     | **Total**                  | **EUR**  | **15.93** |

#### Generated Retail Invoice

| Description         | Type                       | Currency | Amount    |
| ------------------- | -------------------------- | -------- | --------- |
| Product1 Total      | product-total              | EUR      | 15.93     |
| Exchange EUR to IRT | EXCHANGE\_TARGET\_CURRENCY | EUR      | -15.93    |
| Exchange EUR to IRT | EXCHANGE\_BASE\_CURRENCY   | IRT      | 998,811   |
| Fee                 | FEE                        | IRT      | 90,000    |
| Commission          | COMMISSION                 | IRT      | 59,928    |
|                     |                            |          |           |
|                     | **Total**                  | IRT      | 1,148,739 |

{% hint style="info" %}
The total amount in retail invoice is the **full total cost** for that specific product.
{% endhint %}

#### JSON Response

```json
{
// ... rest of order
    "invoice": {
        "status": "pending",
        "paymentMethod": "balance",
        "records": [
            {
                "sku": "039-208-range",
                "items": [
                    {
                        "description": "SUPER_GEVEKORTET_DK",
                        "metaData": {
                            "quantity": 2,
                            "quote": 50
                        },
                        "type": "main-product",
                        "effect": {
                            "currency": "DKK",
                            "amount": 100
                        }
                    },
                    {
                        "description": "Even out target currency: DKK",
                        "metaData": {
                            "targetCurrency": "DKK",
                            "rate": 7.464285714285714,
                            "modifiedDate": {
                                "_seconds": 1716120001,
                                "_nanoseconds": 188000000
                            },
                            "baseCurrency": "EUR"
                        },
                        "type": "exchange-target-currency",
                        "effect": {
                            "currency": "DKK",
                            "amount": -100
                        }
                    },
                    {
                        "description": "Payment in base currency: EUR",
                        "metaData": {
                            "targetCurrency": "DKK",
                            "rate": 7.464285714285714,
                            "modifiedDate": {
                                "_seconds": 1716120001,
                                "_nanoseconds": 188000000
                            },
                            "baseCurrency": "EUR"
                        },
                        "type": "exchange-base-currency",
                        "effect": {
                            "currency": "EUR",
                            "amount": 13.39
                        }
                    },
                    {
                        "description": "Discount",
                        "metaData": {
                            "amount": -2,
                            "adjustmentMode": "percentage"
                        },
                        "type": "discount",
                        "effect": {
                            "currency": "EUR",
                            "amount": -0.26
                        }
                    },
                    {
                        "description": "Fee",
                        "metaData": {
                            "amount": 1,
                            "adjustmentMode": "fixed"
                        },
                        "type": "fee",
                        "effect": {
                            "currency": "EUR",
                            "amount": 2
                        }
                    },
                    {
                        "description": "Commission",
                        "metaData": {
                            "amount": 6,
                            "adjustmentMode": "percentage"
                        },
                        "type": "order-commission",
                        "effect": {
                            "currency": "EUR",
                            "amount": 0.8
                        }
                    }
                ],
                "total": {
                    "DKK": 0,
                    "EUR": 15.93
                }
            }
        ],
        "wallet": "EUR",
        "total": 15.93
    },
    "retailInvoice": {
        "paymentMethod": "bank-transfer",
        "records": [
            {
                "sku": "039-208-range",
                "items": [
                    {
                        "description": "Product item total",
                        "metaData": {
                            "sku": "039-208-range"
                        },
                        "type": "product-total",
                        "effect": {
                            "currency": "EUR",
                            "amount": 15.93
                        }
                    },
                    {
                        "description": "Even out target currency: EUR",
                        "metaData": {
                            "targetCurrency": "EUR",
                            "rate": 0.00001594896331738437,
                            "modifiedDate": {
                                "_seconds": 1716120001,
                                "_nanoseconds": 187000000
                            },
                            "baseCurrency": "IRT"
                        },
                        "type": "exchange-target-currency",
                        "effect": {
                            "currency": "EUR",
                            "amount": -15.93
                        }
                    },
                    {
                        "description": "Payment in base currency: IRT",
                        "metaData": {
                            "targetCurrency": "EUR",
                            "rate": 0.00001594896331738437,
                            "modifiedDate": {
                                "_seconds": 1716120001,
                                "_nanoseconds": 187000000
                            },
                            "baseCurrency": "IRT"
                        },
                        "type": "exchange-base-currency",
                        "effect": {
                            "currency": "IRT",
                            "amount": 998811
                        }
                    },
                    {
                        "description": "Commission",
                        "metaData": {
                            "amount": 6,
                            "adjustmentMode": "percentage"
                        },
                        "type": "order-commission",
                        "effect": {
                            "currency": "IRT",
                            "amount": 59928
                        }
                    },
                    {
                        "description": "Fee",
                        "metaData": {
                            "amount": 45000,
                            "adjustmentMode": "fixed"
                        },
                        "type": "fee",
                        "effect": {
                            "currency": "IRT",
                            "amount": 90000
                        }
                    }
                ],
                "total": {
                    "EUR": 0,
                    "IRT": 1148739
                }
            }
        ],
        "currency": "IRT",
        "total": 1148739
    }

// ... rest of order
}
```
