move edi reader to its own perl mod
authorBill Erickson <berick@esilibrary.com>
Fri, 28 Sep 2012 16:14:06 +0000 (12:14 -0400)
committerBill Erickson <berick@esilibrary.com>
Fri, 28 Sep 2012 16:14:06 +0000 (12:14 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Utils/EDIReader.pm [new file with mode: 0644]
Open-ILS/src/support-scripts/test-scripts/edi_parser.pl

diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Utils/EDIReader.pm b/Open-ILS/src/perlmods/lib/OpenILS/Utils/EDIReader.pm
new file mode 100644 (file)
index 0000000..9dbcb4a
--- /dev/null
@@ -0,0 +1,95 @@
+package OpenILS::Utils::EDIReader;
+use strict; use warnings;
+
+my $NEW_MSG_RE          = '^UNH';
+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 LIN+1++9780786222735:EN
+my $LIN_IDENT_2RE       = '^PIA\+0*5\+([^:]+).*'; # e.g. ISBN PIA+05+1594097801:IB
+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\++([^\+]+).*';
+my $MISC_CHARGE_AMT_RE  = '^MOA\+(8|131):(\d+)';
+
+
+sub new {
+    return bless({}, shift());
+}
+
+sub read_file {
+    my $self = shift;
+    my $file = shift;
+    my @msgs;
+
+    open(EDI_FILE, $file) or die "Cannot open $file: $!\n";
+    my $edi = join('', <EDI_FILE>);
+    close EDI_FILE;
+
+    $edi =~ s/\n//g;
+
+    foreach (split(/'/, $edi)) {
+        my $msg = $msgs[-1];
+
+        if (/$NEW_MSG_RE/) { # starting a new message.
+
+            $msg = {lineitems => [], misc_charges => []};
+            ($msg->{msg_type} = $_) =~ s/$MSG_TYPE_RE/$1/;
+
+            push(@msgs, $msg);
+        }
+
+        next unless $msg;
+
+        ($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 ($msg->{msg_type} eq 'INVOIC') {
+
+            ($msg->{inv_ident} = $_) =~ s/$INV_IDENT_RE/$1/g if /$INV_IDENT_RE/;
+            ($msg->{po_number} = $_) =~ s/$PO_NUM_RE/$1/g if /$PO_NUM_RE/;
+
+            if ($_ =~ /^LIN\+/) { # starting a new lineitem
+
+                $msg->{_current_li} = {};
+                ($msg->{_current_li}->{index} = $_) =~ s/$LIN_INDEX_RE/$1/g;
+                ($msg->{_current_li}->{ident} = $_) =~ s/$LIN_IDENT_RE/$1/g if /$LIN_IDENT_RE/;
+
+                push(@{$msg->{lineitems}}, $msg->{_current_li});
+            }
+
+            if ($msg->{_current_li}) {
+                ($msg->{_current_li}->{ident} = $_) =~ s/$LIN_IDENT_2RE/$1/g if /$LIN_IDENT_2RE/;
+                ($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/$TOTAL_BILLED_RE/$1/g if /$TOTAL_BILLED_RE/;
+
+            if (/$MISC_CHARGE_TYPE_RE/) {
+                (my $type = $_) =~ s/$MISC_CHARGE_TYPE_RE/$1/g;
+                push (@{$msg->{misc_charges}}, {type => $type});
+            }
+
+            if (/$MISC_CHARGE_AMT_RE/) {
+                my $chg = $msg->{misc_charges}[-1];
+                ($chg->{amount} = $_) =~ s/$MISC_CHARGE_AMT_RE/$1/g;
+            }
+        }
+    }
+
+    # remove the state-maintenance keys
+    for my $msg (@msgs) {
+        foreach (grep /^_/, keys %$msg) {
+            delete $msg->{$_};
+        }
+    }
+
+    return \@msgs;
+}
index bb5d8ca..242f905 100644 (file)
@@ -1,86 +1,9 @@
 #!/usr/bin/perl
 use strict; use warnings;
+use OpenILS::Utils::EDIReader;
 use Data::Dumper;
 
-my $edi_file = shift or die "EDI file needed\n";
-open(EDI_FILE, $edi_file) or die "Cannot open $edi_file: $!\n";
+my $reader = OpenILS::Utils::EDIReader->new;
+my $msgs = $reader->read_file(shift());
+print Dumper($msgs);
 
-my $edi = join('', <EDI_FILE>);
-close EDI_FILE;
-
-$edi =~ s/\n//g;
-
-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_IDENT_2RE       = '^PIA\+0*5\+([^:]+).*'; # e.g. ISBN PIA+05+1594097801:IB
-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\++([^\+]+).*';
-my $MISC_CHARGE_AMT_RE  = '^MOA\+(8|131):(\d+)';
-
-my @msgs;
-
-foreach (split(/'/, $edi)) {
-    my $msg = $msgs[-1];
-
-    if ($_ =~ /^UNH/) { # starting a new message.
-
-        $msg = {lineitems => [], misc_charges => []};
-        ($msg->{msg_type} = $_) =~ s/$MSG_TYPE_RE/$1/;
-
-        push(@msgs, $msg);
-    }
-
-    ($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 ($msg and $msg->{msg_type} eq 'INVOIC') {
-
-        ($msg->{inv_ident} = $_) =~ s/$INV_IDENT_RE/$1/g if /$INV_IDENT_RE/;
-        ($msg->{po_number} = $_) =~ s/$PO_NUM_RE/$1/g if /$PO_NUM_RE/;
-
-        if ($_ =~ /^LIN\+/) { # starting a new lineitem
-
-            $msg->{_current_li} = {};
-            ($msg->{_current_li}->{index} = $_) =~ s/$LIN_INDEX_RE/$1/g;
-            ($msg->{_current_li}->{ident} = $_) =~ s/$LIN_IDENT_RE/$1/g if /$LIN_IDENT_RE/;
-
-            push(@{$msg->{lineitems}}, $msg->{_current_li});
-        }
-
-        if ($msg->{_current_li}) {
-            ($msg->{_current_li}->{ident} = $_) =~ s/$LIN_IDENT_2RE/$1/g if /$LIN_IDENT_2RE/;
-            ($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/$TOTAL_BILLED_RE/$1/g if /$TOTAL_BILLED_RE/;
-
-        if (/$MISC_CHARGE_TYPE_RE/) {
-            (my $type = $_) =~ s/$MISC_CHARGE_TYPE_RE/$1/g;
-            push (@{$msg->{misc_charges}}, {type => $type});
-        }
-
-        if (/$MISC_CHARGE_AMT_RE/) {
-            my $chg = $msg->{misc_charges}[-1];
-            ($chg->{amount} = $_) =~ s/$MISC_CHARGE_AMT_RE/$1/g;
-        }
-    }
-}
-
-# remove the state-maintenance keys
-for my $msg (@msgs) {
-    foreach (grep /^_/, keys %$msg) {
-        delete $msg->{$_};
-    }
-}
-
-print Dumper(\@msgs);