#!/usr/bin/perl 

#program takes a medicare remit file and formats a report to be loaded into Macrosoft
#written by Xxxxx Xxxxxxx April 2000

#prompt for file name 

print "Hi Xxxxxx or Xxxxxx\n";
print "Enter the remit file you want to process\n";
chomp($infile = <>);
open (INFILE, $infile) or die "Input file not found or cannot be opened, try again\n";
#open (INFILE, 'mcrmtyh.dat') or die "Input file not found or cannot be opened, try again\n";
open(REMITOUT, '>mcrmt.out') or die "Can't open output msgs file\n";  #open o/p file

#initialize the counters
#$clp_counter = 0;
$seg_count = 0;

#get the current date and time
 @now = localtime;
 $y = $now[5] + 1900 ;
 $m = $now[4] + 1;
 $d = $now[3];
 $cur_date = "$m/$d/$y";
      
#define report format
#format STDOUT_TOP  = 
format REMITOUT_TOP  = 
                          MEDICARE REMITANCE DETAIL REPORT 
 REMIT DT:@<<<<<<<< FOR: @<<<<<<<<<<<<<<<< REM NO: @<<<<<<< LOADED ON:@<<<<<<<<
        $remit_date,   $entity,                   $remit_no,         $cur_date

.



format REMITOUT = 
 PAT NAME: @<<<<<<<<<<<<<<<<             ACCT (CTRL) NO: @<<<<<<<<<<<<
           $pat_name,                                   $acct_no,
 HIC NO:   @<<<<<<<<<<                   ICN NO:         @<<<<<<<<<<<<<<
           $hic_no,                             $icn_no, 
 FROM DT:  @<<<<<<<<                     MED REC NO:
           $from_date,                         $med_rec_no,
 THRU DT:  @<<<<<<<<                     TOB:   @<<<
           $thru_date,                         $tob,
 CLM STATUS:@<<                          NACHG: @<<
           $claim_status,                      $nachg,
 PAT STATUS:@<<                          HICHG: @<<
           $pat_status                         $hichg,
 COST:     @<<<<<                        DRG NO:@<<<                      
           $cost,                              $drg_no,
                            
 COV DAYS:  @#####                       
           $cov_days,                          
 NCOV DAYS: @#####
           $noncov_days,
 REASON CODES:  @>>  @>>   @>>   @>>
       $rc_code[0],$rc_code[1],$rc_code[2],$rc_code[3],
 REMARKS:     @<<<<  @<<<< @<<<< @<<<<   COINSURANCE:   @#########.##
       $rem[0],$rem[1],$rem[2],$rem[3],  $coins,
 DRG AMT:     @#########.##              DEDUCTIBLES:   @#########.## 
       $drg_amt,                         $deduct,
 DRG C-O AMT: @#########.##              REPORTED CHGS: @#########.##
       $drg_co_amt,                      $reported_chgs,
 OUTLIER AMT: @#########.##              NCOVD CHRGS:   @#########.## 
       $outlier_amt,                     $ncovd_chgs,  
 ALLOW/REIMB: @#########.##              COVD CHRGS:    @#########.## 
       $allow_reimb,                     $covd_chgs, 
 PROC CD AMT: @#########.##              CONTRACT ADJ:  @#########.##            
       $proc_code_amt,                   $contract_adj,
 LINE ADJ AMT:@#########.##              HCPCS AMT:     @#########.##        
       $line_adj_amt,                    $hcpcs_amt,
 INTEREST:    @#########.##              ESRD NET ADJ:  @#########.##
       $interest,                        $esrd_net_adj,
 DENIED CHRGS:@#########.##              REIM RATE:            @<<<<<  
       $denied_chgs,                     $reim_rate,
 NET REIM:    @#########.##              MSP PAYMNT:    @#########.## 
       $net_reimb,                       $msp_paymnt, 
 PER DIEM AMT:@#########.##              MSP LIAB MET:  @#########.##
       $per_diem_amt,                    $msp_liab_met,
 COVERED LINE:@#########.##              PROF COMP:     @#########.##
       $covrd_line,                      $prof_comp,         
 NONCVRD LINE:@#########.##             
       $noncovrd_line,                   

-----------------------------------------------------------------------


