From eca0385afaad9f3b36f665671d438720ef773c30 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Fri, 28 Sep 2012 10:34:28 -0400 Subject: [PATCH] Experimenting w/ ad-hoc edi invoice parsing Signed-off-by: Bill Erickson --- .../src/support-scripts/test-scripts/edi_parser.pl | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 Open-ILS/src/support-scripts/test-scripts/edi_parser.pl 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 index 0000000000..c92991dd3d --- /dev/null +++ b/Open-ILS/src/support-scripts/test-scripts/edi_parser.pl @@ -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('', ); +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); -- 2.11.0