Merge remote-tracking branch 'origin/new/bug_7955'
[koha.git] / misc / cronjobs / gather_print_notices.pl
1 #!/usr/bin/perl -w
2
3 # Copyright 2009 Jesse Weaver
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 2 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 strict;
21 use warnings;
22
23 BEGIN {
24     # find Koha's Perl modules
25     # test carefully before changing this
26     use FindBin;
27     eval { require "$FindBin::Bin/../kohalib.pl" };
28 }
29
30 use
31   CGI; # NOT a CGI script, this is just to keep C4::Templates::gettemplate happy
32 use C4::Context;
33 use C4::Dates;
34 use C4::Debug;
35 use C4::Letters;
36 use C4::Templates;
37 use File::Spec;
38 use Getopt::Long;
39
40 sub usage {
41     print STDERR <<USAGE;
42 Usage: $0 OUTPUT_DIRECTORY
43   Will print all waiting print notices to
44   OUTPUT_DIRECTORY/notices-CURRENT_DATE.html .
45
46   -s --split  Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.html
47 USAGE
48     exit $_[0];
49 }
50
51 my ( $stylesheet, $help, $split );
52
53 GetOptions(
54     'h|help'  => \$help,
55     's|split' => \$split,
56 ) || usage(1);
57
58 usage(0) if ($help);
59
60 my $output_directory = $ARGV[0];
61
62 if ( !$output_directory || !-d $output_directory ) {
63     print STDERR
64 "Error: You must specify a valid directory to dump the print notices in.\n";
65     usage(1);
66 }
67
68 my $today        = C4::Dates->new();
69 my @all_messages = @{ GetPrintMessages() };
70 exit unless (@all_messages);
71
72 my $OUTPUT;
73
74 if ($split) {
75     my %messages_by_branch;
76     foreach my $message (@all_messages) {
77         push( @{ $messages_by_branch{ $message->{'branchcode'} } }, $message );
78     }
79
80     foreach my $branchcode ( keys %messages_by_branch ) {
81         my @messages = @{ $messages_by_branch{$branchcode} };
82         open $OUTPUT, '>',
83           File::Spec->catdir( $output_directory,
84             "holdnotices-" . $today->output('iso') . "-$branchcode.html" );
85
86         my $template =
87           C4::Templates::gettemplate( 'batch/print-notices.tmpl', 'intranet',
88             new CGI );
89
90         $template->param(
91             stylesheet => C4::Context->preference("NoticeCSS"),
92             today      => $today->output(),
93             messages   => \@messages,
94         );
95
96         print $OUTPUT $template->output;
97
98         foreach my $message (@messages) {
99             C4::Letters::_set_message_status(
100                 { message_id => $message->{'message_id'}, status => 'sent' } );
101         }
102
103         close $OUTPUT;
104     }
105 }
106 else {
107     open $OUTPUT, '>',
108       File::Spec->catdir( $output_directory,
109         "holdnotices-" . $today->output('iso') . ".html" );
110
111     my $template =
112       C4::Templates::gettemplate( 'batch/print-notices.tmpl', 'intranet',
113         new CGI );
114
115     $template->param(
116         stylesheet => C4::Context->preference("NoticeCSS"),
117         today      => $today->output(),
118         messages   => \@all_messages,
119     );
120
121     print $OUTPUT $template->output;
122
123     foreach my $message (@all_messages) {
124         C4::Letters::_set_message_status(
125             { message_id => $message->{'message_id'}, status => 'sent' } );
126     }
127
128     close $OUTPUT;
129
130 }