@@ -43,3 +43,64 @@ function customer_info($id){
4343 return $ results [0 ] ?? '' ;
4444}
4545
46+ function total_price ($ id ){
47+ global $ db ;
48+ $ sql = "SELECT SUM(CASE
49+ WHEN r.type = 'day' THEN co.priceDay
50+ WHEN r.type = 'night' THEN co.priceNight
51+ ELSE 0
52+ END) AS total,
53+ c.fullname
54+ FROM transactions t
55+ JOIN rentals r ON t.id = r.transact_id
56+ JOIN customers c ON t.customer_id = c.id
57+ JOIN cottages co ON r.cottage_id = co.id
58+ WHERE t.user_id = :id AND t.status = 'Pending'; " ;
59+ $ statement = $ db ->prepare ($ sql );
60+ $ statement ->bindParam (':id ' , $ id );
61+ $ statement ->execute ();
62+ $ result = $ statement ->fetch ();
63+ return number_format ($ result ['total ' ], 2 );
64+ }
65+
66+ function total_cottage ($ id ){
67+ global $ db ;
68+ $ sql = "SELECT COUNT(r.id) AS total,
69+ c.fullname
70+ FROM transactions t
71+ JOIN rentals r ON t.id = r.transact_id
72+ JOIN customers c ON t.customer_id = c.id
73+ JOIN cottages co ON r.cottage_id = co.id
74+ WHERE t.user_id = :id AND t.status = 'Pending'; " ;
75+ $ statement = $ db ->prepare ($ sql );
76+ $ statement ->bindParam (':id ' , $ id );
77+ $ statement ->execute ();
78+ $ result = $ statement ->fetch ();
79+ return $ result ['total ' ];
80+ }
81+
82+ function get_sales ($ period = 'monthly ' ) {
83+ global $ db ;
84+
85+ $ sql = "SELECT SUM(CASE
86+ WHEN r.type = 'day' THEN co.priceDay
87+ WHEN r.type = 'night' THEN co.priceNight
88+ ELSE 0
89+ END) AS total
90+ FROM transactions t
91+ JOIN rentals r ON t.id = r.transact_id
92+ JOIN cottages co ON r.cottage_id = co.id
93+ WHERE t.status = 'Pending' " ;
94+
95+ if ($ period === 'monthly ' ) {
96+ $ sql .= " AND DATE_FORMAT(t.created_at, '%Y-%m') = DATE_FORMAT(CURRENT_DATE, '%Y-%m') " ;
97+ } elseif ($ period === 'annual ' ) {
98+ $ sql .= " AND DATE_FORMAT(t.created_at, '%Y') = DATE_FORMAT(CURRENT_DATE, '%Y') " ;
99+ }
100+
101+ $ statement = $ db ->prepare ($ sql );
102+ $ statement ->execute ();
103+ $ result = $ statement ->fetch ();
104+
105+ return number_format ($ result ['total ' ], 2 );
106+ }
0 commit comments