11<?php
22include_once 'functions/connection.php ' ;
33
4- function daily_chart (){
4+ function daily_chart ()
5+ {
56 global $ db ;
67 $ sql = "SELECT DATE(created_at) AS date, SUM(total) AS total_sales
78 FROM transactions
@@ -14,7 +15,7 @@ function daily_chart(){
1415
1516 $ labels = [];
1617 $ data = [];
17- while ($ row = $ stmt ->fetch (PDO ::FETCH_ASSOC )) {
18+ while ($ row = $ stmt ->fetch (PDO ::FETCH_ASSOC )) {
1819 $ date = date ("M d, Y " , strtotime ($ row ['date ' ]));
1920 $ labels [] = $ date ;
2021 $ data [] = $ row ['total_sales ' ];
@@ -33,13 +34,14 @@ function daily_chart(){
3334 ];
3435
3536 $ chartDataJson = json_encode ($ chartData );
36- ?>
37+ ?>
3738 <canvas data-bss-chart='{"type":"line","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>
38- <?php
39+ <?php
3940}
4041
4142
42- function month_chart (){
43+ function month_chart ()
44+ {
4345 global $ db ;
4446 $ sql = "SELECT YEAR(created_at) AS year, MONTH(created_at) AS month, SUM(total) AS total_sales
4547 FROM transactions
@@ -52,28 +54,78 @@ function month_chart(){
5254
5355 $ labels = [];
5456 $ data = [];
55- while ($ row = $ stmt ->fetch (PDO ::FETCH_ASSOC )) {
56- $ monthName = date ("M " , mktime (0 , 0 , 0 , $ row ['month ' ], 10 ));
57- $ labels [] = $ monthName . ' ' . $ row ['year ' ];
58- $ data [] = $ row ['total_sales ' ];
57+ while ($ row = $ stmt ->fetch (PDO ::FETCH_ASSOC )) {
58+ $ monthName = date ("M " , mktime (0 , 0 , 0 , $ row ['month ' ], 10 ));
59+ $ labels [] = $ monthName . ' ' . $ row ['year ' ];
60+ $ data [] = $ row ['total_sales ' ];
5961 }
6062 $ chartData = [
61- 'labels ' => $ labels ,
62- 'datasets ' => [
63- [
64- 'label ' => 'Earnings ' ,
65- 'fill ' => true ,
66- 'data ' => $ data ,
67- 'backgroundColor ' => 'rgba(78, 115, 223, 0.05) ' ,
68- 'borderColor ' => 'rgba(78, 115, 223, 1) '
69- ]
70- ]
63+ 'labels ' => $ labels ,
64+ 'datasets ' => [
65+ [
66+ 'label ' => 'Earnings ' ,
67+ 'fill ' => true ,
68+ 'data ' => $ data ,
69+ 'backgroundColor ' => 'rgba(78, 115, 223, 0.05) ' ,
70+ 'borderColor ' => 'rgba(78, 115, 223, 1) '
71+ ]
72+ ]
7173 ];
7274
7375
7476 $ chartDataJson = json_encode ($ chartData );
75- ?>
77+ ?>
7678 <canvas data-bss-chart='{"type":"line","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>
77- <?php
79+ <?php
7880}
7981
82+
83+
84+
85+ function expenditures_month_chart ()
86+ {
87+ global $ db ;
88+ $ sql = "SELECT i.name AS label, YEAR(e.created_at) AS year, MONTH(e.created_at) AS month, SUM(e.qty) AS total_expenditures
89+ FROM expenditures e
90+ INNER JOIN items i ON e.item_id = i.id
91+ WHERE MONTH(e.created_at) = MONTH(CURRENT_TIMESTAMP)
92+ GROUP BY i.name, YEAR(e.created_at), MONTH(e.created_at)
93+ ORDER BY YEAR(e.created_at), MONTH(e.created_at) " ;
94+
95+ $ stmt = $ db ->prepare ($ sql );
96+ $ stmt ->execute ();
97+
98+ $ labels = [];
99+ $ datasets = [];
100+ while ($ row = $ stmt ->fetch (PDO ::FETCH_ASSOC )) {
101+ $ monthName = date ("M " , mktime (0 , 0 , 0 , $ row ['month ' ], 10 ));
102+ $ labels [] = $ monthName . ' ' . $ row ['year ' ];
103+ $ itemLabel = $ row ['label ' ];
104+ $ data = $ row ['total_expenditures ' ];
105+
106+ $ existingDatasetIndex = array_search ($ itemLabel , array_column ($ datasets , 'label ' ));
107+ if ($ existingDatasetIndex !== false ) {
108+
109+ $ datasets [$ existingDatasetIndex ]['data ' ][] = $ data ;
110+ } else {
111+
112+ $ datasets [] = [
113+ 'label ' => $ itemLabel ,
114+ 'fill ' => true ,
115+ 'data ' => [$ data ],
116+ 'backgroundColor ' => 'rgba(78, 115, 223, 0.05) ' ,
117+ 'borderColor ' => 'rgba(78, 115, 223, 1) '
118+ ];
119+ }
120+ }
121+
122+ $ chartData = [
123+ 'labels ' => $ labels ,
124+ 'datasets ' => $ datasets
125+ ];
126+
127+ $ chartDataJson = json_encode ($ chartData );
128+ ?>
129+ <canvas data-bss-chart='{"type":"line","data":<?php echo $ chartDataJson ; ?> ,"options":{"maintainAspectRatio":false,"legend":{"display":true,"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>
130+ <?php
131+ }
0 commit comments