Price Calculation - aelassas/bookcars GitHub Wiki

Table of Contents

  1. Pricing Fields
  2. Discounted Prices
  3. Date-Based Price Rates
    1. How to Set
    2. Behavior
  4. Price Change Rate
  5. Calculation Algorithm
    1. Date-Based Price Rates Calculation
    2. Default Pricing Calculation
      1. Basic Rules
      2. Examples
        1. Example 1: 10 Hours
        2. Example 2: 36 Hours
        3. Example 3: 10 Days
        4. Example 4: 42 Days
    3. Function Reference

Pricing Fields

Each car in BookCars supports a set of flexible pricing fields to accommodate different rental durations and discount strategies.

Field Duration Required Description
Daily Price 1 day βœ… Yes Base daily rental price
Discounted Daily Price 1 day ❌ No Reduced daily rate
Hourly Price 1 hour ❌ No Base price for hourly rentals
Discounted Hourly Price 1 hour ❌ No Reduced hourly rate
Bi-Weekly Price 3 days ❌ No Price for a 3-day rental block
Discounted Bi-Weekly Price 3 days ❌ No Reduced price for a 3-day rental block
Weekly Price 7 days ❌ No Price for a 7-day rental
Discounted Weekly Price 7 days ❌ No Reduced weekly price
Monthly Price 30 days ❌ No Price for a 30-day rental
Discounted Monthly Price 30 days ❌ No Reduced monthly price
Date-Based Price Rates Custom dates ❌ No Overrides daily pricing for specific date ranges

Only Daily Price is required. All other pricing fields are optional.

Discounted Prices

For each pricing tier (hourly, daily, bi-weekly, weekly, monthly), a discounted price can be set.

  • If a discounted price is provided, it will override the regular price during price calculation.
  • The discounted price must be the final amount (i.e., already reduced).
  • Example:
    If WeeklyPrice = $200 and you want to give a $20 discount, then
    DiscountedWeeklyPrice = $180

Important: Do not set the discounted price as the discount amount. Always use the total after discount.
Incorrect values will result in inaccurate pricing.

Date Based Price Rates

BookCars allows defining custom daily prices for specific date ranges (e.g. holidays, peak seasons).

How to set:

  1. Go to Create Car or Update Car page in the Admin Panel.
  2. Enable the toggle: Date-Based Price
  3. Add entries with:
    • startDate
    • endDate
    • dailyPrice

To define a rate for a single day (e.g. 02/16/2025), set startDate and endDate to the same value.

Behavior:

  • Date-based rates are used in place of daily price during price calculation.
  • If no match is found for a date, the system falls back to the default Daily Price or Discounted Daily Price.

Price Change Rate

You can set a price change rate (+/- %) from the Create Supplier and Update Supplier pages in the admin dashboard. This rate is applied to calculate the total rental price, and it can be either positive (markup) or negative (discount). Only admins can edit this field.

If the price change rate is positive, the total price increases.

If the price change rate is negative, the total price decreases.

For example if the price change rate is equal to 10% and the rental price is $100, the total price will be: $100 + $100 * 10% = $110

And if the price change rate is equal to -10% and the rental price is $100, the total price will be: $100 - $100 * 10% = $90

This feature allows flexible pricing adjustments based on supplier preferences.

Calculation Algorithm

BookCars supports two modes of pricing calculation:

Date-Based Price Rates Calculation

If the Date-based price rates option is enabled, pricing is calculated per day based on the configured date-specific rates.

How it works:

  1. The calculation starts at the booking start date.
  2. For each day in the rental period:
    • The system checks if the current date falls within any defined price rate range (startDate to endDate).
    • If a match is found, the corresponding custom price rate is applied.
    • If no match is found, the system falls back to the car’s daily or discounted daily price.
  3. The matched price is added to the total.
  4. The loop continues until the booking end date is reached.

Default Pricing Calculation

If date-based pricing is disabled, pricing is calculated using the car’s predefined pricing tiers.

Basic Rules

  • If no pricing tiers are set:
    Price = TotalDays Γ— DailyPrice

  • If hourly price is set and rental duration is less than one day:
    Price = TotalHours Γ— HourlyPrice

  • If hourly price is set and rental duration is more than one day:
    Price = (FullDays Γ— DailyPrice) + (RemainingHours Γ— HourlyPrice)

  • If hourly price is not set and rental is less than one day:
    Price = 1 Γ— DailyPrice

  • If weekly / bi-weekly / monthly prices are set:
    The system selects the most cost-efficient combination of:

    • MonthlyPrice
    • WeeklyPrice
    • BiWeeklyPrice (3-day blocks)
    • DailyPrice
    • HourlyPrice (for leftover hours)

Examples

Example 1: Rental for 10 Hours

  1. If HourlyPrice is set:
    Price = 10 Γ— HourlyPrice

  2. If HourlyPrice is not set:
    Price = 1 Γ— DailyPrice

Example 2: Rental for 36 Hours (1 day and 12 hours)

  1. If HourlyPrice is set:
    Price = 1 Γ— DailyPrice + 12 Γ— HourlyPrice

  2. If HourlyPrice is not set:
    Price = 2 Γ— DailyPrice

Example 3: Rental for 10 Days

  1. If Weekly and Bi-Weekly prices are set:
    Price = 1 Γ— WeeklyPrice + 1 Γ— BiWeeklyPrice

  2. If only Bi-Weekly is set:
    Price = 3 Γ— BiWeeklyPrice + 1 Γ— DailyPrice

  3. If no pricing tiers are set:
    Price = 10 Γ— DailyPrice

Example 4: Rental for 42 Days

  1. If Monthly, Weekly, and Bi-Weekly prices are set:
    Price = 1 Γ— MonthlyPrice + 1 Γ— WeeklyPrice + 1 Γ— BiWeeklyPrice + 2 Γ— DailyPrice

  2. If Monthly and Weekly prices are set:
    Price = 1 Γ— MonthlyPrice + 1 Γ— WeeklyPrice + 5 Γ— DailyPrice

  3. If Monthly and Bi-Weekly prices are set:
    Price = 1 Γ— MonthlyPrice + 4 Γ— BiWeeklyPrice

  4. If only Monthly price is set:
    Price = 1 Γ— MonthlyPrice + 12 Γ— DailyPrice

  5. If no pricing tiers are set:
    Price = 42 Γ— DailyPrice

Function Reference

This pricing logic is implemented in the following function: calculatePrice

This function is used by both the frontend and mobile app to ensure consistent and accurate pricing across all platforms.

It dynamically applies the appropriate price tiers based on:

  • Rental duration (in days and hours)
  • Available pricing fields (hourly, daily, bi-weekly, weekly, monthly)
  • Optional date-based overrides