Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Decimal Price

Description / Background

Decimal price for Singapore expansion

Glossary

Private (https://app.clickup.com/9003122396/docs/8ca1fpw-35796/8ca1fpw-41516)

Objectives

  • User can see the price until the cents number
  • User can pay the prepaid booking with decimal number
  • System will not display for .00
  • This feature have flipper to enable or disable the feature

Backend Implementation

  • app/controllers/api/v5/pages_controller.rb: Added a new feature toggle for enabling decimal prices.
  • Adjusted the calculation for total amount and due amount to handle decimal values more accurately.
  •  Added new attributes to handle prices in both integer and decimal formats.
  •  Changed amount calculation to use floating-point numbers instead of integers.
  • Updated price formatting to conditionally remove cents if the price is a whole number.
  • Updated to handle and display prices accurately, including conditional formatting for whole numbers and decimals.
  • Added validation and monetization for amount in cents.
  • Added methods to handle price in both cents and decimal formats.
  • app/models/hh_package/pricing.rb: Changed price handling from integer to floating-point.
  • app/my_lib/hh_money.rb: Added methods to handle prices with and without cents.
  • app/my_lib/hh_package/reservation_packages/charge_calculator.rb: Updated calculations to handle decimal values accurately.
  • app/my_lib/hh_package/reservation_packages/voucher_calculator.rb: Adjusted voucher calculations to handle cents.
  • app/my_lib/my_active_merchants/gb_primepay_gateway.rb: Changed amount handling to use floating-point numbers.
  • app/my_lib/receptionist/credit_card_checker.rb: Changed charge amount handling to use floating-point numbers.
  • Changed charge amount handling to use floating-point numbers.
  • Added logic to handle prices in cents and decimals based on feature toggles.
  • Updated payment summary to handle decimal values and added a private method to format values to decimals.
  • A new currency/money field named amount_v2 is added to several models and calculations to handle monetary values with higher precision (to_d) while maintaining backward compatibility with the existing amount field (which uses to_i).
  • A new migration file (20250415062310_add_amount_v2_cents_to_reservation_vouchers.rb) adds a field amount_v2_cents to the reservation_vouchers table.
  • The ReservationVoucher model now includes the amount_v2 field using monetize. Methods dealing with amount are updated to use amount_v2 where necessary.
  • Example updates include:
    • Using amount.to_d for precise calculations.
    • Updating assign_amount_voucher to store both the integer (to_i) and decimal (to_d) representations.
  • Controllers like api/v5/vouchers_controller.rb and dashboard/v2/reservations_controller.rb are modified to include amount_v2 in their logic and response payloads.
  • Example: The serialized_voucher method now includes vouchers_amount_v2 in the metadata.
  • Services such as partner_service/bills/calculate_service.rb now use amount_v2_cents for summing voucher values instead of amount_cents.
  • VoucherForm::Apply introduces a new method vouchers_amount_v2 to calculate the total amount using the amount_v2 field.
  • The Api::V5::ReservationSerializer now includes amount_v2 in its serialized output for vouchers.
  • Updates in views like admin/vouchers/show.html.slim to display amount_v2 instead of amount.
  • The voucher_calculator class is updated with logic for handling amount_v2 in various calculations (e.g., get_voucher_amount_v2assign_used_vouchers).
  • Both amount and amount_v2 exist in parallel, ensuring backward compatibility while transitioning to the new field.
  • Handles backfill for the amount_v2 field on charges.
  • Changes include:
    • Skipping charges where amount is nil or zero.
    • If amount is nil, it sets it to 0 and updates the database without validations.
    • Updates the amount_v2_cents and amount_v2_currency fields directly in the database using update_column.
  • Handles backfill for the total_price_v2_cents and charge_price_v2_cents fields on reservations.
  • Changes include:
    • If no reservations are found to process, it raises an ActiveRecord::Rollback instead of just returning.
    • Updates total_price_v2_cents and charge_price_v2_cents directly in the database using update_column.
  • A placeholder worker for backfilling decimals. No diff was generated, so the exact implementation isn't shown.
  • The workers now prefer update_column for direct database updates instead of save! to skip validations and improve performance.
  • Added a new entry in sidekiq_scheduler.yml:
    • backfill_decimals_price_and_charge:
      • Scheduled to run daily at midnight (UTC+7 timezone).
      • Executes the ScheduleWorkers::Decimals::BackfillDecimals worker.

[

github.com

https://github.com/hungryhub-team/hh-server/pull/6395/files

](https://github.com/hungryhub-team/hh-server/pull/6395/files)

[

github.com

https://github.com/hungryhub-team/hh-server/pull/6369

](https://github.com/hungryhub-team/hh-server/pull/6369)

[

github.com

https://github.com/hungryhub-team/hh-server/pull/6395/commits/ca894a1821be89c87893023a1f6a143864a9c80f

](https://github.com/hungryhub-team/hh-server/pull/6395/commits/ca894a1821be89c87893023a1f6a143864a9c80f)

[

github.com

https://github.com/hungryhub-team/hh-server/pull/6542

](https://github.com/hungryhub-team/hh-server/pull/6542)

[

github.com

https://github.com/hungryhub-team/hh-server/pull/6511

](https://github.com/hungryhub-team/hh-server/pull/6511)

Hybrid Implementation

  1. getBookingDetail.ts:
    • Added new fields chargePriceDecimal and totalPriceDecimal, which store price values as strings.
  2. searchRestaurantGraphql.ts:
    • Added amountCents to the priceAndPricingType object, which stores the price in cents.
  3. BookingSummary.vue:
    • Updated the display of the total price to use a new isInCents parameter for formatting money.
  4. RestaurantCardRoot.vue:
    • Added isInCents parameter to the formatMoney function call.
  5. string.ts:
    • Modified the formatMoney function to handle the isInCents parameter. If isInCents is true, it converts cents to the main currency unit before formatting.
  6. onHoldPrice.ts:
    • Changed the pricePerPerson and price fields to use chargePriceCents.
    • Added the isInCents parameter when formatting money.
  7. restaurant.ts:
    • Added amountCents to the priceV2 object.
  8. BookingConfirmationDesktop.vue and BookingConfirmationMobile.vue:
    • Added the isInCents parameter when formatting money.
  9. PaymentSuccessPage.vue:
    • Added the isInCents parameter when formatting money.
    • Modified the calculation of amount and chargePrice to use cents values.
  10. QrPackagePage.vue and QrPackagePageDesktop.vue:
    • Added the isInCents parameter when formatting money.
  11. SearchRestaurantCard.vue:
    • Added currency to the price display.
    • Imported convertToNumber and used it to format the lowest price.
  12. SelectDateTime.vue:
    • Simplified icon class names.
  13. PopUpTotalPrice.vue:
    • Changed the totalPrice field to totalPackagePriceCents and added the isInCents parameter when formatting money.
  14. BookingEditChargeSummary.vue:
    • Added new fields chargePriceCentstotalPriceCents, and totalPackagePriceCents to the chargeObject.
  15. BookingSummary.vue:
    • Changed the totalPrice field to totalPriceCents.
  16. BreadCrumbs.vue:
    • Simplified class names.
  17. bookingCharge.ts:
    • Added chargePriceCents and totalPriceCents fields to the ChargeObject type.
    • Modified functions to use these new fields and convert cents to the main currency unit.
  18. chargeSummary.ts:
    • Added the chargePriceCents and totalPriceCents fields to the ChargeObject type.
    • Modified functions to use these new fields and convert cents to the main currency unit.
  19. Booking.ts:
    • Added chargePriceCentstotalPriceCents, and totalPackagePriceCents to the bookingChargeSchema.
  20. Restaurant.ts:
    • Added amountCents to the pricePerPerson and priceAndPricingType objects.

[

github.com

https://github.com/hungryhub-team/hh-pegasus/pull/1678

](https://github.com/hungryhub-team/hh-pegasus/pull/1678)

Partner Portal Implementation

  • The original implementation used accounting.formatMoney to format the price with no decimal places (precision set to 0).
  • Sets the precision to 2 decimal places (for more accurate monetary representation).
  • Checks if the resulting formattedPrice ends with .00.
    • If true, it removes the trailing .00 for cleaner formatting.
    • Otherwise, it returns the formatted price as-is.
  • The code now checks if there is a preformatted price (item.price_format). If it exists, it uses item.price_format; otherwise, it falls back to formatting the price with formatMoney.
  • This ensures that any preformatted price provided is prioritized over dynamically formatting it.
  • The total package price is now calculated from response.data?.total_package_price_cents by dividing it by 100 (to convert from cents to the base currency). The result is then stored in this.totalPackagePrice.
  • This change standardizes the handling of monetary values to use cents, which is a common practice to avoid floating-point errors in calculations.
  • The function now uses accounting.formatMoney to format the price but replaces trailing .00.X0, or .0X with a more compact representation (e.g., removes unnecessary zeros). The new regex ensures that only significant decimal places are retained.
  • This change improves the formatting of prices, making them cleaner and easier to read.

[

github.com

https://github.com/hungryhub-team/book-bite/pull/656

](https://github.com/hungryhub-team/book-bite/pull/656)

[

github.com

https://github.com/hungryhub-team/book-bite/pull/792

](https://github.com/hungryhub-team/book-bite/pull/792)

PRD & Task

Private (https://app.clickup.com/t/86cy3bqmr)

Design

no design