]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/edi_pusher.pl
The whole point of test mode, NOT actually committing actions.
[Evergreen.git] / Open-ILS / src / support-scripts / edi_pusher.pl
1 #!/usr/bin/perl
2 # ---------------------------------------------------------------
3 # Copyright (C) 2010 Equinox Software, Inc
4 # Author: Joe Atzberger <jatzberger@esilibrary.com>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # ---------------------------------------------------------------
16
17 use strict;
18 use warnings;
19
20 use Data::Dumper;
21 use vars qw/$debug/;
22
23 use OpenILS::Utils::Cronscript;
24 use OpenILS::Utils::Fieldmapper;
25 use OpenILS::Application::AppUtils;
26 use OpenILS::Application::Acq::EDI;
27 use OpenSRF::Utils::Logger q/$logger/;
28
29 INIT {
30     $debug = 1;
31 }
32
33 my %defaults = (
34     'quiet' => 0,
35     'test'  => 0,   # TODO
36     'max-batch-size=i' => -1
37 );
38
39 my $cs = OpenILS::Utils::Cronscript->new(\%defaults);
40
41 my $opts = $cs->MyGetOptions();
42 my $e    = $cs->editor() or die "Failed to get new CStoreEditor";
43 my $hook = 'acqpo.activated';
44 my $defs = $e->search_action_trigger_event_definition({
45     hook    => $hook,
46     reactor => 'GeneratePurchaseOrderJEDI',
47     active  => 't'
48 });
49
50 $opts->{verbose} = 0 if $opts->{quiet};
51
52 print "FTP_PASSIVE is ", ($ENV{FTP_PASSIVE} ? "ON" : "OFF"),  "\n";
53
54 print "\nHook '$hook' is used in ", scalar(@$defs), " event definition(s):\n";
55
56 $Data::Dumper::Indent = 1;
57 my $remaining = $opts->{'max-batch-size'};
58
59 # FIXME: this is the disclusion subquery.  It discludes any PO that has
60 # a non-retry edi_message linked to it.  But that means that if there are
61 # mutliple EDI messages (say, some failed translation) and one marked retry,
62 # the PO is still discluded!  Perhaps there should never be multiple messages,
63 # but that makes testing much trickier (and is not DB-enforced).
64 #
65 # One approach might be to supplementally query for any "retry" messages that 
66 # are on active providers (and deduplicate).  
67
68 my $subq = {
69     select => { acqedim => ['purchase_order'] },
70     from   => 'acqedim',
71     where  => {
72         message_type   => 'ORDERS',
73         status         => {'!=' => 'retry' },
74         purchase_order => {'!=' => undef   }
75     }
76 };
77
78 foreach my $def (@$defs) {
79     last if $remaining == 0;
80     printf "%3s - '%s'\n", $def->id, $def->name;
81
82     # give me all completed JEDI events that link to purchase_orders 
83     # that have no delivery attempts or are in the retry state
84
85     my $query = {
86         select => {atev => ['id']},
87         from   => 'atev',
88         where  => {
89             event_def => $def->id,
90             state  => 'complete',
91             target => {
92                 'not in' => $subq
93             }
94         },
95         order_by => {atev => ['add_time']}
96     };
97
98     $query->{limit} = $remaining if $remaining > 0;
99
100     if ($opts->{verbose}) {
101         # $subq->{'select'}->{'acqedim'} = ['id', 'purchase_order', 'message_type', 'status'];
102         my $excluded = $e->json_query($subq);
103         print "Excluded: ", scalar(@$excluded), " purchase order(s):\n";
104         print join("\n", sort map {sprintf "%7d", $_->{purchase_order}} @$excluded), "\n";
105     }
106
107     my $events = $e->json_query($query);
108
109     if(!$events) {
110         print STDERR   "error querying JEDI events for event definition ", $def->id, "\n";
111         $logger->error("error querying JEDI events for event definition ". $def->id);
112         next;
113     }
114
115     $remaining -= scalar(@$events);
116
117     print "Event definition ", $def->id, " has ", scalar(@$events), " (completed) event(s)\n";
118     foreach (@$events) {
119
120         my $event = $e->retrieve_action_trigger_event([
121             $_->{id}, 
122             {flesh => 1, flesh_fields => {atev => ['template_output']}}
123         ]);
124
125
126         my $target = $e->retrieve_acq_purchase_order([              # instead we retrieve it separately
127             $event->target, {
128                 flesh => 2,
129                 flesh_fields => {
130                     acqpo  => ['provider'],
131                     acqpro => ['edi_default'],
132                 },
133             }
134         ]);
135
136         # this may be a retry attempt.  if so, reuse the original edi_message
137         my $message = $e->search_acq_edi_message({
138             purchase_order => $target->id,
139             message_type => 'ORDERS', 
140             status => 'retry'
141         })->[0];
142
143         if(!$message) {
144             $message = Fieldmapper::acq::edi_message->new;
145             $message->create_time('NOW');   # will need this later when we try to update from the object
146             $message->purchase_order($target->id);
147             $message->message_type('ORDERS');
148             $message->isnew(1);
149         }
150
151         my $logstr = sprintf "provider %s (%s)", $target->provider->id, $target->provider->name;
152         unless ($target->provider->edi_default and $message->account($target->provider->edi_default->id)) {
153             printf STDERR "ERROR: No edi_default account found for $logstr.  File will not be sent!\n";
154         }
155
156         $message->jedi($event->template_output()->data);
157
158         print "\ntarget->provider->edi_default->id: ", $target->provider->edi_default->id, "\n";
159         my $logstr2 = sprintf "event %s, PO %s, template_output %s", $_->{id}, $message->purchase_order, $event->template_output->id;
160         if ($opts->{test}) {
161             print "Test mode, skipping translation/send\n";
162             next;
163         }
164
165         printf "\nNow calling attempt_translation for $logstr2\n\n";
166
167         unless (OpenILS::Application::Acq::EDI->attempt_translation($message, 1)) {
168             print STDERR "ERROR: attempt_translation failed for $logstr2\n";
169             next;
170             # The premise here is that if the translator failed, it is better to try again later from a "fresh" fetched file
171             # than to add a cascade of failing inscrutable copies of the same message(s) to our DB.  
172         }
173
174         print "Writing new message + translation to DB for $logstr2\n";
175
176         $e->xact_begin;
177         if($message->isnew) {
178             unless($e->create_acq_edi_message($message)) {
179                 $logger->error("Error creating acq.edi_message for $logstr2: ".$e->die_event);
180                 next;
181             }
182         } else {
183             unless($e->update_acq_edi_message($message)) {
184                 $logger->error("Error updating acq.edi_message for $logstr2: ".$e->die_event);
185                 next;
186             }
187         }
188         $e->xact_commit;
189
190         print "Calling send_core(...) for message (", $message->id, ")\n";
191         my $res = OpenILS::Application::Acq::EDI->send_core($target->provider->edi_default, [$message->id]);
192         if (@$res) {
193             my $message_out = shift @$res;
194             print "\tmessage ", $message->id, " status: ", $message_out->status, "\n";
195         } else {
196             print STDERR "ERROR: send_core failed for message ", $message->id, "\n";
197         }
198     }
199 }
200
201 print "\ndone\n";
202
203 __END__
204
205 =head1 NAME
206
207 edi_pusher.pl - A script for generating and sending EDI files to remote accounts.
208
209 =head1 DESCRIPTION
210
211 This script is expected to be run via crontab, for the purpose of retrieving vendor EDI files.
212
213 =head1 OPTIONS
214
215   --max-batch-size=i  Limit the processing to a set number of events.
216
217 =head1 TODO
218
219 More docs here.
220
221 =head1 USAGE
222
223 B<FTP_PASSIVE=1> is recommended.  Depending on your vendors' and your own network environments, you may want to set/export
224 the environmental variable FTP_PASSIVE like:
225
226     export FTP_PASSIVE=1
227     # or
228     FTP_PASSIVE=1 Open-ILS/src/support-scripts/edi_pusher.pl
229
230 =head1 SEE ALSO
231
232     OpenILS::Utils::Cronscript
233     edi_fetcher.pl
234
235 =head1 AUTHOR
236
237 Joe Atzberger <jatzberger@esilibrary.com>
238
239 =cut