 <?php 
 if (!isset($_SESSION)){
  session_start();
}
include('accesscheck2.php');
require_once 'Connections/courier.php';
require_once 'functions/functions.php';

if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
    }

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

    switch ($theType) {
        case "text":
        $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
        break;    
        case "long":
        case "int":
        $theValue = ($theValue != "") ? intval($theValue) : "NULL";
        break;
        case "double":
        $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
        break;
        case "date":
        $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
        break;
        case "defined":
        $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
        break;
    }
    return $theValue;
}
}

set_time_limit(90000); 
$action=$_POST["act"];
$searchby = $_POST['searchby'];
$searchtext = $_POST['searchtext'];

if($searchby == 'Patient No'){
    $search = mysql_query("SELECT * FROM patient_information WHERE (card_number = '$searchtext') OR (old_card_no = '$searchtext')", $courier);
    $totalstaff = mysql_num_rows($search);
}
else if($searchby == 'Surname'){
    $search = mysql_query("SELECT * FROM patient_information WHERE surname LIKE '$searchtext%'", $courier);
}
else if($searchby == 'Othernames'){
    $search = mysql_query("SELECT * FROM patient_information WHERE othernames LIKE '%$searchtext%'", $courier);
}
else if($searchby == 'Insurance No'){
    $search = mysql_query("SELECT * FROM patient_information WHERE (insurance_no1 LIKE '%$searchtext%' OR insurance_no2 LIKE '%$searchtext%')", $courier);
}

if($action!= 'sms_page'){
    $totalstaff = mysql_num_rows($search);
}

