Lines 19-33
use C4::Templates;
Link Here
|
19 |
use File::Spec; |
19 |
use File::Spec; |
20 |
use Pod::Usage; |
20 |
use Pod::Usage; |
21 |
use Getopt::Long; |
21 |
use Getopt::Long; |
|
|
22 |
use File::Basename qw( dirname ); |
22 |
use Koha::DateUtils; |
23 |
use Koha::DateUtils; |
23 |
|
24 |
|
24 |
my ( $stylesheet, $help, $split, $html, $csv, @letter_codes ); |
25 |
my ( $stylesheet, $help, $split, $html, $csv, $ods, $delimiter, @letter_codes ); |
25 |
|
26 |
|
26 |
GetOptions( |
27 |
GetOptions( |
27 |
'h|help' => \$help, |
28 |
'h|help' => \$help, |
28 |
's|split' => \$split, |
29 |
's|split' => \$split, |
29 |
'html' => \$html, |
30 |
'html' => \$html, |
30 |
'csv' => \$csv, |
31 |
'csv' => \$csv, |
|
|
32 |
'ods' => \$ods, |
33 |
'd|delimiter:s' => \$delimiter, |
31 |
'letter_code:s' => \@letter_codes, |
34 |
'letter_code:s' => \@letter_codes, |
32 |
) || pod2usage(1); |
35 |
) || pod2usage(1); |
33 |
|
36 |
|
Lines 43-49
if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
Link Here
|
43 |
} |
46 |
} |
44 |
|
47 |
|
45 |
# Default value is html |
48 |
# Default value is html |
46 |
$html = 1 unless $html or $csv; |
49 |
$html = 1 if not $html and not $csv and not $ods; |
47 |
|
50 |
|
48 |
if ( $csv and @letter_codes != 1 ) { |
51 |
if ( $csv and @letter_codes != 1 ) { |
49 |
pod2usage({ |
52 |
pod2usage({ |
Lines 52-57
if ( $csv and @letter_codes != 1 ) {
Link Here
|
52 |
}); |
55 |
}); |
53 |
} |
56 |
} |
54 |
|
57 |
|
|
|
58 |
if ( $ods and @letter_codes != 1 ) { |
59 |
pod2usage({ |
60 |
-exitval => 1, |
61 |
-msg => qq{\nIt is not consistent to use --ods without one (and only one) letter_code\n}, |
62 |
}); |
63 |
} |
64 |
|
65 |
$delimiter ||= q|,|; |
66 |
|
55 |
my $today = C4::Dates->new(); |
67 |
my $today = C4::Dates->new(); |
56 |
my @all_messages = @{ GetPrintMessages() }; |
68 |
my @all_messages = @{ GetPrintMessages() }; |
57 |
|
69 |
|
Lines 64-90
my @all_messages = @{ GetPrintMessages() };
Link Here
|
64 |
} @all_messages; |
76 |
} @all_messages; |
65 |
exit unless @all_messages; |
77 |
exit unless @all_messages; |
66 |
|
78 |
|
67 |
## carriage return replaced by <br/> as output is html |
79 |
my ( $html_filenames, $csv_filenames, $ods_filenames ); |
68 |
foreach my $message (@all_messages) { |
80 |
$csv_filenames = print_notices({ |
69 |
local $_ = $message->{'content'}; |
81 |
messages => \@all_messages, |
70 |
s/\n/<br \/>/g; |
82 |
split => $split, |
71 |
s/\r//g; |
83 |
output_directory => $output_directory, |
72 |
$message->{'content'} = $_; |
84 |
format => 'csv', |
73 |
} |
85 |
}) if $csv; |
74 |
|
86 |
|
75 |
print_notices_html({ messages => \@all_messages, split => $split }) |
87 |
$ods_filenames = print_notices({ |
76 |
if $html; |
88 |
messages => \@all_messages, |
|
|
89 |
split => $split, |
90 |
output_directory => $output_directory, |
91 |
format => 'ods', |
92 |
}) if $ods; |
93 |
|
94 |
if ( $html ) { |
95 |
## carriage return replaced by <br/> as output is html |
96 |
foreach my $message (@all_messages) { |
97 |
local $_ = $message->{'content'}; |
98 |
s/\n/<br \/>/g; |
99 |
s/\r//g; |
100 |
$message->{'content'} = $_; |
101 |
} |
77 |
|
102 |
|
78 |
print_notices_csv({ messages => \@all_messages, split => $split }) |
103 |
$html_filenames = print_notices({ |
79 |
if $csv; |
104 |
messages => \@all_messages, |
|
|
105 |
split => $split, |
106 |
output_directory => $output_directory, |
107 |
format => 'html', |
108 |
}); |
109 |
} |
80 |
|
110 |
|
81 |
sub print_notices_html { |
111 |
sub print_notices { |
82 |
my ( $params ) = @_; |
112 |
my ( $params ) = @_; |
83 |
|
113 |
|
84 |
my $messages = $params->{messages}; |
114 |
my $messages = $params->{messages}; |
85 |
my $split = $params->{split}; |
115 |
my $split = $params->{split}; |
|
|
116 |
my $output_directory = $params->{output_directory}; |
117 |
my $format = $params->{format} // 'html'; |
118 |
|
119 |
die "Format $format is not known" |
120 |
unless $format =~ m[^html$|^csv$|^ods$]; |
121 |
|
122 |
my ( @filenames, $messages_by_branch ); |
86 |
|
123 |
|
87 |
my $messages_by_branch; |
|
|
88 |
if ( $split ) { |
124 |
if ( $split ) { |
89 |
foreach my $message (@$messages) { |
125 |
foreach my $message (@$messages) { |
90 |
push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message ); |
126 |
push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message ); |
Lines 95-118
sub print_notices_html {
Link Here
|
95 |
|
131 |
|
96 |
while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) { |
132 |
while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) { |
97 |
my $filename = $split |
133 |
my $filename = $split |
98 |
? 'holdnotices-' . $today->output('iso') . "-$branchcode.html" |
134 |
? 'holdnotices-' . $today->output('iso') . "-$branchcode.$format" |
99 |
: 'holdnotices-' . $today->output('iso') . ".html"; |
135 |
: 'holdnotices-' . $today->output('iso') . ".$format"; |
100 |
|
136 |
|
101 |
my $template = |
137 |
my $filepath = File::Spec->catdir( $output_directory, $filename ); |
102 |
C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet', |
138 |
if ( $format eq 'html' ) { |
103 |
new CGI ); |
139 |
generate_html({ |
104 |
|
140 |
messages => $branch_messages, |
105 |
$template->param( |
141 |
filepath => $filepath, |
106 |
stylesheet => C4::Context->preference("NoticeCSS"), |
142 |
}); |
107 |
today => $today->output(), |
143 |
} elsif ( $format eq 'csv' ) { |
108 |
messages => $branch_messages, |
144 |
generate_csv ({ |
109 |
); |
145 |
messages => $branch_messages, |
110 |
|
146 |
filepath => $filepath, |
111 |
my $output_file = File::Spec->catdir( $output_directory, $filename ) |
147 |
}); |
112 |
open my $OUTPUT, '>', $output_file |
148 |
} elsif ( $format eq 'ods' ) { |
113 |
or die "Could not open $output_file: $!"; |
149 |
generate_ods ({ |
114 |
print $OUTPUT $template->output; |
150 |
messages => $branch_messages, |
115 |
close $OUTPUT; |
151 |
filepath => $filepath, |
|
|
152 |
}); |
153 |
} |
116 |
|
154 |
|
117 |
foreach my $message ( @$branch_messages ) { |
155 |
foreach my $message ( @$branch_messages ) { |
118 |
C4::Letters::_set_message_status( |
156 |
C4::Letters::_set_message_status( |
Lines 121-179
sub print_notices_html {
Link Here
|
121 |
status => 'sent' |
159 |
status => 'sent' |
122 |
} |
160 |
} |
123 |
); |
161 |
); |
124 |
$message->{status} = 'sent'; |
|
|
125 |
} |
162 |
} |
|
|
163 |
push @filenames, $filename; |
126 |
} |
164 |
} |
|
|
165 |
return \@filenames; |
127 |
} |
166 |
} |
128 |
|
167 |
|
129 |
sub print_notices_csv { |
168 |
sub generate_html { |
130 |
my ( $params ) = @_; |
169 |
my ( $params ) = @_; |
|
|
170 |
my $messages = $params->{messages}; |
171 |
my $filepath = $params->{filepath}; |
172 |
|
173 |
my $template = |
174 |
C4::Templates::gettemplate( 'batch/print-notices.tt', 'intranet', |
175 |
new CGI ); |
176 |
|
177 |
$template->param( |
178 |
stylesheet => C4::Context->preference("NoticeCSS"), |
179 |
today => $today->output(), |
180 |
messages => $messages, |
181 |
); |
182 |
|
183 |
open my $OUTPUT, '>', $filepath |
184 |
or die "Could not open $filepath: $!"; |
185 |
print $OUTPUT $template->output; |
186 |
close $OUTPUT; |
187 |
} |
131 |
|
188 |
|
|
|
189 |
sub generate_csv { |
190 |
my ( $params ) = @_; |
132 |
my $messages = $params->{messages}; |
191 |
my $messages = $params->{messages}; |
133 |
my $split = $params->{split}; |
192 |
my $filepath = $params->{filepath}; |
|
|
193 |
|
194 |
open my $OUTPUT, '>', $filepath |
195 |
or die "Could not open $filepath: $!"; |
196 |
my ( @csv_lines, $headers ); |
197 |
foreach my $message ( @$messages ) { |
198 |
my @lines = split /\n/, $message->{content}; |
199 |
chomp for @lines; |
200 |
|
201 |
# We don't have headers, get them |
202 |
unless ( $headers ) { |
203 |
$headers = $lines[0]; |
204 |
say $OUTPUT Encode::encode( 'UTF8', $headers ); |
205 |
} |
134 |
|
206 |
|
135 |
my $messages_by_branch; |
207 |
shift @lines; |
136 |
if ( $split ) { |
208 |
for my $line ( @lines ) { |
137 |
foreach my $message (@$messages) { |
209 |
next if $line =~ /^\s$/; |
138 |
push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message ); |
210 |
say $OUTPUT Encode::encode( 'UTF8', $line ); |
139 |
} |
211 |
} |
140 |
} else { |
|
|
141 |
$messages_by_branch->{all_branches} = $messages; |
142 |
} |
212 |
} |
|
|
213 |
} |
143 |
|
214 |
|
144 |
while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) { |
215 |
sub generate_ods { |
145 |
my $filename = $split |
216 |
my ( $params ) = @_; |
146 |
? 'holdnotices-' . $today->output('iso') . "-$branchcode.csv" |
217 |
my $messages = $params->{messages}; |
147 |
: 'holdnotices-' . $today->output('iso') . ".csv"; |
218 |
my $filepath = $params->{filepath}; |
148 |
|
219 |
|
149 |
open my $OUTPUT, '>', File::Spec->catdir( $output_directory, $filename ); |
220 |
use OpenOffice::OODoc; |
150 |
my ( @csv_lines, $headers ); |
221 |
my $tmpdir = dirname $filepath; |
151 |
foreach my $message ( @$branch_messages ) { |
222 |
odfWorkingDirectory( $tmpdir ); |
152 |
my @lines = split /\n/, $message->{content}; |
223 |
my $container = odfContainer( $filepath, create => 'spreadsheet' ); |
153 |
|
224 |
my $doc = odfDocument ( |
154 |
# We don't have headers, get them |
225 |
container => $container, |
155 |
unless ( $headers ) { |
226 |
part => 'content' |
156 |
$headers = $lines[0]; |
227 |
); |
157 |
chomp $headers; |
228 |
my $table = $doc->getTable(0); |
158 |
say $OUTPUT $headers; |
229 |
|
|
|
230 |
my @headers; |
231 |
my ( $nb_rows, $nb_cols ) = ( scalar(@$messages), 0 ); |
232 |
foreach my $message ( @$messages ) { |
233 |
my @lines = split /\n/, $message->{content}; |
234 |
chomp for @lines; |
235 |
|
236 |
# We don't have headers, get them |
237 |
unless ( @headers ) { |
238 |
@headers = split $delimiter, $lines[0]; |
239 |
|
240 |
$nb_cols = @headers; |
241 |
$doc->expandTable( $table, $nb_rows + 1, $nb_cols ); |
242 |
my $row = $doc->getRow( $table, 0 ); |
243 |
my $j = 0; |
244 |
for my $header ( @headers ) { |
245 |
$doc->cellValue( $row, $j, Encode::encode( 'UTF8', $header ) ); |
246 |
$j++; |
159 |
} |
247 |
} |
|
|
248 |
} |
160 |
|
249 |
|
161 |
shift @lines; |
250 |
shift @lines; # remove headers |
162 |
for my $line ( @lines ) { |
251 |
my $i = 1; |
163 |
chomp $line; |
252 |
for my $line ( @lines ) { |
164 |
next if $line =~ /^\s$/; |
253 |
my $row_data = split $delimiter, $line; |
165 |
say $OUTPUT $line; |
254 |
my $row = $doc->getRow( $table, $i ); |
|
|
255 |
# Note scalar(@$row_data) should be equal to $nb_cols |
256 |
for ( my $j = 0 ; $j < scalar(@$row_data) ; $j++ ) { |
257 |
my $value = Encode::encode( 'UTF8', $row_data->[$j] ); |
258 |
$doc->cellValue( $row, $j, $value ); |
166 |
} |
259 |
} |
167 |
|
260 |
$i++; |
168 |
C4::Letters::_set_message_status( |
|
|
169 |
{ |
170 |
message_id => $message->{'message_id'}, |
171 |
status => 'sent' |
172 |
} |
173 |
) if $message->{status} ne 'sent'; |
174 |
} |
261 |
} |
175 |
close $OUTPUT; |
|
|
176 |
} |
262 |
} |
|
|
263 |
$doc->save(); |
177 |
} |
264 |
} |
178 |
|
265 |
|
179 |
=head1 NAME |
266 |
=head1 NAME |
Lines 182-192
gather_print_notices - Print waiting print notices
Link Here
|
182 |
|
269 |
|
183 |
=head1 SYNOPSIS |
270 |
=head1 SYNOPSIS |
184 |
|
271 |
|
185 |
gather_print_notices output_directory [-s|--split] [--html] [--csv] [--letter_code=LETTER_CODE] [-h|--help] |
272 |
gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-h|--help] |
186 |
|
273 |
|
187 |
Will print all waiting print notices to the output_directory. |
274 |
Will print all waiting print notices to the output_directory. |
188 |
|
275 |
|
189 |
The generated filename will be holdnotices-TODAY.[csv|html] or holdnotices-TODAY-BRANCHCODE.[csv|html] if the --split parameter is given. |
276 |
The generated filename will be holdnotices-TODAY.[csv|html|ods] or holdnotices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given. |
190 |
|
277 |
|
191 |
=head1 OPTIONS |
278 |
=head1 OPTIONS |
192 |
|
279 |
|
Lines 198-214
Define the output directory where the files will be generated.
Link Here
|
198 |
|
285 |
|
199 |
=item B<-s|--split> |
286 |
=item B<-s|--split> |
200 |
|
287 |
|
201 |
Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html] |
288 |
Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods] |
202 |
|
289 |
|
203 |
=item B<--html> |
290 |
=item B<--html> |
204 |
|
291 |
|
205 |
Generate the print notices in a html file (default if --html and --csv are not given). |
292 |
Generate the print notices in a html file (default if --html, --csv and ods are not given). |
206 |
|
293 |
|
207 |
=item B<--csv> |
294 |
=item B<--csv> |
208 |
|
295 |
|
209 |
Generate the print notices in a csv file. |
296 |
Generate the print notices in a csv file. |
210 |
If you use this parameter, the template should contain 2 lines. |
297 |
If you use this parameter, the template should contain 2 lines. |
211 |
The first one the the csv headers and the second one the value list. |
298 |
The first one the csv headers and the second one the value list. |
212 |
|
299 |
|
213 |
For example: |
300 |
For example: |
214 |
cardnumber:patron:email:item |
301 |
cardnumber:patron:email:item |
Lines 216-221
cardnumber:patron:email:item
Link Here
|
216 |
|
303 |
|
217 |
You have to combine this option without one (and only one) letter_code. |
304 |
You have to combine this option without one (and only one) letter_code. |
218 |
|
305 |
|
|
|
306 |
=item B<--ods> |
307 |
|
308 |
Generate the print notices in a ods file. |
309 |
|
310 |
This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file. |
311 |
|
219 |
=item B<--letter_code> |
312 |
=item B<--letter_code> |
220 |
|
313 |
|
221 |
Filter print messages by letter_code. |
314 |
Filter print messages by letter_code. |
222 |
- |
|
|