# Ticket Model Mathematics

Exploring calculations and logic within Tickets and Ticket Items

The 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 model and related models like `TicketItem`

.

**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.

## Ticket Item Mathematics

The `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:

The "vatRate" stands for `plu.vatInfo.rate`

. The outcome of *vat *is expressed as a UnitPrice, rounded mathematically to six decimal places, refered as "*vat rounded*". The unit price including VAT of ticket item is then calculated using following formula:

The outcome of second formula is expressed as a 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 in which the ticket item 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 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:

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:

The "*de"* stands for effective discount rate. The outcome is expressed as a 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:

The "*up*" stands for unit price including VAT. The "*de"* stands for effective discount rate. The outcome is expressed as a 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, including VAT:

The "*quantity*" stands for `quantity.amount`

. The outcome is expressed as a 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 after applying a discount, including VAT:

The "*up*" stands for unit price after discount including VAT. The "*quantity*" stands for `quantity.amount`

. The outcome is expressed as a 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 including VAT:

The "tp" stands for total price including VAT. The "*tpd*" stands for total price after discount including VAT. The outcome is expressed as a Price, rounded mathematically to two decimal places.

## Ticket Mathematics

The `ticket`

model contains following properties, relevant for mathematics:

`discountRate`

: the discount rate (numeric value between 0 and 100).`items`

: collection of ticket items.`payments`

: collection of ticket payments.`roundingAmount`

: the rounding amount.`type`

: certain formulas might differ depending on the ticket type. For instance, if the ticket is of the`invoice`

type, it won't have any elements in the `items` collection.

### Payments amount of Ticket

Total amount of payments.

The notation represents the summation of the `amount.amount`

property for each ticket payment 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, rounded mathematically to two decimal places.

### Total Price of Ticket Including VAT

Total ticket amount, including VAT, before any discounts are applied.

If ticket is of the `invoice`

type, total price including VAT is equal to payments amount. Othewise, total price including VAT is calculated using the following formula:

The notation represents the summation of the `totalPriceIncludingVat`

property for each ticket item 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, rounded mathematically to two decimal places.

### Total Price After Discount of Ticket Including VAT

Total ticket amount, including VAT, after discounts are applied.

If ticket is of the `invoice`

type, total price after discount including VAT is calculated using the following formula:

The "*paymentsAmount*" stands for payments amount of ticket. The "*roundingAmount*" stands for `roundingAmount`

property of ticket. The outcome is expressed as a Price, rounded mathematically to two decimal places.

Otherwise, if ticket is not of `invoice`

type, total price after discount including VAT is calculated using the following formula:

The notation represents the summation of the total price after discount including VAT for each ticket item 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, rounded mathematically to two decimal places.

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

Total ticket amount, including VAT and rounding, after discounts are applied.

If ticket is of the `invoice`

type, total price after discount including VAT and rounding equal to payments amount of ticket. Otherwise, if ticket is not of `invoice`

type, total price after discount including VAT and rounding is calculated using the following formula:

The "*totalPriceAfterDiscountIncludingVat*" stands for total price after discount of Ticket including VAT. The "*roundingAmount*" stands for `roundingAmount`

property of ticket. The outcome is expressed as a Price, rounded mathematically to two decimal places.

## Examples

### Example 1

Assume ticket item 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:

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

To calculate unit price including VAT, we use formula:

To calculate effective discount rate, we use formula:

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

To calculate total price including VAT, we use formula:

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

Last updated