if ($action == 'sendsms') {
    $message1 = $_POST['msg'];
	//echo $message;
    $sms_permission = mysql_fetch_assoc(mysql_query("SELECT * FROM sms_details", $courier));
    if ($sms_permission['sms_permission'] == 'yes') {
        $i = 0;
        $i2 = 0;
        $sender = $sms_permission['sender_id'];
        $sms_temporal = mysql_query("SELECT *,DATE_FORMAT(dateofbirth, '%D %b %Y') as datenew, (DATE_FORMAT(current_date, '%Y') - DATE_FORMAT(dateofbirth, '%Y') - (DATE_FORMAT(current_date, '00-%m-%d') < DATE_FORMAT(dateofbirth, '00-%m-%d'))) AS age FROM sms_temporal", $courier);
        if (mysql_num_rows($sms_temporal) > 0) {
            while ($row = mysql_fetch_assoc($sms_temporal)) {
               if ($row['type'] == 'individual') {
                   $message = $message1;
                   $message = str_replace("[First name]", $row['othernames'], $message);
                   $message = str_replace("[Last name]", $row['surname'], $message);
                   $message = str_replace("[Full name]", ($row['surname']." ".$row['othernames']), $message);
                   $message = str_replace("[Patient No]", $row['card_number'], $message);
                   $message = str_replace("[Age]", ordinal($row['age']), $message);
                   $phone = substr($row['tel'], 0, 10);
					//echo $message;
					//exit;
                   if(SMS_API_RETURN($phone, $message, $sender)){
                    $i++;
                }else{$i2++;}
            } 
            
            else if ($row['type'] == 'all_patients') {
                $patients = mysql_query("SELECT *,DATE_FORMAT(dateofbirth, '%D %b %Y') as datenew, (DATE_FORMAT(current_date, '%Y') - DATE_FORMAT(dateofbirth, '%Y') - (DATE_FORMAT(current_date, '00-%m-%d') < DATE_FORMAT(dateofbirth, '00-%m-%d'))) AS age FROM patient_information where type_of_client<>'staff'", $courier);
                while ($row2 = mysql_fetch_assoc($patients)) {
                   $message = $message1;
                   $message = str_replace("[First name]", $row2['othernames'], $message);
                   $message = str_replace("[Last name]", $row2['surname'], $message);
                   $message = str_replace("[Full name]", ($row2['surname']." ".$row2['othernames']), $message);
                   $message = str_replace("[Patient No]", $row2['card_number'], $message);
                   $message = str_replace("[Age]", ordinal($row2['age']), $message);
                   $phone = substr($row2['tel'], 0, 10);
                   if(SMS_API_RETURN($phone, $message, $sender)){
                    $i++;
                }else{$i2++;}
            }
        }

        else if ($row['type'] == 'all_staff') {
            $patients = mysql_query("SELECT *,DATE_FORMAT(dateofbirth, '%D %b %Y') as datenew, (DATE_FORMAT(current_date, '%Y') - DATE_FORMAT(dateofbirth, '%Y') - (DATE_FORMAT(current_date, '00-%m-%d') < DATE_FORMAT(dateofbirth, '00-%m-%d'))) AS age FROM patient_information where type_of_client='staff'", $courier);
            while ($row2 = mysql_fetch_assoc($patients)) {
               $message = $message1;
               $message = str_replace("[First name]", $row2['othernames'], $message);
               $message = str_replace("[Last name]", $row2['surname'], $message);
               $message = str_replace("[Full name]", ($row2['surname']." ".$row2['othernames']), $message);
               $message = str_replace("[Patient No]", $row2['card_number'], $message);
               $message = str_replace("[Age]", ordinal($row2['age']), $message);
               if(SMS_API_RETURN($phone, $message, $sender)){
                $i++;
            }else{$i2++;}
        }
    }
    
    else if ($row['type'] == 'all_males') {
        $patients = mysql_query("SELECT *,DATE_FORMAT(dateofbirth, '%D %b %Y') as datenew, (DATE_FORMAT(current_date, '%Y') - DATE_FORMAT(dateofbirth, '%Y') - (DATE_FORMAT(current_date, '00-%m-%d') < DATE_FORMAT(dateofbirth, '00-%m-%d'))) AS age FROM patient_information where sex='Male' and type_of_client<>'staff'", $courier);
        while ($row2 = mysql_fetch_assoc($patients)) {
           $message = $message1;
           $message = str_replace("[First name]", $row2['othernames'], $message);
           $message = str_replace("[Last name]", $row2['surname'], $message);
           $message = str_replace("[Full name]", ($row2['surname']." ".$row2['othernames']), $message);
           $message = str_replace("[Patient No]", $row2['card_number'], $message);
           $message = str_replace("[Age]", ordinal($row2['age']), $message);
           $phone = substr($row2['tel'], 0, 10);
           if(SMS_API_RETURN($phone, $message, $sender)){
            $i++;
        }else{$i2++;}
    }
}

else if ($row['type'] == 'all_females') {
    $patients = mysql_query("SELECT *,DATE_FORMAT(dateofbirth, '%D %b %Y') as datenew, (DATE_FORMAT(current_date, '%Y') - DATE_FORMAT(dateofbirth, '%Y') - (DATE_FORMAT(current_date, '00-%m-%d') < DATE_FORMAT(dateofbirth, '00-%m-%d'))) AS age FROM patient_information where sex='Female' and type_of_client<>'staff'", $courier);
    while ($row2 = mysql_fetch_assoc($patients)) {
       $message = $message1;
       $message = str_replace("[First name]", $row2['othernames'], $message);
       $message = str_replace("[Last name]", $row2['surname'], $message);
       $message = str_replace("[Full name]", ($row2['surname']." ".$row2['othernames']), $message);
       $message = str_replace("[Patient No]", $row2['card_number'], $message);
       $message = str_replace("[Age]", ordinal($row2['age']), $message);
       $phone = substr($row2['tel'], 0, 10);
       if(SMS_API_RETURN($phone, $message, $sender)){
        $i++;
    }else{$i2++;}
}
}

else if ($row['type'] == 'all_birthday_patients') {
					// $message = $message1;
  $birthday=gmdate('d');
  $birthmonth=gmdate('m');
  $patients = mysql_query("SELECT *,DATE_FORMAT(dateofbirth, '%D %b %Y') as datenew, (DATE_FORMAT(current_date, '%Y') - DATE_FORMAT(dateofbirth, '%Y') - (DATE_FORMAT(current_date, '00-%m-%d') < DATE_FORMAT(dateofbirth, '00-%m-%d'))) AS age  FROM patient_information where MONTH(dateofbirth)=$birthmonth and DAY(dateofbirth)=$birthday", $courier);
  while ($row2 = mysql_fetch_assoc($patients)) {	
   $message = $message1;
   $message = str_replace("[First name]", $row2['othernames'], $message);
   $message = str_replace("[Last name]", $row2['surname'], $message);
   $message = str_replace("[Full name]", ($row2['surname']." ".$row2['othernames']), $message);
   $message = str_replace("[Patient No]", $row2['card_number'], $message);
   $message = str_replace("[Age]", ordinal($row2['age']), $message);
   $phone = substr($row2['tel'], 0, 10);
   if(SMS_API_RETURN($phone, $message, $sender)){
    $i++;
}else{$i2++;}
}
}
}
mysql_query("TRUNCATE TABLE sms_temporal", $courier);   
}
}
}

$query_sms_templates = mysql_query("SELECT * FROM sms_templates", $courier);

?>  
<style type="text/css">
   table tbody tr:hover {
    cursor: pointer;
}
.form-control-mine{
    color: #333333;
    background-color: #ffffff;
    border: 1px solid #e5e5e5;
    padding: 3px 8px;
    font-size: 14px;}
</style>

