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
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.
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.
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>.
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 Koha::Script -cron;
34 use Log::Log4perl qw(:easy);
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 );
41 my $logdir = C4::Context->config('logdir');
43 # logging set to trace as this may be what you
44 # want on implementation
45 Log::Log4perl->easy_init(
48 file => ">>$logdir/editrace.log",
52 # we dont have a lock dir in context so use the logdir
53 my $pidfile = "$logdir/edicron.pid";
55 my $pid_handle = check_pidfile();
57 my $schema = Koha::Database->new()->schema();
59 my @edi_accts = $schema->resultset('VendorEdiAccount')->all();
61 my $logger = Log::Log4perl->get_logger();
63 for my $acct (@edi_accts) {
64 if ( $acct->quotes_enabled ) {
65 my $downloader = Koha::Edifact::Transport->new( $acct->id );
66 $downloader->download_messages('QUOTE');
70 if ( $acct->invoices_enabled ) {
73 if ( $acct->plugin ) {
74 $downloader = Koha::Plugins::Handler->run(
76 class => $acct->plugin,
77 method => 'edifact_transport',
79 vendor_edi_account_id => $acct->id,
85 $downloader ||= Koha::Edifact::Transport->new( $acct->id );
87 $downloader->download_messages('INVOICE');
90 if ( $acct->orders_enabled ) {
92 # select pending messages
93 my @pending_orders = $schema->resultset('EdifactMessage')->search(
95 message_type => 'ORDERS',
96 vendor_id => $acct->vendor_id,
100 my $uploader = Koha::Edifact::Transport->new( $acct->id );
101 $uploader->upload_messages(@pending_orders);
103 if ( $acct->responses_enabled ) {
104 my $downloader = Koha::Edifact::Transport->new( $acct->id );
105 $downloader->download_messages('ORDRSP');
109 # process any downloaded quotes
111 my @downloaded_quotes = $schema->resultset('EdifactMessage')->search(
113 message_type => 'QUOTE',
118 foreach my $quote_file (@downloaded_quotes) {
119 my $filename = $quote_file->filename;
120 $logger->trace("Processing quote $filename");
121 process_quote($quote_file);
124 # process any downloaded invoices
125 if ( C4::Context->preference('EdifactInvoiceImport') eq 'automatic' ) {
126 my @downloaded_invoices = $schema->resultset('EdifactMessage')->search(
128 message_type => 'INVOICE',
133 foreach my $invoice (@downloaded_invoices) {
134 my $filename = $invoice->filename();
135 $logger->trace("Processing invoice $filename");
136 process_invoice($invoice);
140 my @downloaded_responses = $schema->resultset('EdifactMessage')->search(
142 message_type => 'ORDRSP',
147 foreach my $response (@downloaded_responses) {
148 my $filename = $response->filename();
149 $logger->trace("Processing order response $filename");
150 process_ordrsp($response);
153 if ( close $pid_handle ) {
158 $logger->error("Error on pidfile close: $!");
164 # sysopen my $fh, $pidfile, O_EXCL | O_RDWR or log_exit "$0 already running"
165 sysopen my $fh, $pidfile, O_RDWR | O_CREAT
166 or log_exit("$0: open $pidfile: $!");
167 flock $fh => LOCK_EX or log_exit("$0: flock $pidfile: $!");
169 sysseek $fh, 0, SEEK_SET or log_exit("$0: sysseek $pidfile: $!");
170 truncate $fh, 0 or log_exit("$0: truncate $pidfile: $!");
171 print $fh "$$\n" or log_exit("$0: print $pidfile: $!");
178 $logger->error($error);