Skip to main content

stats_prices_calculation

Checkout (line items) stats prices calculation​

Main class: Checkouts::CalculateStatsPricesService

  • line_items - array of LineItem objects
  • discounted_price_cents - total price of all line items after applying all discounts (calculated on FE)
  • total_price_cents - total price of all line items before applying any discounts
  • debt_amount - debt amount that customer cannot pay (part of full price of the checkout) and should pay in the future
  • gift_cards - array of gift_card structures that look like {"id": <id of marketing promotion>, "amount_cents": <amount>}
  • deleted - flag which indicates whether the line item has been deleted
  • selling_marketing_promotion_id, accounting_marketing_promotion_id, recharge_marketing_promotion_id - id of different types of marketing promotions that were applied to this line item:
    • selling_marketing_promotion - marketing promotion that is sold to customer
    • accounting_marketing_promotion - marketing promotion (VIP card or subscription bundle) that will be used later in the salon. When the final user buys it, he/she pays online but the salon needs to issue a receipt/invoice for it, and the field is here for it
    • recharge_marketing_promotion - marketing promotion that was recharged by the line item
  • stats_fiscal_price - how much should be accounted (and VAT taxed) for this line item
  • stats_promo_price - how much should be accounted at stats reporting level for this line item
  • taxable_credit_amount_cents - amount of credit cents that are taxable for this line item
  • discounted_price_cents - price of this line item after applying discounts (calculated on FE)
  • gift_cards - gift cards that are applied to the line_item. Array of gift_card structures that look like {"id": <id of marketing promotion>, "amount_cents": <amount>}

Process​

We process each line item in the checkout one by one. On every step we update partial_amount_cents by summing it (0 at start) with stats_fiscal_price_cents of the current line item. We do that to avoid rounding errors in couple of specific cases. For each line item we can have different states (we check condition of each state in the order they are listed below and stop on the first one that is true):

  • deleted - line item was deleted and all stats prices are 0

  • use marketing promotion - line item has marketing promotion applied to it (selling_marketing_promotion/accounting_marketing_promotion/recharge_marketing_promotion). We calculate only stats_fiscal_price.

    • in normal case we just use discounted_price_cents as a stats_fiscal_price_cents
    • in case when line_item is the last one in the checkout's line_items list we set stats_fiscal_price_cents to difference between checkout's discounted_price_cents and [partial_amount_cents](#partial_amount_cents-calculation (sum of all previous line items stats_fiscal_price_cents)
  • taxable subscription bundle - line item has applied subscription bundle that is taxable. In this case we calculate stats_fiscal_price_cents, taxable_credit_amount_cents and stats_promo_price_cents as a multiplication of marketing_promotion_line_item_price_cents and line_item.quantity. We don't update partial_amount_cents in this case because since this amounts are only taxable and not actually paid by customer at checkout time

  • non taxable subscription bundle - line item has applied subscription bundle that is non taxable.

  • taxable prepaid marketing promotion - line item has applied prepaid marketing promotion (gift card of voucher) that is taxable. In this case stats_promo_price_cents and stats_fiscal_price_cents are equal and calculated as discounted_price_cents of the line_item. taxable_credit_amount_cents will be calculated as a sum of taxable amount_cents in the gift_cards array

  • non taxable prepaid marketing promotion - line item has applied prepaid marketing promotion (gift card of voucher) that is non taxable. In this case:

    • stats_fiscal_price_cents will be sum of all non taxable amount_cents in the gift_cards array
    • stats_promo_price_cents will be a difference between discounted_price_cents of the line_item and stats_fiscal_price_cents
  • last line item in the list - in this case stats_fiscal_price_cents and stats_promo_price_cents will be equal to a difference between checkout's discounted_price_cents and partial_amount_cents

  • any other line item - in this case stats_fiscal_price_cents and stats_promo_price_cents are equal to discounted_price_cents

discounted_price_cents calculation​

We have two cases when we don't need to use checkout's global discount (discount that applied to whole checkout):

  • checkout has any kind of gift card applied to it
  • checkout's discounted_price is zero and discount_type is present

In this cases we just use line_item.discounted_price_cents

Also we have cases when we have to use checkout's global discount:

  • checkout's debt_amount is positive
  • checkout's discounted_price is not zero or discount_type is not present

In this cases discounted_price_cents will be calculated as a lint_item.discounted_price_cents after applying of global discount.

partial_amount_cents calculation​

On every step (except subscription bundle cases) we update partial_amount_cents by summing it (0 at start) with stats_fiscal_price_cents of the current line item. We do that to avoid rounding errors in couple of specific cases.

marketing_promotion_line_item_price_cents calculation​

First we trying to find real marketing promotion usage for the specific line_item.

  • If it exists, we get marketing_promotion_resource and calculate the value as marketing_promotion_resource.discounted_price_cents / marketing_promotion_resource.redemptions_count_possible
  • If it doesn't exist, we searching for the used marketing promotion and calculate the value as marketing_promotion.selling_price_cents / redemptions_count_possible