define regex's as top-level vars for easier futzery
authorBill Erickson <berick@esilibrary.com>
Fri, 28 Sep 2012 15:04:02 +0000 (11:04 -0400)
committerBill Erickson <berick@esilibrary.com>
Fri, 28 Sep 2012 15:04:02 +0000 (11:04 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/support-scripts/test-scripts/edi_parser.pl

index 32f32f0..9639cc6 100644 (file)
@@ -10,58 +10,66 @@ close EDI_FILE;
 
 $edi =~ s/\n//g;
 
-# could store regex's as constants instead of inline..
-my $INV_IDENT_RE = '^BGM\+380\+(.*)\+.*';
+my $MSG_TYPE_RE         = '^UNH\+\d+\+(\S{6}):.*'; # ORDRDP, INVOIC, ...
+my $INV_IDENT_RE        = '^BGM\+380\+(.*)\+.*';
+my $PO_NUM_RE           = '^RFF\+ON:(\S+)';
+my $BUYER_SAN_RE        = '^NAD\+BY\+([^:]+).*';
+my $VENDOR_SAN_RE       = '^NAD\+SU\+([^:]+).*';
+my $LIN_INDEX_RE        = '^LIN\+([^\+]+).*';    
+my $LIN_IDENT_RE        = '^LIN\+\S+\++(.*)'; # e.g. ISBN
+my $LIN_QUANTITY_RE     = '^QTY\+47:(\d+)';
+my $LIN_AMOUNT_RE       = '^MOA\+203:(\d+)';
+my $LIN_ID_RE           = '^RFF\+LI:\S+\/(\S+)';
+my $TOTAL_BILLED_RE     = '^MOA\+86:(\d+)';
+my $MISC_CHARGE_TYPE_RE = '^ALC\+C\++(\S+)';
+my $MISC_CHARGE_AMT_RE  = '^MOA\+(8|131):(\d+)';
 
 my @msgs;
 
 foreach (split(/'/, $edi)) {
     my $msg = $msgs[-1];
 
-    if ($_ =~ /^UNH/) { 
-        # header.  start a new message.
+    if ($_ =~ /^UNH/) { # header.  start a new message.
 
         $msg = {lineitems => [], misc_charges => []};
+        ($msg->{msg_type} = $_) =~ s/$MSG_TYPE_RE/$1/;
 
         push(@msgs, $msg);
-
-        ($msg->{msg_type} = $_) =~ s/UNH\+\d+\+(\S{6}):.*/$1/;
     }
 
     if ($msg and $msg->{msg_type} eq 'INVOIC') {
 
         ($msg->{inv_ident} = $_) =~ s/$INV_IDENT_RE/$1/g if /$INV_IDENT_RE/;
-        ($msg->{po_number} = $_) =~ s/^RFF\+ON:(\S+)/$1/g if /^RFF\+ON:/;
-        ($msg->{buyer_san} = $_) =~ s/^NAD\+BY\+([^:]+).*/$1/g if /^NAD\+BY\+/;
-        ($msg->{vendor_san} = $_) =~ s/^NAD\+SU\+([^:]+).*/$1/g if /^NAD\+SU\+/;
+        ($msg->{po_number} = $_) =~ s/$PO_NUM_RE/$1/g if /$PO_NUM_RE/;
+        ($msg->{buyer_san} = $_) =~ s/$BUYER_SAN_RE/$1/g if /$BUYER_SAN_RE/;
+        ($msg->{vendor_san} = $_) =~ s/$VENDOR_SAN_RE/$1/g if /$VENDOR_SAN_RE/;
 
         if ($_ =~ /^LIN\+/) {
             # starting a new lineitem
 
             $msg->{_current_li} = {};
-            push(@{$msg->{lineitems}}, $msg->{_current_li});
-
-            # index in the invoice
-            ($msg->{_current_li}->{index} = $_) =~ s/^LIN\+([^\+]+).*/$1/g;
+            ($msg->{_current_li}->{index} = $_) =~ s/$LIN_INDEX_RE/$1/g;
+            ($msg->{_current_li}->{ident} = $_) =~ s/$LIN_IDENT_RE/$1/g;
 
-            # bib ident, e.g. ISBN
-            ($msg->{_current_li}->{ident} = $_) =~ s/^LIN\+\S+\++(.*)/$1/g;
+            push(@{$msg->{lineitems}}, $msg->{_current_li});
         }
 
-        ($msg->{_current_li}->{quantity} = $_) =~ s/^QTY\+47:(\d+)/$1/g if /^QTY\+47:/;
-        ($msg->{_current_li}->{amount} = $_) =~ s/^MOA\+203:(\d+)/$1/g if /^MOA\+203:/;
-        ($msg->{_current_li}->{id} = $_) =~ s/^RFF\+LI:\S+\/(\S+)/$1/g if /^RFF\+LI:/;
+        if ($msg->{_current_li}) {
+            ($msg->{_current_li}->{quantity} = $_) =~ s/$LIN_QUANTITY_RE/$1/g if /$LIN_QUANTITY_RE/;
+            ($msg->{_current_li}->{amount} = $_) =~ s/$LIN_AMOUNT_RE/$1/g if /$LIN_AMOUNT_RE/;
+            ($msg->{_current_li}->{id} = $_) =~ s/$LIN_ID_RE/$1/g if /$LIN_ID_RE/;
+        }
 
-        ($msg->{total_billed} = $_) =~ s/^MOA\+86:(\d+)/$1/g if /^MOA\+86:/;
+        ($msg->{total_billed} = $_) =~ s/$TOTAL_BILLED_RE/$1/g if /$TOTAL_BILLED_RE/;
 
-        if ($_ =~ /^ALC\+C\++/) {
-            (my $type = $_) =~ s/^ALC\+C\++(\S+)/$1/g;
+        if (/$MISC_CHARGE_TYPE_RE/) {
+            (my $type = $_) =~ s/$MISC_CHARGE_TYPE_RE/$1/g;
             push (@{$msg->{misc_charges}}, {type => $type});
         }
 
-        if ($_ =~ /^MOA\+(8|131):/) {
+        if (/$MISC_CHARGE_AMT_RE/) {
             my $chg = $msg->{misc_charges}[-1];
-            ($chg->{amount} = $_) =~ s/^MOA\+(8|131):(\d+)/$1/g;
+            ($chg->{amount} = $_) =~ s/$MISC_CHARGE_AMT_RE/$1/g;
         }
     }
 }