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