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. EDI account linked to provider must have 'use_attrs' set to true.
53 5. PO must have no EDI ORDERS messages attached or, if it does,
54 the message has a status of "retry".
60 --osrf-config [/openils/conf/opensrf_core.xml]
63 Prints EDI that would be sent to STDOUT. No files are sent
64 and no edi_message's are created.
67 Process a specific PO instead of processing all available PO's
70 Log debug info to STDOUT. This script logs various information
71 via \$logger regardless of this option.
79 help() if $help or !$ops;
81 # $lvl should match a $logger logging function. E.g. 'info', 'error', etc.
87 # always announce errors and warnings
88 return unless $verbose || $lvl =~ /error|warn/;
90 my $date_str = DateTime->now(time_zone => 'local')->strftime('%F %T');
91 print "$date_str $msg\n";
95 OpenSRF::System->bootstrap_client(config_file => $osrf_config);
96 Fieldmapper->import(IDL =>
97 OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
98 OpenILS::Utils::CStoreEditor::init();
99 my $e = OpenILS::Utils::CStoreEditor->new;
101 announce('debug', "FTP_PASSIVE is ".($ENV{FTP_PASSIVE} ? "ON" : "OFF"));
106 # Caller provided a specific PO to process.
110 # Find PO's that have an order date set (i.e. are activated) and are
111 # linked to active providers that support EDI orders, but has no
112 # successful "ORDERS" edi_message attached.
114 my $ids = $e->json_query({
115 select => {acqpo => ['id']},
120 filter => {message_type => 'ORDERS'}
132 state => 'on-order', # on-order only
133 order_date => {'!=' => undef} # activated
137 edi_default => {'!=' => undef}
144 {id => undef}, # no ORDERS message exists
145 {status => 'retry'} # ORDERS needs re-sending
151 $po_ids = [map {$_->{id}} @$ids];
155 announce('info', "No purchase orders to process");
159 for $po_id (@$po_ids) {
161 my $edi = OpenILS::Utils::EDIWriter->new->write($po_id);
164 announce('error', "Unable to generate EDI for PO $po_id");
169 # Caller just wants the EDI printed to STDOUT
170 print "EDI for PO $po_id:\n$edi\n";
174 # this may be a retry attempt. if so, reuse the original edi_message
175 my $message = $e->search_acq_edi_message({
176 purchase_order => $po_id,
177 message_type => 'ORDERS',
182 $message = Fieldmapper::acq::edi_message->new;
183 $message->create_time('NOW');
184 $message->purchase_order($po_id);
185 $message->message_type('ORDERS');
188 my $po = $e->retrieve_acq_purchase_order([$po_id, {
191 acqpo => ['provider'],
192 acqpro => ['edi_default'],
196 if (!$po->provider->edi_default) {
197 announce('error', "Provider for PO $po_id has no default EDI ".
198 "account, EDI message will not be sent.");
202 $message->account($po->provider->edi_default->id);
208 if ($message->isnew) {
209 unless($e->create_acq_edi_message($message)) {
211 "Error creating acq.edi_message for PO $po_id: ".$e->die_event);
215 unless($e->update_acq_edi_message($message)) {
217 "Error updating acq.edi_message for PO $po_id: ".$e->die_event);
223 my $po = $e->retrieve_acq_purchase_order([
227 acqpo => ['provider'],
228 acqpro => ['edi_default'],
233 if (!$po->provider->edi_default) {
234 # Caller has provided a PO ID for a provider that does not
236 announce('error', "Cannot send EDI for PO $po_id, because the ".
237 "provider (".$po->provider->id.") is not configured to use EDI");
241 my $res = OpenILS::Application::Acq::EDI->send_core(
242 $po->provider->edi_default, [$message->id]);
244 if (my $out = $res->[0]) {
245 announce('info', "message ".$message->id." status: ".$out->status);
247 announce('error', "send_core failed for message ".$message->id);