1 # ---------------------------------------------------------------
2 # Copyright (C) 2012 Equinox Software, Inc
3 # Author: Bill Erickson <berickr@esilibrary.com>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
15 package OpenILS::Utils::EDIReader;
16 use strict; use warnings;
18 my $NEW_MSG_RE = '^UNH'; # starts a new message
19 my $NEW_LIN_RE = '^LIN'; # starts a new line item
22 message_type => qr/^UNH\+\d+\+(\S{6})/,
23 buyer_san => qr/^NAD\+BY\+([^:]+)::31B/,
24 buyer_acct => qr/^NAD\+BY\+([^:]+)::91/,
25 vendor_san => qr/^NAD\+SU\+([^:]+)::31B/,
26 vendor_acct => qr/^NAD\+SU\+([^:]+)::91/,
27 purchase_order => qr/^RFF\+ON:(\S+)/,
28 invoice_ident => qr/^BGM\+380\+([^\+]+)/,
29 total_billed => qr/^MOA\+86:(\d+)/,
30 invoice_date => qr/^DTM\+137:([^:]+)/
34 id => qr/^RFF\+LI:\S+\/(\S+)/,
35 index => qr/^LIN\+([^\+]+)/,
36 amount_billed => qr/^MOA\+203:(\d+)/,
37 net_unit_price => qr/^PRI\+AAA:(\d+)/,
38 gross_unit_price=> qr/^PRI\+AAB:(\d+)/,
39 expected_date => qr/^DTM\+44:([^:]+)/,
40 avail_status => qr/^FTX\+LIN\++([^:]+):8B:28/,
41 # "1B" codes are deprecated, but still in use.
42 # Pretend it's "12B" and it should just work
43 order_status => qr/^FTX\+LIN\++([^:]+):12?B:28/
46 my %edi_li_ident_fields = (
47 ident => qr/^LIN\+\S+\++([^:]+):?(\S+)?/,
48 ident2 => qr/^PIA\+0*5\+([^:]+):?(\S+)?/,
51 my %edi_li_quant_fields = (
52 code => qr/^QTY\+(\d+):/,
53 quantity => qr/^QTY\+\d+:(\d+)/
56 my %edi_charge_fields = (
57 charge_type => qr/^ALC\+C\++([^\+]+)/,
58 charge_amount => qr/^MOA\+(8|131):(\d+)/
62 return bless({}, shift());
70 open(EDI_FILE, $file) or die "Cannot open $file: $!\n";
71 my $edi = join('', <EDI_FILE>);
74 return $self->read($edi);
77 # Reads an EDI string and parses the package one "line" at a time, extracting
78 # needed information via regular expressions. Returns an array of messages,
79 # each represented as a hash. See %edi_*fields above for lists of which fields
80 # may be present within a message.
84 my $edi = shift or return [];
89 foreach (split(/'/, $edi)) {
92 # - starting a new message
95 $msg = {lineitems => [], misc_charges => []};
99 # extract top-level message fields
103 for my $field (keys %edi_fields) {
104 ($msg->{$field}) = $_ =~ /$edi_fields{$field}/
105 if /$edi_fields{$field}/;
108 # - starting a new lineitem
111 $msg->{_current_li} = {};
112 push(@{$msg->{lineitems}}, $msg->{_current_li});
115 # - extract lineitem fields
117 if (my $li = $msg->{_current_li}) {
119 for my $field (keys %edi_li_fields) {
120 ($li->{$field}) = $_ =~ /$edi_li_fields{$field}/g
121 if /$edi_li_fields{$field}/;
124 for my $field (keys %edi_li_ident_fields) {
125 if (/$edi_li_ident_fields{$field}/) {
126 my ($ident, $type) = $_ =~ /$edi_li_ident_fields{$field}/;
127 push(@{$li->{identifiers}}, {code => $type, value => $ident});
131 if (/$edi_li_quant_fields{quantity}/) {
133 ($quant->{quantity}) = $_ =~ /$edi_li_quant_fields{quantity}/;
134 ($quant->{code}) = $_ =~ /$edi_li_quant_fields{code}/;
135 push(@{$li->{quantities}}, $quant);
140 # - starting a new misc. charge
142 if (/$edi_charge_fields{charge_type}/) {
143 $msg->{_current_charge} = {};
144 push (@{$msg->{misc_charges}}, $msg->{_current_charge});
147 # - extract charge fields
149 if (my $charge = $msg->{_current_charge}) {
150 for my $field (keys %edi_charge_fields) {
151 ($charge->{$field}) = $_ =~ /$edi_charge_fields{$field}/
152 if /$edi_charge_fields{$field}/;
157 # remove the state-maintenance keys
158 for my $msg (@msgs) {
159 foreach (grep /^_/, keys %$msg) {