Experimenting w/ ad-hoc edi invoice parsing
authorBill Erickson <berick@esilibrary.com>
Fri, 28 Sep 2012 14:34:28 +0000 (10:34 -0400)
committerBill Erickson <berick@esilibrary.com>
Fri, 28 Sep 2012 14:34:28 +0000 (10:34 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/support-scripts/test-scripts/edi_parser.pl [new file with mode: 0644]

diff --git a/Open-ILS/src/support-scripts/test-scripts/edi_parser.pl b/Open-ILS/src/support-scripts/test-scripts/edi_parser.pl
new file mode 100644 (file)
index 0000000..c92991d
--- /dev/null
@@ -0,0 +1,74 @@
+#!/usr/bin/perl
+use strict; use warnings;
+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 $edi = join('', <EDI_FILE>);
+close EDI_FILE;
+
+# could store regex's as constants instead of inline..
+my $INV_IDENT_RE = '^BGM\+380\+(.*)\+.*';
+
+my @msgs;
+
+foreach (split(/'/, $edi)) {
+    my $msg = $msgs[-1];
+
+    if ($_ =~ /^UNH/) { 
+        # header.  start a new message.
+
+        $msg = {lineitems => [], misc_charges => []};
+
+        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\+/;
+
+        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;
+
+            # bib ident, e.g. ISBN
+            ($msg->{_current_li}->{ident} = $_) =~ s/^LIN\+\S+\++(.*)/$1/g;
+        }
+
+        ($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:/;
+
+        ($msg->{total_billed} = $_) =~ s/^MOA\+86:(\d+)/$1/g if /^MOA\+86:/;
+
+        if ($_ =~ /^ALC\+C\++/) {
+            (my $type = $_) =~ s/^ALC\+C\++(\S+)/$1/g;
+            push (@{$msg->{misc_charges}}, {type => $type});
+        }
+
+        if ($_ =~ /^MOA\+(8|131):/) {
+            my $chg = $msg->{misc_charges}[-1];
+            ($chg->{amount} = $_) =~ s/^MOA\+(8|131):(\d+)/$1/g;
+        }
+    }
+}
+
+# remove the state-maintenance keys
+for my $msg (@msgs) {
+    foreach (grep /^_/, keys %$msg) {
+        delete $msg->{$_};
+    }
+}
+
+print Dumper(\@msgs);