|
Lines 23-33
use C4::Log;
Link Here
|
| 23 |
|
23 |
|
| 24 |
use Koha::DateUtils; |
24 |
use Koha::DateUtils; |
| 25 |
|
25 |
|
| 26 |
my ( $stylesheet, $help, $split ); |
26 |
my ( $stylesheet, $help, $split, $html, $csv, @letter_codes ); |
| 27 |
|
27 |
|
| 28 |
GetOptions( |
28 |
GetOptions( |
| 29 |
'h|help' => \$help, |
29 |
'h|help' => \$help, |
| 30 |
's|split' => \$split, |
30 |
's|split' => \$split, |
|
|
31 |
'html' => \$html, |
| 32 |
'csv' => \$csv, |
| 33 |
'letter_code:s' => \@letter_codes, |
| 31 |
) || pod2usage(1); |
34 |
) || pod2usage(1); |
| 32 |
|
35 |
|
| 33 |
pod2usage(0) if $help; |
36 |
pod2usage(0) if $help; |
|
Lines 41-51
if ( !$output_directory || !-d $output_directory || !-w $output_directory ) {
Link Here
|
| 41 |
}); |
44 |
}); |
| 42 |
} |
45 |
} |
| 43 |
|
46 |
|
|
|
47 |
# Default value is html |
| 48 |
$html = 1 unless $html or $csv; |
| 49 |
|
| 50 |
if ( $csv and @letter_codes != 1 ) { |
| 51 |
pod2usage({ |
| 52 |
-exitval => 1, |
| 53 |
-msg => qq{\nIt is not consistent to use --csv without one (and only one) letter_code\n}, |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 44 |
cronlogaction(); |
57 |
cronlogaction(); |
| 45 |
|
58 |
|
| 46 |
my $today = C4::Dates->new(); |
59 |
my $today = C4::Dates->new(); |
| 47 |
my @all_messages = @{ GetPrintMessages() }; |
60 |
my @all_messages = @{ GetPrintMessages() }; |
| 48 |
exit unless (@all_messages); |
61 |
|
|
|
62 |
# Filter by letter_code |
| 63 |
@all_messages = map { |
| 64 |
my $letter_code = $_->{letter_code}; |
| 65 |
( |
| 66 |
grep { /^$letter_code$/ } @letter_codes |
| 67 |
) ? $_ : () |
| 68 |
} @all_messages; |
| 69 |
exit unless @all_messages; |
| 49 |
|
70 |
|
| 50 |
## carriage return replaced by <br/> as output is html |
71 |
## carriage return replaced by <br/> as output is html |
| 51 |
foreach my $message (@all_messages) { |
72 |
foreach my $message (@all_messages) { |
|
Lines 55-63
foreach my $message (@all_messages) {
Link Here
|
| 55 |
$message->{'content'} = $_; |
76 |
$message->{'content'} = $_; |
| 56 |
} |
77 |
} |
| 57 |
|
78 |
|
| 58 |
my $OUTPUT; |
79 |
print_notices_html({ messages => \@all_messages, split => $split }) |
|
|
80 |
if $html; |
| 59 |
|
81 |
|
| 60 |
print_notices_html({ messages => \@all_messages, split => $split }); |
82 |
print_notices_csv({ messages => \@all_messages, split => $split }) |
|
|
83 |
if $csv; |
| 61 |
|
84 |
|
| 62 |
sub print_notices_html { |
85 |
sub print_notices_html { |
| 63 |
my ( $params ) = @_; |
86 |
my ( $params ) = @_; |
|
Lines 102-108
sub print_notices_html {
Link Here
|
| 102 |
status => 'sent' |
125 |
status => 'sent' |
| 103 |
} |
126 |
} |
| 104 |
); |
127 |
); |
|
|
128 |
$message->{status} = 'sent'; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
sub print_notices_csv { |
| 134 |
my ( $params ) = @_; |
| 135 |
|
| 136 |
my $messages = $params->{messages}; |
| 137 |
my $split = $params->{split}; |
| 138 |
|
| 139 |
my $messages_by_branch; |
| 140 |
if ( $split ) { |
| 141 |
foreach my $message (@$messages) { |
| 142 |
push( @{ $messages_by_branch->{ $message->{'branchcode'} } }, $message ); |
| 143 |
} |
| 144 |
} else { |
| 145 |
$messages_by_branch->{all_branches} = $messages; |
| 146 |
} |
| 147 |
|
| 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"; |
| 152 |
|
| 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}; |
| 157 |
|
| 158 |
# We don't have headers, get them |
| 159 |
unless ( $headers ) { |
| 160 |
$headers = $lines[0]; |
| 161 |
chomp $headers; |
| 162 |
say $OUTPUT $headers; |
| 163 |
} |
| 164 |
|
| 165 |
shift @lines; |
| 166 |
for my $line ( @lines ) { |
| 167 |
chomp $line; |
| 168 |
next if $line =~ /^\s$/; |
| 169 |
say $OUTPUT $line; |
| 170 |
} |
| 171 |
|
| 172 |
C4::Letters::_set_message_status( |
| 173 |
{ |
| 174 |
message_id => $message->{'message_id'}, |
| 175 |
status => 'sent' |
| 176 |
} |
| 177 |
) if $message->{status} ne 'sent'; |
| 105 |
} |
178 |
} |
|
|
179 |
close $OUTPUT; |
| 106 |
} |
180 |
} |
| 107 |
} |
181 |
} |
| 108 |
|
182 |
|
|
Lines 112-122
gather_print_notices - Print waiting print notices
Link Here
|
| 112 |
|
186 |
|
| 113 |
=head1 SYNOPSIS |
187 |
=head1 SYNOPSIS |
| 114 |
|
188 |
|
| 115 |
gather_print_notices output_directory [-s|--split] [-h|--help] |
189 |
gather_print_notices output_directory [-s|--split] [--html] [--csv] [--letter_code=LETTER_CODE] [-h|--help] |
| 116 |
|
190 |
|
| 117 |
Will print all waiting print notices to the output_directory. |
191 |
Will print all waiting print notices to the output_directory. |
| 118 |
|
192 |
|
| 119 |
The generated filename will be holdnotices-TODAY.html or holdnotices-TODAY-BRANCHCODE.html if the --split parameter is given. |
193 |
The generated filename will be holdnotices-TODAY.[csv|html] or holdnotices-TODAY-BRANCHCODE.[csv|html] if the --split parameter is given. |
| 120 |
|
194 |
|
| 121 |
=head1 OPTIONS |
195 |
=head1 OPTIONS |
| 122 |
|
196 |
|
|
Lines 128-134
Define the output directory where the files will be generated.
Link Here
|
| 128 |
|
202 |
|
| 129 |
=item B<-s|--split> |
203 |
=item B<-s|--split> |
| 130 |
|
204 |
|
| 131 |
Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.html |
205 |
Split messages into separate file by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html] |
|
|
206 |
|
| 207 |
=item B<--html> |
| 208 |
|
| 209 |
Generate the print notices in a html file (default if --html and --csv are not given). |
| 210 |
|
| 211 |
=item B<--csv> |
| 212 |
|
| 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. |
| 216 |
|
| 217 |
For example: |
| 218 |
cardnumber:patron:email:item |
| 219 |
<<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>> |
| 220 |
|
| 221 |
You have to combine this option without one (and only one) letter_code. |
| 222 |
|
| 223 |
=item B<--letter_code> |
| 224 |
|
| 225 |
Filter print messages by letter_code. |
| 226 |
Several letter_code parameters can be given. |
| 132 |
|
227 |
|
| 133 |
=item B<-h|--help> |
228 |
=item B<-h|--help> |
| 134 |
|
229 |
|
| 135 |
- |
|
|