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

(-)a/misc/cronjobs/gather_print_notices.pl (-25 / +127 lines)
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::Temp qw( tempdir );
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 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
my $csv2ods_cmd;
66
if ( $ods ) {
67
    $csv2ods_cmd = qx|which csv2ods|;
68
    chomp $csv2ods_cmd;
69
    if ( not $csv2ods_cmd or not -f $csv2ods_cmd ) {
70
        my $installation_instructions = qq|
71
Please have a look at https://code.google.com/p/scone-fu.
72
73
Installation instructions:
74
git clone https://code.google.com/p/scone-fu/
75
cd scone-fu/code/python/odfpy-0.9.3/
76
sudo python setup.py install\n|;
77
        say qq{\ncsv2ods seems not to be installed.\n$installation_instructions\n};
78
        exit 1;
79
    }
80
81
    $delimiter ||= q|,|;
82
}
83
55
my $today        = C4::Dates->new();
84
my $today        = C4::Dates->new();
56
my @all_messages = @{ GetPrintMessages() };
85
my @all_messages = @{ GetPrintMessages() };
57
86
Lines 72-88 foreach my $message (@all_messages) { Link Here
72
    $message->{'content'} = $_;
101
    $message->{'content'} = $_;
73
}
102
}
74
103
75
print_notices_html({ messages => \@all_messages, split => $split })
104
print_notices_html({
76
    if $html;
105
    messages => \@all_messages,
106
    split => $split,
107
    output_directory => $output_directory,
108
}) if $html;
109
110
print_notices_csv({
111
    messages => \@all_messages,
112
    split => $split,
113
    output_directory => $output_directory,
114
}) if $csv;
77
115
78
print_notices_csv({ messages => \@all_messages, split => $split })
116
print_notices_ods({
79
    if $csv;
117
    messages => \@all_messages,
118
    split => $split,
119
    output_directory => $output_directory,
120
}) if $ods;
80
121
81
sub print_notices_html {
122
sub print_notices_html {
82
    my ( $params ) = @_;
123
    my ( $params ) = @_;
83
124
84
    my $messages = $params->{messages};
125
    my $messages = $params->{messages};
85
    my $split = $params->{split};
126
    my $split = $params->{split};
127
    my $output_directory = $params->{output_directory};
128
    my $set_status = $params->{set_status} // 1;
129
    my @filenames;
86
130
87
    my $messages_by_branch;
131
    my $messages_by_branch;
88
    if ( $split ) {
132
    if ( $split ) {
Lines 114-129 sub print_notices_html { Link Here
114
        print $OUTPUT $template->output;
158
        print $OUTPUT $template->output;
115
        close $OUTPUT;
159
        close $OUTPUT;
116
160
117
        foreach my $message ( @$branch_messages ) {
161
        if ( $set_status ) {
118
            C4::Letters::_set_message_status(
162
            foreach my $message ( @$branch_messages ) {
119
                {
163
                C4::Letters::_set_message_status(
120
                    message_id => $message->{'message_id'},
164
                    {
121
                    status => 'sent'
165
                        message_id => $message->{'message_id'},
122
                }
166
                        status => 'sent'
123
            );
167
                    }
124
            $message->{status} = 'sent';
168
                );
169
            }
125
        }
170
        }
171
        push @filenames, $filename;
126
    }
172
    }
173
    return \@filenames;
127
}
174
}
128
175
129
sub print_notices_csv {
176
sub print_notices_csv {
Lines 131-136 sub print_notices_csv { Link Here
131
178
132
    my $messages = $params->{messages};
179
    my $messages = $params->{messages};
133
    my $split = $params->{split};
180
    my $split = $params->{split};
181
    my $output_directory = $params->{output_directory};
182
    my $set_status = $params->{set_status} // 1;
183
    my @filenames;
134
184
135
    my $messages_by_branch;
185
    my $messages_by_branch;
136
    if ( $split ) {
186
    if ( $split ) {
Lines 165-179 sub print_notices_csv { Link Here
165
                say $OUTPUT $line;
215
                say $OUTPUT $line;
166
            }
216
            }
167
217
168
            C4::Letters::_set_message_status(
218
            if ( $set_status ) {
169
                {
219
                C4::Letters::_set_message_status(
170
                    message_id => $message->{'message_id'},
220
                    {
171
                    status => 'sent'
221
                        message_id => $message->{'message_id'},
172
                }
222
                        status => 'sent'
173
            ) if $message->{status} ne 'sent';
223
                    }
224
                );
225
            }
174
        }
226
        }
175
        close $OUTPUT;
227
        close $OUTPUT;
228
        push @filenames, $filename;
229
    }
230
    return \@filenames;
231
}
232
233
sub print_notices_ods {
234
    my ( $params ) = @_;
235
236
    my $messages = $params->{messages};
237
    my $split = $params->{split};
238
    my $output_directory = $params->{output_directory};
239
    my $set_status = $params->{set_status} // 1;
240
    my @filenames;
241
242
    my $messages_by_branch;
243
    if ( $split ) {
244
        foreach my $message (@$messages) {
245
            push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message );
246
        }
247
    } else {
248
        $messages_by_branch->{all_branches} = $messages;
249
    }
250
251
    while ( my ( $branchcode, $branch_messages ) = each %$messages_by_branch ) {
252
        my $filename = $split
253
            ? 'holdnotices-' . $today->output('iso') . "-$branchcode.ods"
254
            : 'holdnotices-' . $today->output('iso') . ".ods";
255
256
        my $tmpdir = File::Temp->newdir();
257
258
        my ( $csv_filename ) = @{ print_notices_csv({
259
            messages => $branch_messages,
260
            output_directory => $tmpdir,
261
            set_status => 0,
262
        }) };
263
264
        my $ods_output_filepath = File::Spec->catdir( $output_directory, $filename );
265
        my $csv_filepath = File::Spec->catdir( $tmpdir, $csv_filename );
266
267
        qx|
268
            $csv2ods_cmd -i $csv_filepath -o $ods_output_filepath -d $delimiter
269
        |;
270
271
        push @filenames, $filename;
176
    }
272
    }
