2 # ---------------------------------------------------------------
3 # Copyright (C) 2016 King County Library System
4 # Author: Bill Erickson <berickxx@gmail.com>
6 # Copied heavily from edi_pusher.pl
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 # ---------------------------------------------------------------
21 use OpenSRF::Utils::Logger q/$logger/;
22 use OpenILS::Utils::Fieldmapper;
23 use OpenILS::Application::Acq::EDI;
24 use OpenILS::Utils::EDIWriter;
26 my $osrf_config = '/openils/conf/opensrf_core.xml';
33 'osrf-config=s' => \$osrf_config,
34 'test-mode' => \$test_mode,
36 'verbose' => \$verbose,
45 Generate and deliver 'ORDERS' EDI for purchase orders. Unless a
46 specific PO is provided (via --po-id), EDI messages will be
47 generated for all PO's that meet the following conditions:
49 1. PO must be activated.
50 2. PO provider must be active.
51 3. PO must use a provider that supports EDI delivery (via edi_default)
52 4. PO must have no EDI ORDERS messages attached or, if it does,
53 the message has a status of "retry".
59 --osrf-config [/openils/conf/opensrf_core.xml]
62 Prints EDI that would be sent to STDOUT. No files are sent
63 and no edi_message's are created.
66 Process a specific PO instead of processing all available PO's
69 Log debug info to STDOUT. This script logs various information
70 via \$logger regardless of this option.
78 help() if $help or !$ops;
80 # $lvl should match a $logger logging function. E.g. 'info', 'error', etc.
86 # always announce errors and warnings
87 return unless $verbose || $lvl =~ /error|warn/;
89 my $date_str = DateTime->now(time_zone => 'local')->strftime('%F %T');
90 print "$date_str $msg\n";
94 OpenSRF::System->bootstrap_client(config_file => $osrf_config);
95 Fieldmapper->import(IDL =>
96 OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
97 OpenILS::Utils::CStoreEditor::init();
98 my $e = OpenILS::Utils::CStoreEditor->new;
100 announce('debug', "FTP_PASSIVE is ".($ENV{FTP_PASSIVE} ? "ON" : "OFF"));
105 # Caller provided a specific PO to process.
109 # Find PO's that have an order date set (i.e. are activated) and are
110 # linked to active providers that support EDI orders, but has no
111 # successful "ORDERS" edi_message attached.
113 my $ids = $e->json_query({
114 select => {acqpo => ['id']},
119 filter => {message_type => 'ORDERS'}
126 order_date => {'!=' => undef} # activated
130 edi_default => {'!=' => undef}
134 {id => undef}, # no ORDERS message exists
135 {status => 'retry'} # ORDERS needs re-sending
141 $po_ids = [map {$_->{id}} @$ids];
145 announce('info', "No purchase orders to process");
149 for $po_id (@$po_ids) {
151 my $edi = OpenILS::Utils::EDIWriter->new->write($po_id);
154 announce('error', "Unable to generate EDI for PO $po_id");
159 # Caller just wants the EDI printed to STDOUT
160 print "EDI for PO $po_id:\n$edi\n";
164 # this may be a retry attempt. if so, reuse the original edi_message
165 my $message = $e->search_acq_edi_message({
166 purchase_order => $po_id,
167 message_type => 'ORDERS',
172 $message = Fieldmapper::acq::edi_message->new;
173 $message->create_time('NOW');
174 $message->purchase_order($po_id);
175 $message->message_type('ORDERS');
182 if ($message->isnew) {
183 unless($e->create_acq_edi_message($message)) {
185 "Error creating acq.edi_message for PO $po_id: ".$e->die_event);
189 unless($e->update_acq_edi_message($message)) {
191 "Error updating acq.edi_message for PO $po_id: ".$e->die_event);
197 my $po = $e->retrieve_acq_purchase_order([
201 acqpo => ['provider'],
202 acqpro => ['edi_default'],
207 if (!$po->provider->edi_default) {
208 # Caller has provided a PO ID for a provider that does not
210 announce('error', "Cannot send EDI for PO $po_id, because the ".
211 "provider (".$po->provider->id.") is not configured to use EDI");
215 my $res = OpenILS::Application::Acq::EDI->send_core(
216 $po->provider->edi_default, [$message->id]);
218 if (my $out = $res->[0]) {
219 announce('info', "message ".$message->id." status: ".$out->status);
221 announce('error', "send_core failed for message ".$message->id);