Collect full list if LI identifiers
authorBill Erickson <berick@esilibrary.com>
Wed, 3 Oct 2012 13:32:34 +0000 (09:32 -0400)
committerBill Erickson <berick@esilibrary.com>
Wed, 3 Oct 2012 13:32:34 +0000 (09:32 -0400)
Also use regex match instead of search/replace, as it's not necessary
(and probably slower).

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Utils/EDIReader.pm

index c8b58b0..bd3c8e1 100644 (file)
@@ -3,35 +3,38 @@ use strict; use warnings;
 
 my $NEW_MSG_RE          = '^UNH';
 my $NEW_LIN_RE          = '^LIN';
-my $MISC_CHARGE_TYPE_RE = '^ALC\+C\++([^\+]+).*';
+my $MISC_CHARGE_TYPE_RE = '^ALC\+C\++([^\+]+)';
 my $MISC_CHARGE_AMT_RE  = '^MOA\+(8|131):(\d+)';
 
 my %edi_fields = (
-    message_type    => '^UNH\+\d+\+(\S{6}):.*',
-    buyer_san       => '^NAD\+BY\+([^:]+).*',
-    vendor_san      => '^NAD\+SU\+([^:]+).*',
+    message_type    => '^UNH\+\d+\+(\S{6})',
+    buyer_san       => '^NAD\+BY\+([^:]+)',
+    vendor_san      => '^NAD\+SU\+([^:]+)',
     purchase_order  => '^RFF\+ON:(\S+)',
-    invoiceident2   => '^BGM\+380\+(.*)\+.*',
+    invoice_ident   => '^BGM\+380\+([^\+]+)',
     total_billed    => '^MOA\+86:(\d+)'
 );
 
 my %edi_li_fields = (
     id      => '^RFF\+LI:\S+\/(\S+)',
-    index   => '^LIN\+([^\+]+).*',
-    ident   => '^LIN\+\S+\++([^:]+).*',
-    ident2  => '^PIA\+0*5\+([^:]+).*', 
+    index   => '^LIN\+([^\+]+)',
     amount_billed   => '^MOA\+203:(\d+)',
-    unit_price      => '^PRI\+AAB:(\d+).*',
-    expected_date   => '^DTM\+44:([^:]+).*'
+    unit_price      => '^PRI\+AAB:(\d+)',
+    expected_date   => '^DTM\+44:([^:]+)'
+);
+
+my %edi_li_ident_fields = (
+    ident   => '^LIN\+\S+\++([^:]+):?(\S+)?',
+    ident2  => '^PIA\+0*5\+([^:]+):?(\S+)?', 
 );
 
 my %edi_li_quant_fields = (
-    quantity => '^QTY\+\d+:(\d+).*',
-    code => '^QTY\+(\d+):.*',
+    quantity => '^QTY\+\d+:(\d+)',
+    code => '^QTY\+(\d+):',
 );
 
 my %edi_charge_fields = (
-    charge_type     => '^ALC\+C\++([^\+]+).*',
+    charge_type     => '^ALC\+C\++([^\+]+)',
     charge_amount   => '^MOA\+(8|131):(\d+)'
 );
 
@@ -74,7 +77,7 @@ sub read {
         next unless $msg;
 
         for my $field (keys %edi_fields) {
-            ($msg->{$field} = $_) =~ s/$edi_fields{$field}/$1/g
+            ($msg->{$field}) = $_ =~ /$edi_fields{$field}/
                 if /$edi_fields{$field}/;
         }
 
@@ -90,19 +93,21 @@ sub read {
         if (my $li = $msg->{_current_li}) {
 
             for my $field (keys %edi_li_fields) {
-                ($li->{$field} = $_) =~ s/$edi_li_fields{$field}/$1/g
+                ($li->{$field}) = $_ =~ /$edi_li_fields{$field}/g
                     if /$edi_li_fields{$field}/;
             }
 
-
-            # if no ident value is present, promote ident2
-            $li->{ident} = delete $li->{ident2}
-                if $li->{ident2} and not $li->{ident};
+            for my $field (keys %edi_li_ident_fields) {
+                if (/$edi_li_ident_fields{$field}/) {
+                    my ($ident, $type) = $_ =~ /$edi_li_ident_fields{$field}/;
+                    push(@{$li->{identifiers}}, {code => $type, value => $ident});
+                }
+            }
 
             if (/$edi_li_quant_fields{quantity}/) {
                 my $quant = {};
-                ($quant->{quantity} = $_) =~ s/$edi_li_quant_fields{quantity}/$1/g;
-                ($quant->{code} = $_) =~ s/$edi_li_quant_fields{code}/$1/g;
+                ($quant->{quantity}) = $_ =~ /$edi_li_quant_fields{quantity}/;
+                ($quant->{code}) = $_ =~ /$edi_li_quant_fields{code}/;
                 push(@{$li->{quantities}}, $quant);
             }
 
@@ -119,7 +124,7 @@ sub read {
 
         if (my $charge = $msg->{_current_charge}) {
             for my $field (keys %edi_charge_fields) {
-                ($charge->{$field} = $_) =~ s/$edi_charge_fields{$field}/$1/g
+                ($charge->{$field}) = $_ =~ /$edi_charge_fields{$field}/
                     if /$edi_charge_fields{$field}/;
             }
         }