Bug 7677 - Schema update
[koha.git] / misc / cronjobs / edi_cron.pl
blobc23341ad2651d484a6c08bb48a26347291092d00
1 #!/usr/bin/perl
3 # Copyright 2013,2014,2015 PTFS Europe Ltd
5 # This file is part of Koha.
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.
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.
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.
20 use warnings;
21 use strict;
22 use utf8;
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
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 );
39 my $logdir = C4::Context->config('logdir');
41 # logging set to trace as this may be what you
42 # want on implementation
43 Log::Log4perl->easy_init(
45 level => $TRACE,
46 file => ">>$logdir/editrace.log",
50 # we dont have a lock dir in context so use the logdir
51 my $pidfile = "$logdir/edicron.pid";
53 my $pid_handle = check_pidfile();
55 my $schema = Koha::Database->new()->schema();
57 my @edi_accts = $schema->resultset('VendorEdiAccount')->all();
59 my $logger = Log::Log4perl->get_logger();
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');
68 if ( $acct->invoices_enabled ) {
69 my $downloader;
71 if ( $acct->plugin ) {
72 $downloader = Koha::Plugins::Handler->run(
74 class => $acct->plugin,
75 method => 'edifact_transport',
76 params => {
77 vendor_edi_account_id => $acct->id,
83 $downloader ||= Koha::Edifact::Transport->new( $acct->id );
85 $downloader->download_messages('INVOICE');
88 if ( $acct->orders_enabled ) {
90 # select pending messages
91 my @pending_orders = $schema->resultset('EdifactMessage')->search(
93 message_type => 'ORDERS',
94 vendor_id => $acct->vendor_id,
95 status => 'Pending',
98 my $uploader = Koha::Edifact::Transport->new( $acct->id );
99 $uploader->upload_messages(@pending_orders);
101 if ( $acct->responses_enabled ) {
102 my $downloader = Koha::Edifact::Transport->new( $acct->id );
103 $downloader->download_messages('ORDRSP');
107 # process any downloaded quotes
109 my @downloaded_quotes = $schema->resultset('EdifactMessage')->search(
111 message_type => 'QUOTE',
112 status => 'new',
114 )->all;
116 foreach my $quote_file (@downloaded_quotes) {
117 my $filename = $quote_file->filename;
118 $logger->trace("Processing quote $filename");
119 process_quote($quote_file);
122 # process any downloaded invoices
124 my @downloaded_invoices = $schema->resultset('EdifactMessage')->search(
126 message_type => 'INVOICE',
127 status => 'new',
129 )->all;
131 foreach my $invoice (@downloaded_invoices) {
132 my $filename = $invoice->filename();
133 $logger->trace("Processing invoice $filename");
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(
142 class => $plugin_class,
143 method => 'edifact_process_invoice',
144 params => {
145 invoice => $invoice,
152 process_invoice($invoice) unless $plugin_used;
155 my @downloaded_responses = $schema->resultset('EdifactMessage')->search(
157 message_type => 'ORDRSP',
158 status => 'new',
160 )->all;
162 foreach my $response (@downloaded_responses) {
163 my $filename = $response->filename();
164 $logger->trace("Processing order response $filename");
165 process_ordrsp($response);
168 if ( close $pid_handle ) {
169 unlink $pidfile;
170 exit 0;
172 else {
173 $logger->error("Error on pidfile close: $!");
174 exit 1;
177 sub check_pidfile {
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: $!");
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: $!");
188 return $fh;
191 sub log_exit {
192 my $error = shift;
193 $logger->error($error);
195 exit 1;