View | Details | Raw Unified | Return to bug 11603
Collapse All | Expand All

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

Return to bug 11603