Fix: Correctly fill refueling data for multiple refuelings per day
Problem: Both refueling forms for the same date were being filled with refuel_1 data because the logic checked current_km value which was "0" for both forms initially. Solution: Track refueling form number per date using refuel_counter dict. First refueling form gets data from "Tankováno při 1 [km]", second gets data from "Tankováno při 2 [km]". 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -51,6 +51,7 @@ class JourneybookFiller:
|
||||
|
||||
updates = []
|
||||
deletes = []
|
||||
refuel_counter = {} # Track which refueling number for each date
|
||||
|
||||
for i, row in enumerate(table_rows):
|
||||
form = row.find('form')
|
||||
@@ -80,7 +81,14 @@ class JourneybookFiller:
|
||||
if len(matching_rows) > 0:
|
||||
# Row exists in our data - update it
|
||||
row_data = matching_rows.iloc[0]
|
||||
update = self._prepare_update(form_data, row_data, update_mode=True, is_refueling=is_refueling_form)
|
||||
|
||||
# Track refueling form number for this date
|
||||
refuel_num = None
|
||||
if is_refueling_form:
|
||||
refuel_counter[clean_date] = refuel_counter.get(clean_date, 0) + 1
|
||||
refuel_num = refuel_counter[clean_date]
|
||||
|
||||
update = self._prepare_update(form_data, row_data, update_mode=True, is_refueling=is_refueling_form, refuel_num=refuel_num)
|
||||
updates.append(update)
|
||||
form_type = "refueling" if is_refueling_form else "journey"
|
||||
logger.info(f"Matched {form_type} row {i} with date {clean_date}")
|
||||
@@ -185,7 +193,7 @@ class JourneybookFiller:
|
||||
|
||||
return form_data
|
||||
|
||||
def _prepare_update(self, form_data: Dict, row_data: pd.Series, update_mode: bool = True, is_refueling: bool = False) -> Dict[str, Any]:
|
||||
def _prepare_update(self, form_data: Dict, row_data: pd.Series, update_mode: bool = True, is_refueling: bool = False, refuel_num: int = None) -> Dict[str, Any]:
|
||||
"""Prepare form data with updated values from DataFrame or for deletion
|
||||
|
||||
Args:
|
||||
@@ -193,6 +201,7 @@ class JourneybookFiller:
|
||||
row_data: DataFrame row with journey data (None for delete)
|
||||
update_mode: True to update row, False to delete row
|
||||
is_refueling: True if this is a refueling form, False if journey form
|
||||
refuel_num: Which refueling form this is for the date (1 or 2)
|
||||
"""
|
||||
update = {
|
||||
"action": form_data["action"],
|
||||
@@ -219,27 +228,17 @@ class JourneybookFiller:
|
||||
# Update mode - handle refueling forms vs journey forms differently
|
||||
if is_refueling:
|
||||
# Refueling form - fill the km value from Tankováno při column
|
||||
# We need to determine if this is refueling 1 or 2 for this date
|
||||
# The form should have an f_km field that needs to be filled
|
||||
|
||||
# Check if this date has refueling data in the DataFrame
|
||||
if "Tankováno při 1 [km]" in row_data and pd.notna(row_data["Tankováno při 1 [km]"]):
|
||||
# Use refuel_num to determine which refueling data to use
|
||||
if refuel_num == 1 and "Tankováno při 1 [km]" in row_data and pd.notna(row_data["Tankováno při 1 [km]"]):
|
||||
if "f_km" in update["data"]:
|
||||
# Check if this is the first or second refueling form
|
||||
current_km = update["data"].get("f_km", "0")
|
||||
refuel_1_km = int(row_data["Tankováno při 1 [km]"])
|
||||
|
||||
# If f_km is 0 or empty, fill with refuel 1
|
||||
if current_km == "0" or current_km == "":
|
||||
update["data"]["f_km"] = str(refuel_1_km)
|
||||
logger.info(f"Filling refuel km: f_km={refuel_1_km}")
|
||||
# Otherwise check if there's a second refueling
|
||||
elif "Tankováno při 2 [km]" in row_data and pd.notna(row_data["Tankováno při 2 [km]"]):
|
||||
refuel_2_km = int(row_data["Tankováno při 2 [km]"])
|
||||
if int(current_km) != refuel_1_km:
|
||||
# This might be the second refueling form
|
||||
update["data"]["f_km"] = str(refuel_2_km)
|
||||
logger.info(f"Filling refuel 2 km: f_km={refuel_2_km}")
|
||||
update["data"]["f_km"] = str(refuel_1_km)
|
||||
logger.info(f"Filling refuel 1 km: f_km={refuel_1_km}")
|
||||
elif refuel_num == 2 and "Tankováno při 2 [km]" in row_data and pd.notna(row_data["Tankováno při 2 [km]"]):
|
||||
if "f_km" in update["data"]:
|
||||
refuel_2_km = int(row_data["Tankováno při 2 [km]"])
|
||||
update["data"]["f_km"] = str(refuel_2_km)
|
||||
logger.info(f"Filling refuel 2 km: f_km={refuel_2_km}")
|
||||
else:
|
||||
# Journey form - fill with data from DataFrame
|
||||
# Set BOTH f_cil_km (end km) and f_ujeto (distance traveled)
|
||||
|
||||
Reference in New Issue
Block a user