6 # find Koha's Perl modules
7 # test carefully before changing this
9 eval { require "$FindBin::Bin/../kohalib.pl" };
13 CGI; # NOT a CGI script, this is just to keep C4::Templates::gettemplate happy
26 my ( $stylesheet, $help, $split, $html, $csv, @letter_codes );
33 'letter_code:s' => \@letter_codes,
36 pod2usage(0) if $help;
38 my $output_directory = $ARGV[0];
40 if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
43 -msg => qq{\nError: You must specify a valid and writeable directory to dump the print notices in.\n},
47 # Default value is html
48 $html = 1 unless $html or $csv;
50 if ( $csv and @letter_codes != 1 ) {
53 -msg => qq{\nIt is not consistent to use --csv without one (and only one) letter_code\n},
59 my $today = C4::Dates->new();
60 my @all_messages = @{ GetPrintMessages() };
62 # Filter by letter_code
64 my $letter_code = $_->{letter_code};
66 grep { /^$letter_code$/ } @letter_codes
69 exit unless @all_messages;
71 ## carriage return replaced by <br/> as output is html
72 foreach my $message (@all_messages) {
73 local $_ = $message->{'content'};
76 $message->{'content'} = $_;
79 print_notices_html({ messages => \@all_messages, split => $split })
82 print_notices_csv({ messages => \@all_messages, split => $split })
85 sub print_notices_html {
88 my $messages = $params->{messages};
89 my $split = $params->{split};
91 my $messages_by_branch;
93 foreach my $message (@$messages) {
94 push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
97 $messages_by_branch->{all_branches} = $messages;
100 while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
101 my $filename = $split
102 ? 'holdnotices-' . $today->output('iso') . "-$branchcode.html"
103 : 'holdnotices-' . $today->output('iso') . ".html";
106 C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet',
110 stylesheet => C4::Context->preference("NoticeCSS"),
111 today => $today->output(),
112 messages => $branch_messages,
115 my $output_file = File::Spec->catdir( $output_directory, $filename )
116 open my $OUTPUT, '>', $output_file
117 or die "Could not open $output_file: $!";
118 print $OUTPUT $template->output;
121 foreach my $message ( @$branch_messages ) {
122 C4::Letters::_set_message_status(
124 message_id => $message->{'message_id'},
128 $message->{status} = 'sent';
133 sub print_notices_csv {
136 my $messages = $params->{messages};
137 my $split = $params->{split};
139 my $messages_by_branch;
141 foreach my $message (@$messages) {
142 push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
145 $messages_by_branch->{all_branches} = $messages;
148 while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
149 my $filename = $split
150 ? 'holdnotices-' . $today->output('iso') . "-$branchcode.csv"
151 : 'holdnotices-' . $today->output('iso') . ".csv";
153 open my $OUTPUT, '>', File::Spec->catdir( $output_directory, $filename );
154 my ( @csv_lines, $headers );
155 foreach my $message ( @$branch_messages ) {
156 my @lines = split /\n/, $message->{content};
158 # We don't have headers, get them
159 unless ( $headers ) {
160 $headers = $lines[0];
162 say $OUTPUT $headers;
166 for my $line ( @lines ) {
168 next if $line =~ /^\s$/;
172 C4::Letters::_set_message_status(
174 message_id => $message->{'message_id'},
177 ) if $message->{status} ne 'sent';
185 gather_print_notices - Print waiting print notices
189 gather_print_notices output_directory [-s|--split] [--html] [--csv] [--letter_code=LETTER_CODE] [-h|--help]
191 Will print all waiting print notices to the output_directory.
193 The generated filename will be holdnotices-TODAY.[csv|html] or holdnotices-TODAY-BRANCHCODE.[csv|html] if the --split parameter is given.
199 =item B<output_directory>
201 Define the output directory where the files will be generated.
205 Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html]
209 Generate the print notices in a html file (default if --html and --csv are not given).
213 Generate the print notices in a csv file.
214 If you use this parameter, the template should contain 2 lines.
215 The first one the the csv headers and the second one the value list.
218 cardnumber:patron:email:item
219 <<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
221 You have to combine this option without one (and only one) letter_code.
223 =item B<--letter_code>
225 Filter print messages by letter_code.
226 Several letter_code parameters can be given.
230 Print a brief help message
236 Jesse Weaver <pianohacker@gmail.com>
238 Jonathan Druart <jonathan.druart@biblibre.com>
242 Copyright 2009 Jesse Weaver
244 Copyright 2014 BibLibre
247 This file is part of Koha.
249 Koha is free software; you can redistribute it and/or modify it
250 under the terms of the GNU General Public License as published by
251 the Free Software Foundation; either version 3 of the License, or
252 (at your option) any later version.
254 Koha is distributed in the hope that it will be useful, but
255 WITHOUT ANY WARRANTY; without even the implied warranty of
256 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
257 GNU General Public License for more details.
259 You should have received a copy of the GNU General Public License
260 along with Koha; if not, see <http://www.gnu.org/licenses>.