]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Utils/EDIReader.pm
EDIReader : detect SAN vs. account number in buyer/seller
[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 );
40
41 my %edi_li_ident_fields = (
42     ident  => qr/^LIN\+\S+\++([^:]+):?(\S+)?/,
43     ident2 => qr/^PIA\+0*5\+([^:]+):?(\S+)?/, 
44 );
45
46 my %edi_li_quant_fields = (
47     code     => qr/^QTY\+(\d+):/,
48     quantity => qr/^QTY\+\d+:(\d+)/
49 );
50
51 my %edi_charge_fields = (
52     charge_type   => qr/^ALC\+C\++([^\+]+)/,
53     charge_amount => qr/^MOA\+(8|131):(\d+)/
54 );
55
56 sub new {
57     return bless({}, shift());
58 }
59
60 # see read()
61 sub read_file {
62     my $self = shift;
63     my $file = shift;
64
65     open(EDI_FILE, $file) or die "Cannot open $file: $!\n";
66     my $edi = join('', <EDI_FILE>);
67     close EDI_FILE;
68
69     return $self->read($edi);
70 }
71
72 # Reads an EDI string and parses the package one "line" at a time, extracting 
73 # needed information via regular expressions.  Returns an array of messages, 
74 # each represented as a hash.  See %edi_*fields above for lists of which fields 
75 # may be present within a message.
76
77 sub read {
78     my $self = shift;
79     my $edi = shift or return [];
80     my @msgs;
81
82     $edi =~ s/\n//og;
83
84     foreach (split(/'/, $edi)) {
85         my $msg = $msgs[-1];
86
87         # - starting a new message
88
89         if (/$NEW_MSG_RE/) { 
90             $msg = {lineitems => [], misc_charges => []};
91             push(@msgs, $msg);
92         }
93
94         # extract top-level message fields
95
96         next unless $msg;
97
98         for my $field (keys %edi_fields) {
99             ($msg->{$field}) = $_ =~ /$edi_fields{$field}/
100                 if /$edi_fields{$field}/;
101         }
102
103         # - starting a new lineitem
104
105         if (/$NEW_LIN_RE/) {
106             $msg->{_current_li} = {};
107             push(@{$msg->{lineitems}}, $msg->{_current_li});
108         }
109
110         # - extract lineitem fields
111
112         if (my $li = $msg->{_current_li}) {
113
114             for my $field (keys %edi_li_fields) {
115                 ($li->{$field}) = $_ =~ /$edi_li_fields{$field}/g
116                     if /$edi_li_fields{$field}/;
117             }
118
119             for my $field (keys %edi_li_ident_fields) {
120                 if (/$edi_li_ident_fields{$field}/) {
121                     my ($ident, $type) = $_ =~ /$edi_li_ident_fields{$field}/;
122                     push(@{$li->{identifiers}}, {code => $type, value => $ident});
123                 }
124             }
125
126             if (/$edi_li_quant_fields{quantity}/) {
127                 my $quant = {};
128                 ($quant->{quantity}) = $_ =~ /$edi_li_quant_fields{quantity}/;
129                 ($quant->{code}) = $_ =~ /$edi_li_quant_fields{code}/;
130                 push(@{$li->{quantities}}, $quant);
131             }
132
133         }
134
135         # - starting a new misc. charge
136
137         if (/$edi_charge_fields{charge_type}/) {
138             $msg->{_current_charge} = {};
139             push (@{$msg->{misc_charges}}, $msg->{_current_charge});
140         }
141
142         # - extract charge fields
143
144         if (my $charge = $msg->{_current_charge}) {
145             for my $field (keys %edi_charge_fields) {
146                 ($charge->{$field}) = $_ =~ /$edi_charge_fields{$field}/
147                     if /$edi_charge_fields{$field}/;
148             }
149         }
150     }
151
152     # remove the state-maintenance keys
153     for my $msg (@msgs) {
154         foreach (grep /^_/, keys %$msg) {
155             delete $msg->{$_};
156         }
157     }
158
159     return \@msgs;
160 }