<div class="page-bar">
    <ul class="page-breadcrumb">
        <li>
            <i class="icon-home"></i>
            <a href="index.html">Home</a>
            <i class="fa fa-angle-right"></i>
        </li>
        <li>
            <span>Dashboard</span>
        </li>
    </ul>
    <div class="page-toolbar">
        <div class="btn-group pull-right">
            <button type="button" class="btn btn-fit-height grey-salt dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="1000" data-close-others="true"> Actions
                <i class="fa fa-angle-down"></i>
            </button>
            <ul class="dropdown-menu pull-right" role="menu">
                <li onclick="getstep('sms_page');">
                    <a href="#">
                        <i class="icon-refresh"></i> Refresh</a>
                    </li>
                    <li onclick="getstep('sms_templates');">
                        <a href="#">
                            <i class="icon-envelope-alt"></i> SMS Template</a>
                        </li>

                    </ul>
                </div>
            </div>
        </div>
        <br><br>



        <form name="form" style="" onsubmit="send_sms('sendsms');return false;">
            <div class="row" style="margin-top: -20px">
                <div class="col-md-12">
                 <div class="portlet blue box">
                     <div class="portlet-title" style="background-color: #32c5d2 !important">
                       <div class="caption"><i class="icon-envelope"></i>Notify Patients & Anyone through SMS</div>
                       <div class="tools">
                        <a href="javascript:;" class="collapse"></a>
                        <a href="javascript:;" class="reload"></a>
                    </div>
                </div>
                <div class="portlet-body">
                   <?php if($action=='sendsms'){ ?>
                       <div style="width: 100%;">
                           <div class="note note-success alert-dismissable" style="width: 50%;float: left">
                               <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
                               <i style="color: green;font-size: 20px" class="icon-thumbs-up"></i> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b style="font-size: 20px"><?php echo $i;?></b>&nbsp; SMS Sent to contacts
                           </div>
                           <div class="note note-danger alert-dismissable" style="width: 50%;float: right">
                               <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
                               <i style="color: red;font-size: 20px" class="icon-thumbs-down"></i> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b style="font-size: 20px;color: red"><?php echo $i2;?></b> &nbsp; SMS Pending
                           </div>
                       </div>
                   <?php } ?>
                   <table class="table table-striped table-bordered table-hover table-responsive">
                       <tbody>
                           <tr>
                               <td style="width:15%">Send To</td>
                               <td>
                                   <select id="search_send_to" class="form-control" style="width:100%" onchange="SMSSendToOnChange();">
                                       <option value="">Please Select</option>
                                       <option value="PATIENT">Send to all Patients</option>
                                       <option value="MALES">Send to all Males</option>
                                       <option value="FEMALES">Send to all Females</option>
                                       <option value="STAFF">Send to all Staff</option>                                     
                                       <option value="BIRTHDAY">Send to Patients Celebrating their Birthday</option>
                                       <option value="SEARCH">Search for Patients</option>
                                       <option value="PHONE">Enter Phone Number</option>
                                   </select>
                                   
                                   <div class="hideall" id="search_form" style="padding-top: 5px;display: none">
                                       <select class="form-control-mine" name="searchby" style="width:200px">
                                           <option value="Patient No" <?php if($searchby == 'Patient No'){echo 'selected="selected"';}?>>Patient No / Old Card No</option>
                                           <option value="Surname" <?php if($searchby == 'Surname'){echo 'selected="selected"';}?>>Surname</option>
                                           <option value="Othernames" <?php if($searchby == 'Othernames'){echo 'selected="selected"';}?>>Othernames</option>
                                           <option value="Insurance No" <?php if($searchby == 'Insurance No'){echo 'selected="selected"';}?>>Insurance No</option>
                                       </select>
                                       <input style="padding-top: 4px" type="text" name="searchtext" class="form-control-mine" value="<?php echo $searchtext;?>">   
                                       <button style="padding:5px 7px 5px 7px;margin-top: -4px" type="submit" class="btn green" onclick="getstep('searchforsms');return false;">
                                           Search
                                       </button> 
                                   </div> 
                                   <div id="tel_number_wrapper" style="display: none;">
                                       <input onblur="process_information_sms('sms/sms_step.php', 'addtosmstel', 'receiverswrapper');" name="tel" type="text" class="form-control" style="margin-top: 2px;" placeholder="Eg: 0543023456, 0201245654, 0268987460">
                                   </div>
                                   <?php if($action == "searchforsms"){?>
                                       <div class="hideall" id="searchresult" style="height: 220px;overflow-y: scroll;margin-top:5px">
                                           <table class="table table-striped table-bordered table-advance table-hover">
                                               <thead>
                                                   <tr>
                                                       <th width='1%' style="background-color: #428bca;color: #fff;">&nbsp;</th>
                                                       <th style="background-color: #428bca;color: #fff;">Patient No</th>
                                                       <th style="background-color: #428bca;color: #fff;">Patient Name</th>
                                                       <th style="background-color: #428bca;color: #fff;">Phone No.</th>
                                                       <th style="background-color: #428bca;color: #fff;">Sex</th>
                                                       <th style="background-color: #428bca;color: #fff;text-align: center">Action</th>
                                                   </tr>
                                               </thead>
                                               <tbody>
                                                  <?php $i=1; 
                                                  if( $totalstaff > 0){
                                                      while($row = mysql_fetch_assoc($search)){
                                                          $fullname = $row['surname'].' '.$row['othernames'];?>
                                                          <tr>
                                                           <td><?php echo $i;?></td>
                                                           <td><?php echo $row['card_number'];?></td>
                                                           <td><?php echo $fullname;?></td>
                                                           <td><?php echo $row['tel'];?></td>
                                                           <td><?php echo $row['sex'];?></td>
                                                           <td style="text-align: center;" onclick="process_information_sms('sms/sms_step.php', 'addtosms', 'receiverswrapper', '<?php echo $row['surname'].'@@@'.$row['othernames']."@@@".$row['tel']."@@@".$row['card_number']."@@@".$row['dateofbirth'];?>');"><a style="color: #35aa47;font-weight: bold"><i class="icon-plus-sign-alt"></i></a></td>
                                                       </tr>   
                                                       <?php $i++;
                                                   }
                                               }else{echo '<tr><td style="text-align:center" colspan="6">Sorry,No Patient was Found for your search<td></tr>';}
                                               ?>   
                                           </tbody>
                                       </table> 
                                   </div>
                               <?php }?>
                               <div id="receiverswrapper" style="padding-top: 6px">
                                <?php $query = mysql_query("SELECT * FROM sms_temporal", $courier);
                                while ($row = mysql_fetch_assoc($query)) {
                                    ?> 
                                    <span  id="sms_<?php echo $row['id']; ?>"  class="badge badge-roundless badge-success"><?php
                                    if ($row['patient_name'] != "") {
                                        echo strtoupper($row['patient_name']);
                                    } else {
                                        echo $row['tel'];
                                    }
                                    ?>  <b id="sms<?php echo $row['id']; ?>" style="" onclick="process_information_sms('sms/sms_step.php', 'deletesmsreceiver', 'receiverswrapper','<?php echo $row['id']; ?>');">&times;</b></span> 
                                <?php }
                                if (mysql_num_rows($query) > 1) { ?> 
                                    <span onclick="process_information_sms('sms/sms_step.php', 'deletesmsall', 'receiverswrapper');"  class="badge badge-roundless badge-danger">Delete All  <b >&times;</b></span> 
                                    <?php } ?> </div>
                                </td>
                            </tr>
                            <tr>
                               <td>SMS Template</td>
                               <td>
                                  <select id="search_send_to" class="form-control" style="width:100%" onchange="sms_template(this.value);">
                                   <option value="">No SMS Template</option>
                                   <?php
                                   while($row = mysql_fetch_assoc($query_sms_templates)){ ?>
                                       <option value="<?php echo $row['title'].'@@@'.$row['sms_template'];?>"><?php echo $row['title']; ?></option>
                                   <?php } ?>
                               </select>
                           </td>
                       </tr>
                       <tr>
                           <td>Message</td>
                           <td>
                               <b onclick="addtosms_replace('[First name]')"> [First name] </b> --
                               <b onclick="addtosms_replace('[Last name]')"> [Last name] </b> --
                               <b onclick="addtosms_replace('[Full name]')"> [Full name] </b> -- 
                               <b onclick="addtosms_replace('[Patient No]')"> [Patient No] </b> -- 
                               <b onclick="addtosms_replace('[Age]')"> [Age] </b>
                               <br><br>
                               <textarea required name="msg" class="form-control" style="height:150px;resize: none;" id="msg" onkeyup="SMSCounter(this.value.length)" onkeypress="SMSCounter(this.value.length)"></textarea>
                               <p>
                                   <span id="remaining" style="font-weight: bold;color: deepskyblue" >160 characters remaining</span>
                                   <span id="" style="font-weight: bold;color:#000" > || </span>
                                   <span id="messages" style="font-weight: bold;color: deepskyblue" >1 message(s)</span>
                                   <input type="hidden" value="" id="nosms" name="nosms">
                                   <span id="" style="font-weight: bold;color: deepskyblue;float: right" >160 / per sms</span>
                               </p>
                           </td>
                       </tr>
                       <tr>
                           <td></td>
                           <td>
                               <button type="submit" class="btn green">
                                   Send SMS<i class="icon-ok"></i>
                               </button>            
                           </td>
                       </tr>
                   </tbody>
               </table> 
           </div>
       </div>
   </div>
</div>
</form>
