|
1 | 1 | <?php
|
| 2 | + |
| 3 | +function current_chart($type = 'line'){ |
| 4 | + global $db; |
| 5 | + $sql = "SELECT DATE(t.created_at) AS date, |
| 6 | + SUM(CASE |
| 7 | + WHEN r.type = 'day' THEN co.priceDay |
| 8 | + WHEN r.type = 'night' THEN co.priceNight |
| 9 | + ELSE 0 |
| 10 | + END) AS total_sales |
| 11 | + FROM transactions t |
| 12 | + JOIN rentals r ON t.id = r.transact_id |
| 13 | + JOIN cottages co ON r.cottage_id = co.id |
| 14 | + WHERE t.status = 'Proceed' |
| 15 | + AND DATE(t.created_at) = CURDATE() |
| 16 | + GROUP BY DATE(t.created_at) |
| 17 | + ORDER BY DATE(t.created_at);"; |
| 18 | + |
| 19 | + $stmt = $db->prepare($sql); |
| 20 | + $stmt->execute(); |
| 21 | + |
| 22 | + $labels = []; |
| 23 | + $data = []; |
| 24 | + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
| 25 | + $labels[] = date("D m", strtotime($row['date'])); |
| 26 | + $data[] = $row['total_sales']; |
| 27 | + } |
| 28 | + $chartData = [ |
| 29 | + 'labels' => $labels, |
| 30 | + 'datasets' => [ |
| 31 | + [ |
| 32 | + 'label' => 'Earnings', |
| 33 | + 'fill' => true, |
| 34 | + 'data' => $data, |
| 35 | + 'backgroundColor' => 'rgba(78, 115, 223, 0.05)', |
| 36 | + 'borderColor' => 'rgba(78, 115, 223, 1)' |
| 37 | + ] |
| 38 | + ] |
| 39 | + ]; |
| 40 | + |
| 41 | + $chartDataJson = json_encode($chartData); |
| 42 | + ?> |
| 43 | + <canvas data-bss-chart='{"type":"<?php echo $type?>","data":<?php echo $chartDataJson; ?>,"options":{"maintainAspectRatio":false,"legend":{"display":false,"labels":{"fontStyle":"normal"}},"title":{"fontStyle":"normal"},"scales":{"xAxes":[{"gridLines":{"color":"rgb(234, 236, 244)","zeroLineColor":"rgb(234, 236, 244)","drawBorder":false,"drawTicks":false,"borderDash":["2"],"zeroLineBorderDash":["2"],"drawOnChartArea":false},"ticks":{"fontColor":"#858796","fontStyle":"normal","padding":20}}],"yAxes":[{"gridLines":{"color":"rgb(234, 236, 244)","zeroLineColor":"rgb(234, 236, 244)","drawBorder":false,"drawTicks":false,"borderDash":["2"],"zeroLineBorderDash":["2"]},"ticks":{"fontColor":"#858796","fontStyle":"normal","padding":20}}]}}}'></canvas> |
| 44 | + <?php |
| 45 | + } |
| 46 | + |
| 47 | +function weekly_chart($type = 'line'){ |
| 48 | + global $db; |
| 49 | + $sql = "SELECT YEAR(t.created_at) AS year, WEEK(t.created_at) AS week, SUM(CASE |
| 50 | + WHEN r.type = 'day' THEN co.priceDay |
| 51 | + WHEN r.type = 'night' THEN co.priceNight |
| 52 | + ELSE 0 |
| 53 | + END) AS total_sales |
| 54 | + FROM transactions t |
| 55 | + JOIN rentals r ON t.id = r.transact_id |
| 56 | + JOIN cottages co ON r.cottage_id = co.id |
| 57 | + WHERE t.status = 'Proceed' |
| 58 | + AND DATE_FORMAT(t.created_at, '%Y-%m') = DATE_FORMAT(CURRENT_DATE, '%Y-%m') |
| 59 | + GROUP BY YEAR(t.created_at), WEEK(t.created_at) |
| 60 | + ORDER BY YEAR(t.created_at), WEEK(t.created_at);"; |
| 61 | + |
| 62 | + $stmt = $db->prepare($sql); |
| 63 | + $stmt->execute(); |
| 64 | + |
| 65 | + $labels = []; |
| 66 | + $data = []; |
| 67 | + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
| 68 | + $weekLabel = "Week " . $row['week'] . " (" . $row['year'] . ")"; |
| 69 | + $labels[] = $weekLabel; |
| 70 | + $data[] = $row['total_sales']; |
| 71 | + } |
| 72 | + $chartData = [ |
| 73 | + 'labels' => $labels, |
| 74 | + 'datasets' => [ |
| 75 | + [ |
| 76 | + 'label' => 'Earnings', |
| 77 | + 'fill' => true, |
| 78 | + 'data' => $data, |
| 79 | + 'backgroundColor' => 'rgba(78, 115, 223, 0.05)', |
| 80 | + 'borderColor' => 'rgba(78, 115, 223, 1)' |
| 81 | + ] |
| 82 | + ] |
| 83 | + ]; |
| 84 | + |
| 85 | + $chartDataJson = json_encode($chartData); |
| 86 | + |
| 87 | +?> |
| 88 | +<canvas data-bss-chart='{"type":"<?php echo $type?>","data":<?php echo $chartDataJson; ?>,"options":{"maintainAspectRatio":false,"legend":{"display":false,"labels":{"fontStyle":"normal"}},"title":{"fontStyle":"normal"},"scales":{"xAxes":[{"gridLines":{"color":"rgb(234, 236, 244)","zeroLineColor":"rgb(234, 236, 244)","drawBorder":false,"drawTicks":false,"borderDash":["2"],"zeroLineBorderDash":["2"],"drawOnChartArea":false},"ticks":{"fontColor":"#858796","fontStyle":"normal","padding":20}}],"yAxes":[{"gridLines":{"color":"rgb(234, 236, 244)","zeroLineColor":"rgb(234, 236, 244)","drawBorder":false,"drawTicks":false,"borderDash":["2"],"zeroLineBorderDash":["2"]},"ticks":{"fontColor":"#858796","fontStyle":"normal","padding":20}}]}}}'></canvas> |
| 89 | +<?php |
| 90 | +} |
| 91 | + |
| 92 | +function month_chart($type = 'line'){ |
| 93 | + global $db; |
| 94 | + $sql = "SELECT YEAR(t.created_at) AS year, MONTH(t.created_at) AS month, SUM(CASE |
| 95 | + WHEN r.type = 'day' THEN co.priceDay |
| 96 | + WHEN r.type = 'night' THEN co.priceNight |
| 97 | + ELSE 0 |
| 98 | + END) AS total_sales |
| 99 | + FROM transactions t |
| 100 | + JOIN rentals r ON t.id = r.transact_id |
| 101 | + JOIN cottages co ON r.cottage_id = co.id |
| 102 | + WHERE t.status = 'Proceed' AND DATE_FORMAT(t.created_at, '%Y-%m') = DATE_FORMAT(CURRENT_DATE, '%Y-%m') |
| 103 | + GROUP BY YEAR(t.created_at), MONTH(t.created_at) |
| 104 | + ORDER BY YEAR(t.created_at), MONTH(t.created_at);"; |
| 105 | + |
| 106 | + $stmt = $db->prepare($sql); |
| 107 | + $stmt->execute(); |
| 108 | + |
| 109 | + $labels = []; |
| 110 | + $data = []; |
| 111 | + while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
| 112 | + $monthName = date("M", mktime(0, 0, 0, $row['month'], 10)); |
| 113 | + $labels[] = $monthName . ' ' . $row['year']; |
| 114 | + $data[] = $row['total_sales']; |
| 115 | + } |
| 116 | + $chartData = [ |
| 117 | + 'labels' => $labels, |
| 118 | + 'datasets' => [ |
| 119 | + [ |
| 120 | + 'label' => 'Earnings', |
| 121 | + 'fill' => true, |
| 122 | + 'data' => $data, |
| 123 | + 'backgroundColor' => 'rgba(78, 115, 223, 0.05)', |
| 124 | + 'borderColor' => 'rgba(78, 115, 223, 1)' |
| 125 | + ] |
| 126 | + ] |
| 127 | + ]; |
| 128 | + |
| 129 | + |
| 130 | + $chartDataJson = json_encode($chartData); |
| 131 | + ?> |
| 132 | + <canvas data-bss-chart='{"type":"<?php echo $type?>","data":<?php echo $chartDataJson; ?>,"options":{"maintainAspectRatio":false,"legend":{"display":false,"labels":{"fontStyle":"normal"}},"title":{"fontStyle":"normal"},"scales":{"xAxes":[{"gridLines":{"color":"rgb(234, 236, 244)","zeroLineColor":"rgb(234, 236, 244)","drawBorder":false,"drawTicks":false,"borderDash":["2"],"zeroLineBorderDash":["2"],"drawOnChartArea":false},"ticks":{"fontColor":"#858796","fontStyle":"normal","padding":20}}],"yAxes":[{"gridLines":{"color":"rgb(234, 236, 244)","zeroLineColor":"rgb(234, 236, 244)","drawBorder":false,"drawTicks":false,"borderDash":["2"],"zeroLineBorderDash":["2"]},"ticks":{"fontColor":"#858796","fontStyle":"normal","padding":20}}]}}}'></canvas> |
| 133 | + <?php |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + function annual_chart($type = 'line'){ |
| 138 | + global $db; |
| 139 | + $sql = "SELECT YEAR(t.created_at) AS year, MONTH(t.created_at) AS month, SUM(CASE |
| 140 | + WHEN r.type = 'day' THEN co.priceDay |
| 141 | + WHEN r.type = 'night' THEN co.priceNight |
| 142 | + ELSE 0 |
| 143 | + END) AS total_sales |
| 144 | + FROM transactions t |
| 145 | + JOIN rentals r ON t.id = r.transact_id |
| 146 | + JOIN cottages co ON r.cottage_id = co.id |
| 147 | + WHERE t.status = 'Proceed' AND DATE_FORMAT(t.created_at, '%Y') = DATE_FORMAT(CURRENT_DATE, '%Y') |
| 148 | + GROUP BY YEAR(t.created_at), MONTH(t.created_at) |
| 149 | + ORDER BY YEAR(t.created_at), MONTH(t.created_at);"; |
| 150 | + |
| 151 | + $stmt = $db->prepare($sql); |
| 152 | + $stmt->execute(); |
| 153 | + |
| 154 | + $labels = []; |
| 155 | + $data = []; |
| 156 | + while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
| 157 | + $monthName = date("M", mktime(0, 0, 0, $row['month'], 10)); |
| 158 | + $labels[] = $monthName . ' ' . $row['year']; |
| 159 | + $data[] = $row['total_sales']; |
| 160 | + } |
| 161 | + $chartData = [ |
| 162 | + 'labels' => $labels, |
| 163 | + 'datasets' => [ |
| 164 | + [ |
| 165 | + 'label' => 'Earnings', |
| 166 | + 'fill' => true, |
| 167 | + 'data' => $data, |
| 168 | + 'backgroundColor' => 'rgba(78, 115, 223, 0.05)', |
| 169 | + 'borderColor' => 'rgba(78, 115, 223, 1)' |
| 170 | + ] |
| 171 | + ] |
| 172 | + ]; |
| 173 | + |
| 174 | + |
| 175 | + $chartDataJson = json_encode($chartData); |
| 176 | + ?> |
| 177 | + <canvas data-bss-chart='{"type":"<?php echo $type?>","data":<?php echo $chartDataJson; ?>,"options":{"maintainAspectRatio":false,"legend":{"display":false,"labels":{"fontStyle":"normal"}},"title":{"fontStyle":"normal"},"scales":{"xAxes":[{"gridLines":{"color":"rgb(234, 236, 244)","zeroLineColor":"rgb(234, 236, 244)","drawBorder":false,"drawTicks":false,"borderDash":["2"],"zeroLineBorderDash":["2"],"drawOnChartArea":false},"ticks":{"fontColor":"#858796","fontStyle":"normal","padding":20}}],"yAxes":[{"gridLines":{"color":"rgb(234, 236, 244)","zeroLineColor":"rgb(234, 236, 244)","drawBorder":false,"drawTicks":false,"borderDash":["2"],"zeroLineBorderDash":["2"]},"ticks":{"fontColor":"#858796","fontStyle":"normal","padding":20}}]}}}'></canvas> |
| 178 | + <?php |
| 179 | + } |
0 commit comments