Header('Cache-Control: no-cache');
Header('Pragma: no-cache');
//require("../authen.php");
$db_name = "a2billing";
set_time_limit(99000);
//Change the 3 follow lines for database setup.
$db_host = "172.16.130.212:3307";
//$db_host = "172.16.129.233";
$db_user = "report";
$db_pass = "xreport2008";
$to_email2="canat@jasmine.com";
$db_server = "172.16.130.212";
$last_line = system('ping -c1 -w1 '.$db_server.' |grep 64', $retval);
if( $retval <> 0 ) {
$db_host = "172.16.128.233";
}
//$to_email3="jparinya@jasmine.com";
function fnc_date_calc($this_date,$num_days){
$my_time = strtotime ($this_date); //converts date string to UNIX timestamp
$timestamp = $my_time + ($num_days * 86400); //calculates # of days passed ($num_days) * # seconds in a day (86400)
$return_date = date("Y-m-d",$timestamp); //puts the UNIX timestamp back into string format
return $return_date;//exit function and return string
}//end of function
function isValidDateTime($dateTime)
{
if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $dateTime, $matches)) {
if (checkdate($matches[2], $matches[3], $matches[1])) {
return true;
}
}
return false;
}
$date_today = Date("Y-m-d");
$days_to_add = 1;
$report_data_date = fnc_date_calc($date_today,(($days_to_add)*-1));
/*
if(!empty($_GET["startdate"])){
$startdate = date($_GET["startdate"]);
} else{
// $startdate = $date_today;
$startdate = $report_data_date;
}
if(!empty($_GET["to_date"])){
$to_date = $_GET["to_date"];
}else{
$to_date = $date_today;
}
if(!empty($_GET["starttime"])){
$starttime = $_GET["starttime"];
} else{
$starttime = "00:00:00";
}
if(!empty($_GET["totime"])){
$totime = $_GET["totime"];
}else{
$totime = "23:59:59";
}
$_SESSION['startdate'] = $startdate;
$_SESSION['todate'] = $to_date;
$startdate = $startdate." ".$starttime;
$to_date = $to_date." ".$totime;
if(!isValidDateTime($startdate)){
die("Invalid date format");
}
if(!isValidDateTime($to_date)){
die("Invalid date format");
}
*/
//HTML version of message
$body .= "--$boundary\r\n" .
"Content-Type: text/html; charset=tis-620\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
//$body .= "--$boundary\r\n" .
// "Content-Type: text/html; charset=tis620\r\n" ;
$body_msg = '
Airtime Report
SBD+ Terminal report
';
/*
*/
if(!empty($_GET["phone_id"])){
$phone_id = $_GET["phone_id"];
} else{
$phone_id = "2000";
}
if(!empty($_GET["orderby"])){
$orderbystr = $_GET["orderby"];
} else{
$orderbystr = " phone_id ";
}
$link = mysql_connect($db_host,$db_user,$db_pass) ;
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_name);
// Sabaydee+ Terminals Activation report
//$db_name = "a2billing";
//$db_host = "172.16.130.212:3307";
//$db_host = "172.16.129.233";
//$link = mysql_connect($db_host,$db_user,$db_pass) ;
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_name);
//Check Active terminals make calls Yesterday
$sql = "select count(*) as actnum from usage_minutedesc where active='Act' and terminal <> '2000172' ";
$s = mysql_query($sql);
if ($s == 0)
{
die('Failed to query data to database:
'."\n".mysql_error());
}
$rowcount_phoneid_act = 0;
while ($row = mysql_fetch_assoc($s)) {
$rowcount_phoneid_act = $row['actnum'];
}
/*
$act_phoneid = array();
$act_calls = array();
$act_minutes = array();
$act_flag = array();
$act_sbdacct = array();
$act_phoneid[] = 0;
$act_calls[] = 0;
$act_minutes[] = 0;
$act_flag[] = 0;
$act_sbdacct[] = 0;
$rowcount_phoneid = mysql_num_rows ( $s );
$rowcount_phoneid_act = 0;
$i=0;
while ($row = mysql_fetch_assoc($s)) {
$i++;
$act_phoneid[$i] = $row['terminal'];
$act_calls[$i] = $row['calls'];
$act_minutes[$i] = $row['minutes'];
$act_flag[$i] = $row['active'];
$act_sbdacct[$i] = $row['sbdacct'];
if ($act_flag[$i] == 'Act') $rowcount_phoneid_act++;
}
*/
$sql = "SET NAMES TIS620";
$s = mysql_query($sql);
if ($s == 0)
{
die('Failed to query data to database:
'."\n".mysql_error());
}
$sql = "SELECT firstusedate, phone_id, ship_name, ship_owner, checkout_status, sbdacct, calls,minutes,if(active='Act','Yes','') as makecall , last_location,
(select count(*) from usage_minutedesc where active='Act') as Rank,
(select aces_location from m_loc_map b where b.last_location = a.last_location) AS InACeS , (select simtype from sbdplus_iccid c where c.phone_id=a.phone_id) AS simtype
FROM sbdplus_map_ship a LEFT JOIN usage_minutedesc b ON phone_id=terminal
WHERE firstusedate <> '' AND checkout_status <> 'RESERVED'
ORDER BY ".$orderbystr;
$s = mysql_query($sql);
if ($s == 0)
{
die('Failed to query data to database:
'."\n".mysql_error());
}
$fields = mysql_num_fields ( $s );
$rowcount = mysql_num_rows ( $s );
$body_msg .= '
Sabaydee Plus Terminals Activation report ( '.$rowcount_phoneid_act.' made call(s) yesterday of '.$rowcount.' terminals as of '.$date_today.' at 00:05AM and 3 months back Usage)
| '.mysql_field_name($s, 0).' | ';
for($i=1;$i<$fields;$i++){
if( $i==10)
$body_msg .=''.mysql_field_name($s, $i).' | ';
else
$body_msg .=''.mysql_field_name($s, $i).' | ';
}
$sum_column = array();
$sum_column[] = 0;
$sum_inaces = 0; //no. of terminals in ACeS Coverage
$irow = 0;
while ($row = mysql_fetch_assoc($s)) {
$body_msg .='
';
$irow++;
$ireset = 0;
/*
$act_check = array();
$act_check[] = 0;
$sum_sbdacct = array();
$sum_sbdacct[] = 0;
$sum_calls = array();
$sum_calls[] = 0;
$sum_minutes = array();
$sum_minutes[] = 0;
*/
foreach( $row as $value )
{
$makecall = $row['makecall'];
$phone_id = $row['phone_id'];
$sbdacct = $row['sbdacct'];
/*
$act_check[$irow] = 0;
if($ireset == 1) {
$i=0;
$phone_id=$value;
while ($act_check[$irow]== 0 && $i < $rowcount_phoneid ) {
$i++;
if($value == $act_phoneid[$i]){
if($act_flag[$i] == 'Act') $act_check[$irow] = 1;
$sum_sbdacct[$irow] = $act_sbdacct[$i];
$sum_calls[$irow] = $act_calls[$i];
$sum_minutes[$irow] = $act_minutes[$i];
// echo $rowcount_phoneid," ",$i," ",$irow," ",$act_check[$irow]," ",$act_calls[$i]," ",$act_minutes[$i]," ",";";
}
}
}
*/
// echo $rowcount_phoneid," ",$i," ",$act_check,";";
if($ireset == 3 && $value == ''){
$nvalue = 'N/A'; // Rank Desc
}else if($ireset == 5){
// $nvalue = $rowcount+1-$irow; // Rank Desc
$nvalue = number_format($value,0); // total distinct sbdacct 3 months back until yesterday
}else if($ireset == 6 ){ // calls
$nvalue = number_format($value,0); // total calls 3 months back until yesterday
}else if($ireset == 7 ){ //minutes
$nvalue = number_format($value,2); // total minutes 3 months back until yesterday
}else if($ireset == 10 ){ //minutes
// $nvalue = $rowcount+1-$irow; // Rank Desc
$nvalue = $irow; // Rank Desc
}else if($ireset == 11 && ($value == 'Y' || $value == '') ){ //IN ACeS=Y
$sum_inaces++; // count if in ACeS
if($value == '') $value = 'Y';
$nvalue = $value;
}else{
$nvalue = $value;
}
if(($irow%2)==0 ){
$body_msg .='| ';
if($nvalue == 'SOLD' | $makecall == 'Yes' ){
if( $makecall == 'Yes') $body_msg .= ' ';
else $body_msg .= ' ';
} else if($nvalue == 'N/A'){
$body_msg .= ' ';
} else if($nvalue == 'RENT') $body_msg .= ' ';
if($ireset == 9) { //Deep to location_hist
if($nvalue == '') $nvalue = 'Thailand';
$body_msg .= ' '.$nvalue.' ';
} else if($ireset == 11 && $nvalue=='N') { //IN ACeS Coverage
$body_msg .= ' '.$nvalue;
} else
if($ireset == 1) { //Deep to phone_id
$body_msg .= ' '.$nvalue.' ';
} else
$body_msg .= $nvalue.' | ';
}else{
$body_msg .=' ';
if($nvalue == 'SOLD' | $makecall == 'Yes' ){
if( $makecall == 'Yes') $body_msg .= ' ';
else $body_msg .= ' ';
} else if($nvalue == 'N/A'){
$body_msg .= ' ';
} else if($nvalue == 'RENT') $body_msg .= ' ';
if($ireset == 9) {//Deep to location_hist
$body_msg .= ' '.$nvalue.' ';
} else if($ireset == 11 && $nvalue=='N') { //IN ACeS Coverage
$body_msg .= ' '.$nvalue;
} else
if($ireset == 1) { //Deep to phone_id
$body_msg .= ' '.$nvalue.' ';
} else
$body_msg .= $nvalue.' | ';
}
$ireset++;
}
// $ireset=0;
} //while
$body_msg .= "
";
// mysql_close($link);
/*
// Sum value
$sum_column[0] = "Total";
for($i=0;$i<$fields;$i++){
if(($i >1) && ($i < 1)){
// if(($i ==1) || ($i == 2)|| ($i == 4)|| ($i == 5)|| ($i == 7)){
$body_msg .='| '.number_format($sum_column[$i],2).' | ';
}else{
if($i==0){
$body_msg .=''.$sum_column[0].' | ';
} else {
$body_msg .=' | ';
}
}
}
*/
$body_msg .='
';
//$body_msg .=' | '.mysql_field_name($s, 0).' | ';
// for($i=1;$i<$fields;$i++){
// $body_msg .=''.mysql_field_name($s, $i).' | ';
//
// };
$body_msg .='
';
///Summary by Location
$sql = "select '' as Total,sum(if(last_location like '%Thailand%',1,0)) AS InThailand,
sum(if(last_location not like '%Thailand%',1,0)) AS OutThailand,
'' as '%InThai' , '' as '%OutThai', '' as 'InACeS' , '' as 'OutACeS', '' as '%InACeS' , '' as '%OutACeS'
from sbdplus_map_ship a
WHERE firstusedate <> '' AND checkout_status <> 'RESERVED' and track_status_id=3
";
//where track_status_id=3 and phone_id <> 2299
$s = mysql_query($sql);
if ($s == 0)
{
die('Failed to query data to database:
'."\n".mysql_error());
}
$fields = mysql_num_fields ( $s );
$rowcount = mysql_num_rows ( $s );
$body_msg .= '
Sabaydee Plus Terminals summary by Location
';
for($i=0;$i<$fields;$i++){
if ($i < 5)
$body_msg .='| '.mysql_field_name($s, $i).' | ';
else $body_msg .=''.mysql_field_name($s, $i).' | ';
}
$sum_column = array();
$sum_column[] = 0;
$irow = 0;
while ($row = mysql_fetch_assoc($s)) {
$body_msg .='
';
$irow++;
$ireset = 0;
foreach( $row as $value )
{
$nvalue = $value;
$sum_column[$ireset] += $nvalue;
$ireset++;
}
} //while
$body_msg .= "
";
// Sum value
$sum_column[0] = $sum_column[1]+$sum_column[2];
$sum_column[3] = round($sum_column[1]/($sum_column[1]+$sum_column[2])*100,2); //%Thailand
$sum_column[4] = round($sum_column[2]/($sum_column[1]+$sum_column[2])*100,2); //%OutThailand
$sum_column[5] = $sum_inaces; //InACeS
$sum_column[6] = $sum_column[0]-$sum_inaces; //OutACeS
$sum_column[7] = round($sum_inaces/$sum_column[0]*100,2); //%InACeS
$sum_column[8] = round($sum_column[6]/$sum_column[0]*100,2); //%OutACeS
for($i=0;$i<$fields;$i++){
if ($i < 5)
$body_msg .='| '.$sum_column[$i].' | ';
else $body_msg .=''.$sum_column[$i].' | ';
}
$body_msg .='
';
$body_msg .='
';
$body_msg .='';
$body .= chunk_split(base64_encode($body_msg));
mysql_close($link);
echo $body_msg;
?>