<?php
include_once('ivrconfigv.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';
$vdir=get_storeinfo($phone_arr,$_REQUEST['To'],'voicedir');

$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) ;
      $response-> play ($vdir . '/store_greetingsunny.mp3');



        $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) ;

      $gather->play($vdir . '/tendigit.mp3') ;

       $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) ;
         // $response->play($vdir . '/youentered.mp3') ;
         // $response-> say (padnumberswithspace($_SESSION['phone']),$voice) ;
         
           
           $ajayarr =array(
                        'numDigits'=>1,
                        'action'=>$baseurl . '?type=phoneconfirm',
                        'method'=>'GET',
			'timeout'=>10       
                        );     
        $gather=$response-> gather ($ajayarr);
       // $gather->say("Press 4 to continue.  Press 2 to re-enter your phone number again",$voice) ;
        $gather->play($vdir . '/press1.mp3');
        $response->redirect($baseurl . '?type=phone');

        print $response;
        } else {
        $response = new Services_Twilio_Twiml();
       // $response->  ('Your Phone Number must be 10 digits.',$voice) ;
        $response->play($vdir . '/must10digits.mp3') ;


//        $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) ;
          $gather->play($vdir . '/tendigit.mp3') ;
          $response->redirect($baseurl . '?type=phone');
         print $response;

          
        }                                       
        break;


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

        if ($_REQUEST['Digits']==1) {
	        $deliveryind=get_storeinfo($phone_arr,$_REQUEST['To'],'pickupdelivery');
            if ($deliveryind=='Y') {
                $response->redirect($baseurl . '?type=getdelivery');
            } else {
               $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) ;
                         $gather->play($vdir . '/tendigit.mp3') ;
       
         		   $response->redirect($baseurl . '?type=phone');
                        print $response;                                                       
        }   
        break;

case 'getdelivery':
      $response = new Services_Twilio_Twiml();
        
         $ajayarr =array(
                        'numDigits'=>1,
                        'action'=>$baseurl . '?type=getrxnumber',
                        'method'=>'GET',
                        'timeout'=>30
                        );
                         $gather=$response-> gather ($ajayarr);
            //$gather->say("Press 1 To Pickup at Store. Press 2 for Delivery. " . $pound,$voice) ;
              $gather->play($vdir . '/pickup.mp3');

            $response->redirect($baseurl . '?type=getdelivery');
            print $response;                                                       
        break;





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

 case 'getrxnumber':
	  $response = new Services_Twilio_Twiml();
      if($_REQUEST['Digits']==2) {
         $_SESSION['delivery']='D';
      } else {
         $_SESSION['delivery']='P';
      }

        
         $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) ;
            $gather->play($vdir . '/enterprescriptionsunny.mp3');

            $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=$_SESSION['delivery'];

             $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=1;
                         //$message="Your Prescription has been Ordered Successfully";
                         //$message=$template->orderitems_message;
                           $message=$vdir . '/orderedsunny.mp3';

                      } else { 
                            
  				if($template->orderitems_status=='ok') {
                              $message=$vdir . '/orderedsunny.mp3';
                              $stat=1; 
                            }
                           if($template->orderitems_status=='zerorepeat') {
                              $message=$vdir . '/zerorepeatsunny.mp3';
                              $stat=1; 
                            }
                            if($template->orderitems_status=='TooEarly') {
                              $message=$vdir . '/tooearly.mp3';
                              $stat=1; 
                            }
                    
                            if($template->orderitems_status=='NoPhoneFaxRefills') {
                              $message=$vdir . '/nophonefaxrefills.mp3';
                              $stat=1; 
                            }
                            if($template->orderitems_status=='OnOrder') {
                              $message=$vdir . '/onorder.mp3';
                              $stat=1; 
                            }
                            if($template->orderitems_status=='InvalidRxPhoneMatch') {
                              $message=$vdir . '/invalidrxphonematch.mp3';
                              $stat=1; 
                            }
                            if($template->orderitems_status=='Failed') {
                              $message=$vdir . '/invalidrx.mp3';
                              $stat=1; 
                            }
                            if($template->orderitems_status=='invalidrx') {
                              $message=$vdir . '/invalidrx.mp3';
                              $stat=1; 
                            }
                            if($template->orderitems_status=='inactive') {
                              $message=$vdir . '/inactive.mp3';
                              $stat=1; 
                            }
                            
                           if ($stat==0) {
                            $message=$template->orderitems_message;
                            $stat=4;
                           }                          
                    
                      }      
             } else {
              $stat=2;    
             }     
                   
             
               if ($stat==2) {
                    // $response-> say ("We are currently experiencing technical difficulties . Please try again later or contact your pharmacy directly.",$voice) ;
                       $response->play($vdir . '/technical.mp3') ;
                       print $response;
               } else {
                   if ($stat==1) {
                    $response->play($message) ;

                   } 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) ;
                $gather->play($vdir . '/anothersunny.mp3') ;
               $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) ;
             $gather->play($vdir . '/enterprescriptionsunny.mp3') ;

            $response->redirect($baseurl . '?type=getanotherrx');
            print $response;     
     } else { 
            

        if ($_REQUEST['Digits']==2){
	
  	//	$response = new Services_Twilio_Twiml;
	//	$response->dial( '+17055360707', array(
	//      'callerId' => '+17055360707'));
	//	print $response; 

             $response->play($vdir . '/goodbyesunny.mp3') ;
             $response->hangup();
             print $response;

             
         } else {
//             $response-> say ("Thank you for using the automated refill line. Good Bye.",$voice) ;
             $response->play($vdir . '/goodbyesunny.mp3') ;
             $response->hangup();
             print $response;
         }     
       




       
    
    
     }
   }
    
    
    
    
 ?>





