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+)/
33 id => qr/^RFF\+LI:\S+\/(\S+)/,
34 index => qr/^LIN\+([^\+]+)/,
35 amount_billed => qr/^MOA\+203:(\d+)/,
36 net_unit_price => qr/^PRI\+AAA:(\d+)/,
37 gross_unit_price=> qr/^PRI\+AAB:(\d+)/,
38 expected_date => qr/^DTM\+44:([^:]+)/,
39 avail_status => qr/^FTX\+LIN\++([^:]+):8B:28/,
40 # "1B" codes are deprecated, but still in use.
41 # Pretend it's "12B" and it should just work
42 order_status => qr/^FTX\+LIN\++([^:]+):12?B:28/
45 my %edi_li_ident_fields = (
46 ident => qr/^LIN\+\S+\++([^:]+):?(\S+)?/,
47 ident2 => qr/^PIA\+0*5\+([^:]+):?(\S+)?/,
50 my %edi_li_quant_fields = (
51 code => qr/^QTY\+(\d+):/,
52 quantity => qr/^QTY\+\d+:(\d+)/
55 my %edi_charge_fields = (
56 charge_type => qr/^ALC\+C\++([^\+]+)/,
57 charge_amount => qr/^MOA\+(8|131):(\d+)/
61 return bless({}, shift());
69 open(EDI_FILE, $file) or die "Cannot open $file: $!\n";
70 my $edi = join('', <EDI_FILE>);
73 return $self->read($edi);
76 # Reads an EDI string and parses the package one "line" at a time, extracting
77 # needed information via regular expressions. Returns an array of messages,
78 # each represented as a hash. See %edi_*fields above for lists of which fields
79 # may be present within a message.
83 my $edi = shift or return [];
88 foreach (split(/'/, $edi)) {
91 # - starting a new message
94 $msg = {lineitems => [], misc_charges => []};
98 # extract top-level message fields
102 for my $field (keys %edi_fields) {
103 ($msg->{$field}) = $_ =~ /$edi_fields{$field}/
104 if /$edi_fields{$field}/;
107 # - starting a new lineitem
110 $msg->{_current_li} = {};
111 push(@{$msg->{lineitems}}, $msg->{_current_li});
114 # - extract lineitem fields
116 if (my $li = $msg->{_current_li}) {
118 for my $field (keys %edi_li_fields) {
119 ($li->{$field}) = $_ =~ /$edi_li_fields{$field}/g
120 if /$edi_li_fields{$field}/;
123 for my $field (keys %edi_li_ident_fields) {
124 if (/$edi_li_ident_fields{$field}/) {
125 my ($ident, $type) = $_ =~ /$edi_li_ident_fields{$field}/;
126 push(@{$li->{identifiers}}, {code => $type, value => $ident});
130 if (/$edi_li_quant_fields{quantity}/) {
132 ($quant->{quantity}) = $_ =~ /$edi_li_quant_fields{quantity}/;
133 ($quant->{code}) = $_ =~ /$edi_li_quant_fields{code}/;
134 push(@{$li->{quantities}}, $quant);
139 # - starting a new misc. charge
141 if (/$edi_charge_fields{charge_type}/) {
142 $msg->{_current_charge} = {};
143 push (@{$msg->{misc_charges}}, $msg->{_current_charge});
146 # - extract charge fields
148 if (my $charge = $msg->{_current_charge}) {
149 for my $field (keys %edi_charge_fields) {
150 ($charge->{$field}) = $_ =~ /$edi_charge_fields{$field}/
151 if /$edi_charge_fields{$field}/;
156 # remove the state-maintenance keys
157 for my $msg (@msgs) {
158 foreach (grep /^_/, keys %$msg) {