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