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;
37 OpenILS::Utils::Cronscript->new()->session('open-ils.acq') or die "No session created";
38 OpenILS::Utils::CStoreEditor::init();
41 my $ed = OpenILS::Utils::CStoreEditor->new(@_) or die "Failed to get new CStoreEditor";
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";
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'] }}
58 print "Event definition ", $def->id, " has ", scalar(@$events), " event(s)\n";
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
68 acqpo => ['provider'],
69 acqpro => ['edi_default'],
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";
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";
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.
88 print "Writing new message + translation to DB\n";
90 $e->create_acq_edi_message($message) or warn "create_acq_edi_message failed! $!";
93 print "Calling send_core(...)\n";
94 my $res = OpenILS::Application::Acq::EDI->send_core($target->provider->edi_default, [$message->id]);
96 my $message_out = shift @$res;
97 print "\tmessage ", $message->id, " status: ", $message_out->status, "\n";
99 print STDERR "ERROR: send_core failed for message ", $message->id, "\n";