<?php
include_once('ofc-library/open-flash-chart.php' );
srand((double)microtime()*1000000);

$conn = mysql_connect("localhost", "root", "");

if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}

if (!mysql_select_db("bansbilling")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}


$bar_bd = new bar_3d( 75, '#000066' );
$bar_bd->key( 'Bangladesh', 10 );



$bar_ind = new bar_3d( 75, '#CC0000' );
$bar_ind->key( 'India', 10 );


$bar_pk = new bar_3d( 75, '#000000' );
$bar_pk->key( 'Pakisthan', 10 );


$bar_others = new bar_3d( 75, '#BE0000' );
$bar_others->key( 'Others', 10 );



	for ($i = 0; $i <= 6; $i++)
	{
	   $day_timeDate = mktime(0,0,0,date("m"),date("d")-$i,date("Y"));
	   $callStartDate = date("Y-m-d", $day_timeDate);

       $callDate[$i] = $callStartDate;

        $sqlTotalCall = "SELECT
		SUM(duration) AS totalDuration
		FROM calls WHERE call_status = 'ANSWER'  AND callstart LIKE '$callStartDate%'";

		$resultTotalCall = mysql_query($sqlTotalCall);
		$rowNumTotalCall = mysql_num_rows($resultTotalCall);
		if($rowNumTotalCall >0)
		{
		    while ($rowTotalCall = mysql_fetch_assoc($resultTotalCall))
		    {

                //$callDate[$i] = $callStartDate;

                $totalCallDuration = floor($rowTotalCall['totalDuration']/60);
                //$totalCallDurationArray[$i] = $totalCallDuration;
		    }

           //////// Start BD //////////////

		  $sqlBD = "SELECT
				    SUM(duration) AS totalDuration
				    FROM calls WHERE call_status = 'ANSWER' AND callednumber like '880%' AND callstart LIKE '$callStartDate%'";

		$resultBD = mysql_query($sqlBD);
		$rowNumBD = mysql_num_rows($resultBD);
		if($rowNumBD > 0)
		{

		   while ($rowBD = mysql_fetch_assoc($resultBD))
		   {

			$bdCallDuration  = floor($rowBD['totalDuration']/60);
			$bar_bd->data[] = $bdCallDuration;
			$totalCallDurationArrayBD[$i] = $bdCallDuration;

		   }
		}

        //////// END BD /////////////

        //////// Start India //////////////

		$sqlIN = "SELECT
				  SUM(duration) AS totalDuration
				  FROM calls WHERE call_status = 'ANSWER' AND callednumber like '91%' AND callstart LIKE '$callStartDate%'";

		$resultIN = mysql_query($sqlIN);
		$rowNumIN = mysql_num_rows($resultIN);
		if($rowNumIN > 0)
		{

  			while ($rowIN = mysql_fetch_assoc($resultIN))
   			{
   		        $indiaCallDuration = floor($rowIN['totalDuration']/60);;
   		        $bar_ind->data[] = $indiaCallDuration;
   		        $totalCallDurationArrayIN[$i] = $indiaCallDuration;
   			}
		}

        //////// End India //////////////

        //////// Start Pakisthan //////////////

		$sqlPK = "SELECT
					SUM(duration) AS totalDuration
					FROM calls WHERE call_status = 'ANSWER' AND callednumber like '92%' AND callstart LIKE '$callStartDate%'";

		$resultPK = mysql_query($sqlPK);
		$rowNumPK = mysql_num_rows($resultPK);
		if($rowNumPK > 0)
		{

   			while ($rowPK = mysql_fetch_assoc($resultPK))
   			{
           	   $pkCallDuration = floor($rowPK['totalDuration']/60);
           	   $bar_pk->data[] = $pkCallDuration;
           	   $totalCallDurationArrayPK[$i] = $pkCallDuration;
   			}


		}

        //////// End Pakisthan //////////////


        $totalCallOthers = $totalCallDuration - ($bdCallDuration + $indiaCallDuration + $pkCallDuration);
        $bar_others->data[] = $totalCallOthers;
        $totalCallDurationArrayOthers[$i] = $totalCallOthers;



		}
	}


$maxCallBD = max($totalCallDurationArrayBD);
$maxCallIN = max($totalCallDurationArrayIN);
$maxCallPK = max($totalCallDurationArrayPK);
$maxCallOthers = max($totalCallDurationArrayOthers);

$maxCallDuration = max($maxCallBD,$maxCallIN,$maxCallPK,$maxCallOthers) + 1000;


// create the graph object:
$g = new graph();
$g->title( 'Call Summary', '{font-size:20px; color: #0000; margin: 5px; padding:5px; padding-left: 20px; padding-right: 20px;}' );


$g->data_sets[] = $bar_bd;
$g->data_sets[] = $bar_ind;
$g->data_sets[] = $bar_pk;
$g->data_sets[] = $bar_others;

$g->set_x_axis_3d( 12 );
$g->x_axis_colour( '#909090', '#ADB5C7' );
$g->y_axis_colour( '#909090', '#ADB5C7' );

//$g->set_x_labels( array( 'January','February','March','April','May','June','July','August','September','October' ) );
$g->set_x_labels($callDate);
$g->set_y_max($maxCallDuration); // Number of Calls
$g->y_label_steps( 10 );
$g->set_y_legend( 'Sucess Call Summary', 12, '#736AFF' );
echo $g->render();

?>