# Ticket Model Mathematics

The [Ticket](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#ticket) model and related models like [`TicketItem`](https://developers.portos.sk/data-models#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`](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#unitprice), rounded mathematically to six decimal places, refered as "*vat rounded*". The unit price including VAT of [ticket item](https://developers.portos.sk/data-models#ticketitem) is then calculated using following formula:

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

The outcome of second formula is expressed as a [UnitPrice](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#ticket) in which the [ticket item](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#ticketitem), including VAT:

$$
totalPriceIncludingVat = unitPriceIncludingVat \* quantity
$$

The "*quantity*" refers to `quantity.amount`. The outcome is expressed as a [Price](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#price), rounded mathematically to two decimal places.

## Ticket Mathematics

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

* `discountRate`: the discount rate (numeric value between 0 and 100).
* `items`: collection of [ticket items](https://developers.portos.sk/data-models#ticketitem).
* `payments`: collection of [ticket payments](https://developers.portos.sk/data-models#ticketpayment).
* `roundingAmount`: the rounding amount.
* `type`: certain formulas might differ depending on the [ticket type](https://developers.portos.sk/data-models#tickettype). For instance, if the ticket is of the `invoice` [type](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#price), rounded mathematically to two decimal places.

### Total Price of Ticket Including VAT

Total [ticket](https://developers.portos.sk/data-models#ticket) amount, including VAT, before any discounts are applied.

If ticket is of the `invoice` [type](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#price), rounded mathematically to two decimal places.

### Total Price After Discount of Ticket Including VAT

Total [ticket](https://developers.portos.sk/data-models#ticket) amount, including VAT, after discounts are applied.

If ticket is of the `invoice` [type](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#ticket). The outcome is expressed as a [Price](https://developers.portos.sk/data-models#price), rounded mathematically to two decimal places.

Otherwise, if ticket is not of `invoice` [type](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#price), rounded mathematically to two decimal places.

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

Total [ticket](https://developers.portos.sk/data-models#ticket) amount, including VAT and rounding, after discounts are applied.

If ticket is of the `invoice` [type](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#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](https://developers.portos.sk/data-models#ticket). The outcome is expressed as a [Price](https://developers.portos.sk/data-models#price), rounded mathematically to two decimal places.

## Examples

### Example 1

Assume [ticket item](https://developers.portos.sk/data-models#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
$$
