Skip to content

Commit

Permalink
fix: outstanding report additional costs
Browse files Browse the repository at this point in the history
  • Loading branch information
truc9 committed Oct 14, 2024
1 parent 700fab0 commit 27a5669
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
8 changes: 4 additions & 4 deletions frontend/src/screens/reporting/outstanding-payments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export default function OutstandingPayments() {
<Table.Thead>
<Table.Tr>
<Table.Th>Name</Table.Th>
<Table.Th>Unpaid Amount</Table.Th>
<Table.Th>Matches</Table.Th>
<Table.Th>Summary</Table.Th>
<Table.Th>Unpaid</Table.Th>
</Table.Tr>
</Table.Thead>
<Table.Tbody>
Expand All @@ -29,18 +29,18 @@ export default function OutstandingPayments() {
return (
<Table.Tr key={item.playerId}>
<Table.Td>{item.playerName}</Table.Td>
<Table.Td>{item.matchCount}</Table.Td>
<Table.Td>{item.registrationSummary}</Table.Td>
<Table.Td
className={clsx(
"font-bold",
item.unpaidAmount > 10
item.unpaidAmount > 100
? "text-rose-500"
: "text-emerald-500",
)}
>
<Currency value={item.unpaidAmount} />
</Table.Td>
<Table.Td>{item.matchCount}</Table.Td>
<Table.Td>{item.registrationSummary}</Table.Td>
</Table.Tr>
);
})}
Expand Down
24 changes: 11 additions & 13 deletions service/reporting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 27a5669

Please sign in to comment.