]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/edi_pusher.pl
fixed some typos, thinkos in EDI cross-walk
[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 use OpenSRF::Utils::Logger q/$logger/;
29
30 INIT {
31     $debug = 1;
32 }
33
34 my %opts = (
35     'quiet' => 0,
36     'max-batch-size=i' => -1
37 );
38
39 my $cs = OpenILS::Utils::Cronscript->new(\%opts);
40 $cs->session('open-ils.acq') or die "No session created";
41
42 OpenILS::Utils::CStoreEditor::init();
43
44 sub editor {
45     my $ed = OpenILS::Utils::CStoreEditor->new(@_) or die "Failed to get new CStoreEditor";
46     return $ed;
47 }
48
49 my $e = editor();
50 my $hook = 'acqpo.activated';
51 my $defs = $e->search_action_trigger_event_definition({
52     hook => $hook, 
53     reactor => 'GeneratePurchaseOrderJEDI',
54     active => 't'
55 });
56
57 # print Dumper($defs);
58 print "\nHook '$hook' is used in ", scalar(@$defs), " event definition(s):\n";
59
60 $Data::Dumper::Indent = 1;
61 my $remaining = $cs->first_defined('max-batch-size');
62 foreach my $def (@$defs) {
63     last if $remaining == 0;
64     printf "%3s - '%s'\n", $def->id, $def->name;
65
66     # give me all completed JEDI events that link to purchase_orders 
67     # that have not already been delivered to the vendor
68     my $query = {
69         select => {atev => ['id']},
70         from => 'atev',
71         where => {
72             event_def => $def->id,
73             state => 'complete',
74             target => {
75                 in => {
76                     select => {acqpo => ['id']},
77                     from => 'acqpo',
78                     where => {
79                         id => {
80                             'not in' => {
81                                 select => {acqedim => ['purchase_order']},
82                                 from => 'acqedim',
83                                 where => {purchase_order => {'!=' => undef}}
84                             }
85                         }
86                     }
87                 }
88             }
89         },
90         order_by => {atev => ['add_time']}
91     };
92
93     $query->{limit} = $remaining if $remaining > 0;
94
95     my $events = $e->json_query($query);
96     $remaining -= scalar(@$events);
97
98     print "Event definition ", $def->id, " has ", scalar(@$events), " event(s)\n";
99     foreach (@$events) {
100
101         my $event = $e->retrieve_action_trigger_event([
102             $_->{id}, 
103             {flesh => 1, flesh_fields => {atev => ['template_output']}}
104         ]);
105
106         my $message = Fieldmapper::acq::edi_message->new;
107         $message->create_time('NOW');   # will need this later when we try to update from the object
108         print "Event ", $event->id, " targets PO ", $event->target, ":\n";  # target is an opaque identifier, so we cannot flesh it
109         print Dumper($event), "\n";
110         my $target = $e->retrieve_acq_purchase_order([              # instead we retrieve it separately
111             $event->target, {
112                 flesh => 2,
113                 flesh_fields => {
114                     acqpo  => ['provider'],
115                     acqpro => ['edi_default'],
116                 },
117             }
118         ]);
119
120         $message->purchase_order($target->id);
121
122         $debug and print "Target: ", Dumper($target), "\n";
123         my $logstr = sprintf "provider %s (%s)", $target->provider->id, $target->provider->name;
124         unless ($target->provider->edi_default and $message->account($target->provider->edi_default->id)) {
125             printf STDERR "ERROR: No edi_default account found for $logstr.  File will not be sent!\n";
126         }
127         $message->jedi($event->template_output()->data);
128         print "\ntarget->provider->edi_default->id: ", $target->provider->edi_default->id, "\n";
129         print "\nNow calling attempt_translation\n\n";
130         unless (OpenILS::Application::Acq::EDI->attempt_translation($message, 1)) {
131             print STDERR "ERROR: attempt_translation failed, skipping message\n";
132             next;
133             # The premise here is that if the translator failed, it is better to try again later from a "fresh" fetched file
134             # than to add a cascade of failing inscrutable copies of the same message(s) to our DB.  
135         }
136         print "Writing new message + translation to DB\n";
137         $e->xact_begin;
138         $e->create_acq_edi_message($message) or warn "create_acq_edi_message failed!  $!";
139         $e->xact_commit;
140
141         print "Calling send_core(...)\n";
142         my $res = OpenILS::Application::Acq::EDI->send_core($target->provider->edi_default, [$message->id]);
143         if (@$res) {
144             my $message_out = shift @$res;
145             print "\tmessage ", $message->id, " status: ", $message_out->status, "\n";
146         } else {
147             print STDERR "ERROR: send_core failed for message ", $message->id, "\n";
148         }
149     }
150 }
151
152 print "\ndone\n";