<?php
include_once('ivrconfig.php');
include_once('config.php');
include_once('ivrutils.php');
require('twilio-php/Services/Twilio.php');
$voice=array('voice'=>'woman');
$pound='followed by the number sign';

$baseurl=get_storeinfo($phone_arr,$_REQUEST['To'],'baseurl');                                                            
switch ($_REQUEST['type']) {
    case "greeting":
     $response = new Services_Twilio_Twiml();
     $response->pause("1");
     $response-> say (get_storeinfo($phone_arr,$_REQUEST['To'],'store_greeting'),$voice) ;
        $ajayarr =array(
                        'numDigits'=>10,
                        'action'=>$baseurl . '?type=phone',
                        'method'=>'GET',
			'timeout'=>30
                        );
       $gather=$response-> gather ($ajayarr);
       $gather->say("Please enter your ten digit phone number. " . $pound,$voice) ;
       $response->redirect($baseurl . '?type=phone');
       print $response;     
       break;





  
    case "phone":

//        echo $_REQUEST['Digits'];
        if (strlen($_REQUEST['Digits'])==10) {
           $response = new Services_Twilio_Twiml();
           $_SESSION['phone']=$_REQUEST['Digits'];
           $response-> say ('You entered, ' . padnumberswithspace($_SESSION['phone']),$voice) ;
       
           $ajayarr =array(
                        'numDigits'=>1,
                        'action'=>$baseurl . '?type=phoneconfirm',
                        'method'=>'GET',
			'timeout'=>10       
                        );     
        $gather=$response-> gather ($ajayarr);
        $gather->say("Press 1 to continue.  Press 2 to re-enter your phone number again",$voice) ;
        $response->redirect($baseurl . '?type=phone');

        print $response;
        } else {
        $response = new Services_Twilio_Twiml();
        $response-> say ('Your Phone Number must be 10 digits.',$voice) ;
//        $response->redirect($baseurl . '?type=greeting');
          $ajayarr =array(
          'numDigits'=>10,
          'action'=>$baseurl . '?type=phone',
          'method'=>'GET',
          'timeout'=>30
         );     
         $gather=$response-> gather ($ajayarr);
         $gather->say("Please enter your ten digit phone number. " . $pound,$voice) ;
         $response->redirect($baseurl . '?type=phone');
         print $response;

          
        }                                       
        break;


case 'phoneconfirm':
        $response = new Services_Twilio_Twiml();
//       $_SESSION['phone']='9054555555';

        if ($_REQUEST['Digits']==1) {
	     $response->redirect($baseurl . '?type=getrxnumber');
            print $response;
        



        } else {
                        $ajayarr =array(
                        'numDigits'=>10,
                        'action'=>$baseurl . '?type=phone',
                        'method'=>'GET',
			'timeout'=>30
                        );     
                        $gather=$response-> gather ($ajayarr);
                        $gather->say("Please enter your ten digit phone number. " . $pound,$voice) ;
         		   $response->redirect($baseurl . '?type=phone');
                        print $response;                                                       
        }   
        break;





//*************      

 case 'getrxnumber':
	  $response = new Services_Twilio_Twiml();
        
         $ajayarr =array(
                        'numDigits'=>10,
                        'action'=>$baseurl . '?type=rxconfirm',
                        'method'=>'GET',
			'timeout'=>30
                        );     
            $gather=$response-> gather ($ajayarr);
            $gather->say("Please enter the prescription number you want to order. " . $pound,$voice) ;
            $response->redirect($baseurl . '?type=getrxnumber');


            print $response;     
    break;

case 'rxconfirm':

              $_SESSION['rxnumber']=$_REQUEST['Digits'];
              $rxnumber=$_SESSION['rxnumber'];
            	$response = new Services_Twilio_Twiml();
              $storephone=get_storeinfo($phone_arr,$_REQUEST['To'],'storephone');                                                            

             $tmpdel='1';
             $template = @new orderrefillsbynumber_page("template_orderbynumber.html", $_SESSION['rxnumber'], $_SESSION['phone'],"IVR ORDER",$storephone,$tmpdel); 
             if(!isset($template->error)) {
                      if($template->orderitems_status=='Ordered') {
                         $stat=0;
                         $message="Your Prescription has been Ordered Successfully";
                        
                      } else {
                            $message=$template->orderitems_message;
                            $stat=1;  
                      }      
             } else {
              $stat=2;    
             }     
                   
             
               if ($stat==2) {
                     $response-> say ("We are currently experiencing technical difficulties . Please try again later or contact your pharmacy directly.",$voice) ;
                     print $response;
               } else {
                     $response-> say ($message,$voice) ;
			$response->redirect($baseurl . '?type=getanotherrx');
			 print $response;

               }         
                          
                    
   
    break;
   
case getanotherrx;
        $response = new Services_Twilio_Twiml();
      
               $ajayarr =array(
                     'numDigits'=>1,
                     'action'=>$baseurl . '?type=anotherrx',
                     'method'=>'GET',
                     'timeout'=>10
        
                      );
               $gather=$response-> gather ($ajayarr);
               $gather->say("To order another prescription Press 1. To hangup press 2",$voice) ;
               $response->redirect($baseurl . '?type=getanotherrx');
               print $response;
    break;

case anotherrx:
        $response = new Services_Twilio_Twiml();
        if ($_REQUEST['Digits']==1){
                $ajayarr =array(
                        'numDigits'=>10,
                        'action'=>$baseurl . '?type=rxconfirm',
                        'method'=>'GET',
			'timeout'=>30
                        );     
            $gather=$response-> gather ($ajayarr);
            $gather->say("Please enter the prescription number you would like to order. " . $pound,$voice) ;
            $response->redirect($baseurl . '?type=getanotherrx');
            print $response;     
     } else { 
       $response-> say ("Thank you for using the automated refill line. Good Bye.",$voice) ;
       $response->hangup();
       print $response;      
       
    
    
     }
   }
    
    
    
    
 ?>





