<?php

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

  function ibdateformat($date)
  {
    $year  = substr($date, 0, 4);
    $month = substr($date, 4, 2);
    $day   = substr($date, 6, 2);
    
    switch($month)
    {
     case '01': $newmonth = 'JAN';break;
     case '02': $newmonth = 'FEB';break;
     case '03': $newmonth = 'MAR';break;
     case '04': $newmonth = 'APR';break;
     case '05': $newmonth = 'MAY';break;
     case '06': $newmonth = 'JUN';break;
     case '07': $newmonth = 'JUL';break;
     case '08': $newmonth = 'AUG';break;
     case '09': $newmonth = 'SEP';break;
     case '10': $newmonth = 'OCT';break;
     case '11': $newmonth = 'NOV';break;
     case '12': $newmonth = 'DEC';break;
    } 

    
    return $day.'-'.$newmonth.'-'.$year;  
  }

  function CaptureClaim($username, $password, $practiceno, $filecontents)
  {

    $login = loginWS($username, $password, 0);

    if (!$login)
    {
      return "999|Incorrect Login Details";
    }
    else
    {  

      $Data = explode("\n", $filecontents); //parse the rows
    
      $patient = '';
      $provider = '';
    
      $PATFound  = 'F';
      $PROFound  = 'F';
      $CLMFound  = 'F';
      $TermFound = 'F';

      $claimitems  = 0;
      $updateseqno = 0;
      $batchno     = 0;
      
      $retupdateseqno = 0;
      $retbatchno     = 0;



      foreach($Data as &$Row) 
      {  
        $Row = explode("|", $Row); //parse the items in rows
     
        if ($Row[0] == 'PRO')
        {
          $PROFound = 'T'; 
          $provider = $Row[1];

          if ($provider == '') 
            return '5|Provider Practice No. Required';

          $query = "SELECT PRACNUM
                    FROM PROVIDER
                    WHERE PRACNUM = '$provider' ";
         
          $result = ExecuteQuery($query);

          if ($result)
          {
            $provider = $result[0][0];
          }  
          else
            return '6|Provider Not Found';

        }          
        else
        if ($Row[0] == 'PAT')
        {
          if ($PROFound == 'F')
            return '5|Provider Line must first be populated';
          
          $PATFound = 'T'; 
         
          $birthday     = ibdateformat($Row[1]);
          $membershipno = $Row[2];
          $depno        = $Row[3];
          $em_account   = $Row[4];
          $em_patient   = $Row[5];

         
          if ($membershipno == '') 
            return '1|Membership No. Required';
          else
          if ($birthday == '') 
            return '2|Birthday Required';
          else 
          if ($depno == '') 
            return '3|Dependant No. Required';

          $query = "SELECT ACCOUNT 
                    FROM PATIENT
                    WHERE BIRTHDAY = '$birthday'
                       AND NUMBER = '$membershipno'
                       AND DEPNUM = '$depno'";

         
          $result = ExecuteQuery($query);

          if ($result)
          {
            $account = $result[0][0];
          }  
          else
            return '4|Patient Not Found';

        }  
        else
        if ($Row[0] == 'CLM')
        {
          if ($PATFound == 'F')
            return '5|Patient Line must first be populated';
          
          $CLMFound = 'T'; 

          $claimitems = $Row[1];

          if ($claimitems == '')
              return '7|No. of Clam Lines required';
        }
        else
        if ($Row[0] == 'ITM')
        {

          if ($CLMFound == 'F')
            return '5|Claim Header Line must first be populated';
          
          $itemno    = $Row[1];
          $txdate    = ibdateformat($Row[2]);
          $tariff    = $Row[3];
          $narrative = $Row[4];
          $qty       = $Row[5];
          $claimed   = $Row[6]; 
          $discount  = $Row[7]; 
          $authno    = $Row[8];
          $script    = $Row[9];
          $diag      = $Row[10];
          $em_docno  = $Row[11]; 
          $em_txno   = $Row[12]; 


          if ($itemno == $claimitems)
          {
           $linestatus = 'L';
           $updateseqno = $retupdateseqno;
          }  
          else
          if ($itemno == 1)
          {
           $linestatus  = 'F';
           $updateseqno = 0;  
          }  
          else
          {
           $linestatus  = '';
           $updateseqno = $retupdateseqno;
          }; 

          $narrative = substr($narrative, 0, 30);

          $query = "SELECT RET_UPDATESEQNO, RET_BATCHNO FROM SP_WEBEDITX2('I', '$linestatus', '$updateseqno', NULL, NULL, '$itemno', 
                   '$account', '$provider','$txdate', '$qty', '$claimed', '$discount', '$narrative',  
                   '$tariff', NULL, '$authno', '$diag', '$script', '$username');";

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

            if (!$result) 
            {
              return '8|Could not post invoice for Item:'. $itemno;
            }
            else
            {
              $row = ibase_fetch_row ($result);
            
              $retupdateseqno = $row[0];
              $retbatchno     = $row[1];
            }          
          } 
        }
        else
        if ($Row[0] == '@@@')
        {
          $TermFound = 'T'; 
          $batchno   = $retbatchno;
        }          
      
      }

      if (($PATFound == 'T')&&
          ($PROFound == 'T')&&
          ($CLMFound == 'T')&&
          ($TermFound == 'T'))
        return "Claim Received!".$batchno;
    }  
  }

  ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache

  $server = new SoapServer("CaptureClaimWS.wsdl");
  $server->addFunction("CaptureClaim");
  $server->handle();
?>
