3 # Copyright 2016 Koha Development Team
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
35 template_name => "tools/preview_letter.tt",
39 flagsrequired => { tools => 'edit_notices' },
45 my $code = $input->param('code');
46 my $content = $input->param('content');
47 my $title = $input->param('title');
48 my $is_html = $input->param('is_html');
49 my $data_preview = $input->param('data_preview');
51 unless ( $data_preview ) {
52 $template->param( messages => [{ code => 'no_data_for_preview', type => 'error' }]);
53 output_html_with_http_headers $input, $cookie, $template->output;
57 my $fake_letter = { content => $content, title => $title, is_html => $is_html };
59 my ( $tt_content, $fake_tt_letter );
60 if ( $content =~ m/[^\n]*<<.*>>[^\n]*/so ) {
61 $tt_content = $content;
65 borrowers => 'borrower',
72 subscription => 'subscription',
73 suggestions => 'suggestion',
75 old_issues => 'old_checkout',
76 overdues => 'overdue',
77 borrower_modifications => 'patron_modification',
81 $tt_content =~ s#<<today>>#[% today| \$KohaDates with_hours => 1 %]#sg;
84 for my $date_field ( qw(
86 borrowers.dateenrolled
90 items.datelastborrowed
98 reserves.expirationdate
99 suggestions.suggesteddate
100 suggestions.manageddate
101 suggestions.accepteddate
102 suggestions.rejecteddate
104 aqorders.datereceived
105 aqorders.datecancellationprinted
107 aqorders.claimed_date
109 my ( $table, $field ) = split '\.', $date_field;
111 exists $table_mapping->{$table}
112 ? $table_mapping->{$table} . ".$field"
114 $tt_content =~ s#<<$table\.$field>>#[% $new_field | \$KohaDates %]#sg;
115 $tt_content =~ s#<<$table\.$field\s*|\s*dateonly>>#[% $new_field | \$KohaDates %]#sg;
118 for my $datetime_field ( qw(
123 issues.lastreneweddate
125 reserves.suspend_until
127 my ( $table, $field ) = split '\.', $datetime_field;
129 exists $table_mapping->{$table}
130 ? $table_mapping->{$table} . ".$field"
132 $tt_content =~ s#<<$table\.$field>>#[% $new_field | \$KohaDates with_hours => 1 %]#sg;
133 $tt_content =~ s#<<$table\.$field\s*|\s*dateonly>>#[% $new_field | \$KohaDates %]#sg;
138 while ( my ( $key, $value ) = each %$table_mapping ) {
139 $tt_content =~ s|<<$key\.|<<$value.|sg;
142 $tt_content =~ s|<<|[% |sg;
143 $tt_content =~ s|>>| %]|sg;
145 { content => $tt_content, title => $title, is_html => $is_html };
148 my ( $rendered_message, $rendered_tt_message ) = (q||) x 2;
149 my $messages_are_similar;
150 my $letter_params = {};
151 if ( $code eq 'CHECKIN' ) {
152 my $item = Koha::Items->find( { barcode => $data_preview } );
153 my $checkout = Koha::Checkouts->find( { itemnumber => $item->itemnumber } );
155 my $patron = Koha::Patrons->find( $checkout->borrowernumber );
157 C4::Circulation::_GetCircControlBranch( $item->unblessed,
158 $patron->unblessed );
161 issues => $item->itemnumber,
162 items => $item->itemnumber,
163 biblio => $item->biblionumber,
164 biblioitems => $item->biblionumber,
165 issues => $patron->borrowernumber,
166 branches => $branchcode,
169 push @messages, { code => 'not_checked_in_yet', type => 'message' };
172 push @messages, { code => 'no_checkout', type => 'alert' };
176 elsif ( $code eq 'CHECKOUT' ) {
177 my ( $barcode, $borrowernumber ) = split '\|', $data_preview;
178 my $item = Koha::Items->find( { barcode => $barcode } );
179 my $patron = Koha::Patrons->find( $borrowernumber );
180 if ($item and $patron) {
182 C4::Circulation::_GetCircControlBranch( $item->unblessed,
183 $patron->unblessed );
186 issues => $item->itemnumber,
187 items => $item->itemnumber,
188 biblio => $item->biblionumber,
189 biblioitems => $item->biblionumber,
190 issues => $patron->borrowernumber,
191 branches => $branchcode,
194 push @messages, { code => 'not_checked_out_yet', type => 'message' };
197 push @messages, { code => 'no_item_or_no_patron', type => 'alert' };
201 elsif ( $code eq 'HOLD_SLIP' ) {
202 my ( $biblionumber, $borrowernumber ) = split '\|', $data_preview;
203 my $hold = Koha::Holds->find( { borrowernumber => $borrowernumber, biblionumber => $biblionumber } );
207 reserves => $hold->unblessed,
208 branches => $hold->branchcode,
209 borrowers => $hold->borrowernumber,
210 biblio => $hold->biblionumber,
211 biblioitems => $hold->biblionumber,
212 items => $hold->itemnumber,
217 push @messages, { code => 'no_hold', type => 'alert' };
222 push @messages, { type => 'alert', code => 'preview_not_available', letter_code => $code, };
225 if ( %$letter_params ) {
226 # FIXME Be case here GetPreparedLetter modify $fake_letter
227 $rendered_message = C4::Letters::GetPreparedLetter(
228 letter => $fake_letter,
232 $rendered_tt_message = C4::Letters::GetPreparedLetter(
233 letter => $fake_tt_letter,
236 $messages_are_similar =
237 $rendered_message->{content} eq $rendered_tt_message->{content};
242 original_content => $content,
243 rendered_message => $rendered_message,
244 tt_content => $tt_content,
245 rendered_tt_message => $rendered_tt_message,
246 messages_are_similar => $messages_are_similar,
247 messages => \@messages,
250 output_html_with_http_headers $input, $cookie, $template->output;