| 16 | WITH order_summary AS ( |
| 17 | SELECT co.client_id, |
| 18 | COUNT(co.id) AS total_orders, |
| 19 | COALESCE(SUM(co.total_price), 0) AS total_spent |
| 20 | FROM synergymed.clientorder co |
| 21 | GROUP BY co.client_id |
| 22 | ), |
| 23 | payment_summary AS ( |
| 24 | SELECT p.client_id, |
| 25 | COUNT(*) FILTER (WHERE p.status = 'завршено') AS successful_payments, |
| 26 | COUNT(*) FILTER (WHERE p.status = 'неуспешно') AS failed_payments, |
| 27 | MAX(p.payment_date) AS last_payment_date |
| 28 | FROM synergymed.payment p |
| 29 | GROUP BY p.client_id |
| 30 | ), |
| 31 | allergy_clients AS ( |
| 32 | SELECT DISTINCT hp.client_id |
| 33 | FROM synergymed.healthprofile hp |
| 34 | JOIN synergymed.allergicreaction_healthprofile_medicine ahm |
| 35 | ON ahm.health_profile_id = hp.id |
| 36 | ), |
| 37 | prescription_clients AS ( |
| 38 | SELECT DISTINCT pr.client_id |
| 39 | FROM synergymed.prescription pr |
| 40 | WHERE pr.valid_to >= CURRENT_DATE |
| 41 | ) |
22 | | |
23 | | -- narachki |
24 | | COUNT(DISTINCT co.id) AS total_orders, |
25 | | COALESCE(SUM(co.total_price), 0) AS total_spent, |
26 | | |
27 | | -- plakjanja |
28 | | COUNT(DISTINCT p.id) FILTER (WHERE p.status = 'завршено') AS successful_payments, |
29 | | COUNT(DISTINCT p.id) FILTER (WHERE p.status = 'неуспешно') AS failed_payments, |
30 | | MAX(p.payment_date) AS last_payment_date, |
31 | | |
32 | | -- alergii |
33 | | CASE |
34 | | WHEN EXISTS ( |
35 | | SELECT 1 FROM synergymed.healthprofile hp |
36 | | JOIN synergymed.allergicreaction_healthprofile_medicine ahm ON ahm.health_profile_id = hp.id |
37 | | WHERE hp.client_id = c.user_id |
38 | | ) THEN TRUE ELSE FALSE |
39 | | END AS has_allergies, |
40 | | |
41 | | -- recepti |
42 | | CASE |
43 | | WHEN EXISTS ( |
44 | | SELECT 1 FROM synergymed.prescription pr |
45 | | WHERE pr.client_id = c.user_id |
46 | | AND pr.valid_to >= CURRENT_DATE |
47 | | ) THEN TRUE ELSE FALSE |
48 | | END AS has_active_prescriptions |
| 48 | |
| 49 | COALESCE(order_summary.total_orders, 0) AS total_orders, |
| 50 | COALESCE(order_summary.total_spent, 0) AS total_spent, |
| 51 | |
| 52 | COALESCE(payment_summary.successful_payments, 0) AS successful_payments, |
| 53 | COALESCE(payment_summary.failed_payments, 0) AS failed_payments, |
| 54 | payment_summary.last_payment_date, |
| 55 | |
| 56 | (allergy_clients.client_id IS NOT NULL) AS has_allergies, |
| 57 | (prescription_clients.client_id IS NOT NULL) AS has_active_prescriptions |
53 | | LEFT JOIN synergymed.clientorder co ON co.client_id = c.user_id |
54 | | LEFT JOIN synergymed.payment p ON p.client_id = c.user_id |
55 | | GROUP BY u.id, client_name, u.e_mail, c.is_verified, cc.points; |
| 62 | LEFT JOIN order_summary ON order_summary.client_id = c.user_id |
| 63 | LEFT JOIN payment_summary ON payment_summary.client_id = c.user_id |
| 64 | LEFT JOIN allergy_clients ON allergy_clients.client_id = c.user_id |
| 65 | LEFT JOIN prescription_clients ON prescription_clients.client_id = c.user_id; |