stats_prices_calculation
Checkout (line items) stats prices calculation
Main class: Checkouts::CalculateStatsPricesService
Checkout structure (only related fields):
line_items- array ofLineItemobjectsdiscounted_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 discountsdebt_amount- debt amount that customer cannot pay (part of full price of the checkout) and should pay in the futuregift_cards- array ofgift_cardstructures that look like{"id": <id of marketing promotion>, "amount_cents": <amount>}
LineItem structure (only related fields):
deleted- flag which indicates whether the line item has been deletedselling_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 customeraccounting_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 itrecharge_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 itemstats_promo_price- how much should be accounted at stats reporting level for this line itemtaxable_credit_amount_cents- amount of credit cents that are taxable for this line itemdiscounted_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 ofgift_cardstructures 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 onlystats_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_centsto difference between checkout'sdiscounted_price_centsand [partial_amount_cents](#partial_amount_cents-calculation (sum of all previous line itemsstats_fiscal_price_cents)
- in normal case we just use discounted_price_cents as a
-
taxable subscription bundle- line item has applied subscription bundle that is taxable. In this case we calculatestats_fiscal_price_cents,taxable_credit_amount_centsandstats_promo_price_centsas a multiplication of marketing_promotion_line_item_price_cents andline_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.stats_fiscal_price_centsis 0 in this casestats_promo_price_centsis calculated as a multiplication of marketing_promotion_line_item_price_cents andline_item.quantity
-
taxable prepaid marketing promotion- line item has applied prepaid marketing promotion (gift card of voucher) that is taxable. In this casestats_promo_price_centsandstats_fiscal_price_centsare equal and calculated asdiscounted_price_centsof the line_item.taxable_credit_amount_centswill be calculated as a sum of taxableamount_centsin thegift_cardsarray -
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_centswill be sum of all non taxableamount_centsin thegift_cardsarraystats_promo_price_centswill be a difference betweendiscounted_price_centsof the line_item andstats_fiscal_price_cents
-
last line item in the list- in this casestats_fiscal_price_centsandstats_promo_price_centswill be equal to a difference between checkout'sdiscounted_price_centsand partial_amount_cents -
any other line item- in this casestats_fiscal_price_centsandstats_promo_price_centsare 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_priceis zero anddiscount_typeis 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_amountis positive - checkout's
discounted_priceis not zero ordiscount_typeis 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_resourceand calculate the value asmarketing_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