273
    return \@filenames;
177
}
274
}
178
275
179
=head1 NAME
276
=head1 NAME
Lines 182-192 gather_print_notices - Print waiting print notices Link Here
182
279
183
=head1 SYNOPSIS
280
=head1 SYNOPSIS
184
281
185
gather_print_notices output_directory [-s|--split] [--html] [--csv] [--letter_code=LETTER_CODE] [-h|--help]
282
gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-h|--help]
186
283
187
Will print all waiting print notices to the output_directory.
284
Will print all waiting print notices to the output_directory.
188
285
189
The generated filename will be holdnotices-TODAY.[csv|html] or holdnotices-TODAY-BRANCHCODE.[csv|html] if the --split parameter is given.
286
The generated filename will be holdnotices-TODAY.[csv|html|ods] or holdnotices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given.
190
287
191
=head1 OPTIONS
288
=head1 OPTIONS
192
289
Lines 198-214 Define the output directory where the files will be generated. Link Here
198
295
199
=item B<-s|--split>
296
=item B<-s|--split>
200
297
201
Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html]
298
Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods]
202
299
203
=item B<--html>
300
=item B<--html>
204
301
205
Generate the print notices in a html file (default if --html and --csv are not given).
302
Generate the print notices in a html file (default if --html, --csv and ods are not given).
206
303
207
=item B<--csv>
304
=item B<--csv>
208
305
209
Generate the print notices in a csv file.
306
Generate the print notices in a csv file.
210
If you use this parameter, the template should contain 2 lines.
307
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.
308
The first one the csv headers and the second one the value list.
212
309
213
For example:
310
For example:
214
cardnumber:patron:email:item
311
cardnumber:patron:email:item
Lines 216-221 cardnumber:patron:email:item Link Here
216
313
217
You have to combine this option without one (and only one) letter_code.
314
You have to combine this option without one (and only one) letter_code.
218
315
316
=item B<--ods>
317
318
Generate the print notices in a ods file.
319
320
This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file.
321
219
=item B<--letter_code>
322
=item B<--letter_code>
220
323
221
Filter print messages by letter_code.
324
Filter print messages by letter_code.
222
- 

Return to bug 11603