Bug 25762: Typo in linkitem.tt
[koha.git] / svc / letters / preview
blob2d0c73faeb4009648b3552ef43210d829bc996af
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
21 use CGI qw( -utf8 );
22 use C4::Auth;
23 use C4::Context;
24 use C4::Output;
25 use C4::Circulation;
26 use C4::Letters;
27 use Koha::Checkouts;
28 use Koha::Items;
29 use Koha::Patrons;
31 my $input = new CGI;
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
35 template_name => "tools/preview_letter.tt",
36 query => $input,
37 type => "intranet",
38 authnotrequired => 0,
39 flagsrequired => { tools => 'edit_notices' },
40 debug => 1,
44 my @messages;
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;
54 exit;
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;
63 my $table_mapping = {
64 biblio => 'biblio',
65 borrowers => 'borrower',
66 branches => 'branch',
67 items => 'item',
68 opac_news => 'news',
69 aqorders => 'orders',
70 reserves => 'hold',
71 serial => 'serial',
72 subscription => 'subscription',
73 suggestions => 'suggestion',
74 issues => 'checkout',
75 old_issues => 'old_checkout',
76 overdues => 'overdue',
77 borrower_modifications => 'patron_modification',
80 # Today
81 $tt_content =~ s#<<today>>#[% today| \$KohaDates with_hours => 1 %]#sg;
84 for my $date_field ( qw(
85 borrowers.dateofbirth
86 borrowers.dateenrolled
87 borrowers.dateexpiry
88 borrowers.debarred
89 items.dateaccessioned
90 items.datelastborrowed
91 items.datelastseen
92 items.onloan
93 serials.planneddate
94 serials.publisheddate
95 serials.claimdate
96 reserves.reservedate
97 reserves.waitingdate
98 reserves.expirationdate
99 suggestions.suggesteddate
100 suggestions.manageddate
101 suggestions.accepteddate
102 suggestions.rejecteddate
103 aqorders.entrydate
104 aqorders.datereceived
105 aqorders.datecancellationprinted
106 aqorders.budgetdate
107 aqorders.claimed_date
108 ) ) {
109 my ( $table, $field ) = split '\.', $date_field;
110 my $new_field =
111 exists $table_mapping->{$table}
112 ? $table_mapping->{$table} . ".$field"
113 : "$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(
119 items.itemlost_on
120 items.withdrawn_on
121 issues.date_due
122 issues.returndate
123 issues.lastreneweddate
124 issues.issuedate
125 reserves.suspend_until
126 ) ) {
127 my ( $table, $field ) = split '\.', $datetime_field;
128 my $new_field =
129 exists $table_mapping->{$table}
130 ? $table_mapping->{$table} . ".$field"
131 : "$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;
144 $fake_tt_letter =
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 } );
154 if ($checkout) {
155 my $patron = Koha::Patrons->find( $checkout->borrowernumber );
156 my $branchcode =
157 C4::Circulation::_GetCircControlBranch( $item->unblessed,
158 $patron->unblessed );
159 $letter_params = {
160 tables => {
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' };
171 else {
172 push @messages, { code => 'no_checkout', type => 'alert' };
173 $letter_params = {};
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) {
181 my $branchcode =
182 C4::Circulation::_GetCircControlBranch( $item->unblessed,
183 $patron->unblessed );
184 $letter_params = {
185 tables => {
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' };
196 else {
197 push @messages, { code => 'no_item_or_no_patron', type => 'alert' };
198 $letter_params = {};
201 elsif ( $code eq 'HOLD_SLIP' ) {
202 my ( $biblionumber, $borrowernumber ) = split '\|', $data_preview;
203 my $hold = Koha::Holds->find( { borrowernumber => $borrowernumber, biblionumber => $biblionumber } );
204 if ($hold) {
205 $letter_params = {
206 tables => {
207 reserves => $hold->unblessed,
208 branches => $hold->branchcode,
209 borrowers => $hold->borrowernumber,
210 biblio => $hold->biblionumber,
211 biblioitems => $hold->biblionumber,
212 items => $hold->itemnumber,
216 else {
217 push @messages, { code => 'no_hold', type => 'alert' };
218 $letter_params = {};
221 else {
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,
229 %$letter_params,
231 if ($tt_content) {
232 $rendered_tt_message = C4::Letters::GetPreparedLetter(
233 letter => $fake_tt_letter,
234 %$letter_params,
236 $messages_are_similar =
237 $rendered_message->{content} eq $rendered_tt_message->{content};
241 $template->param(
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;