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;
35 'max-batch-size=i' => -1
38 my $cs = OpenILS::Utils::Cronscript->new(\%opts);
39 $cs->session('open-ils.acq') or die "No session created";
41 OpenILS::Utils::CStoreEditor::init();
44 my $ed = OpenILS::Utils::CStoreEditor->new(@_) or die "Failed to get new CStoreEditor";
49 my $hook = 'acqpo.activated';
50 my $defs = $e->search_action_trigger_event_definition({
52 reactor => 'GeneratePurchaseOrderJEDI',
56 # print Dumper($defs);
57 print "\nHook '$hook' is used in ", scalar(@$defs), " event definition(s):\n";
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;
65 # give me all completed JEDI events that link to purchase_orders
66 # that have not already been delivered to the vendor
68 select => {atev => ['id']},
71 event_def => $def->id,
75 select => {acqpo => ['id']},
80 select => {acqedim => ['purchase_order']},
82 where => {purchase_order => {'!=' => undef}}
89 order_by => {atev => 'add_time'}
92 $query->{limit} = $remaining if $remaining > 0;
94 my $events = $e->json_query($query);
95 $remaining -= scalar(@$events);
97 print "Event definition ", $def->id, " has ", scalar(@$events), " event(s)\n";
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
108 acqpo => ['provider'],
109 acqpro => ['edi_default'],
114 $message->purchase_order($target->id);
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";
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";
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.
130 print "Writing new message + translation to DB\n";
132 $e->create_acq_edi_message($message) or warn "create_acq_edi_message failed! $!";
135 print "Calling send_core(...)\n";
136 my $res = OpenILS::Application::Acq::EDI->send_core($target->provider->edi_default, [$message->id]);
138 my $message_out = shift @$res;
139 print "\tmessage ", $message->id, " status: ", $message_out->status, "\n";
141 print STDERR "ERROR: send_core failed for message ", $message->id, "\n";