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
3 |
+
from datetime import datetime
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
# MongoDB connection details
|
7 |
+
MONGO_URI = "mongodb+srv://kmrlogistics:[email protected]/"
|
8 |
+
DB_NAME = "trucking"
|
9 |
+
COLLECTION_NAME_DRIVERS = "drivers"
|
10 |
+
COLLECTION_NAME_TRUCKS = "trucks"
|
11 |
+
COLLECTION_NAME_OWNER_OPERATORS = "owner_operators"
|
12 |
+
|
13 |
+
# Establish a connection to MongoDB
|
14 |
+
client = MongoClient(MONGO_URI)
|
15 |
+
db = client[DB_NAME]
|
16 |
+
drivers_collection = db[COLLECTION_NAME_DRIVERS]
|
17 |
+
trucks_collection = db[COLLECTION_NAME_TRUCKS]
|
18 |
+
owner_operators_collection = db[COLLECTION_NAME_OWNER_OPERATORS]
|
19 |
+
|
20 |
+
# User authentication function
|
21 |
+
def login(username, password):
|
22 |
+
"""Check the provided username and password."""
|
23 |
+
return username == "KMR LOGISTICS" and password == "kmr2024"
|
24 |
+
|
25 |
+
# Initialize Streamlit session state for login
|
26 |
+
if "logged_in" not in st.session_state:
|
27 |
+
st.session_state.logged_in = False
|
28 |
+
|
29 |
+
# Streamlit app layout
|
30 |
+
st.title("Kmr Logistics Management")
|
31 |
+
|
32 |
+
# Login section
|
33 |
+
st.sidebar.header("User Login")
|
34 |
+
|
35 |
+
if not st.session_state.logged_in:
|
36 |
+
username = st.sidebar.text_input("Username")
|
37 |
+
password = st.sidebar.text_input("Password", type="password")
|
38 |
+
|
39 |
+
if st.sidebar.button("Login"):
|
40 |
+
if login(username, password):
|
41 |
+
st.session_state.logged_in = True
|
42 |
+
st.sidebar.success("Logged in successfully!")
|
43 |
+
else:
|
44 |
+
st.sidebar.error("Invalid username or password.")
|
45 |
+
else:
|
46 |
+
st.sidebar.success("Logged in as kmr logistics.")
|
47 |
+
|
48 |
+
# Show the main application only if the user is logged in
|
49 |
+
if st.session_state.logged_in:
|
50 |
+
# Page navigation
|
51 |
+
page = st.sidebar.selectbox("Select a page", [
|
52 |
+
"Add Truck",
|
53 |
+
"Add Driver",
|
54 |
+
"Add Owner Operator",
|
55 |
+
"Assign Truck",
|
56 |
+
"Add Load",
|
57 |
+
"Update/Delete Load",
|
58 |
+
"View Owner Operator Info",
|
59 |
+
"View Information"
|
60 |
+
])
|
61 |
+
|
62 |
+
if page == "Add Truck":
|
63 |
+
st.header("Add Truck Information")
|
64 |
+
truck_name = st.text_input("Truck Name")
|
65 |
+
truck_plate_number = st.text_input("Truck Plate Number")
|
66 |
+
|
67 |
+
if st.button("Save Truck Info"):
|
68 |
+
if truck_name and truck_plate_number:
|
69 |
+
# Check if truck exists
|
70 |
+
existing_truck = trucks_collection.find_one({"truck_plate_number": truck_plate_number})
|
71 |
+
if existing_truck:
|
72 |
+
st.warning(f"Truck with Plate Number {truck_plate_number} already exists.")
|
73 |
+
else:
|
74 |
+
truck_data = {
|
75 |
+
"truck_name": truck_name,
|
76 |
+
"truck_plate_number": truck_plate_number
|
77 |
+
}
|
78 |
+
trucks_collection.insert_one(truck_data)
|
79 |
+
st.success(f"Truck {truck_name} added successfully!")
|
80 |
+
else:
|
81 |
+
st.error("Please fill in all fields.")
|
82 |
+
|
83 |
+
elif page == "Add Driver":
|
84 |
+
st.header("Add Driver Information")
|
85 |
+
first_name = st.text_input("First Name")
|
86 |
+
last_name = st.text_input("Last Name")
|
87 |
+
contact_info = st.text_input("Contact Info")
|
88 |
+
pay_per_mile = st.number_input("Driver Pay per Mile", min_value=0.0, format="%f")
|
89 |
+
|
90 |
+
if st.button("Save Driver Info"):
|
91 |
+
if first_name and last_name and contact_info and pay_per_mile:
|
92 |
+
full_name = f"{first_name} {last_name}"
|
93 |
+
|
94 |
+
# Check if driver exists
|
95 |
+
existing_driver = drivers_collection.find_one({"contact_info": contact_info})
|
96 |
+
if existing_driver:
|
97 |
+
st.warning(f"Driver with Contact Info {contact_info} already exists.")
|
98 |
+
else:
|
99 |
+
driver_data = {
|
100 |
+
"first_name": first_name,
|
101 |
+
"last_name": last_name,
|
102 |
+
"contact_info": contact_info,
|
103 |
+
"pay_per_mile": pay_per_mile,
|
104 |
+
"trucks": [],
|
105 |
+
"loads": []
|
106 |
+
}
|
107 |
+
drivers_collection.insert_one(driver_data)
|
108 |
+
st.success(f"Driver {full_name} added successfully!")
|
109 |
+
else:
|
110 |
+
st.error("Please fill in all fields.")
|
111 |
+
|
112 |
+
elif page == "Add Owner Operator":
|
113 |
+
st.header("Add Owner Operator")
|
114 |
+
name = st.text_input("Owner Operator Name")
|
115 |
+
contact_info = st.text_input("Contact Number")
|
116 |
+
company_percentage = st.number_input("Company Percentage", min_value=0.0, max_value=100.0, format="%f")
|
117 |
+
|
118 |
+
if st.button("Save Owner Operator Info"):
|
119 |
+
if name and contact_info and company_percentage:
|
120 |
+
# Check if owner operator exists
|
121 |
+
existing_owner_operator = owner_operators_collection.find_one({"contact_info": contact_info})
|
122 |
+
if existing_owner_operator:
|
123 |
+
st.warning(f"Owner Operator with Contact Number {contact_info} already exists.")
|
124 |
+
else:
|
125 |
+
owner_operator_data = {
|
126 |
+
"name": name,
|
127 |
+
"contact_info": contact_info,
|
128 |
+
"company_percentage": company_percentage,
|
129 |
+
"trucks": [],
|
130 |
+
"loads": []
|
131 |
+
}
|
132 |
+
owner_operators_collection.insert_one(owner_operator_data)
|
133 |
+
st.success(f"Owner Operator {name} added successfully!")
|
134 |
+
else:
|
135 |
+
st.error("Please fill in all fields.")
|
136 |
+
|
137 |
+
elif page == "Assign Truck":
|
138 |
+
st.header("Assign Truck to Driver or Owner Operator")
|
139 |
+
drivers = list(drivers_collection.find({}, {"first_name": 1, "last_name": 1}))
|
140 |
+
owner_operators = list(owner_operators_collection.find({}, {"name": 1}))
|
141 |
+
|
142 |
+
driver_names = [f"Driver: {d.get('first_name', '')} {d.get('last_name', '')}" for d in drivers]
|
143 |
+
owner_operator_names = [f"Owner Operator: {o.get('name', '')}" for o in owner_operators]
|
144 |
+
|
145 |
+
all_names = driver_names + owner_operator_names
|
146 |
+
|
147 |
+
selected_name = st.selectbox("Select Driver or Owner Operator", all_names)
|
148 |
+
trucks = list(trucks_collection.find({}, {"truck_name": 1, "truck_plate_number": 1}))
|
149 |
+
truck_names = [f"{t.get('truck_name', '')} ({t.get('truck_plate_number', '')})" for t in trucks]
|
150 |
+
|
151 |
+
selected_truck_name = st.selectbox("Select Truck", truck_names)
|
152 |
+
|
153 |
+
if st.button("Assign Truck"):
|
154 |
+
if selected_name and selected_truck_name:
|
155 |
+
selected_truck = next(t for t in trucks if f"{t.get('truck_name', '')} ({t.get('truck_plate_number', '')})" == selected_truck_name)
|
156 |
+
truck_plate_number = selected_truck["truck_plate_number"]
|
157 |
+
|
158 |
+
if selected_name.startswith("Driver:"):
|
159 |
+
selected_driver = next(d for d in drivers if f"Driver: {d.get('first_name', '')} {d.get('last_name', '')}" == selected_name)
|
160 |
+
# Update driver record to include truck
|
161 |
+
drivers_collection.update_one(
|
162 |
+
{"_id": selected_driver["_id"]},
|
163 |
+
{"$addToSet": {"trucks": truck_plate_number}}
|
164 |
+
)
|
165 |
+
st.success(f"Truck {selected_truck_name} assigned to Driver {selected_name.split(': ')[1]} successfully!")
|
166 |
+
|
167 |
+
elif selected_name.startswith("Owner Operator:"):
|
168 |
+
selected_owner_operator = next(o for o in owner_operators if f"Owner Operator: {o.get('name', '')}" == selected_name)
|
169 |
+
# Update owner operator record to include truck
|
170 |
+
owner_operators_collection.update_one(
|
171 |
+
{"_id": selected_owner_operator["_id"]},
|
172 |
+
{"$addToSet": {"trucks": truck_plate_number}}
|
173 |
+
)
|
174 |
+
st.success(f"Truck {selected_truck_name} assigned to Owner Operator {selected_name.split(': ')[1]} successfully!")
|
175 |
+
else:
|
176 |
+
st.error("Please select both a driver/owner operator and a truck.")
|
177 |
+
|
178 |
+
elif page == "Add Load":
|
179 |
+
st.header("Add Load Information")
|
180 |
+
|
181 |
+
# Fetch drivers, owner operators, and trucks
|
182 |
+
drivers = list(drivers_collection.find({}, {"first_name": 1, "last_name": 1, "trucks": 1, "contact_info": 1, "pay_per_mile": 1}))
|
183 |
+
owner_operators = list(owner_operators_collection.find({}, {"name": 1, "trucks": 1, "contact_info": 1, "company_percentage": 1}))
|
184 |
+
|
185 |
+
driver_names = [f"Driver: {d.get('first_name', '')} {d.get('last_name', '')}" for d in drivers]
|
186 |
+
owner_operator_names = [f"Owner Operator: {o.get('name', '')}" for o in owner_operators]
|
187 |
+
|
188 |
+
all_names = driver_names + owner_operator_names
|
189 |
+
|
190 |
+
selected_name = st.selectbox("Select Driver or Owner Operator", all_names)
|
191 |
+
|
192 |
+
if selected_name:
|
193 |
+
if selected_name.startswith("Driver:"):
|
194 |
+
selected_driver = next(d for d in drivers if f"Driver: {d.get('first_name', '')} {d.get('last_name', '')}" == selected_name)
|
195 |
+
truck_numbers = selected_driver.get('trucks', [])
|
196 |
+
entity_type = "driver"
|
197 |
+
elif selected_name.startswith("Owner Operator:"):
|
198 |
+
selected_owner_operator = next(o for o in owner_operators if f"Owner Operator: {o.get('name', '')}" == selected_name)
|
199 |
+
truck_numbers = selected_owner_operator.get('trucks', [])
|
200 |
+
entity_type = "owner_operator"
|
201 |
+
|
202 |
+
trucks_display = [
|
203 |
+
f"{t['truck_name']} ({t['truck_plate_number']})"
|
204 |
+
for t in trucks_collection.find({"truck_plate_number": {"$in": truck_numbers}})
|
205 |
+
]
|
206 |
+
|
207 |
+
selected_truck_display = st.selectbox("Select Truck for Load", trucks_display)
|
208 |
+
|
209 |
+
if selected_truck_display:
|
210 |
+
selected_truck_no = selected_truck_display.split(" (")[1][:-1]
|
211 |
+
|
212 |
+
load_id = st.text_input("Load ID")
|
213 |
+
date_from = st.date_input("Date From", datetime.now())
|
214 |
+
date_to = st.date_input("Date To", datetime.now())
|
215 |
+
original_miles = st.number_input("Original Miles", min_value=0)
|
216 |
+
bobtail = st.number_input("Bobtail Miles", min_value=0)
|
217 |
+
payout = st.number_input("Payout", min_value=0.0, format="%f")
|
218 |
+
fuel_cost = st.number_input("Fuel Cost", min_value=0.0, format="%f")
|
219 |
+
|
220 |
+
actual_miles = original_miles - bobtail
|
221 |
+
|
222 |
+
if entity_type == "driver":
|
223 |
+
driver_pay_per_mile = selected_driver.get("pay_per_mile", 0)
|
224 |
+
driver_pay = actual_miles * driver_pay_per_mile
|
225 |
+
final_payout = payout - driver_pay
|
226 |
+
|
227 |
+
elif entity_type == "owner_operator":
|
228 |
+
company_percentage = selected_owner_operator.get("company_percentage", 0)
|
229 |
+
pay_percent = (payout * company_percentage) / 100
|
230 |
+
driver_pay = pay_percent
|
231 |
+
final_payout = payout - pay_percent
|
232 |
+
|
233 |
+
if st.button("Save Load Info"):
|
234 |
+
load_data = {
|
235 |
+
"truck_no": selected_truck_no,
|
236 |
+
"truck_name": selected_truck_display.split(" (")[0],
|
237 |
+
"load_id": load_id,
|
238 |
+
"date_from": date_from.strftime("%Y-%m-%d"),
|
239 |
+
"date_to": date_to.strftime("%Y-%m-%d"),
|
240 |
+
"original_miles": original_miles,
|
241 |
+
"bobtail": bobtail,
|
242 |
+
"actual_miles": actual_miles,
|
243 |
+
"payout": payout,
|
244 |
+
"fuel_cost": fuel_cost,
|
245 |
+
"driver_pay": driver_pay,
|
246 |
+
"final_payout": final_payout
|
247 |
+
}
|
248 |
+
|
249 |
+
if entity_type == "driver":
|
250 |
+
drivers_collection.update_one(
|
251 |
+
{"_id": selected_driver["_id"]},
|
252 |
+
{"$push": {"loads": load_data}}
|
253 |
+
)
|
254 |
+
st.success(f"Load information added successfully for Driver {selected_driver.get('first_name', '')} {selected_driver.get('last_name', '')} with Truck {selected_truck_display.split(' (')[0]}!")
|
255 |
+
|
256 |
+
elif entity_type == "owner_operator":
|
257 |
+
owner_operators_collection.update_one(
|
258 |
+
{"_id": selected_owner_operator["_id"]},
|
259 |
+
{"$push": {"loads": load_data}}
|
260 |
+
)
|
261 |
+
st.success(f"Load information added successfully for Owner Operator {selected_owner_operator.get('name', '')} with Truck {selected_truck_display.split(' (')[0]}!")
|
262 |
+
|
263 |
+
elif page == "Update/Delete Load":
|
264 |
+
st.header("Update or Delete Load")
|
265 |
+
|
266 |
+
load_id = st.text_input("Enter Load ID to Update or Delete")
|
267 |
+
|
268 |
+
if load_id:
|
269 |
+
# Search for the load across all drivers and owner operators
|
270 |
+
driver_record = drivers_collection.find_one({"loads.load_id": load_id}, {"loads.$": 1, "first_name": 1, "last_name": 1})
|
271 |
+
owner_operator_record = owner_operators_collection.find_one({"loads.load_id": load_id}, {"loads.$": 1, "name": 1})
|
272 |
+
|
273 |
+
if driver_record:
|
274 |
+
load_data = driver_record["loads"][0]
|
275 |
+
entity_type = "driver"
|
276 |
+
entity_name = f"{driver_record['first_name']} {driver_record['last_name']}"
|
277 |
+
elif owner_operator_record:
|
278 |
+
load_data = owner_operator_record["loads"][0]
|
279 |
+
entity_type = "owner_operator"
|
280 |
+
entity_name = owner_operator_record["name"]
|
281 |
+
else:
|
282 |
+
load_data = None
|
283 |
+
|
284 |
+
if load_data:
|
285 |
+
st.write(f"Load found for {entity_type.capitalize()}: {entity_name}")
|
286 |
+
|
287 |
+
# Display load information
|
288 |
+
load_data_display = {
|
289 |
+
"Truck No": load_data["truck_no"],
|
290 |
+
"Load ID": load_data["load_id"],
|
291 |
+
"Date From": load_data["date_from"],
|
292 |
+
"Date To": load_data["date_to"],
|
293 |
+
"Original Miles": load_data["original_miles"],
|
294 |
+
"Bobtail Miles": load_data["bobtail"],
|
295 |
+
"Payout": load_data["payout"],
|
296 |
+
"Fuel Cost": load_data["fuel_cost"],
|
297 |
+
"Driver/Owner Operator Pay": load_data["driver_pay"],
|
298 |
+
"Final Payout": load_data["final_payout"]
|
299 |
+
}
|
300 |
+
|
301 |
+
for key, value in load_data_display.items():
|
302 |
+
st.write(f"{key}: {value}")
|
303 |
+
|
304 |
+
# Input fields for updating the load
|
305 |
+
st.subheader("Update Load Information")
|
306 |
+
date_from = st.date_input("Date From", datetime.strptime(load_data["date_from"], "%Y-%m-%d"))
|
307 |
+
date_to = st.date_input("Date To", datetime.strptime(load_data["date_to"], "%Y-%m-%d"))
|
308 |
+
original_miles = st.number_input("Original Miles", value=load_data["original_miles"], min_value=0)
|
309 |
+
bobtail = st.number_input("Bobtail Miles", value=load_data["bobtail"], min_value=0)
|
310 |
+
payout = st.number_input("Payout", value=load_data["payout"], min_value=0.0, format="%f")
|
311 |
+
fuel_cost = st.number_input("Fuel Cost", value=load_data["fuel_cost"], min_value=0.0, format="%f")
|
312 |
+
|
313 |
+
actual_miles = original_miles - bobtail
|
314 |
+
|
315 |
+
if entity_type == "driver":
|
316 |
+
driver_pay = actual_miles * driver_record["loads"][0]["driver_pay"] / load_data["actual_miles"]
|
317 |
+
final_payout = payout - driver_pay
|
318 |
+
|
319 |
+
elif entity_type == "owner_operator":
|
320 |
+
company_percentage = owner_operator_record["loads"][0].get("company_percentage", 0)
|
321 |
+
pay_percent = (payout * company_percentage) / 100
|
322 |
+
driver_pay = pay_percent
|
323 |
+
final_payout = payout - pay_percent
|
324 |
+
|
325 |
+
if st.button("Update Load"):
|
326 |
+
# Update the load with new values
|
327 |
+
updated_load_data = {
|
328 |
+
"loads.$.date_from": date_from.strftime("%Y-%m-%d"),
|
329 |
+
"loads.$.date_to": date_to.strftime("%Y-%m-%d"),
|
330 |
+
"loads.$.original_miles": original_miles,
|
331 |
+
"loads.$.bobtail": bobtail,
|
332 |
+
"loads.$.actual_miles": actual_miles,
|
333 |
+
"loads.$.payout": payout,
|
334 |
+
"loads.$.fuel_cost": fuel_cost,
|
335 |
+
"loads.$.driver_pay": driver_pay,
|
336 |
+
"loads.$.final_payout": final_payout
|
337 |
+
}
|
338 |
+
|
339 |
+
if entity_type == "driver":
|
340 |
+
drivers_collection.update_one(
|
341 |
+
{"_id": driver_record["_id"], "loads.load_id": load_id},
|
342 |
+
{"$set": updated_load_data}
|
343 |
+
)
|
344 |
+
st.success(f"Load ID {load_id} updated successfully!")
|
345 |
+
elif entity_type == "owner_operator":
|
346 |
+
owner_operators_collection.update_one(
|
347 |
+
{"_id": owner_operator_record["_id"], "loads.load_id": load_id},
|
348 |
+
{"$set": updated_load_data}
|
349 |
+
)
|
350 |
+
st.success(f"Load ID {load_id} updated successfully!")
|
351 |
+
|
352 |
+
if st.button("Delete Load"):
|
353 |
+
# Remove the load from the driver's or owner operator's record
|
354 |
+
if entity_type == "driver":
|
355 |
+
drivers_collection.update_one(
|
356 |
+
{"_id": driver_record["_id"]},
|
357 |
+
{"$pull": {"loads": {"load_id": load_id}}}
|
358 |
+
)
|
359 |
+
st.success(f"Load ID {load_id} deleted successfully!")
|
360 |
+
elif entity_type == "owner_operator":
|
361 |
+
owner_operators_collection.update_one(
|
362 |
+
{"_id": owner_operator_record["_id"]},
|
363 |
+
{"$pull": {"loads": {"load_id": load_id}}}
|
364 |
+
)
|
365 |
+
st.success(f"Load ID {load_id} deleted successfully!")
|
366 |
+
else:
|
367 |
+
st.warning(f"No load found with Load ID {load_id}.")
|
368 |
+
|
369 |
+
elif page == "View Owner Operator Info":
|
370 |
+
st.header("View Owner Operator Information")
|
371 |
+
|
372 |
+
owner_operators = list(owner_operators_collection.find({}))
|
373 |
+
owner_operator_names = [o.get('name', '') for o in owner_operators]
|
374 |
+
|
375 |
+
selected_owner_operator_name = st.selectbox("Select Owner Operator to View", owner_operator_names)
|
376 |
+
|
377 |
+
if selected_owner_operator_name:
|
378 |
+
owner_operator_record = owner_operators_collection.find_one({"name": selected_owner_operator_name})
|
379 |
+
|
380 |
+
if owner_operator_record:
|
381 |
+
st.write(f"**Name**: {owner_operator_record['name']}")
|
382 |
+
st.write(f"**Contact Number**: {owner_operator_record['contact_info']}")
|
383 |
+
st.write(f"**Company Percentage**: {owner_operator_record['company_percentage']}%")
|
384 |
+
st.write(f"**Trucks Assigned**: {', '.join(owner_operator_record['trucks']) if owner_operator_record['trucks'] else 'None'}")
|
385 |
+
|
386 |
+
# Date range input
|
387 |
+
date_from = st.date_input("Date From", datetime.now())
|
388 |
+
date_to = st.date_input("Date To", datetime.now())
|
389 |
+
|
390 |
+
# Convert the date input from Streamlit to Pandas datetime
|
391 |
+
date_from = pd.to_datetime(date_from)
|
392 |
+
date_to = pd.to_datetime(date_to)
|
393 |
+
|
394 |
+
# Display loads associated with this owner operator within the date range
|
395 |
+
if "loads" in owner_operator_record and owner_operator_record["loads"]:
|
396 |
+
load_df = pd.DataFrame(owner_operator_record["loads"])
|
397 |
+
load_df["date_from"] = pd.to_datetime(load_df["date_from"])
|
398 |
+
load_df["date_to"] = pd.to_datetime(load_df["date_to"])
|
399 |
+
|
400 |
+
# Filter by date range
|
401 |
+
filtered_load_df = load_df[(load_df["date_from"] >= date_from) & (load_df["date_to"] <= date_to)]
|
402 |
+
|
403 |
+
if not filtered_load_df.empty:
|
404 |
+
st.subheader("Load Information")
|
405 |
+
st.dataframe(filtered_load_df)
|
406 |
+
|
407 |
+
# Total Calculations
|
408 |
+
total_payout = filtered_load_df["payout"].sum()
|
409 |
+
total_company_fee = filtered_load_df["driver_pay"].sum()
|
410 |
+
total_final_payout = filtered_load_df["final_payout"].sum()
|
411 |
+
total_fuel_cost = filtered_load_df["fuel_cost"].sum()
|
412 |
+
total_final_payout_after_fuel = total_final_payout - total_fuel_cost
|
413 |
+
|
414 |
+
st.subheader("Total Calculations")
|
415 |
+
st.write(f"Total Payout: ${total_payout:.2f}")
|
416 |
+
st.write(f"Total Company Fee: ${total_company_fee:.2f}")
|
417 |
+
st.write(f"Total Final Payout after Fuel Cost: ${total_final_payout_after_fuel:.2f}")
|
418 |
+
st.write(f"Total Fuel Cost: ${total_fuel_cost:.2f}")
|
419 |
+
else:
|
420 |
+
st.write("No loads found in the selected date range.")
|
421 |
+
else:
|
422 |
+
st.write("No loads associated with this Owner Operator.")
|
423 |
+
|
424 |
+
elif page == "View Information":
|
425 |
+
st.header("View Load Information by Truck Name")
|
426 |
+
|
427 |
+
trucks = list(trucks_collection.find({}, {"truck_name": 1, "truck_plate_number": 1}))
|
428 |
+
truck_names = [f"{t.get('truck_name', '')} ({t.get('truck_plate_number', '')})" for t in trucks]
|
429 |
+
|
430 |
+
selected_truck_name_view = st.selectbox("Select Truck Name to View Records", truck_names)
|
431 |
+
|
432 |
+
if selected_truck_name_view:
|
433 |
+
truck_record = trucks_collection.find_one({"truck_name": selected_truck_name_view.split(" ")[0]})
|
434 |
+
if truck_record:
|
435 |
+
truck_plate_number = truck_record["truck_plate_number"]
|
436 |
+
|
437 |
+
date_from = st.date_input("Date From", datetime.now())
|
438 |
+
date_to = st.date_input("Date To", datetime.now())
|
439 |
+
|
440 |
+
# Filter load records for the selected truck and date range
|
441 |
+
loads_view = drivers_collection.aggregate([
|
442 |
+
{"$match": {"trucks": truck_plate_number}},
|
443 |
+
{"$unwind": "$loads"},
|
444 |
+
{"$match": {
|
445 |
+
"loads.truck_no": truck_plate_number,
|
446 |
+
"loads.date_from": {"$gte": date_from.strftime("%Y-%m-%d")},
|
447 |
+
"loads.date_to": {"$lte": date_to.strftime("%Y-%m-%d")}
|
448 |
+
}}
|
449 |
+
])
|
450 |
+
|
451 |
+
# Group loads by driver or owner operator
|
452 |
+
load_data_view = list(loads_view)
|
453 |
+
if load_data_view:
|
454 |
+
# Create a dataframe for each driver/owner operator
|
455 |
+
entity_data = {}
|
456 |
+
for load in load_data_view:
|
457 |
+
driver_name = f"{load['first_name']} {load['last_name']}" if "first_name" in load else load.get("name", "")
|
458 |
+
if driver_name not in entity_data:
|
459 |
+
entity_data[driver_name] = []
|
460 |
+
entity_data[driver_name].append(load['loads'])
|
461 |
+
|
462 |
+
for entity_name, loads in entity_data.items():
|
463 |
+
st.subheader(f"Driver : {entity_name}")
|
464 |
+
|
465 |
+
load_df = pd.DataFrame(loads)
|
466 |
+
|
467 |
+
st.dataframe(load_df)
|
468 |
+
|
469 |
+
total_original_miles = load_df["original_miles"].sum()
|
470 |
+
total_actual_miles = load_df["actual_miles"].sum()
|
471 |
+
total_payout = load_df["payout"].sum()
|
472 |
+
total_driver_pay = load_df["driver_pay"].sum()
|
473 |
+
total_fuel_cost = load_df["fuel_cost"].sum()
|
474 |
+
|
475 |
+
st.subheader("Total Calculations")
|
476 |
+
st.write(f"Total Original Miles: {total_original_miles}")
|
477 |
+
st.write(f"Total Actual Miles: {total_actual_miles}")
|
478 |
+
st.write(f"Total Payout: ${total_payout:.2f}")
|
479 |
+
st.write(f"Total Pay to Driver: ${total_driver_pay:.2f}")
|
480 |
+
st.write(f"Total Fuel Cost: ${total_fuel_cost:.2f}")
|
481 |
+
|
482 |
+
# Option to save the results to a CSV file
|
483 |
+
if st.button(f"Save Results to CSV for {entity_name}"):
|
484 |
+
csv_filename = f"{entity_name.replace(' ', '_')}_load_data_summary.csv"
|
485 |
+
load_df.to_csv(csv_filename, index=False)
|
486 |
+
st.success(f"Results saved to {csv_filename}")
|
487 |
+
else:
|
488 |
+
st.warning(f"No load records found for Truck {selected_truck_name_view.split(' ')[0]} between {date_from} and {date_to}.")
|