Bug 31239: (QA follow-up) Fixing ternary formatting
[koha.git] / misc / cronjobs / edi_cron.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2013,2014,2015 PTFS Europe Ltd
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use warnings;
21 use strict;
22 use utf8;
23
24 # Handles all the edi processing for a site
25 # loops through the vendor_edifact records and uploads and downloads
26 # edifact files if the appropriate type is enabled
27 # downloaded quotes, invoices and responses are processed here
28 # if orders are enabled and present they are generated and sent
29 # can be run as frequently as required
30 # log messages are appended to logdir/editrace.log
31
32 use Koha::Script -cron;
33 use C4::Context;
34 use Log::Log4perl qw(:easy);
35 use Koha::Database;
36 use Koha::EDI qw( process_quote process_invoice process_ordrsp );
37 use Koha::Edifact::Transport;
38 use Koha::Plugins::Handler;
39 use Fcntl qw( LOCK_EX O_CREAT O_RDWR SEEK_SET );
40
41 die "Syspref 'EDIFACT' is disabled" unless C4::Context->preference('EDIFACT');
42
43 my $logdir = C4::Context->config('logdir');
44
45 # logging set to trace as this may be what you
46 # want on implementation
47 Log::Log4perl->easy_init(
48     {
49         level => $TRACE,
50         file  => ">>$logdir/editrace.log",
51     }
52 );
53
54 # we dont have a lock dir in context so use the logdir
55 my $pidfile = "$logdir/edicron.pid";
56
57 my $pid_handle = check_pidfile();
58
59 my $schema = Koha::Database->new()->schema();
60
61 my @edi_accts = $schema->resultset('VendorEdiAccount')->all();
62
63 my $logger = Log::Log4perl->get_logger();
64
65 for my $acct (@edi_accts) {
66     if ( $acct->quotes_enabled ) {
67         my $downloader = Koha::Edifact::Transport->new( $acct->id );
68         $downloader->download_messages('QUOTE');
69
70     }
71
72     if ( $acct->invoices_enabled ) {
73         my $downloader;
74
75         if ( $acct->plugin ) {
76             $downloader = Koha::Plugins::Handler->run(
77                 {
78                     class  => $acct->plugin,
79                     method => 'edifact_transport',
80                     params => {
81                         vendor_edi_account_id => $acct->id,
82                     }
83                 }
84             );
85         }
86
87         $downloader ||= Koha::Edifact::Transport->new( $acct->id );
88
89         $downloader->download_messages('INVOICE');
90
91     }
92     if ( $acct->orders_enabled ) {
93
94         # select pending messages
95         my @pending_orders = $schema->resultset('EdifactMessage')->search(
96             {
97                 message_type => 'ORDERS',
98                 vendor_id    => $acct->vendor_id,
99                 status       => 'Pending',
100             }
101         );
102         my $uploader = Koha::Edifact::Transport->new( $acct->id );
103         $uploader->upload_messages(@pending_orders);
104     }
105     if ( $acct->responses_enabled ) {
106         my $downloader = Koha::Edifact::Transport->new( $acct->id );
107         $downloader->download_messages('ORDRSP');
108     }
109 }
110
111 # process any downloaded quotes
112
113 my @downloaded_quotes = $schema->resultset('EdifactMessage')->search(
114     {
115         message_type => 'QUOTE',
116         status       => 'new',
117     }
118 )->all;
119
120 foreach my $quote_file (@downloaded_quotes) {
121     my $filename = $quote_file->filename;
122     $logger->trace("Processing quote $filename");
123     process_quote($quote_file);
124 }
125
126 # process any downloaded invoices
127 if ( C4::Context->preference('EdifactInvoiceImport') eq 'automatic' ) {
128     my @downloaded_invoices = $schema->resultset('EdifactMessage')->search(
129         {
130             message_type => 'INVOICE',
131             status       => 'new',
132         }
133     )->all;
134
135     foreach my $invoice (@downloaded_invoices) {
136         my $filename = $invoice->filename();
137         $logger->trace("Processing invoice $filename");
138         process_invoice($invoice);
139     }
140 }
141
142 my @downloaded_responses = $schema->resultset('EdifactMessage')->search(
143     {
144         message_type => 'ORDRSP',
145         status       => 'new',
146     }
147 )->all;
148
149 foreach my $response (@downloaded_responses) {
150     my $filename = $response->filename();
151     $logger->trace("Processing order response $filename");
152     process_ordrsp($response);
153 }
154
155 if ( close $pid_handle ) {
156     unlink $pidfile;
157     exit 0;
158 }
159 else {
160     $logger->error("Error on pidfile close: $!");
161     exit 1;
162 }
163
164 sub check_pidfile {
165
166     # sysopen my $fh, $pidfile, O_EXCL | O_RDWR or log_exit "$0 already running"
167     sysopen my $fh, $pidfile, O_RDWR | O_CREAT
168       or log_exit("$0: open $pidfile: $!");
169     flock $fh => LOCK_EX or log_exit("$0: flock $pidfile: $!");
170
171     sysseek $fh, 0, SEEK_SET or log_exit("$0: sysseek $pidfile: $!");
172     truncate $fh, 0 or log_exit("$0: truncate $pidfile: $!");
173     print $fh "$$\n" or log_exit("$0: print $pidfile: $!");
174
175     return $fh;
176 }
177
178 sub log_exit {
179     my $error = shift;
180     $logger->error($error);
181
182     exit 1;
183 }