# Ticket Model Mathematics

The [Ticket](/data-models.md#ticket) model is defined without supplementary financial properties. Instead, these additional properties can be calculated on the client's side. This article explains how the math works in the [Ticket](/data-models.md#ticket) model and related models like [`TicketItem`](/data-models.md#ticketitem).&#x20;

{% hint style="info" %}
**Understanding Property Naming Conventions**

When a financial property doesn't have the "*IncludingVat*" postfix, its value is indicated without including VAT. For instance, `unitPrice` refers to the value excluding VAT, while `unitPriceIncludingVat` specifies the value including VAT.
{% endhint %}

## Ticket Item Mathematics

The [`ticketItem`](/data-models.md#ticketitem) model contains following properties, relevant for mathematics:

* `unitPrice`: price per single quantity unit, excluding VAT.
* `quantity.amount`: the amount of product or service
* `discountRate`: the discount rate (numeric value between 0 and 100).
* `plu.vatInfo.Rate`: VAT rate percentage
* `plu.isDiscountAllowed`: indicates whether discount can be applied to ticket item

### Unit Price Including VAT

Initially, we must calculate VAT from the base amount, using the following formula:

$$
vat = unitPrice \* ( vatRate / 100)
$$

The "vatRate" refers to `plu.vatInfo.rate`. The outcome of *vat* is expressed as a [UnitPrice](/data-models.md#unitprice), rounded mathematically to six decimal places, refered as "*vat rounded*". The unit price including VAT of [ticket item](/data-models.md#ticketitem) is then calculated using following formula:

$$
unitPriceIncludingVat = unitPrice + vat\_{rounded}
$$

The outcome of second formula is expressed as a [UnitPrice](/data-models.md#unitprice), rounded mathematically to six decimal places as well.

### Effective Discount Rate

The discount can be placed on ticket item based on two properties:

* The discount rate of the [ticket](/data-models.md#ticket) in which the [ticket item](/data-models.md#ticketitem) is located (the `ticket.discountRate` property, referred to as "*dt*" in the formula below)*.* The value is normalized within an interval between 0 and 1, with the value rounded mathematically to four decimal places.
* The discount rate of the [ticket item](/data-models.md#ticketitem) itself (the `ticketItem.discountRate` property, referred to as "*di*" in the formula below). The value is normalized within an interval between 0 and 1, with the value rounded mathematically to four decimal places.

The effective discount rate (referred to as "*de"*) is calculated using the following formula:

$$
d\_{e}= 1- (1 - d\_{t}) \* (1 - d\_{ti})
$$

The outcome is value within an interval between 0 and 1, with the value rounded mathematically to four decimal places.

### Unit Price After Discount

The `plu.isDiscountAllowed` specifies, whether discount can be applied to ticket item. When value is `false`, unit price *after discount* is equal to unit price. However, when `plu.isDiscountAllowed` is set to `true`, the following formula is used to calculate the unit price after applying a discount:

$$
unitPriceIncludingDiscount = unitPrice - unitPrice \* d\_{e}
$$

The "*de"* refers to [effective discount rate](#effective-discount-rate). The outcome is expressed as a [UnitPrice](/data-models.md#unitprice), rounded mathematically to six decimal places.

### Unit Price After Discount Including VAT

The `plu.isDiscountAllowed` specifies, whether discount can be applied to ticket item. When value is `false`, unit price *after discount* including VAT is equal to unit price including VAT. However, when `plu.isDiscountAllowed` is set to `true`, the following formula is used to calculate the unit price after applying a discount, including VAT:

$$
unitPriceAfterDiscountIncludingVat = up - up \* d\_{e}
$$

* The "*up*" refers to [unit price including VAT](#unit-price-including-vat). The "*d*<sub>*e*</sub>*"* refers to [effective discount rate](#effective-discount-rate). The outcome is expressed as a [UnitPrice](/data-models.md#unitprice), rounded mathematically to six decimal places.

### Total Price Including VAT

The following formula is used to calculate the total price of a [ticket item](/data-models.md#ticketitem), including VAT:

$$
totalPriceIncludingVat = unitPriceIncludingVat \* quantity
$$

The "*quantity*" refers to `quantity.amount`. The outcome is expressed as a [Price](/data-models.md#price), rounded mathematically to two decimal places.

### Total Price After Discount Including VAT

The following formula is used to calculate the total price of a [ticket item](/data-models.md#ticketitem) after applying a discount, including VAT:

$$
totalPriceAfterDiscountIncludingVat = up  \* quantity
$$

The "*up*" refers to [unit price after discount including VAT](#unit-price-after-discount-including-vat). The "*quantity*" refers to `quantity.amount`.  The outcome is expressed as a [Price](/data-models.md#price), rounded mathematically to two decimal places.

When a `TicketItem` contains subitems, the formula to calculate the total price after applying a discount, including VAT, involves adding the sum of the total prices of all subitems:

$$
totalPriceAfterDiscountIncludingVat = (up  \* quantity) + \sum\_{i=1}^{n}(subitemPrice\_{i} \* quantity)
$$

Here, each `subitemPrice` refers to the [total price after discount including VAT](#total-price-after-discount-including-vat) for each subitem. The outcome is expressed as a [Price](/data-models.md#price), rounded mathematically to two decimal places.

### Discount Price Including VAT

The following formula is used to calculate the total discount of a [ticket item](/data-models.md#ticketitem) including VAT:

$$
discountPriceIncludingVat = tp - tp\_{d}
$$

The "tp" refers to [total price including VAT](#total-price-including-vat). The "*tpd*" refers to [total price after discount including VAT](#total-price-after-discount-including-vat). The outcome is expressed as a [Price](/data-models.md#price), rounded mathematically to two decimal places.

## Ticket Mathematics

The [`ticket`](/data-models.md#ticket) model contains following properties, relevant for mathematics:

* `discountRate`: the discount rate (numeric value between 0 and 100).
* `items`: collection of [ticket items](/data-models.md#ticketitem).
* `payments`: collection of [ticket payments](/data-models.md#ticketpayment).
* `roundingAmount`: the rounding amount.
* `type`: certain formulas might differ depending on the [ticket type](/data-models.md#tickettype). For instance, if the ticket is of the `invoice` [type](/data-models.md#tickettype), it won't have any elements in the \`items\` collection.

### Payments amount of Ticket

Total amount of payments.

$$
paymentsAmount = Σ (p ∈ payments)
p.amount.amount
$$

The notation represents the summation of the `amount.amount` property for each [ticket payment](/data-models.md#ticketpayment) within the `payments` collection. The expression `p ∈ payments` indicates the iteration over each element in the payments collection. The outcome is expressed as a [Price](/data-models.md#price), rounded mathematically to two decimal places.

### Total Price of Ticket Including VAT

Total [ticket](/data-models.md#ticket) amount, including VAT, before any discounts are applied.

If ticket is of the `invoice` [type](/data-models.md#tickettype), total price including VAT is equal to [payments amount](#payments-amount). Othewise, total price including VAT is calculated using the following formula:

$$
totalPriceIncludingVat = Σ (i ∈ items)  i.totalPriceIncludingVat
$$

The notation represents the summation of the `totalPriceIncludingVat`  property for each [ticket item](/data-models.md#ticketitem) within the `items` collection. The expression `i ∈ items` indicates the iteration over each element in the items collection. The outcome is expressed as a [Price](/data-models.md#price), rounded mathematically to two decimal places.

### Total Price After Discount of Ticket Including VAT

Total [ticket](/data-models.md#ticket) amount, including VAT, after discounts are applied.

If ticket is of the `invoice` [type](/data-models.md#tickettype), total price after discount including VAT is calculated using the following formula:

$$
t\_{invoice} = paymentsAmount - roundingAmount
$$

The "*paymentsAmount*" refers to [payments amount of ticket](#payments-amount-of-ticket). The "*roundingAmount*" refers to `roundingAmount` property of [ticket](/data-models.md#ticket). The outcome is expressed as a [Price](/data-models.md#price), rounded mathematically to two decimal places.

Otherwise, if ticket is not of `invoice` [type](/data-models.md#tickettype), total price after discount including VAT is calculated using the following formula:

$$
t\_{cashRegister} = Σ (i ∈ items) i.totalPriceAfterDiscountIncludingVat
$$

The notation represents the summation of the [total price after discount including VAT](#total-price-after-discount-including-vat)  for each [ticket item](/data-models.md#ticketitem) within the `items` collection. The expression `i ∈ items` indicates the iteration over each element in the items collection. The outcome is expressed as a [Price](/data-models.md#price), rounded mathematically to two decimal places.

### Total Price after Discount of Ticket Including VAT and Rounding

Total [ticket](/data-models.md#ticket) amount, including VAT and rounding, after discounts are applied.

If ticket is of the `invoice` [type](/data-models.md#tickettype), total price after discount including VAT and rounding equal to [payments amount of ticket](#payments-amount-of-ticket).  Otherwise, if ticket is not of `invoice` [type](/data-models.md#tickettype), total price after discount including VAT and rounding is calculated using the following formula:

$$
totalPriceAfterDiscountIncludingVat + roundingAmount
$$

The "*totalPriceAfterDiscountIncludingVat*" refers to [total price after discount of Ticket including VAT](#total-price-after-discount-of-ticket-including-vat). The "*roundingAmount*" refers to `roundingAmount` property of [ticket](/data-models.md#ticket). The outcome is expressed as a [Price](/data-models.md#price), rounded mathematically to two decimal places.

## Examples

### Example 1

Assume [ticket item](/data-models.md#ticketitem) with the following properties:

* unit price = 5.363636
* quantity = 1.234
* discount rate = 20%
* vat rate = 10%
* is discount allowed = yes

Unit price including VAT, VAT must be is calculated first, using following formula:

$$
vat = unitPrice \* ( vatRate / 100) = 5.363636 \* (10 / 100) = 0.5363636
$$

To get rounded VAT, result must be rounded to 6 decimal places:

$$
0.5363636 ≈ 0.536364
$$

To calculate unit price including VAT, we use formula:

$$
unitPriceIncludingVat = unitPrice + vat\_{rounded} = 5.363636 + 0.536364 = 5.90
$$

To calculate effective discount rate, we use formula:

$$
d\_{e}= 1 - (1 - d\_{t}) \* (1 - d\_{ti}) = 1-(1-0)*(1-0.2) = 1-(1*0.8)=0.2
$$

To calculate unit price after discount including VAT, we use formula:

$$
up - up \* d\_{e} =  5.90 - 5.90 \* 0.2 = 5.90 - 1.18 = 4.72
$$

To calculate total price including VAT, we use formula:

$$
totalPriceIncludingVat = unitPriceIncludingVat \* quantity  = 5.90 \* 1.234 ≈ 7.28
$$

To calculate total price after discount including VAT, we use formula:

$$
totalPriceAfterDiscountIncludingVat = up  \* quantity = 4.72 \* 1.234 ≈ 5.82
$$


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.portos.sk/api-reference/tickets/ticket-model-mathematics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
