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

$conn = mysql_connect("localhost", "mybilling", "rbilling");

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

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


	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
		SUBSTRING(callstart, 1, 10) AS callDate,
		SUM(duration) AS totalDuration
		FROM calls WHERE call_status = 'ANSWER'  AND callstart BETWEEN '$callStartStr' AND '$callEndStr'
		GROUP BY CallDate ORDER BY callDate DESC";

		$resultTotal = mysql_query($sqlTotalCall);





	}



$day_time = mktime(0,0,0,date("m"),date("d")-6,date("Y"));
$callStart = date("Y-m-d", $day_time);
$callEnd = date('Y-m-d');


$callStartStr = $callStart." 00:00:00";
$callEndStr = $callEnd." 23:59:59";


// For Bangladesh //

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



$sqlBD = "SELECT
		SUBSTRING(callstart, 1, 10) AS callDate,
		SUM(duration) AS totalDuration
		FROM calls WHERE call_status = 'ANSWER' AND callednumber like '880%' AND callstart BETWEEN '$callStartStr' AND '$callEndStr'
		GROUP BY CallDate ORDER BY callDate DESC";

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

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

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

   }


}



// add random height bars:
///for( $i=0; $i<7; $i++ )
//$bar_bd->data[] = rand(2,9);


// End For Bangladesh //



// For India //

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



$sqlIN = "SELECT
		SUBSTRING(callstart, 1, 10) AS callDate,
		SUM(duration) AS totalDuration
		FROM calls WHERE call_status = 'ANSWER' AND callednumber like '91%' AND callstart BETWEEN '$callStartStr' AND '$callEndStr'
		GROUP BY CallDate ORDER BY callDate DESC";

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

   while ($rowIN = mysql_fetch_assoc($resultIN))
   {

           $bar_ind->data[] = floor($rowIN['totalDuration']/60);

   }


}





// add random height bars:
//for( $i=0; $i<7; $i++ )
//  $bar_ind->data[] = rand(5,9);


// End For India //


// For Pakisthan //


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


$sqlPK = "SELECT
		SUBSTRING(callstart, 1, 10) AS callDate,
		SUM(duration) AS totalDuration
		FROM calls WHERE call_status = 'ANSWER' AND callednumber like '92%' AND callstart BETWEEN '$callStartStr' AND '$callEndStr'
		GROUP BY CallDate ORDER BY callDate DESC";

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

   while ($rowPK = mysql_fetch_assoc($resultPK))
   {

           $bar_pk->data[] = floor($rowPK['totalDuration']/60);

   }


}




// add random height bars:
//for( $i=0; $i<7; $i++ )
 // $bar_pk->data[] = rand(5,9);

// End For Pakisthan //


// For Others //


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

/*
$sqlOthers = "SELECT
		SUBSTRING(callstart, 1, 10) AS callDate,
		SUM(duration) AS totalDuration
		FROM calls WHERE call_status = 'ANSWER' AND callednumber NOT LIKE '92%' OR NOT LIKE '880%' OR  NOT LIKE '91%' AND callstart BETWEEN '$callStartStr' AND '$callEndStr'
		GROUP BY CallDate ORDER BY callDate DESC";

$resultOthers = mysql_query($sqlOthers);
$rowNumOthers = mysql_num_rows($resultOthers);
if($rowNumOthers > 0)
{

   while ($rowOthers = mysql_fetch_assoc($resultOthers))
   {

           $bar_others->data[] = floor($rowOthers['totalDuration']/60);

   }


}
*/



// add random height bars:
//for( $i=0; $i<7; $i++ )
  //$bar_others->data[] = rand(5,9);

// End For Others //


// 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(22000); // Number of Calls
$g->y_label_steps( 10 );
$g->set_y_legend( 'Sucess Call Summary', 12, '#736AFF' );
echo $g->render();

?>