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::Application::Acq::EDI;
24 use OpenILS::Utils::Cronscript;
34 my $core = OpenILS::Utils::Cronscript->new($defaults);
35 my $opts = $core->MyGetOptions() or die "Getting options failed!";
36 my $e = $core->editor();
37 my $debug = $opts->{debug};
40 print join "\n", "OPTIONS:", map {sprintf "%16s: %s", $_, $opts->{$_}} sort keys %$opts;
45 my $select = {'+acqpro' => {active => {"in"=>['t','f']}} }; # either way
46 my %args = @_ ? @_ : ();
47 foreach (keys %args) {
48 $select->{$_} = $args{$_};
50 return $e->search_acq_edi_account([
55 flesh_fields => {acqedi => ['provider']},
60 my $set = main_search() or die "No EDI accounts found in database (table: acq.edi_account)";
62 my $total_accts = scalar(@$set);
64 ($total_accts) or die "No EDI accounts found in database (table: acq.edi_account)";
66 print "EDI Accounts Total : $total_accts\n";
67 my $active = [ grep {$_->provider->active eq 't'} @$set ];
68 print "EDI Accounts Active: ", scalar(@$active), "\n";
71 if ($opts->{inactive} or $opts->{provider} or $opts->{account}) {
72 print "Including inactive accounts\n";
79 if ($opts->{provider}) {
80 print "Limiting by provider: " . $opts->{provider} . "\n";
81 $pro = $e->retrieve_acq_provider($opts->{provider}) or die "provider '" . $opts->{provider} . "' not found";
82 printf "Provider %s found (edi_default %s)\n", $pro->id, $pro->edi_default;
83 $subset = main_search( 'id' => $pro->edi_default );
84 # $subset = [ grep {$_->provider->id == $opts->{provider}} @$subset ];
86 $_->provider($pro); # force provider match (short of LEFT JOINing the main_search query and dealing w/ multiple combos)
88 scalar(@$subset) or die "provider '" . $opts->{provider} . "' edi_default invalid (failed to match acq.edi_account.id)";
89 if ($opts->{account} and $opts->{account} != $pro->edi_default) {
90 die sprintf "ERROR: --provider=%s and --account=%s specify rows that exist, but are not paired by acq.provider.edi_default", $opts->{provider}, $opts->{account};
94 if ($opts->{account}) {
95 print "Limiting by account: " . $opts->{account} . "\n";
96 $subset = [ grep {$opts->{account} == $_->id} @$subset ];
97 scalar(@$subset) or die "No acq.provider.edi_default matches option --account=" . $opts->{account} . " ";
98 scalar(@$subset) > 1 and warn "account '" . $opts->{account} . "' has multiple matches. Ignoring all but the first.";
101 scalar(@$subset) or die "No acq.provider rows match options " .
102 ($opts->{account} ? ("--account=" . $opts->{account} ) : '') .
103 ($opts->{provider} ? ("--provider=" . $opts->{provider}) : '') ;
105 print "Limiting to " . scalar(@$subset) . " account(s)\n";
107 printf "Provider %s - %s, edi_account %s - %s: %s\n", $_->provider->id, $_->provider->name, $_->id, $_->label, $_->host;
111 $opts->{provider} or $opts->{account}
112 or die "ERROR: --account=[ID] or --provider=[ID] option required for local data ingest, with valid edi_account or provider id";
113 print "READING FROM ", scalar(@ARGV), " LOCAL SOURCE(s) ONLY. NO REMOTE SERVER(s) WILL BE USED\n";
114 printf "File will be attributed to edi_account %s - %s: %s\n", $acct->id, $acct->label, $acct->host;
115 my @files = @ARGV; # copy original @ARGV
117 @ARGV = ($_); # We'll use the diamond op, so we can pull from STDIN too
118 my $content = join '', <> or next;
119 $opts->{test} and next;
120 my $in = OpenILS::Application::Acq::EDI->process_retrieval(
122 "localhost:" . File::Spec->rel2abs($_),
123 OpenILS::Application::Acq::EDI->remote_account($acct),
132 my $res = $opts->{test} ? [] : OpenILS::Application::Acq::EDI->retrieve_core($subset);
133 print "Files retrieved: ", scalar(@$res), "\n";
134 $debug and print "retrieve_core returns ", scalar(@$res), " ids: " . join(', ', @$res), "\n";
136 # $Data::Dumper::Indent = 1;
137 $debug and print map {Dumper($_) . "\n"} @$subset;
146 edi_fetcher.pl - A script for retrieving and processing EDI files from remote accounts.
150 This script is expected to be run via crontab, for the purpose of retrieving vendor EDI files.
152 Note: Depending on your vendors' and your own network environments, you may want to set/export
153 the environmental variable FTP_PASSIVE like:
157 FTP_PASSIVE=1 Open-ILS/src/support-scripts/edi_fetcher.pl
161 --account=[id] Target one account, whether or not it is inactive.
162 --inactive Includes inactive provider accounts (default OFF, forced ON if --account specified)
166 edi_fetcher can also read from files specified as arguments on the command line, or from STDIN, or both.
167 In such cases, the filename is not used to check whether the file has been loaded or not.
175 OpenILS::Utils::Cronscript
180 Joe Atzberger <jatzberger@esilibrary.com>