| 5 | | SELECT |
| 6 | | DATE(timestamp) AS payment_date, |
| 7 | | SUM(amount) AS total_revenue |
| 8 | | FROM payments |
| 9 | | GROUP BY DATE(timestamp) |
| 10 | | ORDER BY payment_date; |
| | 5 | CREATE OR REPLACE FUNCTION get_daily_revenue(p_start_date DATE, p_end_date DATE) |
| | 6 | RETURNS TABLE(payment_date DATE, total_revenue NUMERIC) AS $$ |
| | 7 | BEGIN |
| | 8 | RETURN QUERY |
| | 9 | SELECT |
| | 10 | DATE(created_at) AS payment_date, |
| | 11 | SUM(amount) AS total_revenue |
| | 12 | FROM payments |
| | 13 | WHERE DATE(created_at) BETWEEN p_start_date AND p_end_date |
| | 14 | GROUP BY DATE(created_at) |
| | 15 | ORDER BY payment_date; |
| | 16 | END; |
| | 17 | $$ LANGUAGE plpgsql; |
| | 18 | |
| | 19 | SELECT * FROM get_daily_revenue('2025-01-01', '2025-01-31'); |
| 15 | | SELECT |
| 16 | | DATE(o.datetime) AS order_date, |
| 17 | | COUNT(o.id) AS total_orders, |
| 18 | | AVG(p.amount) AS avg_order_value |
| 19 | | FROM orders o |
| 20 | | JOIN payments p ON o.id = p.order_id |
| 21 | | GROUP BY DATE(o.datetime) |
| 22 | | ORDER BY order_date; |
| | 24 | CREATE OR REPLACE FUNCTION get_daily_order_stats(p_start_date DATE, p_end_date DATE) |
| | 25 | RETURNS TABLE(order_date DATE, total_orders BIGINT, avg_order_value NUMERIC) AS $$ |
| | 26 | BEGIN |
| | 27 | RETURN QUERY |
| | 28 | SELECT |
| | 29 | DATE(o.datetime) AS order_date, |
| | 30 | COUNT(o.id) AS total_orders, |
| | 31 | AVG(p.amount) AS avg_order_value |
| | 32 | FROM orders o |
| | 33 | JOIN payments p ON o.id = p.order_id |
| | 34 | WHERE DATE(o.datetime) BETWEEN p_start_date AND p_end_date |
| | 35 | GROUP BY DATE(o.datetime) |
| | 36 | ORDER BY order_date; |
| | 37 | END; |
| | 38 | $$ LANGUAGE plpgsql; |
| | 39 | |
| | 40 | SELECT * FROM get_daily_order_stats('2025-01-01', '2025-01-31'); |
| | 41 | |
| 27 | | SELECT |
| 28 | | c.name AS category, |
| 29 | | COUNT(oi.id) AS total_items_ordered |
| 30 | | FROM order_items oi |
| 31 | | JOIN products p ON oi.product_id = p.id |
| 32 | | JOIN categories c ON p.category_id = c.id |
| 33 | | GROUP BY c.name |
| 34 | | ORDER BY total_items_ordered DESC; |
| | 46 | CREATE OR REPLACE FUNCTION get_revenue_split(p_start_date DATE, p_end_date DATE) |
| | 47 | RETURNS TABLE(order_type TEXT, total_revenue NUMERIC) AS $$ |
| | 48 | BEGIN |
| | 49 | RETURN QUERY |
| | 50 | SELECT 'Online Orders' AS order_type, SUM(p.amount) AS total_revenue |
| | 51 | FROM orders o |
| | 52 | JOIN payments p ON o.id = p.order_id |
| | 53 | JOIN online_orders oo ON o.id = oo.order_id |
| | 54 | WHERE DATE(o.datetime) BETWEEN p_start_date AND p_end_date |
| | 55 | |
| | 56 | UNION ALL |
| | 57 | |
| | 58 | SELECT 'Tab Orders' AS order_type, SUM(p.amount) AS total_revenue |
| | 59 | FROM orders o |
| | 60 | JOIN payments p ON o.id = p.order_id |
| | 61 | JOIN tab_orders tord ON o.id = tord.order_id |
| | 62 | WHERE DATE(o.datetime) BETWEEN p_start_date AND p_end_date; |
| | 63 | END; |
| | 64 | $$ LANGUAGE plpgsql; |
| | 65 | |
| | 66 | SELECT * FROM get_revenue_split('2025-01-01', '2025-01-28'); |
| 51 | | SELECT 'Online Orders' AS order_type, COUNT(*) AS total_orders |
| 52 | | FROM online_orders |
| 53 | | UNION ALL |
| 54 | | SELECT 'Tab Orders' AS order_type, COUNT(*) AS total_orders |
| 55 | | FROM tab_orders; |
| | 83 | CREATE OR REPLACE FUNCTION get_reservations_by_day(p_start_date DATE, p_end_date DATE) |
| | 84 | RETURNS TABLE(day_of_week INT, total_reservations BIGINT) AS $$ |
| | 85 | BEGIN |
| | 86 | RETURN QUERY |
| | 87 | SELECT |
| | 88 | EXTRACT(DOW FROM datetime)::INT AS day_of_week, -- 0=Sunday, 1=Monday, etc. |
| | 89 | COUNT(*) AS total_reservations |
| | 90 | FROM reservations |
| | 91 | WHERE DATE(datetime) BETWEEN p_start_date AND p_end_date |
| | 92 | GROUP BY day_of_week |
| | 93 | ORDER BY total_reservations DESC; |
| | 94 | END; |
| | 95 | $$ LANGUAGE plpgsql; |
| 86 | | SELECT |
| 87 | | fs.employee_id, |
| 88 | | COUNT(tord.order_id) AS total_orders_managed |
| 89 | | FROM tab_orders tord |
| 90 | | JOIN front_staff fs ON tord.front_staff_id = fs.employee_id |
| 91 | | GROUP BY fs.employee_id |
| 92 | | ORDER BY total_orders_managed DESC; |
| | 126 | CREATE OR REPLACE FUNCTION get_yearly_revenue(p_start_date DATE, p_end_date DATE) |
| | 127 | RETURNS TABLE(payment_year DATE, total_revenue NUMERIC) AS $$ |
| | 128 | BEGIN |
| | 129 | RETURN QUERY |
| | 130 | SELECT |
| | 131 | date_trunc('year', created_at)::DATE AS payment_year,* |
| | 132 | SUM(amount) AS total_revenue |
| | 133 | FROM payments |
| | 134 | WHERE DATE(created_at) BETWEEN p_start_date AND p_end_date |
| | 135 | GROUP BY date_trunc('year', created_at) |
| | 136 | ORDER BY payment_year; |
| | 137 | END; |
| | 138 | $$ LANGUAGE plpgsql; |
| 94 | | |
| 95 | | **Average Tip Percentage per Staff Role** |
| 96 | | {{{ |
| 97 | | SELECT |
| 98 | | sr.name AS staff_role, |
| 99 | | AVG(fs.tip_percent) AS avg_tip_percent |
| 100 | | FROM front_staff fs |
| 101 | | JOIN staff_roles sr ON fs.staff_role_id = sr.id |
| 102 | | GROUP BY sr.name; |
| 103 | | }}} |
| 104 | | |
| 105 | | **Inventory Turnover Ratio per Product** |
| 106 | | {{{ |
| 107 | | SELECT |
| 108 | | p.name AS product, |
| 109 | | inv.quantity AS current_inventory, |
| 110 | | SUM(oi.quantity) AS total_quantity_ordered, |
| 111 | | CASE |
| 112 | | WHEN inv.quantity = 0 THEN NULL |
| 113 | | ELSE SUM(oi.quantity)::decimal / inv.quantity |
| 114 | | END AS turnover_ratio |
| 115 | | FROM products p |
| 116 | | JOIN inventories inv ON p.id = inv.product_id |
| 117 | | JOIN order_items oi ON p.id = oi.product_id |
| 118 | | GROUP BY p.name, inv.quantity |
| 119 | | ORDER BY turnover_ratio DESC; |
| 120 | | }}} |
| 121 | | |
| 122 | | **Most Popular Table by Reservations** |
| 123 | | {{{ |
| 124 | | SELECT |
| 125 | | table_number, |
| 126 | | COUNT(*) AS reservation_count |
| 127 | | FROM frontstaff_managed_reservations |
| 128 | | GROUP BY table_number |
| 129 | | ORDER BY reservation_count DESC |
| 130 | | LIMIT 1; |
| 131 | | }}} |
| 132 | | |
| 133 | | **Reservations by Day of Week** |
| 134 | | {{{ |
| 135 | | SELECT |
| 136 | | EXTRACT(DOW FROM datetime) AS day_of_week, -- 0=Sunday, 1=Monday, etc. |
| 137 | | COUNT(*) AS total_reservations |
| 138 | | FROM reservations |
| 139 | | GROUP BY day_of_week |
| 140 | | ORDER BY total_reservations DESC; |
| 141 | | }}} |