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",
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',
71 subscription => 'subscription',
72 suggestions => 'suggestion',
74 old_issues => 'old_checkout',
75 overdues => 'overdue',
76 borrower_modifications => 'patron_modification',
80 $tt_content =~ s#<<today>>#[% today| \$KohaDates with_hours => 1 %]#sg;
83 for my $date_field ( qw(
85 borrowers.dateenrolled
89 items.datelastborrowed
97 reserves.expirationdate
98 suggestions.suggesteddate
99 suggestions.manageddate
100 suggestions.accepteddate
101 suggestions.rejecteddate
103 aqorders.datereceived
104 aqorders.datecancellationprinted
106 aqorders.claimed_date
108 my ( $table, $field ) = split '\.', $date_field;
110 exists $table_mapping->{$table}
111 ? $table_mapping->{$table} . ".$field"
113 $tt_content =~ s#<<$table\.$field>>#[% $new_field | \$KohaDates %]#sg;
114 $tt_content =~ s#<<$table\.$field\s*|\s*dateonly>>#[% $new_field | \$KohaDates %]#sg;
117 for my $datetime_field ( qw(
122 issues.lastreneweddate
124 reserves.suspend_until
126 my ( $table, $field ) = split '\.', $datetime_field;
128 exists $table_mapping->{$table}
129 ? $table_mapping->{$table} . ".$field"
131 $tt_content =~ s#<<$table\.$field>>#[% $new_field | \$KohaDates with_hours => 1 %]#sg;
132 $tt_content =~ s#<<$table\.$field\s*|\s*dateonly>>#[% $new_field | \$KohaDates %]#sg;
137 while ( my ( $key, $value ) = each %$table_mapping ) {
138 $tt_content =~ s|<<$key\.|<<$value.|sg;
141 $tt_content =~ s|<<|[% |sg;
142 $tt_content =~ s|>>| %]|sg;
144 { content => $tt_content, title => $title, is_html => $is_html };
147 my ( $rendered_message, $rendered_tt_message ) = (q||) x 2;
148 my $messages_are_similar;
149 my $letter_params = {};
150 if ( $code eq 'CHECKIN' ) {
151 my $item = Koha::Items->find( { barcode => $data_preview } );
152 my $checkout = Koha::Checkouts->find( { itemnumber => $item->itemnumber } );
154 my $patron = Koha::Patrons->find( $checkout->borrowernumber );
156 C4::Circulation::_GetCircControlBranch( $item->unblessed,
157 $patron->unblessed );
160 issues => $item->itemnumber,
161 items => $item->itemnumber,
162 biblio => $item->biblionumber,
163 biblioitems => $item->biblionumber,
164 issues => $patron->borrowernumber,
165 branches => $branchcode,
168 push @messages, { code => 'not_checked_in_yet', type => 'message' };
171 push @messages, { code => 'no_checkout', type => 'alert' };
175 elsif ( $code eq 'CHECKOUT' ) {
176 my ( $barcode, $borrowernumber ) = split '\|', $data_preview;
177 my $item = Koha::Items->find( { barcode => $barcode } );
178 my $patron = Koha::Patrons->find( $borrowernumber );
179 if ($item and $patron) {
181 C4::Circulation::_GetCircControlBranch( $item->unblessed,
182 $patron->unblessed );
185 issues => $item->itemnumber,
186 items => $item->itemnumber,
187 biblio => $item->biblionumber,
188 biblioitems => $item->biblionumber,
189 issues => $patron->borrowernumber,
190 branches => $branchcode,
193 push @messages, { code => 'not_checked_out_yet', type => 'message' };
196 push @messages, { code => 'no_item_or_no_patron', type => 'alert' };
200 elsif ( $code eq 'HOLD_SLIP' ) {
201 my ( $biblionumber, $borrowernumber ) = split '\|', $data_preview;
202 my $hold = Koha::Holds->find( { borrowernumber => $borrowernumber, biblionumber => $biblionumber } );
206 reserves => $hold->unblessed,
207 branches => $hold->branchcode,
208 borrowers => $hold->borrowernumber,
209 biblio => $hold->biblionumber,
210 biblioitems => $hold->biblionumber,
211 items => $hold->itemnumber,
216 push @messages, { code => 'no_hold', type => 'alert' };
221 push @messages, { type => 'alert', code => 'preview_not_available', letter_code => $code, };
224 if ( %$letter_params ) {
225 # FIXME Be case here GetPreparedLetter modify $fake_letter
226 $rendered_message = C4::Letters::GetPreparedLetter(
227 letter => $fake_letter,
231 $rendered_tt_message = C4::Letters::GetPreparedLetter(
232 letter => $fake_tt_letter,
235 $messages_are_similar =
236 $rendered_message->{content} eq $rendered_tt_message->{content};
241 original_content => $content,
242 rendered_message => $rendered_message,
243 tt_content => $tt_content,
244 rendered_tt_message => $rendered_tt_message,
245 messages_are_similar => $messages_are_similar,
246 messages => \@messages,
249 output_html_with_http_headers $input, $cookie, $template->output;