]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/edi_pusher.pl
Subsequent EDI patch from Joe Atzberger. In this installmanent, EDI really does...
[working/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::CStoreEditor;   # needs init() after IDL is loaded (by Cronscript session)
24 use OpenILS::Utils::Cronscript;
25 use OpenILS::Utils::Fieldmapper;
26 use OpenILS::Application::AppUtils;
27 use OpenILS::Application::Acq::EDI;
28
29 INIT {
30     $debug = 1;
31 }
32
33 my %opts = (
34     'quiet' => 0,
35 );
36
37 OpenILS::Utils::Cronscript->new()->session('open-ils.acq') or die "No session created";
38 OpenILS::Utils::CStoreEditor::init();
39
40 sub editor {
41     my $ed = OpenILS::Utils::CStoreEditor->new(@_) or die "Failed to get new CStoreEditor";
42     return $ed;
43 }
44
45 my $e = editor();
46 my $hook = 'format.po.jedi';
47 my $defs = $e->search_action_trigger_event_definition({hook => $hook});
48 # print Dumper($defs);
49 print "\nHook '$hook' is used in ", scalar(@$defs), " event definition(s):\n";
50
51 $Data::Dumper::Indent = 1;
52 foreach my $def (@$defs) {
53     printf "%3s - '%s'\n", $def->id, $def->name;
54     my $events = $e->search_action_trigger_event([
55         {event_def => $def->id},
56         {flesh => 1, flesh_fields => { atev => ['template_output'] }}
57     ]);
58     print "Event definition ", $def->id, " has ", scalar(@$events), " event(s)\n";
59     foreach (@$events) {
60         my $message = Fieldmapper::acq::edi_message->new;
61         $message->create_time('NOW');   # will need this later when we try to update from the object
62         print "Event ", $_->id, " targets PO ", $_->target, ":\n";  # target is an opaque identifier, so we cannot flesh it
63         print Dumper($_), "\n";
64         my $target = $e->retrieve_acq_purchase_order([              # instead we retrieve it separately
65             $_->target, {
66                 flesh => 2,
67                 flesh_fields => {
68                     acqpo  => ['provider'],
69                     acqpro => ['edi_default'],
70                 },
71             }
72         ]);
73
74         $debug and print "Target: ", Dumper($target), "\n";
75         my $logstr = sprintf "provider %s (%s)", $target->provider->id, $target->provider->name;
76         unless ($target->provider->edi_default and $message->account($target->provider->edi_default->id)) {
77             printf STDERR "ERROR: No edi_default account found for $logstr.  File will not be sent!\n";
78         }
79         $message->jedi($_->template_output()->data);
80         print "\ntarget->provider->edi_default->id: ", $target->provider->edi_default->id, "\n";
81         print "\nNow calling attempt_translation\n\n";
82         unless (OpenILS::Application::Acq::EDI->attempt_translation($message, 1)) {
83             print STDERR "ERROR: attempt_translation failed, skipping message\n";
84             next;
85             # The premise here is that if the translator failed, it is better to try again later from a "fresh" fetched file
86             # than to add a cascade of failing inscrutable copies of the same message(s) to our DB.  
87         }
88         print "Writing new message + translation to DB\n";
89         $e->xact_begin;
90         $e->create_acq_edi_message($message) or warn "create_acq_edi_message failed!  $!";
91         $e->xact_commit;
92
93         print "Calling send_core(...)\n";
94         my $res = OpenILS::Application::Acq::EDI->send_core($target->provider->edi_default, [$message->id]);
95         if (@$res) {
96             my $message_out = shift @$res;
97             print "\tmessage ", $message->id, " status: ", $message_out->status, "\n";
98         } else {
99             print STDERR "ERROR: send_core failed for message ", $message->id, "\n";
100         }
101     }
102 }
103
104 print "\ndone\n";