feat: Add admin modules for Companies and People management
- Add /admin/companies with CRUD operations, filters, CSV export - Add /admin/people with person-company relationship management - Companies: add, edit, toggle status, archive, view linked people - People: add, edit, delete, link/unlink companies by role - Both panels follow existing admin UI patterns (stats, filters, modals) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
9d422ceb64
commit
d5273a8e6a
@ -24,3 +24,5 @@ from . import routes_zopk_knowledge # noqa: E402, F401
|
||||
from . import routes_zopk_timeline # noqa: E402, F401
|
||||
from . import routes_users_api # noqa: E402, F401
|
||||
from . import routes_krs_api # noqa: E402, F401
|
||||
from . import routes_companies # noqa: E402, F401
|
||||
from . import routes_people # noqa: E402, F401
|
||||
|
||||
498
blueprints/admin/routes_companies.py
Normal file
498
blueprints/admin/routes_companies.py
Normal file
@ -0,0 +1,498 @@
|
||||
"""
|
||||
Admin Routes - Companies
|
||||
========================
|
||||
|
||||
CRUD operations for company management in admin panel.
|
||||
"""
|
||||
|
||||
import re
|
||||
import csv
|
||||
import logging
|
||||
from io import StringIO
|
||||
from datetime import datetime
|
||||
|
||||
from flask import render_template, request, redirect, url_for, flash, jsonify, Response
|
||||
from flask_login import login_required, current_user
|
||||
|
||||
from . import bp
|
||||
from database import SessionLocal, Company, Category, User, Person, CompanyPerson
|
||||
|
||||
# Logger
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def validate_nip(nip: str) -> bool:
|
||||
"""Validate Polish NIP number (10 digits, checksum)"""
|
||||
if not nip or not re.match(r'^\d{10}$', nip):
|
||||
return False
|
||||
|
||||
weights = [6, 5, 7, 2, 3, 4, 5, 6, 7]
|
||||
checksum = sum(int(nip[i]) * weights[i] for i in range(9)) % 11
|
||||
return checksum == int(nip[9])
|
||||
|
||||
|
||||
# ============================================================
|
||||
# COMPANIES ADMIN ROUTES
|
||||
# ============================================================
|
||||
|
||||
@bp.route('/companies')
|
||||
@login_required
|
||||
def admin_companies():
|
||||
"""Admin panel for company management"""
|
||||
if not current_user.is_admin:
|
||||
flash('Brak uprawnień do tej strony.', 'error')
|
||||
return redirect(url_for('index'))
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
# Get filter parameters
|
||||
status_filter = request.args.get('status', 'all')
|
||||
category_filter = request.args.get('category', '')
|
||||
quality_filter = request.args.get('quality', '')
|
||||
search_query = request.args.get('q', '').strip()
|
||||
|
||||
# Base query
|
||||
query = db.query(Company)
|
||||
|
||||
# Apply filters
|
||||
if status_filter and status_filter != 'all':
|
||||
query = query.filter(Company.status == status_filter)
|
||||
|
||||
if category_filter:
|
||||
query = query.filter(Company.category_id == int(category_filter))
|
||||
|
||||
if quality_filter:
|
||||
query = query.filter(Company.data_quality == quality_filter)
|
||||
|
||||
if search_query:
|
||||
search_pattern = f'%{search_query}%'
|
||||
query = query.filter(
|
||||
(Company.name.ilike(search_pattern)) |
|
||||
(Company.nip.ilike(search_pattern))
|
||||
)
|
||||
|
||||
# Order and fetch
|
||||
companies = query.order_by(Company.name).all()
|
||||
|
||||
# Get categories for filter dropdown
|
||||
categories = db.query(Category).order_by(Category.name).all()
|
||||
|
||||
# Statistics
|
||||
total_companies = db.query(Company).count()
|
||||
active_count = db.query(Company).filter(Company.status == 'active').count()
|
||||
pending_count = db.query(Company).filter(Company.status == 'pending').count()
|
||||
inactive_count = db.query(Company).filter(Company.status == 'inactive').count()
|
||||
|
||||
logger.info(f"Admin {current_user.email} accessed companies panel - {total_companies} companies")
|
||||
|
||||
return render_template(
|
||||
'admin/companies.html',
|
||||
companies=companies,
|
||||
categories=categories,
|
||||
total_companies=total_companies,
|
||||
active_count=active_count,
|
||||
pending_count=pending_count,
|
||||
inactive_count=inactive_count,
|
||||
current_status=status_filter,
|
||||
current_category=category_filter,
|
||||
current_quality=quality_filter,
|
||||
search_query=search_query
|
||||
)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/companies/add', methods=['POST'])
|
||||
@login_required
|
||||
def admin_company_add():
|
||||
"""Create a new company"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
data = request.get_json() or {}
|
||||
|
||||
name = data.get('name', '').strip()
|
||||
if not name:
|
||||
return jsonify({'success': False, 'error': 'Nazwa firmy jest wymagana'}), 400
|
||||
|
||||
nip = data.get('nip', '').strip().replace('-', '').replace(' ', '')
|
||||
if nip:
|
||||
if not validate_nip(nip):
|
||||
return jsonify({'success': False, 'error': 'Nieprawidłowy NIP'}), 400
|
||||
|
||||
existing = db.query(Company).filter(Company.nip == nip).first()
|
||||
if existing:
|
||||
return jsonify({'success': False, 'error': f'Firma z NIP {nip} już istnieje'}), 400
|
||||
|
||||
# Generate slug from name
|
||||
slug = re.sub(r'[^\w\s-]', '', name.lower())
|
||||
slug = re.sub(r'[\s_]+', '-', slug)
|
||||
slug = re.sub(r'-+', '-', slug).strip('-')
|
||||
|
||||
# Ensure unique slug
|
||||
base_slug = slug
|
||||
counter = 1
|
||||
while db.query(Company).filter(Company.slug == slug).first():
|
||||
slug = f"{base_slug}-{counter}"
|
||||
counter += 1
|
||||
|
||||
new_company = Company(
|
||||
name=name,
|
||||
slug=slug,
|
||||
nip=nip if nip else None,
|
||||
category_id=data.get('category_id') or None,
|
||||
status=data.get('status', 'pending'),
|
||||
email=data.get('email', '').strip() or None,
|
||||
phone=data.get('phone', '').strip() or None,
|
||||
address_city=data.get('address_city', '').strip() or None,
|
||||
address_street=data.get('address_street', '').strip() or None,
|
||||
address_postal=data.get('address_postal', '').strip() or None,
|
||||
data_quality='basic'
|
||||
)
|
||||
|
||||
db.add(new_company)
|
||||
db.commit()
|
||||
db.refresh(new_company)
|
||||
|
||||
logger.info(f"Admin {current_user.email} created new company: {name} (ID: {new_company.id})")
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'company_id': new_company.id,
|
||||
'message': f'Firma "{name}" została utworzona'
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Error creating company: {e}")
|
||||
return jsonify({'success': False, 'error': 'Błąd podczas tworzenia firmy'}), 500
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/companies/<int:company_id>')
|
||||
@login_required
|
||||
def admin_company_get(company_id):
|
||||
"""Get company details (JSON)"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
company = db.query(Company).filter(Company.id == company_id).first()
|
||||
if not company:
|
||||
return jsonify({'success': False, 'error': 'Firma nie istnieje'}), 404
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'company': {
|
||||
'id': company.id,
|
||||
'name': company.name,
|
||||
'nip': company.nip,
|
||||
'category_id': company.category_id,
|
||||
'status': company.status,
|
||||
'email': company.email,
|
||||
'phone': company.phone,
|
||||
'address_city': company.address_city,
|
||||
'address_street': company.address_street,
|
||||
'address_postal': company.address_postal,
|
||||
'data_quality': company.data_quality
|
||||
}
|
||||
})
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/companies/<int:company_id>/update', methods=['POST'])
|
||||
@login_required
|
||||
def admin_company_update(company_id):
|
||||
"""Update company data"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
company = db.query(Company).filter(Company.id == company_id).first()
|
||||
if not company:
|
||||
return jsonify({'success': False, 'error': 'Firma nie istnieje'}), 404
|
||||
|
||||
data = request.get_json() or {}
|
||||
|
||||
if 'name' in data:
|
||||
name = data['name'].strip()
|
||||
if not name:
|
||||
return jsonify({'success': False, 'error': 'Nazwa firmy jest wymagana'}), 400
|
||||
company.name = name
|
||||
|
||||
if 'nip' in data:
|
||||
nip = data['nip'].strip().replace('-', '').replace(' ', '') if data['nip'] else ''
|
||||
if nip:
|
||||
if not validate_nip(nip):
|
||||
return jsonify({'success': False, 'error': 'Nieprawidłowy NIP'}), 400
|
||||
|
||||
existing = db.query(Company).filter(Company.nip == nip, Company.id != company_id).first()
|
||||
if existing:
|
||||
return jsonify({'success': False, 'error': f'Firma z NIP {nip} już istnieje'}), 400
|
||||
company.nip = nip if nip else None
|
||||
|
||||
if 'category_id' in data:
|
||||
company.category_id = data['category_id'] if data['category_id'] else None
|
||||
|
||||
if 'status' in data:
|
||||
if data['status'] in ['active', 'pending', 'inactive', 'archived']:
|
||||
company.status = data['status']
|
||||
|
||||
if 'email' in data:
|
||||
company.email = data['email'].strip() if data['email'] else None
|
||||
|
||||
if 'phone' in data:
|
||||
company.phone = data['phone'].strip() if data['phone'] else None
|
||||
|
||||
if 'address_city' in data:
|
||||
company.address_city = data['address_city'].strip() if data['address_city'] else None
|
||||
|
||||
if 'address_street' in data:
|
||||
company.address_street = data['address_street'].strip() if data['address_street'] else None
|
||||
|
||||
if 'address_postal' in data:
|
||||
company.address_postal = data['address_postal'].strip() if data['address_postal'] else None
|
||||
|
||||
company.last_updated = datetime.utcnow()
|
||||
db.commit()
|
||||
|
||||
logger.info(f"Admin {current_user.email} updated company {company.name} (ID: {company_id})")
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': 'Dane firmy zaktualizowane'
|
||||
})
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Error updating company {company_id}: {e}")
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/companies/<int:company_id>/toggle-status', methods=['POST'])
|
||||
@login_required
|
||||
def admin_company_toggle_status(company_id):
|
||||
"""Toggle company status (active <-> inactive)"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
company = db.query(Company).filter(Company.id == company_id).first()
|
||||
if not company:
|
||||
return jsonify({'success': False, 'error': 'Firma nie istnieje'}), 404
|
||||
|
||||
if company.status == 'active':
|
||||
company.status = 'inactive'
|
||||
else:
|
||||
company.status = 'active'
|
||||
|
||||
company.last_updated = datetime.utcnow()
|
||||
db.commit()
|
||||
|
||||
logger.info(f"Admin {current_user.email} toggled company {company.name} status to {company.status}")
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'status': company.status,
|
||||
'message': f"Status zmieniony na {'aktywna' if company.status == 'active' else 'nieaktywna'}"
|
||||
})
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/companies/<int:company_id>/delete', methods=['POST'])
|
||||
@login_required
|
||||
def admin_company_delete(company_id):
|
||||
"""Soft delete company (set status to archived)"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
company = db.query(Company).filter(Company.id == company_id).first()
|
||||
if not company:
|
||||
return jsonify({'success': False, 'error': 'Firma nie istnieje'}), 404
|
||||
|
||||
company.status = 'archived'
|
||||
company.last_updated = datetime.utcnow()
|
||||
db.commit()
|
||||
|
||||
logger.info(f"Admin {current_user.email} archived company {company.name} (ID: {company_id})")
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': f'Firma "{company.name}" została zarchiwizowana'
|
||||
})
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/companies/<int:company_id>/assign-user', methods=['POST'])
|
||||
@login_required
|
||||
def admin_company_assign_user(company_id):
|
||||
"""Assign a user to a company"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
data = request.get_json() or {}
|
||||
user_id = data.get('user_id')
|
||||
|
||||
company = db.query(Company).filter(Company.id == company_id).first()
|
||||
if not company:
|
||||
return jsonify({'success': False, 'error': 'Firma nie istnieje'}), 404
|
||||
|
||||
if user_id:
|
||||
user = db.query(User).filter(User.id == user_id).first()
|
||||
if not user:
|
||||
return jsonify({'success': False, 'error': 'Użytkownik nie istnieje'}), 404
|
||||
|
||||
user.company_id = company_id
|
||||
db.commit()
|
||||
|
||||
logger.info(f"Admin {current_user.email} assigned user {user.email} to company {company.name}")
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': f'Użytkownik {user.email} przypisany do {company.name}'
|
||||
})
|
||||
else:
|
||||
return jsonify({'success': False, 'error': 'Nie podano ID użytkownika'}), 400
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/companies/<int:company_id>/people')
|
||||
@login_required
|
||||
def admin_company_people(company_id):
|
||||
"""Get people associated with a company"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
company = db.query(Company).filter(Company.id == company_id).first()
|
||||
if not company:
|
||||
return jsonify({'success': False, 'error': 'Firma nie istnieje'}), 404
|
||||
|
||||
# Get CompanyPerson relationships
|
||||
people_roles = db.query(CompanyPerson).filter(
|
||||
CompanyPerson.company_id == company_id
|
||||
).all()
|
||||
|
||||
people_list = []
|
||||
for cp in people_roles:
|
||||
person = cp.person
|
||||
people_list.append({
|
||||
'id': cp.id,
|
||||
'person_id': person.id,
|
||||
'imiona': person.imiona,
|
||||
'nazwisko': person.nazwisko,
|
||||
'pesel_masked': f"{person.pesel[:4]}******" if person.pesel else None,
|
||||
'role': cp.role,
|
||||
'role_category': cp.role_category,
|
||||
'shares_percent': float(cp.shares_percent) if cp.shares_percent else None
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'company_name': company.name,
|
||||
'people': people_list
|
||||
})
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/companies/<int:company_id>/users')
|
||||
@login_required
|
||||
def admin_company_users(company_id):
|
||||
"""Get users assigned to a company"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
company = db.query(Company).filter(Company.id == company_id).first()
|
||||
if not company:
|
||||
return jsonify({'success': False, 'error': 'Firma nie istnieje'}), 404
|
||||
|
||||
users = db.query(User).filter(User.company_id == company_id).all()
|
||||
|
||||
users_list = [{
|
||||
'id': u.id,
|
||||
'name': u.name,
|
||||
'email': u.email,
|
||||
'is_admin': u.is_admin,
|
||||
'is_verified': u.is_verified
|
||||
} for u in users]
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'company_name': company.name,
|
||||
'users': users_list
|
||||
})
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/companies/export')
|
||||
@login_required
|
||||
def admin_companies_export():
|
||||
"""Export companies to CSV"""
|
||||
if not current_user.is_admin:
|
||||
flash('Brak uprawnień do tej strony.', 'error')
|
||||
return redirect(url_for('index'))
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
companies = db.query(Company).order_by(Company.name).all()
|
||||
|
||||
output = StringIO()
|
||||
writer = csv.writer(output)
|
||||
|
||||
# Header
|
||||
writer.writerow([
|
||||
'ID', 'Nazwa', 'NIP', 'Kategoria', 'Status',
|
||||
'Email', 'Telefon', 'Miasto', 'Ulica', 'Kod pocztowy',
|
||||
'Jakość danych', 'Data utworzenia'
|
||||
])
|
||||
|
||||
# Data rows
|
||||
for c in companies:
|
||||
writer.writerow([
|
||||
c.id,
|
||||
c.name,
|
||||
c.nip or '',
|
||||
c.category.name if c.category else '',
|
||||
c.status or '',
|
||||
c.email or '',
|
||||
c.phone or '',
|
||||
c.address_city or '',
|
||||
c.address_street or '',
|
||||
c.address_postal or '',
|
||||
c.data_quality or '',
|
||||
c.created_at.strftime('%Y-%m-%d') if c.created_at else ''
|
||||
])
|
||||
|
||||
output.seek(0)
|
||||
|
||||
logger.info(f"Admin {current_user.email} exported {len(companies)} companies to CSV")
|
||||
|
||||
return Response(
|
||||
output.getvalue(),
|
||||
mimetype='text/csv',
|
||||
headers={
|
||||
'Content-Disposition': f'attachment; filename=companies_{datetime.now().strftime("%Y%m%d")}.csv'
|
||||
}
|
||||
)
|
||||
finally:
|
||||
db.close()
|
||||
467
blueprints/admin/routes_people.py
Normal file
467
blueprints/admin/routes_people.py
Normal file
@ -0,0 +1,467 @@
|
||||
"""
|
||||
Admin Routes - People
|
||||
=====================
|
||||
|
||||
CRUD operations for person management in admin panel.
|
||||
People are linked to companies via CompanyPerson relationships.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
from flask import render_template, request, redirect, url_for, flash, jsonify
|
||||
from flask_login import login_required, current_user
|
||||
|
||||
from . import bp
|
||||
from database import SessionLocal, Company, Person, CompanyPerson
|
||||
|
||||
# Logger
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# PEOPLE ADMIN ROUTES
|
||||
# ============================================================
|
||||
|
||||
@bp.route('/people')
|
||||
@login_required
|
||||
def admin_people():
|
||||
"""Admin panel for person management"""
|
||||
if not current_user.is_admin:
|
||||
flash('Brak uprawnień do tej strony.', 'error')
|
||||
return redirect(url_for('index'))
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
# Get search query
|
||||
search_query = request.args.get('q', '').strip()
|
||||
role_filter = request.args.get('role', '')
|
||||
|
||||
# Base query
|
||||
query = db.query(Person)
|
||||
|
||||
if search_query:
|
||||
search_pattern = f'%{search_query}%'
|
||||
query = query.filter(
|
||||
(Person.imiona.ilike(search_pattern)) |
|
||||
(Person.nazwisko.ilike(search_pattern))
|
||||
)
|
||||
|
||||
# Order and fetch
|
||||
people = query.order_by(Person.nazwisko, Person.imiona).all()
|
||||
|
||||
# Apply role filter after fetching (need to check relationships)
|
||||
if role_filter:
|
||||
filtered_people = []
|
||||
for person in people:
|
||||
roles = [cp.role_category for cp in person.company_roles]
|
||||
if role_filter in roles:
|
||||
filtered_people.append(person)
|
||||
people = filtered_people
|
||||
|
||||
# Statistics
|
||||
total_people = db.query(Person).count()
|
||||
|
||||
# Count people with company relationships
|
||||
with_companies = db.query(Person).join(CompanyPerson).distinct().count()
|
||||
|
||||
# Count by role_category
|
||||
zarzad_count = db.query(CompanyPerson).filter(
|
||||
CompanyPerson.role_category == 'zarzad'
|
||||
).distinct(CompanyPerson.person_id).count()
|
||||
|
||||
wspolnik_count = db.query(CompanyPerson).filter(
|
||||
CompanyPerson.role_category == 'wspolnik'
|
||||
).distinct(CompanyPerson.person_id).count()
|
||||
|
||||
# Prepare people data with relationships
|
||||
people_data = []
|
||||
for person in people:
|
||||
roles_list = []
|
||||
for cp in person.company_roles:
|
||||
roles_list.append({
|
||||
'role': cp.role,
|
||||
'role_category': cp.role_category,
|
||||
'company_name': cp.company.name if cp.company else 'Nieznana',
|
||||
'company_id': cp.company_id
|
||||
})
|
||||
|
||||
people_data.append({
|
||||
'id': person.id,
|
||||
'imiona': person.imiona,
|
||||
'nazwisko': person.nazwisko,
|
||||
'pesel_masked': f"{person.pesel[:4]}******" if person.pesel else None,
|
||||
'full_name': person.full_name(),
|
||||
'roles': roles_list,
|
||||
'roles_count': len(roles_list)
|
||||
})
|
||||
|
||||
logger.info(f"Admin {current_user.email} accessed people panel - {total_people} people")
|
||||
|
||||
return render_template(
|
||||
'admin/people.html',
|
||||
people=people_data,
|
||||
total_people=total_people,
|
||||
with_companies=with_companies,
|
||||
zarzad_count=zarzad_count,
|
||||
wspolnik_count=wspolnik_count,
|
||||
search_query=search_query,
|
||||
current_role=role_filter
|
||||
)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/people/add', methods=['POST'])
|
||||
@login_required
|
||||
def admin_person_add():
|
||||
"""Create a new person"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
data = request.get_json() or {}
|
||||
|
||||
imiona = data.get('imiona', '').strip()
|
||||
nazwisko = data.get('nazwisko', '').strip()
|
||||
|
||||
if not imiona or not nazwisko:
|
||||
return jsonify({'success': False, 'error': 'Imiona i nazwisko są wymagane'}), 400
|
||||
|
||||
pesel = data.get('pesel', '').strip() if data.get('pesel') else None
|
||||
|
||||
# Validate PESEL if provided
|
||||
if pesel:
|
||||
if len(pesel) != 11 or not pesel.isdigit():
|
||||
return jsonify({'success': False, 'error': 'PESEL musi mieć 11 cyfr'}), 400
|
||||
|
||||
existing = db.query(Person).filter(Person.pesel == pesel).first()
|
||||
if existing:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': f'Osoba z tym PESEL już istnieje: {existing.full_name()}'
|
||||
}), 400
|
||||
|
||||
new_person = Person(
|
||||
imiona=imiona,
|
||||
nazwisko=nazwisko,
|
||||
pesel=pesel
|
||||
)
|
||||
|
||||
db.add(new_person)
|
||||
db.commit()
|
||||
db.refresh(new_person)
|
||||
|
||||
logger.info(f"Admin {current_user.email} created new person: {new_person.full_name()} (ID: {new_person.id})")
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'person_id': new_person.id,
|
||||
'message': f'Osoba "{new_person.full_name()}" została utworzona'
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Error creating person: {e}")
|
||||
return jsonify({'success': False, 'error': 'Błąd podczas tworzenia osoby'}), 500
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/people/<int:person_id>')
|
||||
@login_required
|
||||
def admin_person_get(person_id):
|
||||
"""Get person details (JSON)"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
person = db.query(Person).filter(Person.id == person_id).first()
|
||||
if not person:
|
||||
return jsonify({'success': False, 'error': 'Osoba nie istnieje'}), 404
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'person': {
|
||||
'id': person.id,
|
||||
'imiona': person.imiona,
|
||||
'nazwisko': person.nazwisko,
|
||||
'pesel_masked': f"{person.pesel[:4]}******" if person.pesel else None,
|
||||
'full_name': person.full_name()
|
||||
}
|
||||
})
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/people/<int:person_id>/update', methods=['POST'])
|
||||
@login_required
|
||||
def admin_person_update(person_id):
|
||||
"""Update person data"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
person = db.query(Person).filter(Person.id == person_id).first()
|
||||
if not person:
|
||||
return jsonify({'success': False, 'error': 'Osoba nie istnieje'}), 404
|
||||
|
||||
data = request.get_json() or {}
|
||||
|
||||
if 'imiona' in data:
|
||||
imiona = data['imiona'].strip()
|
||||
if not imiona:
|
||||
return jsonify({'success': False, 'error': 'Imiona są wymagane'}), 400
|
||||
person.imiona = imiona
|
||||
|
||||
if 'nazwisko' in data:
|
||||
nazwisko = data['nazwisko'].strip()
|
||||
if not nazwisko:
|
||||
return jsonify({'success': False, 'error': 'Nazwisko jest wymagane'}), 400
|
||||
person.nazwisko = nazwisko
|
||||
|
||||
if 'pesel' in data:
|
||||
pesel = data['pesel'].strip() if data['pesel'] else None
|
||||
if pesel:
|
||||
if len(pesel) != 11 or not pesel.isdigit():
|
||||
return jsonify({'success': False, 'error': 'PESEL musi mieć 11 cyfr'}), 400
|
||||
|
||||
existing = db.query(Person).filter(Person.pesel == pesel, Person.id != person_id).first()
|
||||
if existing:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': f'Osoba z tym PESEL już istnieje: {existing.full_name()}'
|
||||
}), 400
|
||||
person.pesel = pesel
|
||||
|
||||
person.updated_at = datetime.utcnow()
|
||||
db.commit()
|
||||
|
||||
logger.info(f"Admin {current_user.email} updated person {person.full_name()} (ID: {person_id})")
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': 'Dane osoby zaktualizowane'
|
||||
})
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Error updating person {person_id}: {e}")
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/people/<int:person_id>/delete', methods=['POST'])
|
||||
@login_required
|
||||
def admin_person_delete(person_id):
|
||||
"""Delete person (hard delete with CASCADE on CompanyPerson)"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
person = db.query(Person).filter(Person.id == person_id).first()
|
||||
if not person:
|
||||
return jsonify({'success': False, 'error': 'Osoba nie istnieje'}), 404
|
||||
|
||||
person_name = person.full_name()
|
||||
roles_count = len(person.company_roles)
|
||||
|
||||
# Delete person (CASCADE will handle CompanyPerson)
|
||||
db.delete(person)
|
||||
db.commit()
|
||||
|
||||
logger.info(f"Admin {current_user.email} deleted person {person_name} (ID: {person_id}, had {roles_count} roles)")
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': f'Osoba "{person_name}" została usunięta'
|
||||
})
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/people/<int:person_id>/companies')
|
||||
@login_required
|
||||
def admin_person_companies(person_id):
|
||||
"""Get companies associated with a person"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
person = db.query(Person).filter(Person.id == person_id).first()
|
||||
if not person:
|
||||
return jsonify({'success': False, 'error': 'Osoba nie istnieje'}), 404
|
||||
|
||||
companies_list = []
|
||||
for cp in person.company_roles:
|
||||
company = cp.company
|
||||
companies_list.append({
|
||||
'link_id': cp.id,
|
||||
'company_id': company.id if company else None,
|
||||
'company_name': company.name if company else 'Nieznana',
|
||||
'company_nip': company.nip if company else None,
|
||||
'role': cp.role,
|
||||
'role_category': cp.role_category,
|
||||
'shares_percent': float(cp.shares_percent) if cp.shares_percent else None
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'person_name': person.full_name(),
|
||||
'companies': companies_list
|
||||
})
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/people/<int:person_id>/link-company', methods=['POST'])
|
||||
@login_required
|
||||
def admin_person_link_company(person_id):
|
||||
"""Link person to a company"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
data = request.get_json() or {}
|
||||
|
||||
person = db.query(Person).filter(Person.id == person_id).first()
|
||||
if not person:
|
||||
return jsonify({'success': False, 'error': 'Osoba nie istnieje'}), 404
|
||||
|
||||
company_id = data.get('company_id')
|
||||
if not company_id:
|
||||
return jsonify({'success': False, 'error': 'Nie podano firmy'}), 400
|
||||
|
||||
company = db.query(Company).filter(Company.id == company_id).first()
|
||||
if not company:
|
||||
return jsonify({'success': False, 'error': 'Firma nie istnieje'}), 404
|
||||
|
||||
role = data.get('role', '').strip()
|
||||
role_category = data.get('role_category', '').strip()
|
||||
|
||||
if not role or not role_category:
|
||||
return jsonify({'success': False, 'error': 'Rola i kategoria roli są wymagane'}), 400
|
||||
|
||||
if role_category not in ['zarzad', 'wspolnik', 'prokurent']:
|
||||
return jsonify({'success': False, 'error': 'Nieprawidłowa kategoria roli'}), 400
|
||||
|
||||
# Check if link already exists
|
||||
existing = db.query(CompanyPerson).filter(
|
||||
CompanyPerson.company_id == company_id,
|
||||
CompanyPerson.person_id == person_id,
|
||||
CompanyPerson.role_category == role_category,
|
||||
CompanyPerson.role == role
|
||||
).first()
|
||||
|
||||
if existing:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': f'Ta osoba jest już powiązana z firmą w tej roli'
|
||||
}), 400
|
||||
|
||||
new_link = CompanyPerson(
|
||||
company_id=company_id,
|
||||
person_id=person_id,
|
||||
role=role,
|
||||
role_category=role_category,
|
||||
shares_percent=data.get('shares_percent') if role_category == 'wspolnik' else None,
|
||||
source='admin_panel'
|
||||
)
|
||||
|
||||
db.add(new_link)
|
||||
db.commit()
|
||||
|
||||
logger.info(f"Admin {current_user.email} linked person {person.full_name()} to company {company.name} as {role}")
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': f'Powiązano {person.full_name()} z {company.name} jako {role}'
|
||||
})
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Error linking person to company: {e}")
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/people/<int:person_id>/unlink-company/<int:company_id>', methods=['POST'])
|
||||
@login_required
|
||||
def admin_person_unlink_company(person_id, company_id):
|
||||
"""Remove person-company link"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
data = request.get_json() or {}
|
||||
link_id = data.get('link_id')
|
||||
|
||||
if link_id:
|
||||
# Delete specific link by ID
|
||||
link = db.query(CompanyPerson).filter(
|
||||
CompanyPerson.id == link_id,
|
||||
CompanyPerson.person_id == person_id,
|
||||
CompanyPerson.company_id == company_id
|
||||
).first()
|
||||
else:
|
||||
# Delete first matching link
|
||||
link = db.query(CompanyPerson).filter(
|
||||
CompanyPerson.person_id == person_id,
|
||||
CompanyPerson.company_id == company_id
|
||||
).first()
|
||||
|
||||
if not link:
|
||||
return jsonify({'success': False, 'error': 'Powiązanie nie istnieje'}), 404
|
||||
|
||||
person = link.person
|
||||
company = link.company
|
||||
role = link.role
|
||||
|
||||
db.delete(link)
|
||||
db.commit()
|
||||
|
||||
logger.info(f"Admin {current_user.email} unlinked person {person.full_name()} from company {company.name} (role: {role})")
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': f'Usunięto powiązanie z firmą {company.name}'
|
||||
})
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@bp.route('/people/search')
|
||||
@login_required
|
||||
def admin_people_search():
|
||||
"""Search people for autocomplete"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Brak uprawnień'}), 403
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
query = request.args.get('q', '').strip()
|
||||
if len(query) < 2:
|
||||
return jsonify({'success': True, 'results': []})
|
||||
|
||||
search_pattern = f'%{query}%'
|
||||
people = db.query(Person).filter(
|
||||
(Person.imiona.ilike(search_pattern)) |
|
||||
(Person.nazwisko.ilike(search_pattern))
|
||||
).limit(10).all()
|
||||
|
||||
results = [{
|
||||
'id': p.id,
|
||||
'text': p.full_name(),
|
||||
'pesel_masked': f"{p.pesel[:4]}******" if p.pesel else None
|
||||
} for p in people]
|
||||
|
||||
return jsonify({'success': True, 'results': results})
|
||||
finally:
|
||||
db.close()
|
||||
983
templates/admin/companies.html
Normal file
983
templates/admin/companies.html
Normal file
@ -0,0 +1,983 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Zarządzanie Firmami - Norda Biznes Partner{% endblock %}
|
||||
|
||||
{% block extra_css %}
|
||||
<style>
|
||||
.admin-header {
|
||||
margin-bottom: var(--spacing-xl);
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.admin-header-content h1 {
|
||||
font-size: var(--font-size-3xl);
|
||||
color: var(--text-primary);
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.admin-header-content p {
|
||||
margin: var(--spacing-xs) 0 0 0;
|
||||
}
|
||||
|
||||
.header-buttons {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.btn-add {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
padding: var(--spacing-sm) var(--spacing-lg);
|
||||
background: var(--primary);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: var(--radius);
|
||||
font-size: var(--font-size-base);
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.btn-add:hover { opacity: 0.9; }
|
||||
.btn-add svg { width: 20px; height: 20px; }
|
||||
|
||||
.btn-export {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
padding: var(--spacing-sm) var(--spacing-lg);
|
||||
background: var(--surface);
|
||||
color: var(--text-primary);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
font-size: var(--font-size-base);
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: var(--transition);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.btn-export:hover { background: var(--background); }
|
||||
.btn-export svg { width: 20px; height: 20px; }
|
||||
|
||||
.stats-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
||||
gap: var(--spacing-lg);
|
||||
margin-bottom: var(--spacing-2xl);
|
||||
}
|
||||
|
||||
.stat-card {
|
||||
background: var(--surface);
|
||||
padding: var(--spacing-lg);
|
||||
border-radius: var(--radius-lg);
|
||||
box-shadow: var(--shadow);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.stat-value {
|
||||
font-size: var(--font-size-3xl);
|
||||
font-weight: 700;
|
||||
color: var(--primary);
|
||||
}
|
||||
|
||||
.stat-label {
|
||||
color: var(--text-secondary);
|
||||
font-size: var(--font-size-sm);
|
||||
margin-top: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.filters-row {
|
||||
display: flex;
|
||||
gap: var(--spacing-md);
|
||||
margin-bottom: var(--spacing-lg);
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.filter-group {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.filter-group label {
|
||||
font-size: var(--font-size-sm);
|
||||
color: var(--text-secondary);
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.filter-select, .filter-input {
|
||||
padding: var(--spacing-xs) var(--spacing-sm);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
font-size: var(--font-size-sm);
|
||||
background: var(--surface);
|
||||
}
|
||||
|
||||
.filter-select:focus, .filter-input:focus {
|
||||
outline: none;
|
||||
border-color: var(--primary);
|
||||
}
|
||||
|
||||
.section {
|
||||
background: var(--surface);
|
||||
padding: var(--spacing-xl);
|
||||
border-radius: var(--radius-lg);
|
||||
box-shadow: var(--shadow);
|
||||
margin-bottom: var(--spacing-xl);
|
||||
}
|
||||
|
||||
.section h2 {
|
||||
font-size: var(--font-size-xl);
|
||||
margin-bottom: var(--spacing-lg);
|
||||
color: var(--text-primary);
|
||||
border-bottom: 2px solid var(--border);
|
||||
padding-bottom: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.data-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
.data-table th, .data-table td {
|
||||
padding: var(--spacing-md);
|
||||
text-align: left;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.data-table th {
|
||||
font-weight: 600;
|
||||
color: var(--text-secondary);
|
||||
font-size: var(--font-size-sm);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
}
|
||||
|
||||
.data-table tr:hover { background: var(--background); }
|
||||
|
||||
.company-info {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2px;
|
||||
}
|
||||
|
||||
.company-name {
|
||||
font-weight: 500;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.company-city {
|
||||
font-size: var(--font-size-sm);
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.badge {
|
||||
display: inline-block;
|
||||
padding: 2px 8px;
|
||||
border-radius: var(--radius-sm);
|
||||
font-size: var(--font-size-xs);
|
||||
font-weight: 500;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.badge-active { background: #D1FAE5; color: #065F46; }
|
||||
.badge-pending { background: #FEF3C7; color: #92400E; }
|
||||
.badge-inactive { background: #E5E7EB; color: #374151; }
|
||||
.badge-archived { background: #FEE2E2; color: #991B1B; }
|
||||
.badge-basic { background: #E5E7EB; color: #374151; }
|
||||
.badge-enhanced { background: #DBEAFE; color: #1D4ED8; }
|
||||
.badge-complete { background: #D1FAE5; color: #065F46; }
|
||||
|
||||
.action-buttons {
|
||||
display: flex;
|
||||
gap: var(--spacing-xs);
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.btn-icon {
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
padding: 0;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border-radius: var(--radius);
|
||||
border: 1px solid var(--border);
|
||||
background: var(--surface);
|
||||
cursor: pointer;
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.btn-icon:hover { background: var(--background); }
|
||||
.btn-icon svg { width: 16px; height: 16px; }
|
||||
|
||||
.btn-icon.status-toggle { background: #D1FAE5; border-color: #10B981; color: #065F46; }
|
||||
.btn-icon.status-toggle.inactive { background: #E5E7EB; border-color: #9CA3AF; color: #374151; }
|
||||
.btn-icon.danger:hover { background: var(--error); border-color: var(--error); color: white; }
|
||||
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: var(--spacing-2xl);
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
/* Modal styles */
|
||||
.modal {
|
||||
display: none;
|
||||
position: fixed;
|
||||
z-index: 1000;
|
||||
left: 0;
|
||||
top: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.modal.active { display: flex; }
|
||||
|
||||
.modal-content {
|
||||
background: var(--surface);
|
||||
padding: var(--spacing-xl);
|
||||
border-radius: var(--radius-lg);
|
||||
max-width: 500px;
|
||||
width: 90%;
|
||||
box-shadow: var(--shadow-lg);
|
||||
max-height: 90vh;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.modal-header {
|
||||
font-size: var(--font-size-xl);
|
||||
margin-bottom: var(--spacing-md);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.modal-body { margin-bottom: var(--spacing-lg); }
|
||||
|
||||
.modal-footer {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.form-group { margin-bottom: var(--spacing-md); }
|
||||
|
||||
.form-label {
|
||||
display: block;
|
||||
margin-bottom: var(--spacing-xs);
|
||||
color: var(--text-secondary);
|
||||
font-size: var(--font-size-sm);
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.form-control {
|
||||
width: 100%;
|
||||
padding: var(--spacing-sm);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
font-size: var(--font-size-base);
|
||||
font-family: inherit;
|
||||
}
|
||||
|
||||
.form-control:focus {
|
||||
outline: none;
|
||||
border-color: var(--primary);
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: var(--spacing-sm) var(--spacing-md);
|
||||
border: none;
|
||||
border-radius: var(--radius);
|
||||
font-size: var(--font-size-base);
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.btn-primary { background: var(--primary); color: white; }
|
||||
.btn-primary:hover { opacity: 0.9; }
|
||||
.btn-secondary { background: var(--background); color: var(--text-secondary); }
|
||||
.btn-secondary:hover { background: var(--border); }
|
||||
.btn-danger { background: #EF4444; color: white; }
|
||||
.btn-danger:hover { background: #DC2626; }
|
||||
|
||||
/* Toast styles */
|
||||
.toast-container {
|
||||
position: fixed;
|
||||
top: 20px;
|
||||
right: 20px;
|
||||
z-index: 2000;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.toast {
|
||||
background: var(--surface);
|
||||
padding: var(--spacing-md) var(--spacing-lg);
|
||||
border-radius: var(--radius-lg);
|
||||
box-shadow: var(--shadow-lg);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
min-width: 280px;
|
||||
max-width: 400px;
|
||||
animation: slideIn 0.3s ease;
|
||||
border-left: 4px solid var(--primary);
|
||||
}
|
||||
|
||||
.toast.success { border-left-color: #10B981; }
|
||||
.toast.error { border-left-color: #EF4444; }
|
||||
|
||||
.toast-icon { width: 24px; height: 24px; flex-shrink: 0; }
|
||||
.toast-icon.success { color: #10B981; }
|
||||
.toast-icon.error { color: #EF4444; }
|
||||
|
||||
.toast-message { flex: 1; color: var(--text-primary); }
|
||||
|
||||
.toast-close {
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--text-secondary);
|
||||
cursor: pointer;
|
||||
padding: 4px;
|
||||
}
|
||||
|
||||
@keyframes slideIn {
|
||||
from { transform: translateX(100%); opacity: 0; }
|
||||
to { transform: translateX(0); opacity: 1; }
|
||||
}
|
||||
|
||||
@keyframes slideOut {
|
||||
from { transform: translateX(0); opacity: 1; }
|
||||
to { transform: translateX(100%); opacity: 0; }
|
||||
}
|
||||
|
||||
/* Confirmation modal */
|
||||
.modal-icon {
|
||||
width: 48px;
|
||||
height: 48px;
|
||||
margin: 0 auto var(--spacing-md);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border-radius: 50%;
|
||||
}
|
||||
|
||||
.modal-icon.warning { background: #FEF3C7; color: #F59E0B; }
|
||||
.modal-icon.danger { background: #FEE2E2; color: #EF4444; }
|
||||
.modal-icon svg { width: 24px; height: 24px; }
|
||||
|
||||
.modal-title {
|
||||
font-size: var(--font-size-lg);
|
||||
font-weight: 600;
|
||||
text-align: center;
|
||||
margin-bottom: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.modal-description {
|
||||
text-align: center;
|
||||
color: var(--text-secondary);
|
||||
margin-bottom: var(--spacing-lg);
|
||||
}
|
||||
|
||||
/* People list in modal */
|
||||
.people-list {
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.people-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: var(--spacing-sm);
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.people-item:last-child { border-bottom: none; }
|
||||
|
||||
.people-info { flex: 1; }
|
||||
.people-name { font-weight: 500; }
|
||||
.people-role { font-size: var(--font-size-sm); color: var(--text-secondary); }
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.data-table { font-size: var(--font-size-sm); }
|
||||
.data-table th:nth-child(3), .data-table td:nth-child(3),
|
||||
.data-table th:nth-child(6), .data-table td:nth-child(6) { display: none; }
|
||||
.filters-row { flex-direction: column; align-items: stretch; }
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="admin-header">
|
||||
<div class="admin-header-content">
|
||||
<h1>Zarządzanie Firmami</h1>
|
||||
<p class="text-muted">Przeglądaj i zarządzaj firmami w katalogu</p>
|
||||
</div>
|
||||
<div class="header-buttons">
|
||||
<a href="{{ url_for('admin.admin_companies_export') }}" class="btn-export">
|
||||
<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
<path d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
|
||||
</svg>
|
||||
Eksport CSV
|
||||
</a>
|
||||
<button class="btn-add" onclick="openAddModal()">
|
||||
<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
<path d="M12 6v6m0 0v6m0-6h6m-6 0H6"/>
|
||||
</svg>
|
||||
Dodaj firmę
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Stats Grid -->
|
||||
<div class="stats-grid">
|
||||
<div class="stat-card">
|
||||
<div class="stat-value">{{ total_companies }}</div>
|
||||
<div class="stat-label">Wszystkich</div>
|
||||
</div>
|
||||
<div class="stat-card">
|
||||
<div class="stat-value" style="color: #10B981;">{{ active_count }}</div>
|
||||
<div class="stat-label">Aktywnych</div>
|
||||
</div>
|
||||
<div class="stat-card">
|
||||
<div class="stat-value" style="color: #F59E0B;">{{ pending_count }}</div>
|
||||
<div class="stat-label">Oczekujących</div>
|
||||
</div>
|
||||
<div class="stat-card">
|
||||
<div class="stat-value" style="color: #6B7280;">{{ inactive_count }}</div>
|
||||
<div class="stat-label">Nieaktywnych</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Filters -->
|
||||
<form method="GET" class="filters-row">
|
||||
<div class="filter-group">
|
||||
<label>Status:</label>
|
||||
<select name="status" class="filter-select" onchange="this.form.submit()">
|
||||
<option value="all" {{ 'selected' if current_status == 'all' else '' }}>Wszystkie</option>
|
||||
<option value="active" {{ 'selected' if current_status == 'active' else '' }}>Aktywne</option>
|
||||
<option value="pending" {{ 'selected' if current_status == 'pending' else '' }}>Oczekujące</option>
|
||||
<option value="inactive" {{ 'selected' if current_status == 'inactive' else '' }}>Nieaktywne</option>
|
||||
<option value="archived" {{ 'selected' if current_status == 'archived' else '' }}>Zarchiwizowane</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="filter-group">
|
||||
<label>Kategoria:</label>
|
||||
<select name="category" class="filter-select" onchange="this.form.submit()">
|
||||
<option value="">Wszystkie</option>
|
||||
{% for cat in categories %}
|
||||
<option value="{{ cat.id }}" {{ 'selected' if current_category == cat.id|string else '' }}>{{ cat.name }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="filter-group">
|
||||
<label>Jakość:</label>
|
||||
<select name="quality" class="filter-select" onchange="this.form.submit()">
|
||||
<option value="">Wszystkie</option>
|
||||
<option value="basic" {{ 'selected' if current_quality == 'basic' else '' }}>Podstawowa</option>
|
||||
<option value="enhanced" {{ 'selected' if current_quality == 'enhanced' else '' }}>Rozszerzona</option>
|
||||
<option value="complete" {{ 'selected' if current_quality == 'complete' else '' }}>Kompletna</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="filter-group">
|
||||
<label>Szukaj:</label>
|
||||
<input type="text" name="q" class="filter-input" placeholder="Nazwa lub NIP..." value="{{ search_query }}" style="width: 200px;">
|
||||
<button type="submit" class="btn btn-primary" style="padding: var(--spacing-xs) var(--spacing-sm);">Szukaj</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<!-- Companies Table -->
|
||||
<div class="section">
|
||||
<h2>Firmy ({{ companies|length }})</h2>
|
||||
|
||||
{% if companies %}
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Firma</th>
|
||||
<th>NIP</th>
|
||||
<th>Kategoria</th>
|
||||
<th>Status</th>
|
||||
<th>Jakość</th>
|
||||
<th>Użyt.</th>
|
||||
<th>Akcje</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for company in companies %}
|
||||
<tr data-company-id="{{ company.id }}">
|
||||
<td>{{ company.id }}</td>
|
||||
<td>
|
||||
<div class="company-info">
|
||||
<a href="{{ url_for('company_detail_by_slug', slug=company.slug) }}" class="company-name" target="_blank">{{ company.name }}</a>
|
||||
{% if company.address_city %}
|
||||
<span class="company-city">{{ company.address_city }}</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
</td>
|
||||
<td style="font-family: monospace;">{{ company.nip or '-' }}</td>
|
||||
<td>{{ company.category.name if company.category else '-' }}</td>
|
||||
<td>
|
||||
<span class="badge badge-{{ company.status or 'pending' }}">{{ company.status or 'pending' }}</span>
|
||||
</td>
|
||||
<td>
|
||||
<span class="badge badge-{{ company.data_quality or 'basic' }}">{{ company.data_quality or 'basic' }}</span>
|
||||
</td>
|
||||
<td>{{ company.users|length if company.users else 0 }}</td>
|
||||
<td>
|
||||
<div class="action-buttons">
|
||||
<button class="btn-icon" onclick="openEditModal({{ company.id }})" title="Edytuj">
|
||||
<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
<path d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
|
||||
</svg>
|
||||
</button>
|
||||
<button class="btn-icon status-toggle {{ 'inactive' if company.status != 'active' else '' }}"
|
||||
onclick="toggleStatus({{ company.id }})"
|
||||
title="{{ 'Dezaktywuj' if company.status == 'active' else 'Aktywuj' }}">
|
||||
<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
<path d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
|
||||
</svg>
|
||||
</button>
|
||||
<button class="btn-icon" onclick="openPeopleModal({{ company.id }}, '{{ company.name|e }}')" title="Osoby powiązane">
|
||||
<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
<path d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"/>
|
||||
</svg>
|
||||
</button>
|
||||
<button class="btn-icon danger" onclick="deleteCompany({{ company.id }}, '{{ company.name|e }}')" title="Archiwizuj">
|
||||
<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
<path d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% else %}
|
||||
<div class="empty-state">
|
||||
<p>Brak firm spełniających kryteria</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<!-- Add Company Modal -->
|
||||
<div id="addModal" class="modal">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">Dodaj nową firmę</div>
|
||||
<div class="modal-body">
|
||||
<div class="form-group">
|
||||
<label class="form-label">Nazwa firmy *</label>
|
||||
<input type="text" id="addName" class="form-control" placeholder="Nazwa firmy" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">NIP</label>
|
||||
<input type="text" id="addNip" class="form-control" placeholder="1234567890" maxlength="10">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Kategoria</label>
|
||||
<select id="addCategory" class="form-control">
|
||||
<option value="">-- Wybierz --</option>
|
||||
{% for cat in categories %}
|
||||
<option value="{{ cat.id }}">{{ cat.name }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Status</label>
|
||||
<select id="addStatus" class="form-control">
|
||||
<option value="pending">Oczekująca</option>
|
||||
<option value="active">Aktywna</option>
|
||||
<option value="inactive">Nieaktywna</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Email</label>
|
||||
<input type="email" id="addEmail" class="form-control" placeholder="kontakt@firma.pl">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Telefon</label>
|
||||
<input type="text" id="addPhone" class="form-control" placeholder="+48 123 456 789">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Miasto</label>
|
||||
<input type="text" id="addCity" class="form-control" placeholder="Wejherowo">
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button class="btn btn-secondary" onclick="closeAddModal()">Anuluj</button>
|
||||
<button class="btn btn-primary" onclick="confirmAdd()">Utwórz firmę</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Edit Company Modal -->
|
||||
<div id="editModal" class="modal">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">Edytuj firmę</div>
|
||||
<div class="modal-body">
|
||||
<div class="form-group">
|
||||
<label class="form-label">Nazwa firmy *</label>
|
||||
<input type="text" id="editName" class="form-control" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">NIP</label>
|
||||
<input type="text" id="editNip" class="form-control" maxlength="10">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Kategoria</label>
|
||||
<select id="editCategory" class="form-control">
|
||||
<option value="">-- Wybierz --</option>
|
||||
{% for cat in categories %}
|
||||
<option value="{{ cat.id }}">{{ cat.name }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Status</label>
|
||||
<select id="editStatus" class="form-control">
|
||||
<option value="pending">Oczekująca</option>
|
||||
<option value="active">Aktywna</option>
|
||||
<option value="inactive">Nieaktywna</option>
|
||||
<option value="archived">Zarchiwizowana</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Email</label>
|
||||
<input type="email" id="editEmail" class="form-control">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Telefon</label>
|
||||
<input type="text" id="editPhone" class="form-control">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Miasto</label>
|
||||
<input type="text" id="editCity" class="form-control">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Ulica</label>
|
||||
<input type="text" id="editStreet" class="form-control">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Kod pocztowy</label>
|
||||
<input type="text" id="editPostal" class="form-control" maxlength="6">
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button class="btn btn-secondary" onclick="closeEditModal()">Anuluj</button>
|
||||
<button class="btn btn-primary" onclick="saveEdit()">Zapisz zmiany</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- People Modal -->
|
||||
<div id="peopleModal" class="modal">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header" id="peopleModalTitle">Osoby powiązane</div>
|
||||
<div class="modal-body">
|
||||
<div id="peopleList" class="people-list">
|
||||
<div class="empty-state">Ładowanie...</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button class="btn btn-secondary" onclick="closePeopleModal()">Zamknij</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Confirm Modal -->
|
||||
<div id="confirmModal" class="modal">
|
||||
<div class="modal-content">
|
||||
<div id="confirmIcon" class="modal-icon warning">
|
||||
<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
<path d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/>
|
||||
</svg>
|
||||
</div>
|
||||
<div id="confirmTitle" class="modal-title">Potwierdzenie</div>
|
||||
<div id="confirmDescription" class="modal-description"></div>
|
||||
<div class="modal-footer" style="justify-content: center;">
|
||||
<button class="btn btn-secondary" onclick="closeConfirmModal()">Anuluj</button>
|
||||
<button id="confirmAction" class="btn btn-danger">Potwierdź</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Toast Container -->
|
||||
<div id="toastContainer" class="toast-container"></div>
|
||||
{% endblock %}
|
||||
|
||||
{% block extra_js %}
|
||||
const csrfToken = '{{ csrf_token() }}';
|
||||
let editCompanyId = null;
|
||||
let confirmCallback = null;
|
||||
|
||||
function showToast(message, type = 'success') {
|
||||
const container = document.getElementById('toastContainer');
|
||||
const toast = document.createElement('div');
|
||||
toast.className = `toast ${type}`;
|
||||
|
||||
const iconSvg = {
|
||||
success: '<path d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>',
|
||||
error: '<path d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"/>'
|
||||
};
|
||||
|
||||
toast.innerHTML = `
|
||||
<svg class="toast-icon ${type}" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
${iconSvg[type] || iconSvg.success}
|
||||
</svg>
|
||||
<span class="toast-message">${message}</span>
|
||||
<button class="toast-close" onclick="this.parentElement.remove()">
|
||||
<svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
<path d="M6 18L18 6M6 6l12 12"/>
|
||||
</svg>
|
||||
</button>
|
||||
`;
|
||||
|
||||
container.appendChild(toast);
|
||||
setTimeout(() => {
|
||||
toast.style.animation = 'slideOut 0.3s ease forwards';
|
||||
setTimeout(() => toast.remove(), 300);
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
// Add Modal
|
||||
function openAddModal() {
|
||||
document.getElementById('addName').value = '';
|
||||
document.getElementById('addNip').value = '';
|
||||
document.getElementById('addCategory').value = '';
|
||||
document.getElementById('addStatus').value = 'pending';
|
||||
document.getElementById('addEmail').value = '';
|
||||
document.getElementById('addPhone').value = '';
|
||||
document.getElementById('addCity').value = '';
|
||||
document.getElementById('addModal').classList.add('active');
|
||||
}
|
||||
|
||||
function closeAddModal() {
|
||||
document.getElementById('addModal').classList.remove('active');
|
||||
}
|
||||
|
||||
async function confirmAdd() {
|
||||
const name = document.getElementById('addName').value.trim();
|
||||
if (!name) {
|
||||
showToast('Nazwa firmy jest wymagana', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch('/admin/companies/add', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRFToken': csrfToken
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name: name,
|
||||
nip: document.getElementById('addNip').value.trim() || null,
|
||||
category_id: document.getElementById('addCategory').value || null,
|
||||
status: document.getElementById('addStatus').value,
|
||||
email: document.getElementById('addEmail').value.trim() || null,
|
||||
phone: document.getElementById('addPhone').value.trim() || null,
|
||||
address_city: document.getElementById('addCity').value.trim() || null
|
||||
})
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (data.success) {
|
||||
closeAddModal();
|
||||
showToast(data.message, 'success');
|
||||
setTimeout(() => location.reload(), 1000);
|
||||
} else {
|
||||
showToast(data.error || 'Wystąpił błąd', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('Błąd połączenia', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
// Edit Modal
|
||||
async function openEditModal(companyId) {
|
||||
editCompanyId = companyId;
|
||||
|
||||
try {
|
||||
const response = await fetch(`/admin/companies/${companyId}`);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
const c = data.company;
|
||||
document.getElementById('editName').value = c.name || '';
|
||||
document.getElementById('editNip').value = c.nip || '';
|
||||
document.getElementById('editCategory').value = c.category_id || '';
|
||||
document.getElementById('editStatus').value = c.status || 'pending';
|
||||
document.getElementById('editEmail').value = c.email || '';
|
||||
document.getElementById('editPhone').value = c.phone || '';
|
||||
document.getElementById('editCity').value = c.address_city || '';
|
||||
document.getElementById('editStreet').value = c.address_street || '';
|
||||
document.getElementById('editPostal').value = c.address_postal || '';
|
||||
document.getElementById('editModal').classList.add('active');
|
||||
} else {
|
||||
showToast(data.error || 'Nie można pobrać danych', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('Błąd połączenia', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
function closeEditModal() {
|
||||
editCompanyId = null;
|
||||
document.getElementById('editModal').classList.remove('active');
|
||||
}
|
||||
|
||||
async function saveEdit() {
|
||||
if (!editCompanyId) return;
|
||||
|
||||
const name = document.getElementById('editName').value.trim();
|
||||
if (!name) {
|
||||
showToast('Nazwa firmy jest wymagana', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`/admin/companies/${editCompanyId}/update`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRFToken': csrfToken
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name: name,
|
||||
nip: document.getElementById('editNip').value.trim() || null,
|
||||
category_id: document.getElementById('editCategory').value || null,
|
||||
status: document.getElementById('editStatus').value,
|
||||
email: document.getElementById('editEmail').value.trim() || null,
|
||||
phone: document.getElementById('editPhone').value.trim() || null,
|
||||
address_city: document.getElementById('editCity').value.trim() || null,
|
||||
address_street: document.getElementById('editStreet').value.trim() || null,
|
||||
address_postal: document.getElementById('editPostal').value.trim() || null
|
||||
})
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (data.success) {
|
||||
closeEditModal();
|
||||
showToast(data.message, 'success');
|
||||
setTimeout(() => location.reload(), 1000);
|
||||
} else {
|
||||
showToast(data.error || 'Wystąpił błąd', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('Błąd połączenia', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
// Toggle Status
|
||||
async function toggleStatus(companyId) {
|
||||
try {
|
||||
const response = await fetch(`/admin/companies/${companyId}/toggle-status`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRFToken': csrfToken
|
||||
}
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (data.success) {
|
||||
showToast(data.message, 'success');
|
||||
setTimeout(() => location.reload(), 1000);
|
||||
} else {
|
||||
showToast(data.error || 'Wystąpił błąd', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('Błąd połączenia', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
// People Modal
|
||||
async function openPeopleModal(companyId, companyName) {
|
||||
document.getElementById('peopleModalTitle').textContent = `Osoby powiązane - ${companyName}`;
|
||||
document.getElementById('peopleList').innerHTML = '<div class="empty-state">Ładowanie...</div>';
|
||||
document.getElementById('peopleModal').classList.add('active');
|
||||
|
||||
try {
|
||||
const response = await fetch(`/admin/companies/${companyId}/people`);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
if (data.people.length === 0) {
|
||||
document.getElementById('peopleList').innerHTML = '<div class="empty-state">Brak powiązanych osób</div>';
|
||||
} else {
|
||||
let html = '';
|
||||
data.people.forEach(p => {
|
||||
html += `
|
||||
<div class="people-item">
|
||||
<div class="people-info">
|
||||
<div class="people-name">${p.imiona} ${p.nazwisko}</div>
|
||||
<div class="people-role">${p.role} (${p.role_category})${p.shares_percent ? ' - ' + p.shares_percent + '%' : ''}</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
document.getElementById('peopleList').innerHTML = html;
|
||||
}
|
||||
} else {
|
||||
document.getElementById('peopleList').innerHTML = '<div class="empty-state">Błąd pobierania danych</div>';
|
||||
}
|
||||
} catch (error) {
|
||||
document.getElementById('peopleList').innerHTML = '<div class="empty-state">Błąd połączenia</div>';
|
||||
}
|
||||
}
|
||||
|
||||
function closePeopleModal() {
|
||||
document.getElementById('peopleModal').classList.remove('active');
|
||||
}
|
||||
|
||||
// Delete (Archive) Company
|
||||
function deleteCompany(companyId, companyName) {
|
||||
document.getElementById('confirmTitle').textContent = 'Archiwizuj firmę';
|
||||
document.getElementById('confirmDescription').textContent = `Czy na pewno chcesz zarchiwizować firmę "${companyName}"? Firma nie będzie widoczna w katalogu.`;
|
||||
confirmCallback = async () => {
|
||||
try {
|
||||
const response = await fetch(`/admin/companies/${companyId}/delete`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRFToken': csrfToken
|
||||
}
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (data.success) {
|
||||
showToast(data.message, 'success');
|
||||
setTimeout(() => location.reload(), 1000);
|
||||
} else {
|
||||
showToast(data.error || 'Wystąpił błąd', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('Błąd połączenia', 'error');
|
||||
}
|
||||
};
|
||||
document.getElementById('confirmModal').classList.add('active');
|
||||
}
|
||||
|
||||
function closeConfirmModal() {
|
||||
document.getElementById('confirmModal').classList.remove('active');
|
||||
confirmCallback = null;
|
||||
}
|
||||
|
||||
document.getElementById('confirmAction').addEventListener('click', function() {
|
||||
if (confirmCallback) confirmCallback();
|
||||
closeConfirmModal();
|
||||
});
|
||||
{% endblock %}
|
||||
971
templates/admin/people.html
Normal file
971
templates/admin/people.html
Normal file
@ -0,0 +1,971 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Zarządzanie Osobami - Norda Biznes Partner{% endblock %}
|
||||
|
||||
{% block extra_css %}
|
||||
<style>
|
||||
.admin-header {
|
||||
margin-bottom: var(--spacing-xl);
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.admin-header-content h1 {
|
||||
font-size: var(--font-size-3xl);
|
||||
color: var(--text-primary);
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.admin-header-content p { margin: var(--spacing-xs) 0 0 0; }
|
||||
|
||||
.btn-add {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
padding: var(--spacing-sm) var(--spacing-lg);
|
||||
background: var(--primary);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: var(--radius);
|
||||
font-size: var(--font-size-base);
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.btn-add:hover { opacity: 0.9; }
|
||||
.btn-add svg { width: 20px; height: 20px; }
|
||||
|
||||
.stats-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
||||
gap: var(--spacing-lg);
|
||||
margin-bottom: var(--spacing-2xl);
|
||||
}
|
||||
|
||||
.stat-card {
|
||||
background: var(--surface);
|
||||
padding: var(--spacing-lg);
|
||||
border-radius: var(--radius-lg);
|
||||
box-shadow: var(--shadow);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.stat-value {
|
||||
font-size: var(--font-size-3xl);
|
||||
font-weight: 700;
|
||||
color: var(--primary);
|
||||
}
|
||||
|
||||
.stat-label {
|
||||
color: var(--text-secondary);
|
||||
font-size: var(--font-size-sm);
|
||||
margin-top: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.filters-row {
|
||||
display: flex;
|
||||
gap: var(--spacing-md);
|
||||
margin-bottom: var(--spacing-lg);
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.filter-group {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.filter-group label {
|
||||
font-size: var(--font-size-sm);
|
||||
color: var(--text-secondary);
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.filter-select, .filter-input {
|
||||
padding: var(--spacing-xs) var(--spacing-sm);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
font-size: var(--font-size-sm);
|
||||
background: var(--surface);
|
||||
}
|
||||
|
||||
.filter-select:focus, .filter-input:focus {
|
||||
outline: none;
|
||||
border-color: var(--primary);
|
||||
}
|
||||
|
||||
.section {
|
||||
background: var(--surface);
|
||||
padding: var(--spacing-xl);
|
||||
border-radius: var(--radius-lg);
|
||||
box-shadow: var(--shadow);
|
||||
margin-bottom: var(--spacing-xl);
|
||||
}
|
||||
|
||||
.section h2 {
|
||||
font-size: var(--font-size-xl);
|
||||
margin-bottom: var(--spacing-lg);
|
||||
color: var(--text-primary);
|
||||
border-bottom: 2px solid var(--border);
|
||||
padding-bottom: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.data-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
.data-table th, .data-table td {
|
||||
padding: var(--spacing-md);
|
||||
text-align: left;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.data-table th {
|
||||
font-weight: 600;
|
||||
color: var(--text-secondary);
|
||||
font-size: var(--font-size-sm);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
}
|
||||
|
||||
.data-table tr:hover { background: var(--background); }
|
||||
|
||||
.person-name { font-weight: 500; color: var(--text-primary); }
|
||||
|
||||
.pesel-masked {
|
||||
font-family: monospace;
|
||||
font-size: var(--font-size-sm);
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.roles-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.role-item {
|
||||
font-size: var(--font-size-sm);
|
||||
}
|
||||
|
||||
.role-item .role-name {
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.role-item .company-name {
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.badge {
|
||||
display: inline-block;
|
||||
padding: 2px 8px;
|
||||
border-radius: var(--radius-sm);
|
||||
font-size: var(--font-size-xs);
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.badge-zarzad { background: #DBEAFE; color: #1D4ED8; }
|
||||
.badge-wspolnik { background: #D1FAE5; color: #065F46; }
|
||||
.badge-prokurent { background: #FEF3C7; color: #92400E; }
|
||||
|
||||
.action-buttons {
|
||||
display: flex;
|
||||
gap: var(--spacing-xs);
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.btn-icon {
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
padding: 0;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border-radius: var(--radius);
|
||||
border: 1px solid var(--border);
|
||||
background: var(--surface);
|
||||
cursor: pointer;
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.btn-icon:hover { background: var(--background); }
|
||||
.btn-icon svg { width: 16px; height: 16px; }
|
||||
.btn-icon.danger:hover { background: var(--error); border-color: var(--error); color: white; }
|
||||
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: var(--spacing-2xl);
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
/* Modal styles */
|
||||
.modal {
|
||||
display: none;
|
||||
position: fixed;
|
||||
z-index: 1000;
|
||||
left: 0;
|
||||
top: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.modal.active { display: flex; }
|
||||
|
||||
.modal-content {
|
||||
background: var(--surface);
|
||||
padding: var(--spacing-xl);
|
||||
border-radius: var(--radius-lg);
|
||||
max-width: 500px;
|
||||
width: 90%;
|
||||
box-shadow: var(--shadow-lg);
|
||||
max-height: 90vh;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.modal-header {
|
||||
font-size: var(--font-size-xl);
|
||||
margin-bottom: var(--spacing-md);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.modal-body { margin-bottom: var(--spacing-lg); }
|
||||
|
||||
.modal-footer {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.form-group { margin-bottom: var(--spacing-md); }
|
||||
|
||||
.form-label {
|
||||
display: block;
|
||||
margin-bottom: var(--spacing-xs);
|
||||
color: var(--text-secondary);
|
||||
font-size: var(--font-size-sm);
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.form-control {
|
||||
width: 100%;
|
||||
padding: var(--spacing-sm);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
font-size: var(--font-size-base);
|
||||
font-family: inherit;
|
||||
}
|
||||
|
||||
.form-control:focus {
|
||||
outline: none;
|
||||
border-color: var(--primary);
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: var(--spacing-sm) var(--spacing-md);
|
||||
border: none;
|
||||
border-radius: var(--radius);
|
||||
font-size: var(--font-size-base);
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: var(--transition);
|
||||
}
|
||||
|
||||
.btn-primary { background: var(--primary); color: white; }
|
||||
.btn-primary:hover { opacity: 0.9; }
|
||||
.btn-secondary { background: var(--background); color: var(--text-secondary); }
|
||||
.btn-secondary:hover { background: var(--border); }
|
||||
.btn-danger { background: #EF4444; color: white; }
|
||||
.btn-danger:hover { background: #DC2626; }
|
||||
|
||||
/* Toast */
|
||||
.toast-container {
|
||||
position: fixed;
|
||||
top: 20px;
|
||||
right: 20px;
|
||||
z-index: 2000;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.toast {
|
||||
background: var(--surface);
|
||||
padding: var(--spacing-md) var(--spacing-lg);
|
||||
border-radius: var(--radius-lg);
|
||||
box-shadow: var(--shadow-lg);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
min-width: 280px;
|
||||
max-width: 400px;
|
||||
animation: slideIn 0.3s ease;
|
||||
border-left: 4px solid var(--primary);
|
||||
}
|
||||
|
||||
.toast.success { border-left-color: #10B981; }
|
||||
.toast.error { border-left-color: #EF4444; }
|
||||
|
||||
.toast-icon { width: 24px; height: 24px; flex-shrink: 0; }
|
||||
.toast-icon.success { color: #10B981; }
|
||||
.toast-icon.error { color: #EF4444; }
|
||||
.toast-message { flex: 1; color: var(--text-primary); }
|
||||
|
||||
.toast-close {
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--text-secondary);
|
||||
cursor: pointer;
|
||||
padding: 4px;
|
||||
}
|
||||
|
||||
@keyframes slideIn {
|
||||
from { transform: translateX(100%); opacity: 0; }
|
||||
to { transform: translateX(0); opacity: 1; }
|
||||
}
|
||||
|
||||
@keyframes slideOut {
|
||||
from { transform: translateX(0); opacity: 1; }
|
||||
to { transform: translateX(100%); opacity: 0; }
|
||||
}
|
||||
|
||||
/* Confirm modal */
|
||||
.modal-icon {
|
||||
width: 48px;
|
||||
height: 48px;
|
||||
margin: 0 auto var(--spacing-md);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border-radius: 50%;
|
||||
}
|
||||
|
||||
.modal-icon.danger { background: #FEE2E2; color: #EF4444; }
|
||||
.modal-icon svg { width: 24px; height: 24px; }
|
||||
|
||||
.modal-title {
|
||||
font-size: var(--font-size-lg);
|
||||
font-weight: 600;
|
||||
text-align: center;
|
||||
margin-bottom: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.modal-description {
|
||||
text-align: center;
|
||||
color: var(--text-secondary);
|
||||
margin-bottom: var(--spacing-lg);
|
||||
}
|
||||
|
||||
/* Companies list in modal */
|
||||
.companies-list {
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.company-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: var(--spacing-sm);
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.company-item:last-child { border-bottom: none; }
|
||||
|
||||
.company-item-info { flex: 1; }
|
||||
.company-item-name { font-weight: 500; }
|
||||
.company-item-role { font-size: var(--font-size-sm); color: var(--text-secondary); }
|
||||
|
||||
.btn-remove-link {
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--error);
|
||||
cursor: pointer;
|
||||
padding: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.btn-remove-link:hover { opacity: 0.7; }
|
||||
|
||||
.add-link-form {
|
||||
margin-top: var(--spacing-md);
|
||||
padding-top: var(--spacing-md);
|
||||
border-top: 1px solid var(--border);
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.data-table { font-size: var(--font-size-sm); }
|
||||
.filters-row { flex-direction: column; align-items: stretch; }
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="admin-header">
|
||||
<div class="admin-header-content">
|
||||
<h1>Zarządzanie Osobami</h1>
|
||||
<p class="text-muted">Osoby powiązane z firmami (zarząd, wspólnicy, prokurenci)</p>
|
||||
</div>
|
||||
<button class="btn-add" onclick="openAddModal()">
|
||||
<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
<path d="M18 9v3m0 0v3m0-3h3m-3 0h-3m-2-5a4 4 0 11-8 0 4 4 0 018 0zM3 20a6 6 0 0112 0v1H3v-1z"/>
|
||||
</svg>
|
||||
Dodaj osobę
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- Stats Grid -->
|
||||
<div class="stats-grid">
|
||||
<div class="stat-card">
|
||||
<div class="stat-value">{{ total_people }}</div>
|
||||
<div class="stat-label">Wszystkich</div>
|
||||
</div>
|
||||
<div class="stat-card">
|
||||
<div class="stat-value" style="color: #3B82F6;">{{ with_companies }}</div>
|
||||
<div class="stat-label">Z powiązaniami</div>
|
||||
</div>
|
||||
<div class="stat-card">
|
||||
<div class="stat-value" style="color: #1D4ED8;">{{ zarzad_count }}</div>
|
||||
<div class="stat-label">W zarządach</div>
|
||||
</div>
|
||||
<div class="stat-card">
|
||||
<div class="stat-value" style="color: #065F46;">{{ wspolnik_count }}</div>
|
||||
<div class="stat-label">Wspólników</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Filters -->
|
||||
<form method="GET" class="filters-row">
|
||||
<div class="filter-group">
|
||||
<label>Rola:</label>
|
||||
<select name="role" class="filter-select" onchange="this.form.submit()">
|
||||
<option value="">Wszystkie</option>
|
||||
<option value="zarzad" {{ 'selected' if current_role == 'zarzad' else '' }}>Zarząd</option>
|
||||
<option value="wspolnik" {{ 'selected' if current_role == 'wspolnik' else '' }}>Wspólnicy</option>
|
||||
<option value="prokurent" {{ 'selected' if current_role == 'prokurent' else '' }}>Prokurenci</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="filter-group">
|
||||
<label>Szukaj:</label>
|
||||
<input type="text" name="q" class="filter-input" placeholder="Imię lub nazwisko..." value="{{ search_query }}" style="width: 200px;">
|
||||
<button type="submit" class="btn btn-primary" style="padding: var(--spacing-xs) var(--spacing-sm);">Szukaj</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<!-- People Table -->
|
||||
<div class="section">
|
||||
<h2>Osoby ({{ people|length }})</h2>
|
||||
|
||||
{% if people %}
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Imiona i nazwisko</th>
|
||||
<th>PESEL</th>
|
||||
<th>Powiązania</th>
|
||||
<th>Akcje</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for person in people %}
|
||||
<tr data-person-id="{{ person.id }}">
|
||||
<td>{{ person.id }}</td>
|
||||
<td>
|
||||
<span class="person-name">{{ person.full_name }}</span>
|
||||
</td>
|
||||
<td>
|
||||
<span class="pesel-masked">{{ person.pesel_masked or '-' }}</span>
|
||||
</td>
|
||||
<td>
|
||||
{% if person.roles %}
|
||||
<div class="roles-list">
|
||||
{% for role in person.roles[:3] %}
|
||||
<div class="role-item">
|
||||
<span class="badge badge-{{ role.role_category }}">{{ role.role_category }}</span>
|
||||
<span class="role-name">{{ role.role }}</span>
|
||||
<span class="company-name">@ {{ role.company_name }}</span>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% if person.roles|length > 3 %}
|
||||
<div class="role-item">
|
||||
<span style="color: var(--text-secondary);">+{{ person.roles|length - 3 }} więcej...</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% else %}
|
||||
<span style="color: var(--text-secondary);">Brak powiązań</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>
|
||||
<div class="action-buttons">
|
||||
<button class="btn-icon" onclick="openEditModal({{ person.id }})" title="Edytuj">
|
||||
<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
<path d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
|
||||
</svg>
|
||||
</button>
|
||||
<button class="btn-icon" onclick="openCompaniesModal({{ person.id }}, '{{ person.full_name|e }}')" title="Powiązania z firmami">
|
||||
<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
<path d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4"/>
|
||||
</svg>
|
||||
</button>
|
||||
<button class="btn-icon danger" onclick="deletePerson({{ person.id }}, '{{ person.full_name|e }}')" title="Usuń">
|
||||
<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
<path d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% else %}
|
||||
<div class="empty-state">
|
||||
<p>Brak osób spełniających kryteria</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<!-- Add Person Modal -->
|
||||
<div id="addModal" class="modal">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">Dodaj nową osobę</div>
|
||||
<div class="modal-body">
|
||||
<div class="form-group">
|
||||
<label class="form-label">Imiona *</label>
|
||||
<input type="text" id="addImiona" class="form-control" placeholder="Jan Adam" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Nazwisko *</label>
|
||||
<input type="text" id="addNazwisko" class="form-control" placeholder="Kowalski" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">PESEL (opcjonalnie)</label>
|
||||
<input type="text" id="addPesel" class="form-control" placeholder="12345678901" maxlength="11">
|
||||
<small style="color: var(--text-secondary);">11 cyfr. Pozostaw puste jeśli brak.</small>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button class="btn btn-secondary" onclick="closeAddModal()">Anuluj</button>
|
||||
<button class="btn btn-primary" onclick="confirmAdd()">Utwórz osobę</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Edit Person Modal -->
|
||||
<div id="editModal" class="modal">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">Edytuj osobę</div>
|
||||
<div class="modal-body">
|
||||
<div class="form-group">
|
||||
<label class="form-label">Imiona *</label>
|
||||
<input type="text" id="editImiona" class="form-control" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Nazwisko *</label>
|
||||
<input type="text" id="editNazwisko" class="form-control" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">PESEL (opcjonalnie)</label>
|
||||
<input type="text" id="editPesel" class="form-control" maxlength="11">
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button class="btn btn-secondary" onclick="closeEditModal()">Anuluj</button>
|
||||
<button class="btn btn-primary" onclick="saveEdit()">Zapisz zmiany</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Companies Modal -->
|
||||
<div id="companiesModal" class="modal">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header" id="companiesModalTitle">Powiązania z firmami</div>
|
||||
<div class="modal-body">
|
||||
<div id="companiesList" class="companies-list">
|
||||
<div class="empty-state">Ładowanie...</div>
|
||||
</div>
|
||||
<div class="add-link-form" id="addLinkForm" style="display: none;">
|
||||
<h4 style="margin-bottom: var(--spacing-sm);">Dodaj powiązanie</h4>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Firma</label>
|
||||
<input type="text" id="linkCompanySearch" class="form-control" placeholder="Szukaj firmy...">
|
||||
<select id="linkCompanyId" class="form-control" style="margin-top: var(--spacing-xs);">
|
||||
<option value="">-- Wybierz firmę --</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Rola</label>
|
||||
<input type="text" id="linkRole" class="form-control" placeholder="np. PREZES ZARZĄDU">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label">Kategoria roli</label>
|
||||
<select id="linkRoleCategory" class="form-control">
|
||||
<option value="zarzad">Zarząd</option>
|
||||
<option value="wspolnik">Wspólnik</option>
|
||||
<option value="prokurent">Prokurent</option>
|
||||
</select>
|
||||
</div>
|
||||
<button class="btn btn-primary" onclick="confirmLink()">Dodaj powiązanie</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button class="btn btn-secondary" onclick="toggleAddLinkForm()" id="toggleLinkBtn">Dodaj powiązanie</button>
|
||||
<button class="btn btn-secondary" onclick="closeCompaniesModal()">Zamknij</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Confirm Modal -->
|
||||
<div id="confirmModal" class="modal">
|
||||
<div class="modal-content">
|
||||
<div id="confirmIcon" class="modal-icon danger">
|
||||
<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
<path d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
|
||||
</svg>
|
||||
</div>
|
||||
<div id="confirmTitle" class="modal-title">Potwierdzenie</div>
|
||||
<div id="confirmDescription" class="modal-description"></div>
|
||||
<div class="modal-footer" style="justify-content: center;">
|
||||
<button class="btn btn-secondary" onclick="closeConfirmModal()">Anuluj</button>
|
||||
<button id="confirmAction" class="btn btn-danger">Usuń</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Toast Container -->
|
||||
<div id="toastContainer" class="toast-container"></div>
|
||||
{% endblock %}
|
||||
|
||||
{% block extra_js %}
|
||||
const csrfToken = '{{ csrf_token() }}';
|
||||
let editPersonId = null;
|
||||
let currentPersonId = null;
|
||||
let confirmCallback = null;
|
||||
|
||||
function showToast(message, type = 'success') {
|
||||
const container = document.getElementById('toastContainer');
|
||||
const toast = document.createElement('div');
|
||||
toast.className = `toast ${type}`;
|
||||
|
||||
const iconSvg = {
|
||||
success: '<path d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>',
|
||||
error: '<path d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"/>'
|
||||
};
|
||||
|
||||
toast.innerHTML = `
|
||||
<svg class="toast-icon ${type}" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
${iconSvg[type] || iconSvg.success}
|
||||
</svg>
|
||||
<span class="toast-message">${message}</span>
|
||||
<button class="toast-close" onclick="this.parentElement.remove()">
|
||||
<svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
|
||||
<path d="M6 18L18 6M6 6l12 12"/>
|
||||
</svg>
|
||||
</button>
|
||||
`;
|
||||
|
||||
container.appendChild(toast);
|
||||
setTimeout(() => {
|
||||
toast.style.animation = 'slideOut 0.3s ease forwards';
|
||||
setTimeout(() => toast.remove(), 300);
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
// Add Modal
|
||||
function openAddModal() {
|
||||
document.getElementById('addImiona').value = '';
|
||||
document.getElementById('addNazwisko').value = '';
|
||||
document.getElementById('addPesel').value = '';
|
||||
document.getElementById('addModal').classList.add('active');
|
||||
}
|
||||
|
||||
function closeAddModal() {
|
||||
document.getElementById('addModal').classList.remove('active');
|
||||
}
|
||||
|
||||
async function confirmAdd() {
|
||||
const imiona = document.getElementById('addImiona').value.trim();
|
||||
const nazwisko = document.getElementById('addNazwisko').value.trim();
|
||||
|
||||
if (!imiona || !nazwisko) {
|
||||
showToast('Imiona i nazwisko są wymagane', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch('/admin/people/add', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRFToken': csrfToken
|
||||
},
|
||||
body: JSON.stringify({
|
||||
imiona: imiona,
|
||||
nazwisko: nazwisko,
|
||||
pesel: document.getElementById('addPesel').value.trim() || null
|
||||
})
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (data.success) {
|
||||
closeAddModal();
|
||||
showToast(data.message, 'success');
|
||||
setTimeout(() => location.reload(), 1000);
|
||||
} else {
|
||||
showToast(data.error || 'Wystąpił błąd', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('Błąd połączenia', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
// Edit Modal
|
||||
async function openEditModal(personId) {
|
||||
editPersonId = personId;
|
||||
|
||||
try {
|
||||
const response = await fetch(`/admin/people/${personId}`);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
document.getElementById('editImiona').value = data.person.imiona || '';
|
||||
document.getElementById('editNazwisko').value = data.person.nazwisko || '';
|
||||
document.getElementById('editPesel').value = '';
|
||||
document.getElementById('editModal').classList.add('active');
|
||||
} else {
|
||||
showToast(data.error || 'Nie można pobrać danych', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('Błąd połączenia', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
function closeEditModal() {
|
||||
editPersonId = null;
|
||||
document.getElementById('editModal').classList.remove('active');
|
||||
}
|
||||
|
||||
async function saveEdit() {
|
||||
if (!editPersonId) return;
|
||||
|
||||
const imiona = document.getElementById('editImiona').value.trim();
|
||||
const nazwisko = document.getElementById('editNazwisko').value.trim();
|
||||
|
||||
if (!imiona || !nazwisko) {
|
||||
showToast('Imiona i nazwisko są wymagane', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
const payload = { imiona, nazwisko };
|
||||
const pesel = document.getElementById('editPesel').value.trim();
|
||||
if (pesel) payload.pesel = pesel;
|
||||
|
||||
try {
|
||||
const response = await fetch(`/admin/people/${editPersonId}/update`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRFToken': csrfToken
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (data.success) {
|
||||
closeEditModal();
|
||||
showToast(data.message, 'success');
|
||||
setTimeout(() => location.reload(), 1000);
|
||||
} else {
|
||||
showToast(data.error || 'Wystąpił błąd', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('Błąd połączenia', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
// Companies Modal
|
||||
async function openCompaniesModal(personId, personName) {
|
||||
currentPersonId = personId;
|
||||
document.getElementById('companiesModalTitle').textContent = `Powiązania - ${personName}`;
|
||||
document.getElementById('companiesList').innerHTML = '<div class="empty-state">Ładowanie...</div>';
|
||||
document.getElementById('addLinkForm').style.display = 'none';
|
||||
document.getElementById('toggleLinkBtn').textContent = 'Dodaj powiązanie';
|
||||
document.getElementById('companiesModal').classList.add('active');
|
||||
|
||||
try {
|
||||
const response = await fetch(`/admin/people/${personId}/companies`);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
if (data.companies.length === 0) {
|
||||
document.getElementById('companiesList').innerHTML = '<div class="empty-state">Brak powiązań z firmami</div>';
|
||||
} else {
|
||||
let html = '';
|
||||
data.companies.forEach(c => {
|
||||
html += `
|
||||
<div class="company-item">
|
||||
<div class="company-item-info">
|
||||
<div class="company-item-name">${c.company_name}</div>
|
||||
<div class="company-item-role">${c.role} (${c.role_category})${c.shares_percent ? ' - ' + c.shares_percent + '%' : ''}</div>
|
||||
</div>
|
||||
<button class="btn-remove-link" onclick="unlinkCompany(${personId}, ${c.company_id}, ${c.link_id})" title="Usuń powiązanie">
|
||||
<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" width="16" height="16">
|
||||
<path d="M6 18L18 6M6 6l12 12"/>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
document.getElementById('companiesList').innerHTML = html;
|
||||
}
|
||||
} else {
|
||||
document.getElementById('companiesList').innerHTML = '<div class="empty-state">Błąd pobierania danych</div>';
|
||||
}
|
||||
} catch (error) {
|
||||
document.getElementById('companiesList').innerHTML = '<div class="empty-state">Błąd połączenia</div>';
|
||||
}
|
||||
}
|
||||
|
||||
function closeCompaniesModal() {
|
||||
currentPersonId = null;
|
||||
document.getElementById('companiesModal').classList.remove('active');
|
||||
}
|
||||
|
||||
function toggleAddLinkForm() {
|
||||
const form = document.getElementById('addLinkForm');
|
||||
const btn = document.getElementById('toggleLinkBtn');
|
||||
if (form.style.display === 'none') {
|
||||
form.style.display = 'block';
|
||||
btn.textContent = 'Anuluj dodawanie';
|
||||
loadCompaniesForSelect();
|
||||
} else {
|
||||
form.style.display = 'none';
|
||||
btn.textContent = 'Dodaj powiązanie';
|
||||
}
|
||||
}
|
||||
|
||||
async function loadCompaniesForSelect() {
|
||||
try {
|
||||
const response = await fetch('/admin/companies?status=active');
|
||||
const parser = new DOMParser();
|
||||
const doc = parser.parseFromString(await response.text(), 'text/html');
|
||||
const rows = doc.querySelectorAll('[data-company-id]');
|
||||
|
||||
const select = document.getElementById('linkCompanyId');
|
||||
select.innerHTML = '<option value="">-- Wybierz firmę --</option>';
|
||||
rows.forEach(row => {
|
||||
const id = row.dataset.companyId;
|
||||
const name = row.querySelector('.company-name')?.textContent || `Firma ${id}`;
|
||||
select.innerHTML += `<option value="${id}">${name}</option>`;
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error loading companies:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async function confirmLink() {
|
||||
if (!currentPersonId) return;
|
||||
|
||||
const companyId = document.getElementById('linkCompanyId').value;
|
||||
const role = document.getElementById('linkRole').value.trim();
|
||||
const roleCategory = document.getElementById('linkRoleCategory').value;
|
||||
|
||||
if (!companyId || !role) {
|
||||
showToast('Wybierz firmę i podaj rolę', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`/admin/people/${currentPersonId}/link-company`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRFToken': csrfToken
|
||||
},
|
||||
body: JSON.stringify({
|
||||
company_id: parseInt(companyId),
|
||||
role: role,
|
||||
role_category: roleCategory
|
||||
})
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (data.success) {
|
||||
showToast(data.message, 'success');
|
||||
openCompaniesModal(currentPersonId, document.getElementById('companiesModalTitle').textContent.replace('Powiązania - ', ''));
|
||||
} else {
|
||||
showToast(data.error || 'Wystąpił błąd', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('Błąd połączenia', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
async function unlinkCompany(personId, companyId, linkId) {
|
||||
try {
|
||||
const response = await fetch(`/admin/people/${personId}/unlink-company/${companyId}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRFToken': csrfToken
|
||||
},
|
||||
body: JSON.stringify({ link_id: linkId })
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (data.success) {
|
||||
showToast(data.message, 'success');
|
||||
openCompaniesModal(personId, document.getElementById('companiesModalTitle').textContent.replace('Powiązania - ', ''));
|
||||
} else {
|
||||
showToast(data.error || 'Wystąpił błąd', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('Błąd połączenia', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
// Delete Person
|
||||
function deletePerson(personId, personName) {
|
||||
document.getElementById('confirmTitle').textContent = 'Usuń osobę';
|
||||
document.getElementById('confirmDescription').textContent = `Czy na pewno chcesz usunąć osobę "${personName}"? Zostaną również usunięte wszystkie powiązania z firmami.`;
|
||||
confirmCallback = async () => {
|
||||
try {
|
||||
const response = await fetch(`/admin/people/${personId}/delete`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRFToken': csrfToken
|
||||
}
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (data.success) {
|
||||
showToast(data.message, 'success');
|
||||
setTimeout(() => location.reload(), 1000);
|
||||
} else {
|
||||
showToast(data.error || 'Wystąpił błąd', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('Błąd połączenia', 'error');
|
||||
}
|
||||
};
|
||||
document.getElementById('confirmModal').classList.add('active');
|
||||
}
|
||||
|
||||
function closeConfirmModal() {
|
||||
document.getElementById('confirmModal').classList.remove('active');
|
||||
confirmCallback = null;
|
||||
}
|
||||
|
||||
document.getElementById('confirmAction').addEventListener('click', function() {
|
||||
if (confirmCallback) confirmCallback();
|
||||
closeConfirmModal();
|
||||
});
|
||||
{% endblock %}
|
||||
Loading…
Reference in New Issue
Block a user