diff --git a/frontend/src/screens/reporting/outstanding-payments.tsx b/frontend/src/screens/reporting/outstanding-payments.tsx
index 19e6097..7262900 100644
--- a/frontend/src/screens/reporting/outstanding-payments.tsx
+++ b/frontend/src/screens/reporting/outstanding-payments.tsx
@@ -17,9 +17,9 @@ export default function OutstandingPayments() {
Name
+ Unpaid Amount
Matches
Summary
- Unpaid
@@ -29,18 +29,18 @@ export default function OutstandingPayments() {
return (
{item.playerName}
- {item.matchCount}
- {item.registrationSummary}
10
+ item.unpaidAmount > 100
? "text-rose-500"
: "text-emerald-500",
)}
>
+ {item.matchCount}
+ {item.registrationSummary}
);
})}
diff --git a/service/reporting.go b/service/reporting.go
index 460d727..5c57b2b 100644
--- a/service/reporting.go
+++ b/service/reporting.go
@@ -27,19 +27,18 @@ func (s *ReportingService) GetUnpaidReport() ([]UnpaidByPlayer, error) {
SELECT
m.id AS match_id,
m.sport_center_id AS sport_center_id,
- m.cost / COUNT(r.id) AS individual_cost
+ -- (Base cost + Total additional cost) / Number of Player
+ (m.cost + COALESCE(acx.additional_cost, 0)) / COUNT(r.id) AS individual_cost
FROM matches m
JOIN registrations r ON m.id = r.match_id
- GROUP BY m.id, m.sport_center_id
- ),
- cte_match_additional_costs AS (
- SELECT
- m.id AS match_id,
- SUM (ac.amount) / COUNT(r.id) AS individual_additional_cost
- FROM matches m
- JOIN registrations r ON m.id = r.match_id
- JOIN additional_costs ac on ac.match_id = m.id
- GROUP BY m.id
+
+ -- Getting SUM of additional cost to add with base cost
+ LEFT JOIN LATERAL (
+ SELECT ac.match_id, SUM(ac.amount) AS additional_cost
+ FROM additional_costs ac
+ GROUP BY ac.match_id
+ ) acx ON acx.match_id = m.id
+ GROUP BY m.id, m.sport_center_id, acx.additional_cost
),
cte_registration_history AS (
SELECT p.id AS player_id, STRING_AGG(TO_CHAR(m.start, 'DD-Mon'), ', ' ORDER BY m.start) AS registration_summary
@@ -53,14 +52,13 @@ func (s *ReportingService) GetUnpaidReport() ([]UnpaidByPlayer, error) {
p.id AS player_id,
CONCAT(p.first_name,' ', p.last_name) AS player_name,
COUNT(cte.match_id) AS match_count,
- SUM(cte.individual_cost + COALESCE(cte3.individual_additional_cost, 0)) AS unpaid_amount,
+ SUM(cte.individual_cost) AS unpaid_amount,
cte2.registration_summary
FROM registrations r
JOIN cte_match_costs cte ON cte.match_id = r.match_id
JOIN sport_centers sc ON cte.sport_center_id = sc.id
JOIN players p ON r.player_id = p.id
JOIN cte_registration_history cte2 ON cte2.player_id = p.id
- LEFT JOIN cte_match_additional_costs cte3 ON cte3.match_id = r.match_id
WHERE r.is_paid = false
GROUP BY p.id, cte2.registration_summary
ORDER by unpaid_amount DESC