From a7a59d278db38a1462f386c3abea2f7b34685be4 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Mon, 1 Oct 2012 17:05:05 -0400 Subject: [PATCH] Improve code self-documentation Remove an unnecessary layer of abstraction by mapping the final message fields directly to their extraction regex's. Signed-off-by: Bill Erickson --- .../src/perlmods/lib/OpenILS/Utils/EDIReader.pm | 109 +++++++++++---------- 1 file changed, 59 insertions(+), 50 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Utils/EDIReader.pm b/Open-ILS/src/perlmods/lib/OpenILS/Utils/EDIReader.pm index 769b96e436..4c65996627 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Utils/EDIReader.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Utils/EDIReader.pm @@ -2,27 +2,37 @@ package OpenILS::Utils::EDIReader; use strict; use warnings; my $NEW_MSG_RE = '^UNH'; -my $NEW_LIN_RE = '^LIN\+'; -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_NUM_INVOICED_RE = '^QTY\+47:(\d+).*'; -my $LIN_NUM_ORDERED_RE = '^QTY\+21:(\d+).*'; -my $LIN_NUM_DISPATCH_RE = '^QTY\+12:(\d+).*'; -my $LIN_NUM_BACKORDER_RE= '^QTY\+83:(\d+).*'; -my $LIN_NUM_DELIVERED_RE= '^QTY\+46:(\d+).*'; -my $LIN_AMOUNT_BILLED_RE = '^MOA\+203:(\d+)'; -my $LIN_UNIT_PRICE_RE = '^PRI\+AAB:(\d+).*'; # PRI+AAB:49::SRP -my $LIN_ID_RE = '^RFF\+LI:\S+\/(\S+)'; -my $TOTAL_BILLED_RE = '^MOA\+86:(\d+)'; +my $NEW_LIN_RE = '^LIN'; 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\+([^:]+).*', + po_number => '^RFF\+ON:(\S+)', + invoice_ident => '^BGM\+380\+(.*)\+.*', + total_billed => '^MOA\+86:(\d+)' +); + +my %edi_li_fields = ( + id => '^RFF\+LI:\S+\/(\S+)', + index => '^LIN\+([^\+]+).*', + ident => '^LIN\+\S+\++([^:]+).*', + _ident => '^PIA\+0*5\+([^:]+).*', # ident may live in LIN or PIA + invoice_count => '^QTY\+47:(\d+).*', + order_count => '^QTY\+21:(\d+).*', + dispatch_count => '^QTY\+12:(\d+).*', + backorder_count => '^QTY\+83:(\d+).*', + delivered_count => '^QTY\+46:(\d+).*', + amount_billed => '^MOA\+203:(\d+)', + unit_price => '^PRI\+AAB:(\d+).*' +); + +my %edi_charge_fields = ( + charge_type => '^ALC\+C\++([^\+]+).*', + charge_amount => '^MOA\+(8|131):(\d+)' +); sub new { return bless({}, shift()); @@ -42,58 +52,57 @@ sub read_file { foreach (split(/'/, $edi)) { my $msg = $msgs[-1]; - if (/$NEW_MSG_RE/) { # starting a new message. + # - starting a new message + if (/$NEW_MSG_RE/) { $msg = {lineitems => [], misc_charges => []}; - ($msg->{msg_type} = $_) =~ s/$MSG_TYPE_RE/$1/; - push(@msgs, $msg); } + # extract top-level message fields + 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/; - ($msg->{po_number} = $_) =~ s/$PO_NUM_RE/$1/g if /$PO_NUM_RE/; + for my $field (keys %edi_fields) { + ($msg->{$field} = $_) =~ s/$edi_fields{$field}/$1/g + if /$edi_fields{$field}/; + } - if ($_ =~ /$NEW_LIN_RE/) { # starting a new lineitem + # - starting a new lineitem + if (/$NEW_LIN_RE/) { $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}->{id} = $_) =~ s/$LIN_ID_RE/$1/g if /$LIN_ID_RE/; - ($msg->{_current_li}->{ident} = $_) =~ s/$LIN_IDENT_2RE/$1/g if /$LIN_IDENT_2RE/; + # - extract lineitem fields - # counts - ($msg->{_current_li}->{invoice_count} = $_) =~ s/$LIN_NUM_INVOICED_RE/$1/g if /$LIN_NUM_INVOICED_RE/; - ($msg->{_current_li}->{order_count} = $_) =~ s/$LIN_NUM_ORDERED_RE/$1/g if /$LIN_NUM_ORDERED_RE/; - ($msg->{_current_li}->{dispatch_count} = $_) =~ s/$LIN_NUM_DISPATCH_RE/$1/g if /$LIN_NUM_DISPATCH_RE/; - ($msg->{_current_li}->{backorder_count} = $_) =~ s/$LIN_NUM_BACKORDER_RE/$1/g if /$LIN_NUM_BACKORDER_RE/; - ($msg->{_current_li}->{delivered_count} = $_) =~ s/$LIN_NUM_DELIVERED_RE/$1/g if /$LIN_NUM_DELIVERED_RE/; + if (my $li = $msg->{_current_li}) { - # prices - ($msg->{_current_li}->{amount_billed} = $_) =~ s/$LIN_AMOUNT_BILLED_RE/$1/g if /$LIN_AMOUNT_BILLED_RE/; - ($msg->{_current_li}->{unit_price} = $_) =~ s/$LIN_UNIT_PRICE_RE/$1/g if /$LIN_UNIT_PRICE_RE/; - } + for my $field (keys %edi_li_fields) { + ($li->{$field} = $_) =~ s/$edi_li_fields{$field}/$1/g + if /$edi_li_fields{$field}/; + } - # primarily for invoices + # move ident value found at secondary location to 'ident' + $li->{ident} = delete $li->{_ident} + if $li->{_ident} and not $li->{ident}; + } - ($msg->{invoice_ident} = $_) =~ s/$INV_IDENT_RE/$1/g if /$INV_IDENT_RE/; - ($msg->{total_billed} = $_) =~ s/$TOTAL_BILLED_RE/$1/g if /$TOTAL_BILLED_RE/; + # - starting a new misc. charge - if (/$MISC_CHARGE_TYPE_RE/) { - (my $type = $_) =~ s/$MISC_CHARGE_TYPE_RE/$1/g; - push (@{$msg->{misc_charges}}, {type => $type}); + if (/$edi_charge_fields{charge_type}/) { + $msg->{_current_charge} = {}; + push (@{$msg->{misc_charges}}, $msg->{_current_charge}); } - if (/$MISC_CHARGE_AMT_RE/) { - my $chg = $msg->{misc_charges}[-1]; - ($chg->{amount} = $_) =~ s/$MISC_CHARGE_AMT_RE/$1/g; + # - extract charge fields + + if (my $charge = $msg->{_current_charge}) { + for my $field (keys %edi_charge_fields) { + ($charge->{$field} = $_) =~ s/$edi_charge_fields{$field}/$1/g + if /$edi_charge_fields{$field}/; + } } } -- 2.11.0