Bug 21068: Remove NorwegianPatronDB related code
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 C4::Context;
33 use Log::Log4perl qw(:easy);
34 use Koha::Database;
35 use Koha::EDI qw( process_quote process_invoice process_ordrsp);
36 use Koha::Edifact::Transport;
37 use Fcntl qw( :DEFAULT :flock :seek );
38
39 my $logdir = C4::Context->config('logdir');
40
41 # logging set to trace as this may be what you
42 # want on implementation
43 Log::Log4perl->easy_init(
44     {
45         level => $TRACE,
46         file  => ">>$logdir/editrace.log",
47     }
48 );
49
50 # we dont have a lock dir in context so use the logdir
51 my $pidfile = "$logdir/edicron.pid";
52
53 my $pid_handle = check_pidfile();
54
55 my $schema = Koha::Database->new()->schema();
56
57 my @edi_accts = $schema->resultset('VendorEdiAccount')->all();
58
59 my $logger = Log::Log4perl->get_logger();
60
61 for my $acct (@edi_accts) {
62     if ( $acct->quotes_enabled ) {
63         my $downloader = Koha::Edifact::Transport->new( $acct->id );
64         $downloader->download_messages('QUOTE');
65
66     }
67
68     if ( $acct->invoices_enabled ) {
69         my $downloader;
70
71         if ( $acct->plugin ) {
72             $downloader = Koha::Plugins::Handler->run(
73                 {
74                     class  => $acct->plugin,
75                     method => 'edifact_transport',
76                     params => {
77                         vendor_edi_account_id => $acct->id,
78                     }
79                 }
80             );
81         }
82
83         $downloader ||= Koha::Edifact::Transport->new( $acct->id );
84
85         $downloader->download_messages('INVOICE');
86
87     }
88     if ( $acct->orders_enabled ) {
89
90         # select pending messages
91         my @pending_orders = $schema->resultset('EdifactMessage')->search(
92             {
93                 message_type => 'ORDERS',
94                 vendor_id    => $acct->vendor_id,
95                 status       => 'Pending',
96             }
97         );
98         my $uploader = Koha::Edifact::Transport->new( $acct->id );
99         $uploader->upload_messages(@pending_orders);
100     }
101     if ( $acct->responses_enabled ) {
102         my $downloader = Koha::Edifact::Transport->new( $acct->id );
103         $downloader->download_messages('ORDRSP');
104     }
105 }
106
107 # process any downloaded quotes
108
109 my @downloaded_quotes = $schema->resultset('EdifactMessage')->search(
110     {
111         message_type => 'QUOTE',
112         status       => 'new',
113     }
114 )->all;
115
116 foreach my $quote_file (@downloaded_quotes) {
117     my $filename = $quote_file->filename;
118     $logger->trace("Processing quote $filename");
119     process_quote($quote_file);
120 }
121
122 # process any downloaded invoices
123
124 my @downloaded_invoices = $schema->resultset('EdifactMessage')->search(
125     {
126         message_type => 'INVOICE',
127         status       => 'new',
128     }
129 )->all;
130
131 foreach my $invoice (@downloaded_invoices) {
132     my $filename = $invoice->filename();
133     $logger->trace("Processing invoice $filename");
134
135     my $plugin_used = 0;
136     if ( my $plugin_class = $invoice->edi_acct->plugin ) {
137         my $plugin = $plugin_class->new();
138         if ( $plugin->can('edifact_process_invoice') ) {
139             $plugin_used = 1;
140             Koha::Plugins::Handler->run(
141                 {
142                     class  => $plugin_class,
143                     method => 'edifact_process_invoice',
144                     params => {
145                         invoice => $invoice,
146                     }
147                 }
148             );
149         }
150     }
151
152     process_invoice($invoice) unless $plugin_used;
153 }
154
155 my @downloaded_responses = $schema->resultset('EdifactMessage')->search(
156     {
157         message_type => 'ORDRSP',
158         status       => 'new',
159     }
160 )->all;
161
162 foreach my $response (@downloaded_responses) {
163     my $filename = $response->filename();
164     $logger->trace("Processing order response $filename");
165     process_ordrsp($response);
166 }
167
168 if ( close $pid_handle ) {
169     unlink $pidfile;
170     exit 0;
171 }
172 else {
173     $logger->error("Error on pidfile close: $!");
174     exit 1;
175 }
176
177 sub check_pidfile {
178
179     # sysopen my $fh, $pidfile, O_EXCL | O_RDWR or log_exit "$0 already running"
180     sysopen my $fh, $pidfile, O_RDWR | O_CREAT
181       or log_exit("$0: open $pidfile: $!");
182     flock $fh => LOCK_EX or log_exit("$0: flock $pidfile: $!");
183
184     sysseek $fh, 0, SEEK_SET or log_exit("$0: sysseek $pidfile: $!");
185     truncate $fh, 0 or log_exit("$0: truncate $pidfile: $!");
186     print $fh "$$\n" or log_exit("$0: print $pidfile: $!");
187
188     return $fh;
189 }
190
191 sub log_exit {
192     my $error = shift;
193     $logger->error($error);
194
195     exit 1;
196 }