2 # ---------------------------------------------------------------
3 # Copyright (C) 2010 Equinox Software, Inc
4 # Author: Joe Atzberger <jatzberger@esilibrary.com>
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.
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 # ---------------------------------------------------------------
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/;
36 'max-batch-size=i' => -1
39 my $cs = OpenILS::Utils::Cronscript->new(\%opts);
40 $cs->session('open-ils.acq') or die "No session created";
42 OpenILS::Utils::CStoreEditor::init();
45 my $ed = OpenILS::Utils::CStoreEditor->new(@_) or die "Failed to get new CStoreEditor";
50 my $hook = 'acqpo.activated';
51 my $defs = $e->search_action_trigger_event_definition({
53 reactor => 'GeneratePurchaseOrderJEDI',
57 # print Dumper($defs);
58 print "\nHook '$hook' is used in ", scalar(@$defs), " event definition(s):\n";
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;
66 # give me all completed JEDI events that link to purchase_orders
67 # that have not already been delivered to the vendor
69 select => {atev => ['id']},
72 event_def => $def->id,
76 select => {acqpo => ['id']},
81 select => {acqedim => ['purchase_order']},
83 where => {purchase_order => {'!=' => undef}}
90 order_by => {atev => ['add_time']}
93 $query->{limit} = $remaining if $remaining > 0;
95 my $events = $e->json_query($query);
96 $remaining -= scalar(@$events);
98 print "Event definition ", $def->id, " has ", scalar(@$events), " event(s)\n";
101 my $event = $e->retrieve_action_trigger_event([
103 {flesh => 1, flesh_fields => {atev => ['template_output']}}
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
114 acqpo => ['provider'],
115 acqpro => ['edi_default'],
120 $message->purchase_order($target->id);
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";
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";
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.
136 print "Writing new message + translation to DB\n";
138 $e->create_acq_edi_message($message) or warn "create_acq_edi_message failed! $!";
141 print "Calling send_core(...)\n";
142 my $res = OpenILS::Application::Acq::EDI->send_core($target->provider->edi_default, [$message->id]);
144 my $message_out = shift @$res;
145 print "\tmessage ", $message->id, " status: ", $message_out->status, "\n";
147 print STDERR "ERROR: send_core failed for message ", $message->id, "\n";