PageRenderTime 37ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/A2Billing_UI/graph_statbar.php

https://github.com/xrg/a2billing
PHP | 405 lines | 199 code | 129 blank | 77 comment | 62 complexity | a683b1ff395e8c62b09d3e48382b7224 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. include_once(dirname(__FILE__) . "/../lib/defines.php");
  3. include_once(dirname(__FILE__) . "/../lib/module.access.php");
  4. include_once(dirname(__FILE__) . "/jpgraph_lib/jpgraph.php");
  5. include_once(dirname(__FILE__) . "/jpgraph_lib/jpgraph_bar.php");
  6. if (! has_rights (ACX_CALL_REPORT)){
  7. Header ("HTTP/1.0 401 Unauthorized");
  8. Header ("Location: PP_error.php?c=accessdenied");
  9. die();
  10. }
  11. // this variable specifie the debug type (0 => nothing, 1 => sql result, 2 => boucle checking, 3 other value checking)
  12. $FG_DEBUG = 0;
  13. getpost_ifset(array('min_call', 'fromstatsday_sday', 'days_compare', 'fromstatsmonth_sday', 'dsttype', 'srctype', 'clidtype', 'channel', 'resulttype', 'dst', 'src', 'clid', 'userfieldtype', 'userfield', 'accountcodetype', 'accountcode', 'customer', 'entercustomer', 'enterprovider', 'entertrunk', 'enterratecard', 'entertariffgroup'));
  14. // http://localhost/Asterisk/asterisk-stat-v1_4/graph_stat.php?min_call=0&fromstatsday_sday=11&days_compare=2&fromstatsmonth_sday=2005-02&dsttype=1&srctype=1&clidtype=1&channel=&resulttype=&dst=1649&src=&clid=&userfieldtype=1&userfield=&accountcodetype=1&accountcode=
  15. // The variable FG_TABLE_NAME define the table name to use
  16. $FG_TABLE_NAME="cc_call t1 LEFT OUTER JOIN cc_trunk t3 ON t1.id_trunk = t3.id_trunk";
  17. //$link = DbConnect();
  18. $DBHandle = DbConnect();
  19. // The variable Var_col would define the col that we want show in your table
  20. // First Name of the column in the html page, second name of the field
  21. $FG_TABLE_COL = array();
  22. /*******
  23. Calldate Clid Src Dst Dcontext Channel Dstchannel Lastapp Lastdata Duration Billsec Disposition Amaflags Accountcode Uniqueid Serverid
  24. *******/
  25. $FG_TABLE_COL[]=array (gettext("Calldate"), "starttime", "15%", "center", "SORT", "19", "", "", "", "", "", "display_dateformat");
  26. //$FG_TABLE_COL[]=array ("Callend", "stoptime", "15%", "center", "SORT", "19");
  27. //$FG_TABLE_COL[]=array ("Source", "source", "20%", "center", "SORT", "30");
  28. $FG_TABLE_COL[]=array (gettext("CalledNumber"), "calledstation", "15%", "center", "SORT", "30", "", "", "", "", "", "remove_prefix");
  29. $FG_TABLE_COL[]=array (gettext("Destination"), "destination", "15%", "center", "SORT", "30", "", "", "", "", "", "remove_prefix");
  30. //$FG_TABLE_COL[]=array ("Country", "calledcountry", "10%", "center", "SORT", "30", "lie", "country", "countryname", "countrycode='%id'", "%1");
  31. //$FG_TABLE_COL[]=array ("Site", "site_id", "7%", "center", "sort", "15", "lie", "site", "name", "id='%id'", "%1");
  32. $FG_TABLE_COL[]=array (gettext("Duration"), "sessiontime", "7%", "center", "SORT", "30", "", "", "", "", "", "display_minute");
  33. $FG_TABLE_COL[]=array (gettext("CardUsed"), "username", "11%", "center", "SORT", "30");
  34. $FG_TABLE_COL[]=array (gettext("terminatecause"), "terminatecause", "10%", "center", "SORT", "30");
  35. $FG_TABLE_COL[]=array (gettext("IAX/SIP"), "sipiax", "6%", "center", "SORT", "", "list", $yesno);
  36. //$FG_TABLE_COL[]=array ("DestID", "destID", "12%", "center", "SORT", "30");
  37. //if ($_SESSION["is_admin"]==1) $FG_TABLE_COL[]=array ("Con_charg", "connectcharge", "12%", "center", "SORT", "30");
  38. //if ($_SESSION["is_admin"]==1) $FG_TABLE_COL[]=array ("Dis_charg", "disconnectcharge", "12%", "center", "SORT", "30");
  39. //if ($_SESSION["is_admin"]==1) $FG_TABLE_COL[]=array ("Sec/mn", "secpermin", "12%", "center", "SORT", "30");
  40. //if ($_SESSION["is_admin"]==1) $FG_TABLE_COL[]=array ("Buycosts", "buycosts", "12%", "center", "SORT", "30");
  41. $FG_TABLE_COL[]=array (gettext("InitialRate"), "calledrate", "10%", "center", "SORT", "30", "", "", "", "", "", "display_2dec");
  42. $FG_TABLE_COL[]=array (gettext("Cost"), "sessionbill", "10%", "center", "SORT", "30", "", "", "", "", "", "display_2bill");
  43. // ??? cardID
  44. $FG_TABLE_DEFAULT_ORDER = "t1.starttime";
  45. $FG_TABLE_DEFAULT_SENS = "DESC";
  46. // This Variable store the argument for the SQL query
  47. $FG_COL_QUERY='t1.starttime, t1.sessiontime';
  48. $FG_COL_QUERY_GRAPH='t1.starttime, t1.sessiontime';
  49. // The variable LIMITE_DISPLAY define the limit of record to display by page
  50. $FG_LIMITE_DISPLAY=100;
  51. // Number of column in the html table
  52. $FG_NB_TABLE_COL=count($FG_TABLE_COL);
  53. if ($FG_DEBUG == 3) echo "<br>Table : $FG_TABLE_NAME - Col_query : $FG_COL_QUERY";
  54. $instance_table = new Table($FG_TABLE_NAME, $FG_COL_QUERY);
  55. $instance_table_graph = new Table($FG_TABLE_NAME, $FG_COL_QUERY_GRAPH);
  56. if ( is_null ($order) || is_null($sens) ){
  57. $order = $FG_TABLE_DEFAULT_ORDER;
  58. $sens = $FG_TABLE_DEFAULT_SENS;
  59. }
  60. $SQLcmd = '';
  61. //$SQLcmd = do_field($SQLcmd, 'src', 'source');
  62. $SQLcmd = do_field($SQLcmd, 'dst', 'calledstation');
  63. if ($_GET['before']) {
  64. if (strpos($SQLcmd, 'WHERE') > 0) { $SQLcmd = "$SQLcmd AND ";
  65. }else{ $SQLcmd = "$SQLcmd WHERE "; }
  66. $SQLcmd = "$SQLcmd starttime <'".$_POST['before']."'";
  67. }
  68. if ($_GET['after']) { if (strpos($SQLcmd, 'WHERE') > 0) { $SQLcmd = "$SQLcmd AND ";
  69. } else { $SQLcmd = "$SQLcmd WHERE "; }
  70. $SQLcmd = "$SQLcmd starttime >'".$_GET['after']."'";
  71. }
  72. $date_clause='';
  73. $min_call= intval($min_call);
  74. if (($min_call!=0) && ($min_call!=1)) $min_call=0;
  75. if (!isset($fromstatsday_sday)){
  76. $fromstatsday_sday = date("d");
  77. $fromstatsmonth_sday = date("Y-m");
  78. }
  79. if (isset($customer) && ($customer>0)){
  80. if (strlen($SQLcmd)>0) $SQLcmd.=" AND ";
  81. else $SQLcmd.=" WHERE ";
  82. $SQLcmd.=" username='$customer' ";
  83. }else{
  84. if (isset($entercustomer) && ($entercustomer>0)){
  85. if (strlen($SQLcmd)>0) $SQLcmd.=" AND ";
  86. else $SQLcmd.=" WHERE ";
  87. $SQLcmd.=" username='$entercustomer' ";
  88. }
  89. }
  90. if ($_SESSION["is_admin"] == 1)
  91. {
  92. if (isset($enterprovider) && $enterprovider > 0) {
  93. if (strlen($SQLcmd) > 0) $SQLcmd .= " AND "; else $SQLcmd .= " WHERE ";
  94. $SQLcmd .= " t3.id_provider = '$enterprovider' ";
  95. }
  96. if (isset($entertrunk) && $entertrunk > 0) {
  97. if (strlen($SQLcmd) > 0) $SQLcmd .= " AND "; else $SQLcmd .= " WHERE ";
  98. $SQLcmd .= " t3.id_trunk = '$entertrunk' ";
  99. }
  100. if (isset($entertariffgroup) && $entertariffgroup > 0) {
  101. if (strlen($SQLcmd) > 0) $SQLcmd .= " AND "; else $SQLcmd .= " WHERE ";
  102. $SQLcmd .= "t1.id_tariffgroup = '$entertariffgroup'";
  103. }
  104. if (isset($enterratecard) && $enterratecard > 0) {
  105. if (strlen($SQLcmd) > 0) $SQLcmd .= " AND "; else $SQLcmd .= " WHERE ";
  106. $SQLcmd .= "t1.id_ratecard = '$enterratecard'";
  107. }
  108. }
  109. if (DB_TYPE == "postgres"){
  110. if (isset($fromstatsday_sday) && isset($fromstatsmonth_sday))
  111. $date_clause.=" AND starttime < date'$fromstatsmonth_sday-$fromstatsday_sday'+ INTERVAL '1 DAY' AND starttime >= '$fromstatsmonth_sday-$fromstatsday_sday'";
  112. }else{
  113. if (isset($fromstatsday_sday) && isset($fromstatsmonth_sday)) $date_clause.=" AND starttime < ADDDATE('$fromstatsmonth_sday-$fromstatsday_sday',INTERVAL 1 DAY) AND starttime >= '$fromstatsmonth_sday-$fromstatsday_sday'";
  114. }
  115. //-- $date_clause=" AND calldate < date'$fromstatsmonth_sday-$fromstatsday_sday'+ INTERVAL '1 DAY' AND calldate >= '$fromstatsmonth_sday-$fromstatsday_sday 12:00:00'";
  116. if (strpos($SQLcmd, 'WHERE') > 0) {
  117. $FG_TABLE_CLAUSE = substr($SQLcmd,6).$date_clause;
  118. }elseif (strpos($date_clause, 'AND') > 0){
  119. $FG_TABLE_CLAUSE = substr($date_clause,5);
  120. }
  121. if ($FG_DEBUG == 3) echo $FG_TABLE_CLAUSE;
  122. //$list = $instance_table -> Get_list ($DBHandle, $FG_TABLE_CLAUSE, $order, $sens, null, null, null, null);
  123. $list_total = $instance_table_graph -> Get_list ($DBHandle, $FG_TABLE_CLAUSE, 't1.starttime', 'ASC', null, null, null, null);
  124. /**************************************/
  125. $table_graph=array();
  126. $table_graph_hours=array();
  127. $numm=0;
  128. foreach ($list_total as $recordset){
  129. $numm++;
  130. $mydate= substr($recordset[0],0,10);
  131. $mydate_hours= substr($recordset[0],0,13);
  132. //echo "$mydate<br>";
  133. if (is_array($table_graph_hours[$mydate_hours])){
  134. $table_graph_hours[$mydate_hours][0]++;
  135. $table_graph_hours[$mydate_hours][1]=$table_graph_hours[$mydate_hours][1]+$recordset[1];
  136. }else{
  137. $table_graph_hours[$mydate_hours][0]=1;
  138. $table_graph_hours[$mydate_hours][1]=$recordset[1];
  139. }
  140. if (is_array($table_graph[$mydate])){
  141. $table_graph[$mydate][0]++;
  142. $table_graph[$mydate][1]=$table_graph[$mydate][1]+$recordset[1];
  143. }else{
  144. $table_graph[$mydate][0]=1;
  145. $table_graph[$mydate][1]=$recordset[1];
  146. }
  147. }
  148. //print_r($table_graph_hours);
  149. //exit();
  150. $mmax=0;
  151. $totalcall==0;
  152. $totalminutes=0;
  153. foreach ($table_graph as $tkey => $data){
  154. if ($mmax < $data[1]) $mmax=$data[1];
  155. $totalcall+=$data[0];
  156. $totalminutes+=$data[1];
  157. }
  158. /************************************************/
  159. $datax1 = array_keys($table_graph_hours);
  160. $datay1 = array_values ($table_graph_hours);
  161. //$days_compare // 3
  162. $nbday=0; // in tableau_value and tableau_hours to select the day in which you store the data
  163. //$min_call=0; // min_call variable : 0 > get the number of call 1 > number minutes
  164. $table_subtitle[]=gettext("Statistic : Load by hours");
  165. $table_subtitle[]=gettext("Statistic : Minutes by Hours");
  166. $table_colors[]="yellow@0.3";
  167. $table_colors[]="purple@0.3";
  168. $table_colors[]="green@0.3";
  169. $table_colors[]="blue@0.3";
  170. $table_colors[]="red@0.3";
  171. /*$table_graph_hours = array();
  172. $table_graph_hours["2004-01-08 15"] = array (100, 15);
  173. $table_graph_hours["2004-01-08 16"] = array (100, 15);
  174. $table_graph_hours["2004-01-08 17"] = array (100, 15);
  175. */
  176. $jour = substr($datax1[0],8,2); //le jour courant
  177. $legend[0] = substr($datax1[0],0,10); //l
  178. //print_r ($table_graph_hours);
  179. // Create the graph to compare the day
  180. // extract all minutes/nb call for each hours
  181. foreach ($table_graph_hours as $key => $value) {
  182. $jour_suivant = substr($key,8,2);
  183. if($jour_suivant != $jour) {
  184. $nbday++;
  185. $legend[$nbday] = substr($key,0,10);
  186. $jour = $jour_suivant;
  187. }
  188. $heure = intval(substr($key,11,2));
  189. if ($min_call == 0) $div = 1; else $div = 60;
  190. $tableau_value[$nbday][$heure] = $value[$min_call]/$div;
  191. }
  192. // je remplie les cases vide par des 0
  193. for ($i=0; $i<=$nbday; $i++)
  194. for ($j=0; $j<24; $j++)
  195. if (!isset($tableau_value[$i][$j])) $tableau_value[$i][$j]=0;
  196. //Je remplace les 0 par null pour pour les heures
  197. $i = 23;
  198. while ($tableau_value[$nbday][$i] == 0) {
  199. $tableau_value[$nbday][$i] = null;
  200. $i--;
  201. }
  202. //print_r($tableau_value);
  203. //print_r($tableau_hours);
  204. /*echo "<br>nb tableau_value:".count($tableau_value);
  205. echo "<br>nb tableau_hours:".count($tableau_hours);
  206. print_r($tableau_value[0]);
  207. echo "<br><br>";
  208. print_r($tableau_hours[0]);
  209. echo "<br><br>";
  210. */
  211. foreach ($datay1 as $tkey => $data){
  212. $dataz1[]=$data[1];
  213. $dataz2[]=$data[0];
  214. }
  215. /*$datay1 = array(2,6,7,12,13,18);
  216. echo "<br>nb x1:".count($datax1);
  217. echo "<br>nb z1:".count($dataz1);
  218. print_r($datax1);
  219. echo "<br><br>";
  220. print_r($dataz1);
  221. echo "<br><br>";
  222. print_r($datay1);*/
  223. //print_r($dataz1);
  224. //$dataz1 = array(2,6,7,12,13,2,6,7,12,13,2,6,7,12,13,2,6,7,12,13,2,6,7,12,13);
  225. //print_r($dataz1);
  226. //$datax1 = array(5,12,12,19,25,20);
  227. // Setup the graph
  228. $graph = new Graph(750,450);
  229. $graph->SetMargin(40,40,45,90); //droit,gauche,haut,bas
  230. $graph->SetMarginColor('white');
  231. //$graph->SetScale("linlin");
  232. $graph->SetScale("textlin");
  233. $graph->yaxis->scale->SetGrace(3);
  234. // Hide the frame around the graph
  235. $graph->SetFrame(false);
  236. // Setup title
  237. $graph->title->Set("Graphic");
  238. //$graph->title->SetFont(FF_VERDANA,FS_BOLD,14);
  239. // Note: requires jpgraph 1.12p or higher
  240. $graph->SetBackgroundGradient('#FFFFFF','#CDDEFF:0.8',GRAD_HOR,BGRAD_PLOT);
  241. $graph->tabtitle->Set($table_subtitle[$min_call]);
  242. $graph->tabtitle->SetWidth(TABTITLE_WIDTHFULL);
  243. // Enable X and Y Grid
  244. $graph->xgrid->Show();
  245. $graph->xgrid->SetColor('gray@0.5');
  246. $graph->ygrid->SetColor('gray@0.5');
  247. $graph->yaxis->HideZeroLabel();
  248. $graph->xaxis->HideZeroLabel();
  249. $graph->ygrid->SetFill(true,'#EFEFEF@0.5','#CDDEFF@0.5');
  250. //$graph->xaxis->SetTickLabels($tableau_hours[0]);
  251. // initialisaton fixe de AXE X
  252. $tableau_hours[0] = array("00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23");
  253. $graph->xaxis->SetTickLabels($tableau_hours[0]);
  254. // Setup X-scale
  255. //$graph->xaxis->SetTickLabels($tableau_hours[0]);
  256. $graph->xaxis->SetLabelAngle(90);
  257. // Format the legend box
  258. $graph->legend->SetColor('navy');
  259. $graph->legend->SetFillColor('gray@0.8');
  260. $graph->legend->SetLineWeight(1);
  261. //$graph->legend->SetFont(FF_ARIAL,FS_BOLD,8);
  262. $graph->legend->SetShadow('gray@0.4',3);
  263. $graph->legend->SetAbsPos(15,130,'right','bottom');
  264. for ($indgraph=0;$indgraph<=$nbday;$indgraph++){
  265. $bplot[$indgraph] = new BarPlot($tableau_value[$indgraph]);
  266. $bplot[$indgraph]->SetColor($table_colors[$indgraph]);
  267. $bplot[$indgraph]->SetWeight(2);
  268. $bplot[$indgraph]->SetFillColor('orange');
  269. $bplot[$indgraph]->SetShadow();
  270. $bplot[$indgraph]->value->Show();
  271. $bplot[$indgraph]->SetLegend($legend[$indgraph]);
  272. $graph->Add($bplot[$indgraph]);
  273. }
  274. // Output the graph
  275. $graph->Stroke();
  276. ?>