diff --git a/app/main.py b/app/main.py index 528fd7e..ad9fd28 100644 --- a/app/main.py +++ b/app/main.py @@ -365,6 +365,22 @@ def stations(): page = request.args.get("page", 1, type=int) sort = request.args.get("sort", "company_name") direction = request.args.get("direction", "asc") + companies_q = CompanySettings.query.filter_by(active=True).order_by(CompanySettings.name.asc()) + if current_user.role == "boss": + companies_q = companies_q.filter_by(id=current_user.company_id) + companies = companies_q.all() + requested_company_id = request.args.get("company_id", type=int) + if current_user.role == "boss": + company = current_user.company + elif requested_company_id is None: + return render_template("stations_company_select.html", companies=companies) + else: + company = db.session.get(CompanySettings, requested_company_id) + if company not in companies: + abort(404, description="Nie znaleziono aktywnej firmy") + if company is None: + abort(404, description="Brak aktywnej firmy do konfiguracji") + query = FuelStationCompany.query.filter_by(active=True) if q: terms = [term for term in q.split() if term] @@ -375,19 +391,6 @@ def stations(): column = columns.get(sort, FuelStationCompany.company_name) query = query.order_by(column.desc() if direction == "desc" else column.asc(), FuelStationCompany.brand_name.asc()) station_page = query.paginate(page=page, per_page=30, error_out=False) - companies_q = CompanySettings.query.filter_by(active=True).order_by(CompanySettings.name.asc()) - if current_user.role == "boss": - companies_q = companies_q.filter_by(id=current_user.company_id) - companies = companies_q.all() - requested_company_id = request.args.get("company_id", type=int) - if current_user.role == "boss": - company = current_user.company - else: - company = db.session.get(CompanySettings, requested_company_id) if requested_company_id else None - if company not in companies: - company = companies[0] if companies else None - if company is None: - abort(404, description="Brak aktywnej firmy do konfiguracji") cards_q=FuelCard.query.filter_by(active=True, company_id=company.id) cards=cards_q.order_by(FuelCard.name).all() rules={(r.fuel_card_id,r.station_company_id):r for r in FuelCardStationRule.query.filter(FuelCardStationRule.fuel_card_id.in_([c.id for c in cards] or [-1])).all()} diff --git a/app/templates/stations_company_select.html b/app/templates/stations_company_select.html new file mode 100644 index 0000000..fb45a8d --- /dev/null +++ b/app/templates/stations_company_select.html @@ -0,0 +1,36 @@ +{% extends 'base.html' %} +{% block title %}Wybierz firmę – stacje paliw{% endblock %} +{% block content %} +
+
+

Predefiniowane stacje paliw

+

Wybierz firmę, dla której chcesz skonfigurować dostęp, ulubione stacje i warunki kart.

+
+
+
+
+ {% if companies %} +
+
+ + +
+
+ +
+
+ {% else %} +
+

Brak aktywnych firm

+

Najpierw dodaj lub aktywuj firmę w panelu administracyjnym.

+ Przejdź do firm +
+ {% endif %} +
+
+{% endblock %} diff --git a/tests/test_app.py b/tests/test_app.py index c58ab2b..f4144b7 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -252,3 +252,14 @@ def test_company_settings_show_station_access_and_favorite_pagination(tmp_path): assert 'favorite-pagination' in html assert 'Zapisz ulubione' in html assert 'Usuń firmę' in html + +def test_admin_stations_without_company_shows_company_picker(tmp_path): + app = make_app(tmp_path, "stations-picker.db") + client = app.test_client() + client.post('/login', data={'email':'admin@example.com','password':'admin123!'}) + response = client.get('/stations') + html = response.get_data(as_text=True) + assert response.status_code == 200 + assert 'Wybierz firmę' in html + assert 'name="company_id"' in html + assert 'Przejdź do konfiguracji' in html