The sample shows how to calculate fields like "total amount", depending on the changes of the other fields, including sub-collections like items in an order.
Created in response to the "total amount" qestion on the CUBA support forum.
The Order entity contains two sets of order items:
- Order items with products, specifying a product and quantity of the product in the order
- Order items with services, specifying a service and a number of hours to be provided in the order
The total price field of an order is calculated in the OrderEdit controller of the order-edit screen as sum of amount values of all order items, products and services.
Amounts of each line of the order items are calculated in the OrderProductItemEdit and OrderServiceItemEdit controllers of the product-item-edit and service-item-edit screens accordingly, using the following logic:
- For product order items: the product price (defined in the Product entity) is multiplied by the quantity field (defined in the OrderProductItem entity)
- For service order items: the service hourly price (defined in the Service entity) is multiplied by the hours field (defined in the OrderServiceItem entity)