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>.
22 use C4::Auth qw( get_template_and_user );
24 use C4::Output qw( output_html_with_http_headers );
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
35 template_name => "tools/preview_letter.tt",
38 flagsrequired => { tools => 'edit_notices' },
44 my $code = $input->param('code');
45 my $content = $input->param('content');
46 my $title = $input->param('title');
47 my $is_html = $input->param('is_html');
48 my $data_preview = $input->param('data_preview');
50 unless ( $data_preview ) {
51 $template->param( messages => [{ code => 'no_data_for_preview', type => 'error' }]);
52 output_html_with_http_headers $input, $cookie, $template->output;
56 my $fake_letter = { content => $content, title => $title, is_html => $is_html };
58 my ( $tt_content, $fake_tt_letter );
59 if ( $content =~ m/[^\n]*<<.*>>[^\n]*/so ) {
60 $tt_content = $content;
64 borrowers => 'borrower',
70 subscription => 'subscription',
71 suggestions => 'suggestion',
73 old_issues => 'old_checkout',
74 overdues => 'overdue',
75 borrower_modifications => 'patron_modification',
79 $tt_content =~ s#<<today>>#[% today| \$KohaDates with_hours => 1 %]#sg;
82 for my $date_field ( qw(
84 borrowers.dateenrolled
88 items.datelastborrowed
96 reserves.expirationdate
97 suggestions.suggesteddate
98 suggestions.manageddate
99 suggestions.accepteddate
100 suggestions.rejecteddate
102 aqorders.datereceived
103 aqorders.datecancellationprinted
105 aqorders.claimed_date
107 my ( $table, $field ) = split '\.', $date_field;
109 exists $table_mapping->{$table}
110 ? $table_mapping->{$table} . ".$field"
112 $tt_content =~ s#<<$table\.$field>>#[% $new_field | \$KohaDates %]#sg;
113 $tt_content =~ s#<<$table\.$field\s*|\s*dateonly>>#[% $new_field | \$KohaDates %]#sg;
116 for my $datetime_field ( qw(
121 issues.lastreneweddate
123 reserves.suspend_until
125 my ( $table, $field ) = split '\.', $datetime_field;
127 exists $table_mapping->{$table}
128 ? $table_mapping->{$table} . ".$field"
130 $tt_content =~ s#<<$table\.$field>>#[% $new_field | \$KohaDates with_hours => 1 %]#sg;
131 $tt_content =~ s#<<$table\.$field\s*|\s*dateonly>>#[% $new_field | \$KohaDates %]#sg;
136 while ( my ( $key, $value ) = each %$table_mapping ) {
137 $tt_content =~ s|<<$key\.|<<$value.|sg;
140 $tt_content =~ s|<<|[% |sg;
141 $tt_content =~ s|>>| %]|sg;
143 { content => $tt_content, title => $title, is_html => $is_html };
146 my ( $rendered_message, $rendered_tt_message ) = (q||) x 2;
147 my $messages_are_similar;
148 my $letter_params = {};
149 if ( $code eq 'CHECKIN' ) {
150 my $item = Koha::Items->find( { barcode => $data_preview } );
151 my $checkout = Koha::Checkouts->find( { itemnumber => $item->itemnumber } );
153 my $patron = Koha::Patrons->find( $checkout->borrowernumber );
155 C4::Circulation::_GetCircControlBranch( $item->unblessed,
156 $patron->unblessed );
159 issues => $item->itemnumber,
160 items => $item->itemnumber,
161 biblio => $item->biblionumber,
162 biblioitems => $item->biblionumber,
163 issues => $patron->borrowernumber,
164 branches => $branchcode,
167 push @messages, { code => 'not_checked_in_yet', type => 'message' };
170 push @messages, { code => 'no_checkout', type => 'alert' };
174 elsif ( $code eq 'CHECKOUT' ) {
175 my ( $barcode, $borrowernumber ) = split '\|', $data_preview;
176 my $item = Koha::Items->find( { barcode => $barcode } );
177 my $patron = Koha::Patrons->find( $borrowernumber );
178 if ($item and $patron) {
180 C4::Circulation::_GetCircControlBranch( $item->unblessed,
181 $patron->unblessed );
184 issues => $item->itemnumber,
185 items => $item->itemnumber,
186 biblio => $item->biblionumber,
187 biblioitems => $item->biblionumber,
188 issues => $patron->borrowernumber,
189 branches => $branchcode,
192 push @messages, { code => 'not_checked_out_yet', type => 'message' };
195 push @messages, { code => 'no_item_or_no_patron', type => 'alert' };
199 elsif ( $code eq 'HOLD_SLIP' ) {
200 my ( $biblionumber, $borrowernumber ) = split '\|', $data_preview;
201 my $hold = Koha::Holds->find( { borrowernumber => $borrowernumber, biblionumber => $biblionumber } );
205 reserves => $hold->unblessed,
206 branches => $hold->branchcode,
207 borrowers => $hold->borrowernumber,
208 biblio => $hold->biblionumber,
209 biblioitems => $hold->biblionumber,
210 items => $hold->itemnumber,
215 push @messages, { code => 'no_hold', type => 'alert' };
220 push @messages, { type => 'alert', code => 'preview_not_available', letter_code => $code, };
223 if ( %$letter_params ) {
224 # FIXME Be case here GetPreparedLetter modify $fake_letter
225 $rendered_message = C4::Letters::GetPreparedLetter(
226 letter => $fake_letter,
230 $rendered_tt_message = C4::Letters::GetPreparedLetter(
231 letter => $fake_tt_letter,
234 $messages_are_similar =
235 $rendered_message->{content} eq $rendered_tt_message->{content};
240 original_content => $content,
241 rendered_message => $rendered_message,
242 tt_content => $tt_content,
243 rendered_tt_message => $rendered_tt_message,
244 messages_are_similar => $messages_are_similar,
245 messages => \@messages,
248 output_html_with_http_headers $input, $cookie, $template->output;