PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/A2Billing_UI/graph_stat.php

https://github.com/xrg/a2billing
PHP | 434 lines | 219 code | 124 blank | 91 comment | 63 complexity | 2404d6c1f67c78c672315632b497f26c MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. include_once(dirname(__FILE__) . "/../lib/defines.php");
  3. include_once(dirname(__FILE__) . "/jpgraph_lib/jpgraph.php");
  4. include_once(dirname(__FILE__) . "/jpgraph_lib/jpgraph_line.php");
  5. include_once(dirname(__FILE__) . "/../lib/module.access.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', 'entertariffgroup', 'entertrunk', 'enterratecard'));
  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.calledstation, t1.destination, t1.sessiontime, t1.username, t1.terminatecause, t1.sipiax, t1.calledrate, t1.sessionbill';
  48. // t1.stoptime,
  49. $FG_COL_QUERY_GRAPH='t1.starttime, t1.sessiontime, t1.sessionbill-t1.buycost as profit, t1.sessionbill, t1.buycost';
  50. // The variable LIMITE_DISPLAY define the limit of record to display by page
  51. $FG_LIMITE_DISPLAY=100;
  52. // Number of column in the html table
  53. $FG_NB_TABLE_COL=count($FG_TABLE_COL);
  54. if ($FG_DEBUG == 3) echo "<br>Table : $FG_TABLE_NAME - Col_query : $FG_COL_QUERY";
  55. $instance_table = new Table($FG_TABLE_NAME, $FG_COL_QUERY);
  56. $instance_table_graph = new Table($FG_TABLE_NAME, $FG_COL_QUERY_GRAPH);
  57. if ( is_null ($order) || is_null($sens) ){
  58. $order = $FG_TABLE_DEFAULT_ORDER;
  59. $sens = $FG_TABLE_DEFAULT_SENS;
  60. }
  61. $SQLcmd = '';
  62. if ($_GET['before']) {
  63. if (strpos($SQLcmd, 'WHERE') > 0) { $SQLcmd = "$SQLcmd AND ";
  64. }else{ $SQLcmd = "$SQLcmd WHERE "; }
  65. $SQLcmd = "$SQLcmd starttime <'".$_POST['before']."'";
  66. }
  67. if ($_GET['after']) { if (strpos($SQLcmd, 'WHERE') > 0) { $SQLcmd = "$SQLcmd AND ";
  68. } else { $SQLcmd = "$SQLcmd WHERE "; }
  69. $SQLcmd = "$SQLcmd starttime >'".$_GET['after']."'";
  70. }
  71. $SQLcmd = do_field($SQLcmd, 'dst', 'calledstation');;
  72. if (isset($customer) && ($customer>0)){
  73. if (strlen($SQLcmd)>0) $SQLcmd.=" AND ";
  74. else $SQLcmd.=" WHERE ";
  75. $SQLcmd.=" username='$customer' ";
  76. }else{
  77. if (isset($entercustomer) && ($entercustomer>0)){
  78. if (strlen($SQLcmd)>0) $SQLcmd.=" AND ";
  79. else $SQLcmd.=" WHERE ";
  80. $SQLcmd.=" username='$entercustomer' ";
  81. }
  82. }
  83. if ($_SESSION["is_admin"] == 1)
  84. {
  85. if (isset($enterprovider) && $enterprovider > 0) {
  86. if (strlen($SQLcmd) > 0) $SQLcmd .= " AND "; else $SQLcmd .= " WHERE ";
  87. $SQLcmd .= " t3.id_provider = '$enterprovider' ";
  88. }
  89. if (isset($entertrunk) && $entertrunk > 0) {
  90. if (strlen($SQLcmd) > 0) $SQLcmd .= " AND "; else $SQLcmd .= " WHERE ";
  91. $SQLcmd .= " t3.id_trunk = '$entertrunk' ";
  92. }
  93. if (isset($entertariffgroup) && $entertariffgroup > 0) {
  94. if (strlen($SQLcmd) > 0) $SQLcmd .= " AND "; else $SQLcmd .= " WHERE ";
  95. $SQLcmd .= "t1.id_tariffgroup = '$entertariffgroup'";
  96. }
  97. if (isset($enterratecard) && $enterratecard > 0) {
  98. if (strlen($SQLcmd) > 0) $SQLcmd .= " AND "; else $SQLcmd .= " WHERE ";
  99. $SQLcmd .= "t1.id_ratecard = '$enterratecard'";
  100. }
  101. }
  102. $date_clause='';
  103. $min_call= intval($min_call);
  104. if (!(($min_call>=0) && ($min_call<=4))) $min_call=0;
  105. if (!isset($fromstatsday_sday)){
  106. $fromstatsday_sday = date("d");
  107. $fromstatsmonth_sday = date("Y-m");
  108. }
  109. if (!isset($days_compare) ){
  110. $days_compare=2;
  111. }
  112. if (DB_TYPE == "postgres"){
  113. if (isset($fromstatsday_sday) && isset($fromstatsmonth_sday))
  114. $date_clause.=" AND t1.starttime < date'$fromstatsmonth_sday-$fromstatsday_sday'+ INTERVAL '1 DAY' AND t1.starttime >= date'$fromstatsmonth_sday-$fromstatsday_sday' - INTERVAL '$days_compare DAY'";
  115. }else{
  116. if (isset($fromstatsday_sday) && isset($fromstatsmonth_sday)) $date_clause.=" AND t1.starttime < ADDDATE('$fromstatsmonth_sday-$fromstatsday_sday',INTERVAL 1 DAY) AND t1.starttime >= SUBDATE('$fromstatsmonth_sday-$fromstatsday_sday',INTERVAL $days_compare DAY)";
  117. }
  118. if (strpos($SQLcmd, 'WHERE') > 0) {
  119. $FG_TABLE_CLAUSE = substr($SQLcmd,6).$date_clause;
  120. }elseif (strpos($date_clause, 'AND') > 0){
  121. $FG_TABLE_CLAUSE = substr($date_clause,5);
  122. }
  123. if ($FG_DEBUG == 3) echo $FG_TABLE_CLAUSE;
  124. //$list = $instance_table -> Get_list ($DBHandle, $FG_TABLE_CLAUSE, $order, $sens, null, null, null, null);
  125. $list_total = $instance_table_graph -> Get_list ($DBHandle, $FG_TABLE_CLAUSE, 't1.starttime', 'ASC', null, null, null, null);
  126. /**************************************/
  127. $table_graph=array();
  128. $table_graph_hours=array();
  129. $numm=0;
  130. foreach ($list_total as $recordset){
  131. $numm++;
  132. $mydate= substr($recordset[0],0,10);
  133. $mydate_hours= substr($recordset[0],0,13);
  134. //echo "$mydate<br>";
  135. if (is_array($table_graph_hours[$mydate_hours])){
  136. $table_graph_hours[$mydate_hours][0]++;
  137. $table_graph_hours[$mydate_hours][1]=$table_graph_hours[$mydate_hours][1]+$recordset[1];
  138. $table_graph_hours[$mydate_hours][2]=$table_graph_hours[$mydate_hours][2]+$recordset[2];
  139. $table_graph_hours[$mydate_hours][3]=$table_graph_hours[$mydate_hours][3]+$recordset[3];
  140. $table_graph_hours[$mydate_hours][4]=$table_graph_hours[$mydate_hours][4]+$recordset[4];
  141. }else{
  142. $table_graph_hours[$mydate_hours][0]=1;
  143. $table_graph_hours[$mydate_hours][1]=$recordset[1];
  144. $table_graph_hours[$mydate_hours][2]=$recordset[2];
  145. $table_graph_hours[$mydate_hours][3]=$recordset[3];
  146. $table_graph_hours[$mydate_hours][4]=$recordset[4];
  147. }
  148. if (is_array($table_graph[$mydate])){
  149. $table_graph[$mydate][0]++;
  150. $table_graph[$mydate][1]=$table_graph[$mydate][1]+$recordset[1];
  151. $table_graph[$mydate][2]=$table_graph[$mydate][2]+$recordset[2];
  152. $table_graph[$mydate][3]=$table_graph[$mydate][3]+$recordset[3];
  153. $table_graph[$mydate][4]=$table_graph[$mydate][4]+$recordset[4];
  154. }else{
  155. $table_graph[$mydate][0]=1;
  156. $table_graph[$mydate][1]=$recordset[1];
  157. $table_graph[$mydate][2]=$recordset[2];
  158. $table_graph[$mydate][3]=$recordset[3];
  159. $table_graph[$mydate][4]=$recordset[4];
  160. }
  161. }
  162. //print_r($table_graph_hours);
  163. //exit();
  164. $mmax=0;
  165. $totalcall==0;
  166. $totalminutes=0;
  167. $totalprofit=0;
  168. $totalsell=0;
  169. $totalbuy=0;
  170. foreach ($table_graph as $tkey => $data){
  171. if ($mmax < $data[1]) $mmax=$data[1];
  172. $totalcall+=$data[0];
  173. $totalminutes+=$data[1];
  174. $totalprofit+=$data[2];
  175. $totalsell+=$data[3];
  176. $totalbuy+=$data[4];
  177. }
  178. //echo "totalcall = $totalcall - totalminutes=$totalminutes - totalprofit=$totalprofit - totalsell=$totalsell - totalbuy=$totalbuy ";
  179. /************************************************/
  180. $datax1 = array_keys($table_graph_hours);
  181. $datay1 = array_values ($table_graph_hours);
  182. //$days_compare // 3
  183. $nbday=0; // in tableau_value and tableau_hours to select the day in which you store the data
  184. //$min_call=0; // min_call variable : 0 > get the number of call 1 > number minutes
  185. $table_subtitle[]=gettext("Statistic : Number of call by Hours");
  186. $table_subtitle[]=gettext("Statistic : Minutes by Hours");
  187. $table_subtitle[]=gettext("Statistic : Profits by Hours");
  188. $table_subtitle[]=gettext("Statistic : Sells by Hours");
  189. $table_subtitle[]=gettext("Statistic : Buys by Hours");
  190. $table_colors[]="green@0.3";
  191. $table_colors[]="blue@0.3";
  192. $table_colors[]="red@0.3";
  193. $table_colors[]="yellow@0.3";
  194. $table_colors[]="purple@0.3";
  195. /*$table_graph_hours = array();
  196. $table_graph_hours["2004-01-08 15"] = array (100, 15);
  197. $table_graph_hours["2004-01-08 16"] = array (100, 15);
  198. $table_graph_hours["2004-01-08 17"] = array (100, 15);
  199. */
  200. $jour = substr($datax1[0],8,2); //le jour courant
  201. $legend[0] = substr($datax1[0],0,10); //l
  202. //print_r ($table_graph_hours);
  203. // Create the graph to compare the day
  204. // extract all minutes/nb call for each hours
  205. //print_r($table_graph_hours);
  206. //exit;
  207. foreach ($table_graph_hours as $key => $value) {
  208. $jour_suivant = substr($key,8,2);
  209. if($jour_suivant != $jour) {
  210. $nbday++;
  211. $legend[$nbday] = substr($key,0,10);
  212. $jour = $jour_suivant;
  213. }
  214. $heure = intval(substr($key,11,2));
  215. if ($min_call == 1) $div = 60; else $div = 1;
  216. $tableau_value[$nbday][$heure] = $value[$min_call]/$div;
  217. }
  218. // je remplie les cases vide par des 0
  219. for ($i=0; $i<=$nbday; $i++)
  220. for ($j=0; $j<24; $j++)
  221. if (!isset($tableau_value[$i][$j])) $tableau_value[$i][$j]=0;
  222. //Je remplace les 0 par null pour pour les heures
  223. $i = 23;
  224. while ($tableau_value[$nbday][$i] == 0) {
  225. $tableau_value[$nbday][$i] = null;
  226. $i--;
  227. }
  228. // print_r($tableau_value);
  229. //print_r($tableau_hours);
  230. /*echo "<br>nb tableau_value:".count($tableau_value);
  231. echo "<br>nb tableau_hours:".count($tableau_hours);
  232. print_r($tableau_value[0]);
  233. echo "<br><br>";
  234. print_r($tableau_hours[0]);
  235. echo "<br><br>";
  236. */
  237. foreach ($datay1 as $tkey => $data){
  238. $dataz1[]=$data[1];
  239. $dataz2[]=$data[0];
  240. }
  241. /*$datay1 = array(2,6,7,12,13,18);
  242. echo "<br>nb x1:".count($datax1);
  243. echo "<br>nb z1:".count($dataz1);
  244. print_r($datax1);
  245. echo "<br><br>";
  246. print_r($dataz1);
  247. echo "<br><br>";
  248. print_r($datay1);*/
  249. //print_r($dataz1);
  250. //$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);
  251. //print_r($dataz1);
  252. //$datax1 = array(5,12,12,19,25,20);
  253. // Setup the graph
  254. $graph = new Graph(750,450);
  255. $graph->SetMargin(40,40,45,90); //droit,gauche,haut,bas
  256. $graph->SetMarginColor('white');
  257. $graph->SetScale("linlin");
  258. // Hide the frame around the graph
  259. $graph->SetFrame(false);
  260. // Setup title
  261. $graph->title->Set("Graphic");
  262. //$graph->title->SetFont(FF_VERDANA,FS_BOLD,14);
  263. // Note: requires jpgraph 1.12p or higher
  264. $graph->SetBackgroundGradient('#FFFFFF','#CDDEFF:0.8',GRAD_HOR,BGRAD_PLOT);
  265. $graph->tabtitle->Set($table_subtitle[$min_call]);
  266. $graph->tabtitle->SetWidth(TABTITLE_WIDTHFULL);
  267. // Enable X and Y Grid
  268. $graph->xgrid->Show();
  269. $graph->xgrid->SetColor('gray@0.5');
  270. $graph->ygrid->SetColor('gray@0.5');
  271. $graph->yaxis->HideZeroLabel();
  272. $graph->xaxis->HideZeroLabel();
  273. $graph->ygrid->SetFill(true,'#EFEFEF@0.5','#CDDEFF@0.5');
  274. //$graph->xaxis->SetTickLabels($tableau_hours[0]);
  275. // initialisaton fixe de AXE X
  276. $tableau_hours[0] = array("","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23");
  277. $graph->xaxis->SetTickLabels($tableau_hours[0]);
  278. // Setup X-scale
  279. //$graph->xaxis->SetTickLabels($tableau_hours[0]);
  280. $graph->xaxis->SetLabelAngle(90);
  281. // Format the legend box
  282. $graph->legend->SetColor('navy');
  283. $graph->legend->SetFillColor('gray@0.8');
  284. $graph->legend->SetLineWeight(1);
  285. //$graph->legend->SetFont(FF_ARIAL,FS_BOLD,8);
  286. $graph->legend->SetShadow('gray@0.4',3);
  287. $graph->legend->SetAbsPos(15,130,'right','bottom');
  288. // Create the line plots
  289. /*$p1 = new LinePlot($datax1);
  290. $p1->SetColor("red");
  291. $p1->SetFillColor("yellow@0.5");
  292. $p1->SetWeight(2);
  293. $p1->mark->SetType(MARK_IMG_DIAMOND,5,0.6);
  294. $p1->SetLegend('2006');
  295. $graph->Add($p1);
  296. */
  297. for ($indgraph=0;$indgraph<=$nbday;$indgraph++){
  298. $p2[$indgraph] = new LinePlot($tableau_value[$indgraph]);
  299. $p2[$indgraph]->SetColor($table_colors[$indgraph]);
  300. $p2[$indgraph]->SetWeight(2);
  301. $p2[$indgraph]->SetLegend($legend[$indgraph]);
  302. //$p2->mark->SetType(MARK_IMG_MBALL,'red');
  303. $graph->Add($p2[$indgraph]);
  304. }
  305. // Add a vertical line at the end scale position '7'
  306. //$l1 = new PlotLine(VERTICAL,7);
  307. //$graph->Add($l1);
  308. // Output the graph
  309. $graph->Stroke();
  310. ?>