fix
This commit is contained in:
+25
-16
@@ -15,6 +15,7 @@ from ..extensions import db
|
||||
from ..models import AppSetting, CompanySettings, FuelCard, FuelCardPolicy, FuelCardStationRule, FuelEntry, FuelStationCompany, FuelStationPoint, OrlenPrice, User, Vehicle
|
||||
from ..services import POLISH_REGIONS, aggregate_ure_companies, calculate_costs, fetch_orlen_price, fetch_orlen_range, fetch_ure_stations, invoice_period
|
||||
from ..station_catalog import sync_station_catalog
|
||||
from ..orlen_sync import sync_orlen_prices
|
||||
|
||||
FUEL_TYPES = ("PB95", "PB98", "DIESEL", "LPG")
|
||||
|
||||
@@ -28,23 +29,31 @@ def orlen_prices():
|
||||
@api.post('/orlen/sync')
|
||||
@role_required('boss','admin')
|
||||
def orlen_sync():
|
||||
d=payload(); fuels=d.get('fuels',[]); fuels=[fuels] if isinstance(fuels,str) else fuels; fuels=[f.upper() for f in fuels if f.upper() in FUEL_TYPES]; year=int(d.get('year',date.today().year))
|
||||
regions=d.get('regions',[]); regions=[regions] if isinstance(regions,str) else regions; regions=[str(r).strip().lower() for r in regions if str(r).strip().lower() in POLISH_REGIONS]
|
||||
if not fuels:return fail('Wybierz co najmniej jedno paliwo')
|
||||
settings=CompanySettings.query.first(); result={}; total=0
|
||||
d = payload()
|
||||
fuels = d.get('fuels', [])
|
||||
fuels = [fuels] if isinstance(fuels, str) else fuels
|
||||
fuels = [fuel.upper() for fuel in fuels if fuel.upper() in FUEL_TYPES]
|
||||
regions = d.get('regions', [])
|
||||
regions = [regions] if isinstance(regions, str) else regions
|
||||
regions = [str(region).strip().lower() for region in regions if str(region).strip().lower() in POLISH_REGIONS]
|
||||
if not fuels:
|
||||
return fail('Wybierz co najmniej jedno paliwo')
|
||||
|
||||
try:
|
||||
for fuel in fuels:
|
||||
added=updated=0
|
||||
for row in fetch_orlen_range(fuel,date(year,1,1),min(date(year,12,31),date.today()),'all' if fuel=='LPG' else settings.region):
|
||||
if fuel=='LPG' and regions and row.get('region') not in regions: continue
|
||||
x=OrlenPrice.query.filter_by(fuel_type=row['fuel_type'],effective_date=row['effective_date'],region=row['region']).first()
|
||||
if x:
|
||||
for k,v in row.items():setattr(x,k,v)
|
||||
x.fetched_at=datetime.utcnow();updated+=1
|
||||
else:db.session.add(OrlenPrice(**row));added+=1
|
||||
result[fuel]={'added':added,'updated':updated};total+=added+updated
|
||||
db.session.commit();return response(result,f'Przetworzono {total} rekordów')
|
||||
except Exception as exc:db.session.rollback();return fail(str(exc),502)
|
||||
year = int(d.get('year', date.today().year))
|
||||
settings = CompanySettings.query.first()
|
||||
result, total = sync_orlen_prices(
|
||||
fuels=fuels,
|
||||
year=year,
|
||||
regions=regions,
|
||||
default_region=settings.region,
|
||||
full_refresh=boolv(d, 'full_refresh'),
|
||||
)
|
||||
db.session.commit()
|
||||
return response(result, f'Przetworzono {total} rekordów')
|
||||
except Exception as exc:
|
||||
db.session.rollback()
|
||||
return fail(str(exc), 502)
|
||||
|
||||
@api.get('/orlen/preview')
|
||||
@login_required
|
||||
|
||||
Reference in New Issue
Block a user