Ejemplo n.º 1
0
    def is_active(self, dt):
        """
        Check if the rule is active on day, or on day and time.
        @param dt: a datetime instance
        @return: True if rule is active, else False
        """
        dt = dt.astimezone(TZ_INFO["Asia/Jerusalem"])

        day = dt.date()
        t = dt.time()

        if self.from_date and self.to_date and self.from_date <= day <= self.to_date:
            return True if t is None else self.from_hour <= t <= self.to_hour
        elif convert_python_weekday(day.weekday()) in self.get_weekdays():
            return True if t is None else self.from_hour <= t <= self.to_hour

        return False
Ejemplo n.º 2
0
    def is_active(self, dt):
        """
        Check if the rule is active on day, or on day and time.
        @param dt: a datetime instance
        @return: True if rule is active, else False
        """
        dt = dt.astimezone(TZ_INFO["Asia/Jerusalem"])

        day = dt.date()
        t = dt.time()

        if self.from_date and self.to_date and self.from_date <= day <= self.to_date:
            return True if t is None else self.from_hour <= t <= self.to_hour
        elif convert_python_weekday(day.weekday()) in self.get_weekdays():
            return True if t is None else self.from_hour <= t <= self.to_hour

        return False
Ejemplo n.º 3
0
def estimate_cost(est_duration, est_distance, country_code=settings.DEFAULT_COUNTRY_CODE, cities=None,
                  day=None, time=None, extras=None, meter_rules=None):
    """
    Calculate estimated cost for a taxi ride by meter (by estimated time and duration) or a flat rate.
    Returns a (cost, type) tuple.
    
    cities : a list of city ids. Should contain exactly two different cities.
    day    : day of ride (1-7)
    time   : datetime.time of ride
    extras : a list of extra costs to add. Phone order is automatically added to all rides.
    """

    # init defaults
    country = Country.objects.get(code=country_code)

    if extras is None:
        extras = []

    if not IsraelExtraCosts.PHONE_ORDER in extras:
        extras.append(IsraelExtraCosts.PHONE_ORDER)

    if cities is None:
        cities = []

    if day is None:
        day = convert_python_weekday(datetime.datetime.now().weekday())

    if time is None:
        time = datetime.datetime.now(TZ_INFO[settings.TIME_ZONE]).time()

    logging.debug("Calculating estimated cost with (duration, distance, day, time) = (%d. %d, %d, %s)" % (est_duration,est_distance, day, time))

    # Step 1: flat rate or by meter ?
    flat_rate_rules = []
    if flat_rate(cities):
        city_a = City.objects.get(id=cities[0])
        city_b = City.objects.get(id=cities[1])

        flat_rate_rules = list(country.flat_rate_rules.filter(city1=city_a, city2=city_b))
        flat_rate_rules.extend(list(country.flat_rate_rules.filter(city1=city_b, city2=city_a)))

        flat_rate_rules = filter_relevant_daytime_rules(flat_rate_rules, day, time)

        if len(flat_rate_rules) > 1:
            raise InvalidRuleSetup("Multiple flat rules for same city pair encountered: %s, %s" % (city_a.name, city_b.name))

    if flat_rate_rules:
        type = CostType.FLAT
        cost = flat_rate_rules.pop().fixed_cost
    else:
        type = CostType.METER
        if not meter_rules:
            meter_rules = country.metered_rules.all()

        cost = calculate_meter_cost(meter_rules, est_duration, est_distance, day, time)

    # Step 2: add extra costs (airport, etc.). Phone order is always added
    extra_charge_rules = [country.extra_charge_rules.filter(rule_name=extra).get() for extra in extras]
    extra_cost = sum([rule.cost for rule in extra_charge_rules])

    # return total cost
    logging.debug(u"Estimation %s cost: %d" % (type, cost + extra_cost))
    return cost + extra_cost, type
Ejemplo n.º 4
0
def estimate_cost(est_duration,
                  est_distance,
                  country_code=settings.DEFAULT_COUNTRY_CODE,
                  cities=None,
                  day=None,
                  time=None,
                  extras=None,
                  meter_rules=None):
    """
    Calculate estimated cost for a taxi ride by meter (by estimated time and duration) or a flat rate.
    Returns a (cost, type) tuple.
    
    cities : a list of city ids. Should contain exactly two different cities.
    day    : day of ride (1-7)
    time   : datetime.time of ride
    extras : a list of extra costs to add. Phone order is automatically added to all rides.
    """

    # init defaults
    country = Country.objects.get(code=country_code)

    if extras is None:
        extras = []

    if not IsraelExtraCosts.PHONE_ORDER in extras:
        extras.append(IsraelExtraCosts.PHONE_ORDER)

    if cities is None:
        cities = []

    if day is None:
        day = convert_python_weekday(datetime.datetime.now().weekday())

    if time is None:
        time = datetime.datetime.now(TZ_INFO[settings.TIME_ZONE]).time()

    logging.debug(
        "Calculating estimated cost with (duration, distance, day, time) = (%d. %d, %d, %s)"
        % (est_duration, est_distance, day, time))

    # Step 1: flat rate or by meter ?
    flat_rate_rules = []
    if flat_rate(cities):
        city_a = City.objects.get(id=cities[0])
        city_b = City.objects.get(id=cities[1])

        flat_rate_rules = list(
            country.flat_rate_rules.filter(city1=city_a, city2=city_b))
        flat_rate_rules.extend(
            list(country.flat_rate_rules.filter(city1=city_b, city2=city_a)))

        flat_rate_rules = filter_relevant_daytime_rules(
            flat_rate_rules, day, time)

        if len(flat_rate_rules) > 1:
            raise InvalidRuleSetup(
                "Multiple flat rules for same city pair encountered: %s, %s" %
                (city_a.name, city_b.name))

    if flat_rate_rules:
        type = CostType.FLAT
        cost = flat_rate_rules.pop().fixed_cost
    else:
        type = CostType.METER
        if not meter_rules:
            meter_rules = country.metered_rules.all()

        cost = calculate_meter_cost(meter_rules, est_duration, est_distance,
                                    day, time)

    # Step 2: add extra costs (airport, etc.). Phone order is always added
    extra_charge_rules = [
        country.extra_charge_rules.filter(rule_name=extra).get()
        for extra in extras
    ]
    extra_cost = sum([rule.cost for rule in extra_charge_rules])

    # return total cost
    logging.debug(u"Estimation %s cost: %d" % (type, cost + extra_cost))
    return cost + extra_cost, type