This commit is contained in:
Mateusz Gruszczyński
2026-08-02 00:19:34 +02:00
parent 5299a7fc66
commit 84ceb23316
7 changed files with 299 additions and 38 deletions
+25 -18
View File
@@ -13,6 +13,7 @@ from .station_catalog import sync_station_catalog
from . import THEMES
from .vat import entry_vat_rate, freeze_vat_action, global_vat_override, save_global_vat_override
from .settlement import calculate as calculate_snapshot_settlement, freeze as freeze_settlement
from .orlen_sync import sync_orlen_prices
main = Blueprint("main", __name__)
FUEL_TYPES = ("PB95", "PB98", "DIESEL", "LPG")
@@ -499,28 +500,34 @@ def orlen_data():
@login_required
@roles("boss", "admin")
def orlen_sync():
payload = request.get_json(silent=True) or request.form
fuels = payload.get("fuels", [])
if isinstance(fuels, str): fuels = [fuels]
fuels = [f.upper() for f in fuels if f.upper() in FUEL_TYPES]
year = int(payload.get("year", date.today().year))
if not fuels: return jsonify({"ok": False, "error": "Wybierz co najmniej jedno paliwo."}), 400
settings = CompanySettings.query.first(); start = date(year,1,1); end = min(date(year,12,31), date.today())
result = {}; total = 0
data = request.get_json(silent=True) or request.form
fuels = data.get("fuels", [])
if isinstance(fuels, str):
fuels = [fuels]
fuels = [fuel.upper() for fuel in fuels if fuel.upper() in FUEL_TYPES]
if not fuels:
return jsonify({"ok": False, "error": "Wybierz co najmniej jedno paliwo."}), 400
regions = data.get("regions", [])
if isinstance(regions, str):
regions = [regions]
regions = [str(region).strip().lower() for region in regions if str(region).strip().lower() in POLISH_REGIONS]
full_refresh = data.get("full_refresh") in (True, 1, "1", "true", "on", "yes")
try:
for fuel in fuels:
imported = 0; updated = 0
for row in fetch_orlen_range(fuel, start, end, "all" if fuel == "LPG" else settings.region):
existing = OrlenPrice.query.filter_by(fuel_type=row["fuel_type"], effective_date=row["effective_date"], region=row["region"]).first()
if existing:
existing.price_per_liter=row["price_per_liter"]; existing.raw_value=row["raw_value"]; existing.product_name=row["product_name"]; existing.source=row["source"]; existing.fetched_at=datetime.utcnow(); updated += 1
else:
db.session.add(OrlenPrice(**row)); imported += 1
result[fuel] = {"added": imported, "updated": updated}; total += imported + updated
settings = CompanySettings.query.first()
result, total = sync_orlen_prices(
fuels=fuels,
year=int(data.get("year", date.today().year)),
regions=regions,
default_region=settings.region,
full_refresh=full_refresh,
)
db.session.commit()
return jsonify({"ok": True, "message": f"Przetworzono {total} rekordów.", "result": result})
except Exception as exc:
db.session.rollback(); return jsonify({"ok": False, "error": str(exc)}), 502
db.session.rollback()
return jsonify({"ok": False, "error": str(exc)}), 502