]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/edi_order_pusher.pl
LP#1373690 EDI 'use_attrs' configuration option
[Evergreen.git] / Open-ILS / src / support-scripts / edi_order_pusher.pl
1 #!/usr/bin/perl
2 # ---------------------------------------------------------------
3 # Copyright (C) 2016 King County Library System
4 # Author: Bill Erickson <berickxx@gmail.com>
5 #
6 # Copied heavily from edi_pusher.pl
7 #
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.
12 #
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 # ---------------------------------------------------------------
18 use strict;
19 use warnings;
20 use Getopt::Long;
21 use OpenSRF::Utils::Logger q/$logger/;
22 use OpenILS::Utils::Fieldmapper;
23 use OpenILS::Application::Acq::EDI;
24 use OpenILS::Utils::EDIWriter;
25
26 my $osrf_config = '/openils/conf/opensrf_core.xml';
27 my $po_id;
28 my $test_mode;
29 my $verbose;
30 my $help;
31
32 my $ops = GetOptions(
33     'osrf-config=s' => \$osrf_config,
34     'test-mode'     => \$test_mode,
35     'po-id=i'       => \$po_id,
36     'verbose'       => \$verbose,
37     'help'          => \$help
38 );
39
40 sub help {
41     print <<HELP;
42
43     Synopsis:
44
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:
48         
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".
55
56     Usage:
57
58         $0
59
60         --osrf-config [/openils/conf/opensrf_core.xml]
61
62         --test-mode
63             Prints EDI that would be sent to STDOUT.  No files are sent
64             and no edi_message's are created.
65
66         --po-id <po-id-value>
67             Process a specific PO instead of processing all available PO's
68
69         --verbose
70             Log debug info to STDOUT.  This script logs various information
71             via \$logger regardless of this option.
72
73         --help
74             Show this message.
75 HELP
76     exit 0;
77 }
78
79 help() if $help or !$ops;
80
81 # $lvl should match a $logger logging function.  E.g. 'info', 'error', etc.
82 sub announce {
83     my $lvl = shift;
84         my $msg = shift;
85     $logger->$lvl($msg);
86
87     # always announce errors and warnings
88     return unless $verbose || $lvl =~ /error|warn/;
89
90     my $date_str = DateTime->now(time_zone => 'local')->strftime('%F %T');
91     print "$date_str $msg\n";
92 }
93
94 # connect to osrf...
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;
100
101 announce('debug', "FTP_PASSIVE is ".($ENV{FTP_PASSIVE} ? "ON" : "OFF"));
102
103 my $po_ids;
104
105 if ($po_id) {
106     # Caller provided a specific PO to process.
107     $po_ids = [$po_id];
108
109 } else {
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.
113     
114     my $ids = $e->json_query({
115         select => {acqpo => ['id']},
116         from => {
117             acqpo => {
118                 acqedim => {
119                     type => 'left',
120                     filter => {message_type => 'ORDERS'}
121                 },
122                 acqpro => {
123                     join => {
124                         acqedi => {
125                         }
126                     }
127                 }
128             }
129         },
130         where => {
131             '+acqpo' => {
132                 order_date => {'!=' => undef} # activated
133             },
134             '+acqpro' => {
135                 active => 't', 
136                 edi_default => {'!=' => undef}
137             },
138             '+acqedi' => {
139                 use_attrs => 't'
140             },
141             '+acqedim' => {
142                 '-or' => [
143                     {id => undef}, # no ORDERS message exists
144                     {status => 'retry'} # ORDERS needs re-sending
145                 ]
146             }
147         }
148     });
149
150     $po_ids = [map {$_->{id}} @$ids];
151 }
152
153 if (!@$po_ids) {
154     announce('info', "No purchase orders to process");
155     exit 0;
156 }
157
158 for $po_id (@$po_ids) {
159
160     my $edi = OpenILS::Utils::EDIWriter->new->write($po_id);
161
162     if (!$edi) {
163         announce('error', "Unable to generate EDI for PO $po_id");
164         next;
165     }
166
167     if ($test_mode) {
168         # Caller just wants the EDI printed to STDOUT
169         print "EDI for PO $po_id:\n$edi\n";
170         next;
171     }
172
173     # this may be a retry attempt.  if so, reuse the original edi_message
174     my $message = $e->search_acq_edi_message({
175         purchase_order => $po_id,
176         message_type => 'ORDERS', 
177         status => 'retry'
178     })->[0];
179
180     if (!$message) {
181         $message = Fieldmapper::acq::edi_message->new;
182         $message->create_time('NOW');
183         $message->purchase_order($po_id);
184         $message->message_type('ORDERS');
185         $message->isnew(1);
186     }
187
188     $message->edi($edi);
189
190     $e->xact_begin;
191     if ($message->isnew) {
192         unless($e->create_acq_edi_message($message)) {
193             announce('error', 
194                 "Error creating acq.edi_message for PO $po_id: ".$e->die_event);
195             next;
196         }
197     } else {
198         unless($e->update_acq_edi_message($message)) {
199             announce('error', 
200                 "Error updating acq.edi_message for PO $po_id: ".$e->die_event);
201             next;
202         }
203     }
204     $e->xact_commit;
205
206     my $po = $e->retrieve_acq_purchase_order([
207         $po_id, {
208             flesh => 2,
209             flesh_fields => {
210                 acqpo  => ['provider'],
211                 acqpro => ['edi_default'],
212             }
213         }
214     ]);
215
216     if (!$po->provider->edi_default) {
217         # Caller has provided a PO ID for a provider that does not
218         # support EDI.  
219         announce('error', "Cannot send EDI for PO $po_id, because the ".
220             "provider (".$po->provider->id.") is not configured to use EDI");
221         next;
222     }
223
224     my $res = OpenILS::Application::Acq::EDI->send_core(
225         $po->provider->edi_default, [$message->id]);
226
227     if (my $out = $res->[0]) {
228         announce('info', "message ".$message->id." status: ".$out->status);
229     } else {
230         announce('error', "send_core failed for message ".$message->id);
231     }
232 }
233
234