]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/edi_order_pusher.pl
LP#1373690 EDI ORDERS generator script
[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. PO must have no EDI ORDERS messages attached or, if it does, 
53            the message has a status of "retry".
54
55     Usage:
56
57         $0
58
59         --osrf-config [/openils/conf/opensrf_core.xml]
60
61         --test-mode
62             Prints EDI that would be sent to STDOUT.  No files are sent
63             and no edi_message's are created.
64
65         --po-id <po-id-value>
66             Process a specific PO instead of processing all available PO's
67
68         --verbose
69             Log debug info to STDOUT.  This script logs various information
70             via \$logger regardless of this option.
71
72         --help
73             Show this message.
74 HELP
75     exit 0;
76 }
77
78 help() if $help or !$ops;
79
80 # $lvl should match a $logger logging function.  E.g. 'info', 'error', etc.
81 sub announce {
82     my $lvl = shift;
83         my $msg = shift;
84     $logger->$lvl($msg);
85
86     # always announce errors and warnings
87     return unless $verbose || $lvl =~ /error|warn/;
88
89     my $date_str = DateTime->now(time_zone => 'local')->strftime('%F %T');
90     print "$date_str $msg\n";
91 }
92
93 # connect to osrf...
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;
99
100 announce('debug', "FTP_PASSIVE is ".($ENV{FTP_PASSIVE} ? "ON" : "OFF"));
101
102 my $po_ids;
103
104 if ($po_id) {
105     # Caller provided a specific PO to process.
106     $po_ids = [$po_id];
107
108 } else {
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.
112     
113     my $ids = $e->json_query({
114         select => {acqpo => ['id']},
115         from => {
116             acqpo => {
117                 acqedim => {
118                     type => 'left',
119                     filter => {message_type => 'ORDERS'}
120                 },
121                 acqpro => {}
122             }
123         },
124         where => {
125             '+acqpo' => {
126                 order_date => {'!=' => undef} # activated
127             },
128             '+acqpro' => {
129                 active => 't', 
130                 edi_default => {'!=' => undef}
131             },
132             '+acqedim' => {
133                 '-or' => [
134                     {id => undef}, # no ORDERS message exists
135                     {status => 'retry'} # ORDERS needs re-sending
136                 ]
137             }
138         }
139     });
140
141     $po_ids = [map {$_->{id}} @$ids];
142 }
143
144 if (!@$po_ids) {
145     announce('info', "No purchase orders to process");
146     exit 0;
147 }
148
149 for $po_id (@$po_ids) {
150
151     my $edi = OpenILS::Utils::EDIWriter->new->write($po_id);
152
153     if (!$edi) {
154         announce('error', "Unable to generate EDI for PO $po_id");
155         next;
156     }
157
158     if ($test_mode) {
159         # Caller just wants the EDI printed to STDOUT
160         print "EDI for PO $po_id:\n$edi\n";
161         next;
162     }
163
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', 
168         status => 'retry'
169     })->[0];
170
171     if (!$message) {
172         $message = Fieldmapper::acq::edi_message->new;
173         $message->create_time('NOW');
174         $message->purchase_order($po_id);
175         $message->message_type('ORDERS');
176         $message->isnew(1);
177     }
178
179     $message->edi($edi);
180
181     $e->xact_begin;
182     if ($message->isnew) {
183         unless($e->create_acq_edi_message($message)) {
184             announce('error', 
185                 "Error creating acq.edi_message for PO $po_id: ".$e->die_event);
186             next;
187         }
188     } else {
189         unless($e->update_acq_edi_message($message)) {
190             announce('error', 
191                 "Error updating acq.edi_message for PO $po_id: ".$e->die_event);
192             next;
193         }
194     }
195     $e->xact_commit;
196
197     my $po = $e->retrieve_acq_purchase_order([
198         $po_id, {
199             flesh => 2,
200             flesh_fields => {
201                 acqpo  => ['provider'],
202                 acqpro => ['edi_default'],
203             }
204         }
205     ]);
206
207     if (!$po->provider->edi_default) {
208         # Caller has provided a PO ID for a provider that does not
209         # support EDI.  
210         announce('error', "Cannot send EDI for PO $po_id, because the ".
211             "provider (".$po->provider->id.") is not configured to use EDI");
212         next;
213     }
214
215     my $res = OpenILS::Application::Acq::EDI->send_core(
216         $po->provider->edi_default, [$message->id]);
217
218     if (my $out = $res->[0]) {
219         announce('info', "message ".$message->id." status: ".$out->status);
220     } else {
221         announce('error', "send_core failed for message ".$message->id);
222     }
223 }
224
225