<?php
include_once('ivrconfig.php');
include_once('ivrutils.php');
include_once('config.php');
require "Services/Twilio.php";

print $_REQUEST['To'];
print $_REQUEST['From'];
print $_REQUEST['Body'] ;
$myFile =$smsdir ."/". $_REQUEST['SmsSid'];
echo $myFile;
if ( !file_exists($smsdir) ) {
  mkdir ($smsdir, 0755);
 } 
 
 $storename=get_storeinfo($phone_arr,$_REQUEST['To'],'name');
 $storecall=get_storeinfo($phone_arr,$_REQUEST['To'],'storephone');
 $storephn=get_storeinfo($phone_arr,$_REQUEST['To'],'storeshortform');
// $tacct=get_storeinfo($phone_arr,$_REQUEST['To'],'account');
// $tauth=get_storeinfo($phone_arr,$_REQUEST['To'],'auth');
 $tacct=$account;
 $tauth=$auth;






 $validreq=0;   
$newbody=strtolower($_REQUEST['Body']); 

$i=strpos($newbody,'refill') ;        

if ($i!==false) {
    if ($i>=0){

        $validreq=1;
        $newbody=str_replace('refill',' ',$newbody);   
        $newbody=str_replace(',',' ',$newbody);   
        $newbody=str_replace('/',' ',$newbody);   
        $newbody=str_replace('\\',' ',$newbody);
        $newbody=str_replace("\n",' ',$newbody);


        $newbody=trim($newbody);
        $rxnumstr=str_replace(' ',',',$newbody);

        echo $rxnumstr ;
        $patphone=$_REQUEST['From'];
        if(strlen($patphone)==11) {
           $patphone=substr($patphone,1);    
        } 
        if(strlen($patphone)==12) {
           $patphone=substr($patphone,2);    
        } 
        
        //$phone_arr['14155992671']['storeshortform'];
        echo $storephn;
        $template = @new orderrefillsbynumber_page("template_orderbynumber.html", $rxnumstr, $patphone,"TEXT ORDER",$storephn);

        if(!isset($template->error))
        {
            $scmsg="";
            $flmsg="";
            foreach ($template->statarray as $statvalue) {
                $tmpmessage=strtolower($statvalue->message);
                $j=strpos($tmpmessage,"succ") ;
              
                if($statvalue->status=='Ordered') {
                     if ($j && $j>0) {
                        $scmsg=$scmsg.$statvalue->rxnumber."\n";
                     } else {
                       $msg=$storename."\n".'Order Successful'."\n"."Rx:" .$statvalue->rxnumber."\n"."Reason:".$statvalue->message."\n" ;
                       sendsms($tacct,$tauth,$_REQUEST['To'],$_REQUEST['From'],$msg);
                     }   
                 }
            }
            if(strlen($scmsg)>0) {
               $msg=$storename." has received your order for Rx#"."\n".$scmsg; 
               sendsms($tacct,$tauth,$_REQUEST['To'],$_REQUEST['From'],$msg);
 
            }
            foreach ($template->statarray as $statvalue) {
                
                if ($statvalue->status<>'Ordered') {
                  $msg='Order Failed'."\n"."Rx:" .$statvalue->rxnumber."\n"."Reason:".$statvalue->message."\n".$storename."\n"."Phone:".$storecall ;
                  $msg2="";
		  if (strlen($msg)>160) {
		    $msg2='Continued-'.substr($msg,160);
                    $msg=substr($msg,0,160);
		  }
                  sendsms($tacct,$tauth,$_REQUEST['To'],$_REQUEST['From'],$msg);
		  if($msg2<>'') {
		    sendsms($tacct,$tauth,$_REQUEST['To'],$_REQUEST['From'],$msg2);

		  }				
                }
                
            }
        } 
        else {
                  $msg="Problems Ordering "."\n".$storename."\n"."Phone:".$storecall;
                  sendsms($tacct,$tauth,$_REQUEST['To'],$_REQUEST['From'],$msg);
        }
    }
}
   
$i=strpos($newbody,'moreinfo') ;        
if ($i!==false) { 
   $validreq=1;
    if($i>=0){
                 //$msg="To Order Rxs text the word refill and then the rxnumbers separated by a space or enter to \n Example refill 123456 123457".$storephn."\n"."Call the store Phone:".$storecall;
                 $msg="1: Give your pharmacy your mobile phone number." . "\n" ;
                 $msg=$msg . "2: Text the word refill and then the rxnumbers separated by  space or comma. Example refill 123456 123457". "\n";
                 sendsms($tacct,$tauth,$_REQUEST['To'],$_REQUEST['From'],$msg);
    }         
}

if($validreq==0)  {
    $msg="Your text does not have the required format. Please text the word moreinfo for instructions";
    sendsms($tacct,$tauth,$_REQUEST['To'],$_REQUEST['From'],$msg);
 }
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $_REQUEST['To'].$_REQUEST['From'].$_REQUEST['Body'];
fwrite($fh, $stringData);
fwrite($fh, $storename);
fwrite($fh, $storecall);
fwrite($fh, $storephn);
fwrite($fh, $tacct);
fwrite($fh, $tauth);
fwrite($fh, $newbody);
fwrite($fh, $rxnumstr);
fwrite($fh, "before cal".$rxnumstr.$patphone.$storephn);
fclose($fh);
?>





