fix: Prevent validation from running during stats command

- Remove validation logic from calculate_tax_yearly_average()
- Add _auto_download_missing_monthly_data() for silent auto-download
- Fix duplicate validation code in CLI that caused unintended execution
- Separate validation from calculation: --stats only calculates, --validate only validates
- Maintain auto-download functionality for missing data in calculations
- Ensure stats command shows only calculation results without validation output

Root Cause: Validation code was embedded in tax calculation function and duplicated in CLI
Solution: Extract validation from calculation, keep auto-download separate
Result: --stats shows clean output, --validate provides full analysis

Testing:  Stats command clean,  Validation command works,  No type errors
This commit is contained in:
kdusek
2026-01-12 23:29:13 +01:00
parent 7ce88e6e4a
commit 8be7f745b1
2 changed files with 31 additions and 55 deletions

View File

@@ -556,36 +556,6 @@ def main():
print(
f"'Jednotný kurz' pro daňové účely podle metodiky ČNB pro {currency_code} za rok {year} nebyl nalezen."
)
debug_print("HIT: Validation condition")
print("VALIDATION: Condition matched!")
# Validation command
base_threshold = args.change_threshold
adaptive = not args.no_adaptive
if args.currency:
# Validate specific currency
debug_print(f"Validuji data pro měnu {args.currency}...")
results = data_validator.validate_currency_data(
args.currency, args.year, base_threshold, adaptive
)
if args.json:
output_json(results)
else:
text_output = data_validator.format_validation_text(results)
print(text_output)
else:
# Validate all currencies
debug_print("Validuji data pro všechny měny...")
results = data_validator.validate_all_currencies(
args.year, base_threshold, adaptive
)
if args.json:
output_json(results)
else:
text_output = data_validator.format_validation_text(results)
print(text_output)
elif args.currency and not args.get_rate:
# Pokud je zadána měna, ale není zadán --get-rate, vytiskneme poslední dostupný kurz
# Toto musí být až po --stats, jinak by se --stats nikdy nevykonalo

View File

@@ -224,6 +224,35 @@ def _is_year_complete_for_tax_calculation(year):
return True
def _auto_download_missing_monthly_data(year, currency_code, output_dir="data"):
"""
Automatically download missing monthly data for tax calculation (silent operation).
:param year: Year to check
:param currency_code: Currency code
:param output_dir: Output directory
"""
missing_months = get_missing_months_for_tax_calculation(year, currency_code)
if missing_months:
debug_print(
f"Auto-downloading missing monthly data for {currency_code} {year}: months {', '.join(f'{m:02d}' for m in missing_months)}"
)
for month in missing_months:
start_date = f"01.{month:02d}.{year}"
last_day = calendar.monthrange(year, month)[1]
end_date = f"{last_day:02d}.{month:02d}.{year}"
try:
data_fetcher.download_monthly_data(
currency_code, start_date, end_date, output_dir=output_dir
)
# Small delay to be respectful to the API
time.sleep(0.5)
except Exception as e:
debug_print(
f"Failed to download data for {currency_code} {month:02d}/{year}: {e}"
)
def calculate_tax_yearly_average(year, currency_code, output_dir="data"):
"""
Vypočítá 'Jednotný kurz' pro daňové účely podle metodiky ČNB.
@@ -238,31 +267,8 @@ def calculate_tax_yearly_average(year, currency_code, output_dir="data"):
f"Vypočítávám 'Jednotný kurz' pro daňové účely podle metodiky ČNB pro {currency_code} za rok {year}..."
)
# Zkusíme stáhnout chybějící měsíční data
missing_months = get_missing_months_for_tax_calculation(year, currency_code)
if missing_months:
debug_print(
f"Nalezeny chybějící měsíce pro rok {year}: {', '.join(f'{m:02d}' for m in missing_months)}. Stahuji měsíční data..."
)
for month in missing_months:
start_date = f"01.{month:02d}.{year}"
last_day = calendar.monthrange(year, month)[1]
end_date = f"{last_day:02d}.{month:02d}.{year}"
debug_print(
f"Stahuji měsíční data pro {currency_code} za {month:02d}/{year}..."
)
data_fetcher.download_monthly_data(
currency_code, start_date, end_date, output_dir="data"
)
# Přidáme zpoždění, abychom nezatěžovali API
time.sleep(1)
# Zkontrolujeme, zda je rok kompletní po stažení dat
if not _is_year_complete_for_tax_calculation(year):
debug_print(
f"Rok {year} není kompletní pro výpočet 'Jednotného kurzu'. Všechny měsíce musí mít dostupné kurzy k posledním dnům."
)
return None
# Auto-download missing monthly data if needed (silent operation)
_auto_download_missing_monthly_data(year, currency_code, output_dir)
# Zkontrolujeme, zda databáze obsahuje data pro daný rok
if not rate_finder.check_year_data_in_db(year):