REV DATE     HCPCS     MODS    UNT/VIS  CHGS ALLW/REIMB GC  RSN   AMT    RMK CDS
@<<<@<<<<<<<<@<<<<<    @<<<@<< @#### @#####.##@#####.## @<<<@<<<<@####.##@<<<@<<<  
~~  @rev, @s_date,@hcpcs, @md1,@md2, @unit, @chgs,  @allow,@gc,@rsn_cd,@amt,@rmk_codes,
.

#########+#########+#########+#########+#########+#########+#########+#########+#########+
$remitlines = <INFILE>;  # reads the file into a list called remitlines

@segments = split(/\~/, $remitlines);

#this reads through the remit file, segment by segment

while ($line = $segments[$seg_count])
  {
    ++$seg_count;
       @sub_segs = split(/\*/,$line);

    if ($sub_segs[0] eq 'CLP')
      {
       write(REMITOUT);
   #   write;
       $sub = 0;
       &INIT_routine;
      $acct_no = $sub_segs[1];
      $icn_no  = $sub_segs[7];
      $drg_no  = $sub_segs[11];
      $net_reimb = $sub_segs[4];
      $claim_status = $sub_segs[2];
      $tob          = $sub_segs[8].$sub_segs[9];
      }
    elsif ($sub_segs[0] eq 'NM1' and $sub_segs[1] eq 'QC')
      {
      $hic_no    = $sub_segs[9];
      $pat_name  = "$sub_segs[3] $sub_segs[4]";
      $nachg     = $sub_segs[1];
      $hichg     = $sub_segs[8];
      }
    elsif ($sub_segs[0] eq 'N1'and $sub_segs[1] eq 'PR' and $sub_segs[2] eq 'EMPIRE MEDICARE SERVICES' )
      {
      $remit_name = "MC REMIT";
      } 	 	
    elsif ($sub_segs[0] eq 'N1'and $sub_segs[1] eq 'PE' and $sub_segs[2] eq 'YALE-NEW HAVEN HOSP' )
      {
      $entity = "Yale-New Haven Hosp";
      } 
    elsif ($sub_segs[0] eq 'N1'and $sub_segs[1] eq 'PE' and $sub_segs[2] eq 'BRIDGEPORT HOSP' )
      {
      $entity = "Bridgeport Hosp";
      } 
    elsif ($sub_segs[0] eq 'BPR') 
      {
      @date = $sub_segs[16];
      $r_d = $date[4] . $date[5];
      $r_m = $date[2] . $date[3];
#     $remit_date = "$r_d/$r_m";
     $remit_date = $sub_segs[16];
      }
    elsif ($sub_segs[0] eq 'NM1')
      {
      $hachg = $sub_segs[1];
      $hachg = $sub_segs[1];
      }  
    elsif ($sub_segs[0] eq 'DTM' and $sub_segs[1] eq '232')
      {
      $from_date = $sub_segs[2];
      } 
    elsif ($sub_segs[0] eq 'DTM' and $sub_segs[1] eq '233')
      {
      $thru_date = $sub_segs[2];
      } 
    elsif ($sub_segs[0] eq 'MIA')
      {
      $drg_cap_amt = $sub_segs[8];
      $rem[0]      = $sub_segs[5];
      } 
    elsif ($sub_segs[0] eq 'CAS')
      {
      &CAS_routine;
      }
    elsif ($sub_segs[0] eq 'SVC')
      {
      &SVC_routine;
      }
    elsif ($sub_segs[0] eq 'QTY' and $sub_segs[1] eq 'CA') 
      {
      $cov_days = $sub_segs[2];
      }
    elsif ($sub_segs[0] eq 'QTY' and $sub_segs[1] eq 'NA') 
      {
      $ncov_days = $sub_segs[2];
      } 
    elsif ($sub_segs[0] eq 'CLP' and $sub_segs[2] eq '4')
      {
      $denied_chgs = $sub_segs[3];
      }
    elsif ($sub_segs[0] eq 'QTY' and $sub_segs[1] eq 'CA')
      {
      $cost = $sub_segs[2];
      }
    elsif ($sub_segs[0] eq 'MOA')
      {
      $per_diem_rate = $sub_segs[1];
      $hcpcs_amt = $sub_segs[2];
      }
    elsif ($sub_segs[0] eq 'AMT' and $sub_segs[1] eq 'I')
      {
      $interest = $sub_segs[2];
      }
    elsif ($sub_segs[0] eq 'AMT' and $sub_segs[1] eq 'AU')
      {               
      $covd_chgs = $sub_segs[2];
      }

sub  CAS_routine {
      if (length$sub_segs[2] ne 0)
      {
      $rc_code[$sub]= $sub_segs[2];
      ++$sub;
      };
      if ($sub_segs[0] eq 'CAS' and $sub_segs[1] eq 'CO') 
      {
      $contract_adj = $sub_segs[3];
      $msp_paymnt   = $sub_segs[6];
      }; 
     if ($sub_segs[0] eq 'CAS' and $sub_segs[1] eq 'PR' and $sub_segs[2] eq '2') 
      {
      $coins = $sub_segs[3];
      };
     if ($sub_segs[0] eq 'CAS' and $sub_segs[1] eq 'PR' and $sub_segs[2] eq '1') 
      {
      $deduct = $sub_segs[3];
      };
                }

# ROUTINE TO READ THE SVC RECORD                
sub  SVC_routine {
      if ($sub_segs[1] =~ /HC/)
      {
      ++$svc_sub;
      print " the svc_sub = $svc_sub\n";
 #    print " the svc line = "; print "$line\n";
 #    print "svc sub seg = "; print "$sub_segs[1] $sub_segs[2]  $sub_segs[3]\n";
      $rev[svc_sub] = $sub_segs[6];
      @svc = split(/\>/, $sub_segs[1]);
      $hcpcs[svc_sub] = $svc[1];
      print "hcpcs = $hcpcs[svc_sub] svc_sub\n";
      if (length$svc[2] ne 0)
        {
        $md1[svc_sub]   = $svc[2];
        };
      if (length$svc[3] ne 0)
        {
        $md2[svc_sub]   = $svc[3];
        };
      };
      $chgs[svc_sub]  = $sub_segs[2];
      $allow[svc_sub] = $sub_segs[3];
      $rev[svc_sub]   = $sub_segs[4];
      if (length$sub_segs[3] ne 0)
      {
      $allow_reimb[svc_sub] = ($allow[svc_sub] + $allow_reimb);
      };
                    }

sub  INIT_routine {
          $svc_sub = "";
          $pat_name = "";
          $acct_no  = "";
          $hic_no   = ""; 
          $icn_no   = "";
          $med_rec_no = "";
          $from_date = "";                         
          $drg_no   = "";
          $thru_date = "";
          $tob      =  "";
          $claim_status = "";              
          $nachg        = "";
          $pat_status   = "";
          $cost         = "";
          $drg_no        = "";
          $hichg        = "";
          $cov_days     = "";
          $noncov_days  = "";
          $rc_code[0]   = "";
          $rc_code[1]   = "";
          $rc_code[2]   = "";
          $rc_code[3]   = "";
          $rem[0]       = "";
          $rem[1]       = "";
          $rem[2]       = "";
          $rem[3]       = "";
          $drg_amt      = "";
          $coins        = "";
          $drg_co_amt   = "";
          $reported_chgs = "";
          $outlier_amt   = "";
          $proc_code_amt = "";
          $line_adj_amt  = "";
          $deduct       = "";
          $covd_chgs    = "";
          $prof_comp    = "";
          $ncovd_chgs   = "";
          $interest     = "";
          $denied_chgs  = "";
          $reim_rate    = "";
          $contract_adj = "";
          $net_reimb    = "";
          $hcpcs_amt    = "";
          $pat_refund   = "";
          $esrd_net_adj = "";
          $msp_paymnt   = "";
          $drg_opr_amt  = "";
          $per_diem_rate = "";
          $per_diem_amt  = "";
          $msp_liag_met  = "";
          $covrd_line    = "";
          $noncovrd_line = "";      
          $allow_reimb   = "";
          $rev           = "";
          $s_date        = "";
          $hcpcs         = "";
          $md1           = "";
          $md2           = "";
          $unit          = "";
          $chgs          = "";
          $allow         = "";
          $gc            = "";
          $rsn_cd        = "";
          $amt           = "";
          $rmk_codes     = "";
          }

  }

