<?php

  session_start();
  
  include ('include_fns.php');

  function WriteLogFile($fp, $claimslog)
  {
    fwrite($fp, $claimslog);
    fclose($fp);
  }


  function CaptureProvider($filecontents)
  {
     $ipaddr = $_SERVER['REMOTE_ADDR'];

     $fp = fopen($_SERVER['DOCUMENT_ROOT']."/E2/providercaplog.txt", "a");
     $claimslog = '---------------------------------------------------------------------------'."\r\n";
     $claimslog .= 'PROVIDER'.date("dmY H:i:s").'|'.$username.'|'.$password.'|'.$ipaddr."\r\n";

    $login = login('', '', 0);

    if (!$login)
    {
      $claimslog .= 'INCORRECT LOGIN'."\r\n";
      
      WriteLogFile($fp, $claimslog);

      return "999|Incorrect Login Details";
    }
    else
    { 
     
      $claimslog .= $filecontents;
	
      $Data = explode("\n", $filecontents); //parse the rows
    
      $provider = '';
      
      $PROFound  = 'F';
      $TermFound = 'F';

      // do prelim checks to make sure PRO and Term Rows exist
      foreach($Data as $Row) 
      {  
        $Row = explode("|", $Row); //parse the items in rows
     
        if ($Row[0] == 'PRO')
        {  
          $PROFound = 'T'; 
        }
        else  
        if ($Row[0] == '@@@')
        {
          $TermFound = 'T'; 
        }    
      }    

      if ($TermFound == 'F')
      {
       $claimslog .= 'RSP|4|Terminator Not Found|E|-1'."\r\n";  
       WriteLogFile($fp, $claimslog);
       return 'RSP|4|Terminator Not Found|E|-1';
      }


      foreach($Data as $Row) 
      {  
        $iberror = '';

        $Row = explode("|", $Row); //parse the items in rows
     
        if ($Row[0] == 'PRO')
        {
         
          $practiceno       = $Row[1];
          $practicename     = $Row[2];
          $e2identifier     = $Row[3];
          $phone1           = $Row[4];
          $phone2           = $Row[5];
          $email            = $Row[6]; 
          $postaladdr1      = $Row[7]; 
          $postaladdr2      = $Row[8];
          $postaladdr3      = $Row[9];
          $postalprovince   = $Row[10];
          $postalpcode      = $Row[11];
          $physaddr1        = $Row[12];
          $physaddr2        = $Row[13];
          $physaddr3        = $Row[14];
          $physprovince     = $Row[15];
          $physpcode        = $Row[16]; 
          $bankname         = $Row[17]; 
          $accountholder    = $Row[18]; 
          $bankaccno        = $Row[19]; 
          $banksortcode     = $Row[20]; 
          $optomtitle       = $Row[21]; 
          $optomfirstname   = $Row[22]; 
          $optomsurname     = $Row[23]; 
          $optomsamdcno     = $Row[24]; 

          if ($practiceno == '') 
          {  
            $claimslog .= 'RSP|1|Provider Practice No. Required|E|-1'."\r\n";  
            WriteLogFile($fp, $claimslog);
            return 'RSP|1|Provider Practice No. Required|E|-1';
          }
          
          if ($practicename == '') 
          {  
            $claimslog .= 'RSP|2|Provider Practice Name Required|E|-1'."\r\n";  
            WriteLogFile($fp, $claimslog);
            return 'RSP|2|Provider Practice Name Required|E|-1';
          }

          $query = "SELECT COUNT(*)
                    FROM PROVIDER
                    WHERE PRACNUM = '$practiceno' ";
         
          $result = ExecuteQuery($query);

          if (ibase_errmsg()) 
          {
            $iberror = ibase_errmsg();
            
            $claimslog .= 'RSP|3|Database Error'.$iberror.'|E|-1'."\r\n";  
            WriteLogFile($fp, $claimslog);
            
            return 'RSP|3|Database Error'.$iberror.'|E|-1';
          }
          else
          {  
            $provcount  = $result[0][0];

            if ($provcount == 0)
              $status = 'I';
            else
              $status = 'U';
          }
        }
        }          
        
		      $query = "SELECT RET_UNIQUEID  
                    FROM SP_CAPTUREPROVIDER('$status', '$practiceno', '$practicename','$e2identifier','$phone1','$phone2','$email',
                      '$postaladdr1','$postaladdr2','$postaladdr3','$postalprovince','$postalpcode',
                      '$physaddr1','$physaddr2','$physaddr3','$physprovince','$physpcode',
                      '$bankname','$accountholder','$bankaccno','$banksortcode',
                      '$optomtitle','$optomfirstname','$optomsurname','$optomsamdcno')";

          $claimslog .= 'QUERY:'.$query."\r\n";

          if($conn = db_connect())
          {  
            $result = ibase_query($conn, $query); 

            if (ibase_errmsg()) 
            {
              $iberror = ibase_errmsg();
              $claimslog .= 'RSP|3|Could not save provider ERROR:'.$iberror.'|E|-1'."\r\n";  
              WriteLogFile($fp, $claimslog);  

              return 'RSP|3|Could not save provider ERROR:'.$iberror.'|E|-1';
            }
            else
            if (!$result) 
            {
              $claimslog .= 'RSP|3|Could not save provider|E|-1'."\r\n";  
              WriteLogFile($fp, $claimslog);  
              return 'RSP|3|Could not save provider|E|-1';
            }
            else
            {
              $row = ibase_fetch_row ($result);
            
              $retuniqueid = $row[0];
              
              $claimslog .= 'RESULT:'.$retuniqueid."\r\n";
            }          
          }
      }

     
      if (($PROFound == 'T')&&
          ($TermFound == 'T'))
      {	
        
        $claimheaderresponse = 'RSP|0|PROVIDER CAPTURED|'.$status.'|'.$retuniqueid;

        $claimslog .= $claimheaderresponse."\r\n";

        WriteLogFile($fp, $claimslog); 
        return $claimheaderresponse;


      }  
      else
        WriteLogFile($fp, $claimslog); 

  }


  $filecontents =  urldecode($_POST['filecontents']);

  $result = CaptureProvider($filecontents);
  echo $result;


?>
