File size: 3,937 Bytes
e1835d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import googlemaps
import datetime
import os

# Crear un cliente de la API de Google Maps
gmaps = googlemaps.Client(key=os.environ["GOOGLE_MAPS_API_KEY"])


class Trip:
    def __init__(self, origin, destination, departure_date, return_date, cost, car_type, pax):
        self.origin = origin
        self.destination = destination
        self.departure_date = departure_date
        self.return_date = return_date  # esto puede ser None ahora
        self.cost = cost
        self.car_type = car_type
        self.pax = pax

    def to_dict(self):
        trip_dict = {
            'origin': self.origin,
            'destination': self.destination,
            'cost': self.cost,
            'car_type': self.car_type,
            'departure_date': self.departure_date.isoformat(),
            'pax': self.pax
        }
        if self.return_date is not None:
            trip_dict['return_date'] = self.return_date.isoformat()
        return trip_dict


def determine_car_type(pax):
    if 0 < pax < 6:
        return "Transporter"
    elif pax < 16:
        return "Crafter"
    elif pax < 23:
        return "Minibus"
    elif pax < 50:
        return "Bus"
    else:
        return "Invalid"


def get_distance(origin: str, destination: str):
    matrix = gmaps.distance_matrix(origin, destination)
    distance_meters = matrix['rows'][0]['elements'][0]['distance']['value']
    return distance_meters / 1000


def calculate_cost(distance, car_type):
    base_cost_per_km = {
        "Transporter": 50,
        "Crafter": 75,
        "Minibus": 100,
        "Bus": 150
    }
    cost = distance * base_cost_per_km[car_type]
    return round(max(500, min(20000, cost)))


def generate_trip_data(origin: str, destination: str, date_from: datetime, date_to: datetime, car_type: str, pax: int):
    distance = get_distance(origin, destination)
    base_cost = calculate_cost(distance, car_type)
    trip = Trip(origin, destination, date_from,
                date_to, base_cost, car_type, pax)
    return trip


def get_trip(origin: str, destination: str, date_from: str, date_to=None, pax=1, round_trip: bool = False):
    date_from = datetime.datetime.strptime(date_from, "%Y-%m-%d")

    if date_to is not None:
        date_to = datetime.datetime.strptime(date_to, "%Y-%m-%d")

    car_type = determine_car_type(pax)

    trip_origin = generate_trip_data(
        origin, destination, date_from, date_to if round_trip else None, car_type, pax)

    trip_return = None
    if round_trip and date_to is not None:
        trip_return = generate_trip_data(
            destination, origin, date_to, None, car_type, pax)

    trip_origin_dict = trip_origin.to_dict()

    trip_info = "The trip to your destination is from " + trip_origin_dict['origin'] + " to " + trip_origin_dict['destination'] + " with a cost of " + str(
        trip_origin_dict['cost']) + " and a car type of " + trip_origin_dict['car_type'] + ". "

    if trip_return is not None:
        trip_return_dict = trip_return.to_dict()
        trip_info += "The return trip is from " + trip_return_dict['origin'] + " to " + trip_return_dict['destination'] + " with a cost of " + str(
            trip_return_dict['cost']) + " and a car type of " + trip_return_dict['car_type'] + "."

    return trip_info


def get_trip_info(trip_origin: Trip, trip_return: Trip = None):
    trip_origin = trip_origin.to_dict()
    trip_info = "The trip to your destination is from " + trip_origin['origin'] + " to " + trip_origin['destination'] + " with a cost of " + str(
        trip_origin['cost']) + " and a car type of " + trip_origin['car_type'] + ". "

    if trip_return is not None:
        trip_return = trip_return.to_dict()
        trip_info += "The return trip is from " + trip_return['origin'] + " to " + trip_return['destination'] + " with a cost of " + str(
            trip_return['cost']) + " and a car type of " + trip_return['car_type'] + "."

    return trip_info