]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Utils/EDIReader.pm
EDI response honor lineitem-level status; debit cleanup
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Utils / EDIReader.pm
1 # ---------------------------------------------------------------
2 # Copyright (C) 2012 Equinox Software, Inc
3 # Author: Bill Erickson <berickr@esilibrary.com>
4 #
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.
9
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;
17
18 my $NEW_MSG_RE = '^UNH'; # starts a new message
19 my $NEW_LIN_RE = '^LIN'; # starts a new line item
20
21 my %edi_fields = (
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 );
31
32 my %edi_li_fields = (
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/
43 );
44
45 my %edi_li_ident_fields = (
46     ident  => qr/^LIN\+\S+\++([^:]+):?(\S+)?/,
47     ident2 => qr/^PIA\+0*5\+([^:]+):?(\S+)?/, 
48 );
49
50 my %edi_li_quant_fields = (
51     code     => qr/^QTY\+(\d+):/,
52     quantity => qr/^QTY\+\d+:(\d+)/
53 );
54
55 my %edi_charge_fields = (
56     charge_type   => qr/^ALC\+C\++([^\+]+)/,
57     charge_amount => qr/^MOA\+(8|131):(\d+)/
58 );
59
60 sub new {
61     return bless({}, shift());
62 }
63
64 # see read()
65 sub read_file {
66     my $self = shift;
67     my $file = shift;
68
69     open(EDI_FILE, $file) or die "Cannot open $file: $!\n";
70     my $edi = join('', <EDI_FILE>);
71     close EDI_FILE;
72
73     return $self->read($edi);
74 }
75
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.
80
81 sub read {
82     my $self = shift;
83     my $edi = shift or return [];
84     my @msgs;
85
86     $edi =~ s/\n//og;
87
88     foreach (split(/'/, $edi)) {
89         my $msg = $msgs[-1];
90
91         # - starting a new message
92
93         if (/$NEW_MSG_RE/) { 
94             $msg = {lineitems => [], misc_charges => []};
95             push(@msgs, $msg);
96         }
97
98         # extract top-level message fields
99
100         next unless $msg;
101
102         for my $field (keys %edi_fields) {
103             ($msg->{$field}) = $_ =~ /$edi_fields{$field}/
104                 if /$edi_fields{$field}/;
105         }
106
107         # - starting a new lineitem
108
109         if (/$NEW_LIN_RE/) {
110             $msg->{_current_li} = {};
111             push(@{$msg->{lineitems}}, $msg->{_current_li});
112         }
113
114         # - extract lineitem fields
115
116         if (my $li = $msg->{_current_li}) {
117
118             for my $field (keys %edi_li_fields) {
119                 ($li->{$field}) = $_ =~ /$edi_li_fields{$field}/g
120                     if /$edi_li_fields{$field}/;
121             }
122
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});
127                 }
128             }
129
130             if (/$edi_li_quant_fields{quantity}/) {
131                 my $quant = {};
132                 ($quant->{quantity}) = $_ =~ /$edi_li_quant_fields{quantity}/;
133                 ($quant->{code}) = $_ =~ /$edi_li_quant_fields{code}/;
134                 push(@{$li->{quantities}}, $quant);
135             }
136
137         }
138
139         # - starting a new misc. charge
140
141         if (/$edi_charge_fields{charge_type}/) {
142             $msg->{_current_charge} = {};
143             push (@{$msg->{misc_charges}}, $msg->{_current_charge});
144         }
145
146         # - extract charge fields
147
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}/;
152             }
153         }
154     }
155
156     # remove the state-maintenance keys
157     for my $msg (@msgs) {
158         foreach (grep /^_/, keys %$msg) {
159             delete $msg->{$_};
160         }
161     }
162
163     return \@msgs;
164 }