diff --git a/blueprints/admin/routes.py b/blueprints/admin/routes.py index 773b0f8..90b5f99 100644 --- a/blueprints/admin/routes.py +++ b/blueprints/admin/routes.py @@ -681,8 +681,7 @@ def admin_fees(): status_filter = request.args.get('status', '') companies = db.query(Company).filter( - Company.status == 'active', - Company.fee_included_in_parent != True + Company.status == 'active' ).order_by(Company.name).all() fee_query = db.query(MembershipFee).filter(MembershipFee.fee_year == year) @@ -691,6 +690,18 @@ def admin_fees(): fees = {(f.company_id, f.fee_month): f for f in fee_query.all()} + # Build parent/child relationship data for fee analysis + all_companies_for_brands = db.query(Company).filter(Company.status == 'active').all() + children_by_parent = {} + for c in all_companies_for_brands: + if c.parent_company_id: + children_by_parent.setdefault(c.parent_company_id, []).append(c) + + child_companies_set = set() + for c in all_companies_for_brands: + if c.fee_included_in_parent or c.parent_company_id: + child_companies_set.add(c.id) + companies_fees = [] for company in companies: if month: @@ -701,7 +712,14 @@ def admin_fees(): 'status': fee.status if fee else 'brak' }) else: - company_data = {'company': company, 'months': {}, 'monthly_rate': 0, 'has_data': False, 'reminder': None} + is_child = company.id in child_companies_set + company_data = { + 'company': company, 'months': {}, 'monthly_rate': 0, + 'has_data': False, 'reminder': None, 'is_child': is_child, + 'child_brands': [], 'child_count': 0, + 'expected_fees': {}, 'rate_change_month': None, + 'parent_months': {}, + } has_unpaid = False for m in range(1, 13): fee = fees.get((company.id, m)) @@ -712,8 +730,33 @@ def admin_fees(): company_data['monthly_rate'] = int(fee.amount) if fee.status in ('pending', 'partial', 'overdue'): has_unpaid = True - # Find last reminder message for this company (to any linked user) - if has_unpaid: + + if not is_child: + child_brands = children_by_parent.get(company.id, []) + company_data['child_brands'] = child_brands + company_data['child_count'] = len(child_brands) + + expected_fees = {} + rate_change_month = None + for m in range(1, 13): + month_date = datetime(year, m, 1) + active_children = sum( + 1 for ch in child_brands + if ch.created_at and ch.created_at.replace(day=1) <= month_date + ) + total_brands = 1 + active_children + expected_fees[m] = 300 if total_brands >= 2 else 200 + if total_brands >= 2 and rate_change_month is None: + rate_change_month = m + company_data['expected_fees'] = expected_fees + company_data['rate_change_month'] = rate_change_month + else: + if company.parent_company_id: + for m in range(1, 13): + company_data['parent_months'][m] = fees.get((company.parent_company_id, m)) + + # Find last reminder + if not is_child and has_unpaid: from database import PrivateMessage, UserCompany company_user_ids = [cu.user_id for cu in db.query(UserCompany).filter(UserCompany.company_id == company.id).all()] if company_user_ids: @@ -729,9 +772,21 @@ def admin_fees(): } companies_fees.append(company_data) - # Sort: companies with fee data first, then without + # Sort and group: parents with children if not month: - companies_fees.sort(key=lambda cf: (0 if cf.get('has_data') else 1, cf['company'].name)) + non_children = [cf for cf in companies_fees if not cf.get('is_child')] + non_children.sort(key=lambda cf: (0 if cf.get('has_data') else 1, cf['company'].name)) + + children_by_pid = {} + for cf in companies_fees: + if cf.get('is_child') and cf['company'].parent_company_id: + children_by_pid.setdefault(cf['company'].parent_company_id, []).append(cf) + + companies_fees = [] + for cf in non_children: + companies_fees.append(cf) + for child_cf in sorted(children_by_pid.get(cf['company'].id, []), key=lambda x: x['company'].name): + companies_fees.append(child_cf) if status_filter: if month: diff --git a/templates/admin/fees.html b/templates/admin/fees.html index bf5341b..44c435f 100755 --- a/templates/admin/fees.html +++ b/templates/admin/fees.html @@ -4,6 +4,8 @@ {% block extra_css %}