PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/A2Billing_UI/graph_pie.php

https://github.com/xrg/a2billing
PHP | 296 lines | 169 code | 77 blank | 50 comment | 55 complexity | bc0753146be5ce2b9ac70cf9f157ff9a 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_pie.php");
  5. include_once(dirname(__FILE__) . "/jpgraph_lib/jpgraph_pie3d.php");
  6. include_once(dirname(__FILE__) . "/../lib/module.access.php");
  7. if (! has_rights (ACX_CALL_REPORT)){
  8. Header ("HTTP/1.0 401 Unauthorized");
  9. Header ("Location: PP_error.php?c=accessdenied");
  10. die();
  11. }
  12. /*
  13. NOTE GENERER LES SOUSTRACTIONS SUR LES DATES NOUS-MEME
  14. RAPIDE
  15. cdrasterisk=> SELECT sum(duration) FROM cdr WHERE calldate < '2005-02-01' AND calldate >= '2005-01-01';
  16. sum
  17. ----------
  18. 69076793
  19. (1 row)
  20. TRES LENT
  21. cdrasterisk=> SELECT sum(duration) FROM cdr WHERE calldate < date '2005-02-01' - interval '0 months' AND calldate >= date '2005-02-01' - interval '1 months' ;
  22. sum
  23. ----------
  24. 69076793
  25. (1 row)
  26. */
  27. getpost_ifset(array('months_compare', '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', 'graphtype', 'choose_agent'));
  28. // graphtype = 1, 2, 3
  29. // 1 : traffic
  30. // 2 : Profit
  31. // 3 : Sells
  32. // 4 : Buys
  33. $FG_DEBUG = 0;
  34. $months = Array ( 0 => 'Jan', 1 => 'Feb', 2 => 'Mar', 3 => 'Apr', 4 => 'May', 5 => 'Jun', 6 => 'Jul', 7 => 'Aug', 8 => 'Sep', 9 => 'Oct', 10 => 'Nov', 11 => 'Dec' );
  35. if (!isset($months_compare)) $months_compare = 3;
  36. if (!isset($fromstatsmonth_sday)) $fromstatsmonth_sday = date("Y-m");
  37. //print_r (array_reverse ($mylegend));
  38. // 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=
  39. // The variable FG_TABLE_NAME define the table name to use
  40. $FG_TABLE_NAME="cc_call t1 LEFT OUTER JOIN cc_trunk t3 ON t1.id_trunk = t3.id_trunk";
  41. //$link = DbConnect();
  42. $DBHandle = DbConnect();
  43. // The variable Var_col would define the col that we want show in your table
  44. // First Name of the column in the html page, second name of the field
  45. $FG_TABLE_COL = array();
  46. /*******
  47. Calldate Clid Src Dst Dcontext Channel Dstchannel Lastapp Lastdata Duration Billsec Disposition Amaflags Accountcode Uniqueid Serverid
  48. *******/
  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. $FG_COL_QUERY = ' sum(sessiontime), sum(sessionbill-buycost), sum(sessionbill), sum(buycost) ';
  54. if ($FG_DEBUG == 3) echo "<br>Table : $FG_TABLE_NAME - Col_query : $FG_COL_QUERY";
  55. $instance_table_graph = new Table($FG_TABLE_NAME, $FG_COL_QUERY);
  56. if ( is_null ($order) || is_null($sens) ){
  57. $order = $FG_TABLE_DEFAULT_ORDER;
  58. $sens = $FG_TABLE_DEFAULT_SENS;
  59. }
  60. $SQLcmd = '';
  61. if ($_GET['before']) {
  62. if (strpos($SQLcmd, 'WHERE') > 0) {
  63. $SQLcmd = "$SQLcmd AND ";
  64. }else{
  65. $SQLcmd = "$SQLcmd WHERE ";
  66. }
  67. $SQLcmd = "$SQLcmd calldate<'".$_POST['before']."'";
  68. }
  69. if ($_GET['after']) {
  70. if (strpos($SQLcmd, 'WHERE') > 0) {
  71. $SQLcmd = "$SQLcmd AND ";
  72. } else {
  73. $SQLcmd = "$SQLcmd WHERE ";
  74. }
  75. $SQLcmd = "$SQLcmd calldate>'".$_GET['after']."'";
  76. }
  77. //$SQLcmd = do_field($SQLcmd, 'src', 'source');
  78. $SQLcmd = do_field($SQLcmd, 'dst', 'calledstation');
  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($FG_TABLE_CLAUSE) > 0) $FG_TABLE_CLAUSE .= " AND ";
  102. $FG_TABLE_CLAUSE .= "t1.id_tariffgroup = '$entertariffgroup'";
  103. }
  104. if (isset($enterratecard) && $enterratecard > 0) {
  105. if (strlen($FG_TABLE_CLAUSE) > 0) $FG_TABLE_CLAUSE .= " AND ";
  106. $FG_TABLE_CLAUSE .= "t1.id_ratecard = '$enterratecard'";
  107. }
  108. }
  109. $date_clause='';
  110. $min_call= intval($min_call);
  111. if (($min_call!=0) && ($min_call!=1)) $min_call=0;
  112. if (!isset($fromstatsday_sday)){
  113. $fromstatsday_sday = date("d");
  114. $fromstatsmonth_sday = date("Y-m");
  115. }
  116. if (!isset($days_compare) ){
  117. $days_compare=2;
  118. }
  119. list($myyear, $mymonth)= split ("-", $fromstatsmonth_sday);
  120. $mymonth = $mymonth +1;
  121. if ($current_mymonth==13) {
  122. $mymonth=1;
  123. $myyear = $myyear + 1;
  124. }
  125. if (isset($choose_agent) && ($choose_agent != '')) {
  126. switch ($choose_agent) {
  127. case 'all':
  128. $tmp_agent_clause = 't1.username IN (SELECT cc_card.username FROM cc_card, cc_agent_cards WHERE cc_card.id = cc_agent_cards.card_id)';
  129. break;
  130. case 'no':
  131. $tmp_agent_clause = 't1.username NOT IN (SELECT cc_card.username FROM cc_card, cc_agent_cards WHERE cc_card.id = cc_agent_cards.card_id)';
  132. break;
  133. default:
  134. $tmp_agent_clause = str_dbparams($DBHandle,'t1.username IN (SELECT cc_card.username FROM cc_card, cc_agent_cards WHERE cc_card.id = cc_agent_cards.card_id AND cc_agent_cards.agentid = %1)',
  135. array((integer)$choose_agent ));
  136. break;
  137. }
  138. }
  139. for ($i=0; $i<$months_compare+1; $i++){
  140. // creer un table legende
  141. $current_mymonth = $mymonth -$i;
  142. if ($current_mymonth<=0) {
  143. $current_mymonth=$current_mymonth+12;
  144. $minus_oneyar = 1;
  145. }
  146. $current_myyear = $myyear - $minus_oneyar;
  147. $current_mymonth2 = $mymonth -$i -1;
  148. if ($current_mymonth2<=0) {
  149. $current_mymonth2=$current_mymonth2+12;
  150. $minus_oneyar = 1;
  151. }
  152. $current_myyear2 = $myyear - $minus_oneyar;
  153. //echo "<br>$current_myyear-".sprintf("%02d",intval($current_mymonth));
  154. //echo '<br>'.$date_clause;
  155. if (DB_TYPE == "postgres"){
  156. $date_clause= " AND starttime >= '$current_myyear2-".sprintf("%02d",intval($current_mymonth2))."-01' AND starttime < '$current_myyear-".sprintf("%02d",intval($current_mymonth))."-01'";
  157. }else{
  158. $date_clause= " AND starttime >= '$current_myyear2-".sprintf("%02d",intval($current_mymonth2))."-01' AND starttime < '$current_myyear-".sprintf("%02d",intval($current_mymonth))."-01'";
  159. }
  160. if (strpos($SQLcmd, 'WHERE') > 0) {
  161. $FG_TABLE_CLAUSE = substr($SQLcmd,6).$date_clause;
  162. }elseif (strpos($date_clause, 'AND') > 0){
  163. $FG_TABLE_CLAUSE = substr($date_clause,5);
  164. }
  165. if (isset($tmp_agent_clause)){
  166. if (strlen($FG_TABLE_CLAUSE)>0) $FG_TABLE_CLAUSE.=" AND ";
  167. $FG_TABLE_CLAUSE.=$tmp_agent_clause;
  168. }
  169. if ($FG_DEBUG >= 3) echo $FG_TABLE_CLAUSE;
  170. $list_total = $instance_table_graph -> Get_list ($DBHandle, $FG_TABLE_CLAUSE, null, null, null, null, null, null);
  171. if ($graphtype==1){
  172. // Traffic
  173. $data[] = $list_total[0][0];
  174. $mylegend[] = $months[$current_mymonth2-1]." $current_myyear : ".intval($list_total[0][0]/60)." min";
  175. $title_graph = "Traffic Last $months_compare Months";
  176. }elseif($graphtype==2){
  177. // Profit
  178. $data[] = $list_total[0][1];
  179. $mylegend[] = $months[$current_mymonth2-1]." $current_myyear : ".number_format($list_total[0][1],3).' '.BASE_CURRENCY;
  180. $title_graph = "Profit Last $months_compare Months";
  181. }elseif($graphtype==3){
  182. // Sell
  183. $data[] = $list_total[0][2];
  184. $mylegend[] = $months[$current_mymonth2-1]." $current_myyear : ".number_format($list_total[0][2],3).' '.BASE_CURRENCY;
  185. $title_graph = "Sell Last $months_compare Months";
  186. }elseif($graphtype==4){
  187. // Buy
  188. $data[] = $list_total[0][3];
  189. $mylegend[] = $months[$current_mymonth2-1]." $current_myyear : ".number_format($list_total[0][3],3).' '.BASE_CURRENCY;
  190. $title_graph = "Buy Last $months_compare Months";
  191. }
  192. }
  193. //print_r($data);
  194. //print_r($mylegend);
  195. /**************************************/
  196. $data = array_reverse($data);
  197. //$data = array(40,60,21,33, 10, NULL);
  198. $graph = new PieGraph(475,200,"auto");
  199. $graph->SetShadow();
  200. $graph->title->Set($title_graph);
  201. $graph->title->SetFont(FF_FONT1,FS_BOLD);
  202. $p1 = new PiePlot3D($data);
  203. $p1->ExplodeSlice(2);
  204. $p1->SetCenter(0.35);
  205. //print_r($gDateLocale->GetShortMonth());
  206. //Array ( [0] => Jan [1] => Feb [2] => Mar [3] => Apr [4] => May [5] => Jun [6] => Jul [7] => Aug [8] => Sep [9] => Oct [10] => Nov [11] => Dec )
  207. //$p1->SetLegends($gDateLocale->GetShortMonth());
  208. $p1->SetLegends($mylegend);
  209. // Format the legend box
  210. $graph->legend->SetColor('navy');
  211. $graph->legend->SetFillColor('gray@0.8');
  212. $graph->legend->SetLineWeight(1);
  213. //$graph->legend->SetFont(FF_ARIAL,FS_BOLD,8);
  214. $graph->legend->SetShadow('gray@0.4',3);
  215. //$graph->legend->SetAbsPos(10,80,'right','bottom');
  216. $graph->Add($p1);
  217. $graph->Stroke();
  218. ?>