Bug 26976: Display 0 if renewalsallowed is not defined
[koha.git] / svc / letters / preview
1 #!/usr/bin/perl
2
3 # Copyright 2016 Koha Development Team
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
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;
30
31 my $input = CGI->new;
32
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
34     {
35         template_name   => "tools/preview_letter.tt",
36         query           => $input,
37         type            => "intranet",
38         flagsrequired   => { tools => 'edit_notices' },
39         debug           => 1,
40     }
41 );
42
43 my @messages;
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');
49
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;
53     exit;
54 }
55
56 my $fake_letter = { content => $content, title => $title, is_html => $is_html };
57
58 my ( $tt_content, $fake_tt_letter );
59 if ( $content =~ m/[^\n]*<<.*>>[^\n]*/so ) {
60     $tt_content = $content;
61
62     my $table_mapping = {
63         biblio                 => 'biblio',
64         borrowers              => 'borrower',
65         branches               => 'branch',
66         items                  => 'item',
67         opac_news              => 'news',
68         aqorders               => 'orders',
69         reserves               => 'hold',
70         serial                 => 'serial',
71         subscription           => 'subscription',
72         suggestions            => 'suggestion',
73         issues                 => 'checkout',
74         old_issues             => 'old_checkout',
75         overdues               => 'overdue',
76         borrower_modifications => 'patron_modification',
77     };
78
79     # Today
80     $tt_content =~ s#<<today>>#[% today| \$KohaDates with_hours => 1 %]#sg;
81
82
83     for my $date_field ( qw(
84             borrowers.dateofbirth
85             borrowers.dateenrolled
86             borrowers.dateexpiry
87             borrowers.debarred
88             items.dateaccessioned
89             items.datelastborrowed
90             items.datelastseen
91             items.onloan
92             serials.planneddate
93             serials.publisheddate
94             serials.claimdate
95             reserves.reservedate
96             reserves.waitingdate
97             reserves.expirationdate
98             suggestions.suggesteddate
99             suggestions.manageddate
100             suggestions.accepteddate
101             suggestions.rejecteddate
102             aqorders.entrydate
103             aqorders.datereceived
104             aqorders.datecancellationprinted
105             aqorders.budgetdate
106             aqorders.claimed_date
107     ) ) {
108         my ( $table, $field ) = split '\.', $date_field;
109         my $new_field =
110           exists $table_mapping->{$table}
111           ? $table_mapping->{$table} . ".$field"
112           : "$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;
115     }
116
117     for my $datetime_field ( qw(
118             items.itemlost_on
119             items.withdrawn_on
120             issues.date_due
121             issues.returndate
122             issues.lastreneweddate
123             issues.issuedate
124             reserves.suspend_until
125     ) ) {
126         my ( $table, $field ) = split '\.', $datetime_field;
127         my $new_field =
128           exists $table_mapping->{$table}
129           ? $table_mapping->{$table} . ".$field"
130           : "$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;
133     }
134
135
136
137     while ( my ( $key, $value ) = each %$table_mapping ) {
138         $tt_content =~ s|<<$key\.|<<$value.|sg;
139     }
140
141     $tt_content =~ s|<<|[% |sg;
142     $tt_content =~ s|>>| %]|sg;
143     $fake_tt_letter =
144       { content => $tt_content, title => $title, is_html => $is_html };
145 }
146
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 } );
153     if ($checkout) {
154         my $patron = Koha::Patrons->find( $checkout->borrowernumber );
155         my $branchcode =
156           C4::Circulation::_GetCircControlBranch( $item->unblessed,
157             $patron->unblessed );
158         $letter_params = {
159             tables => {
160                 issues      => $item->itemnumber,
161                 items       => $item->itemnumber,
162                 biblio      => $item->biblionumber,
163                 biblioitems => $item->biblionumber,
164                 issues      => $patron->borrowernumber,
165                 branches    => $branchcode,
166             }
167         };
168         push @messages, { code => 'not_checked_in_yet', type => 'message' };
169     }
170     else {
171         push @messages, { code => 'no_checkout', type => 'alert' };
172         $letter_params = {};
173     }
174 }
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) {
180         my $branchcode =
181           C4::Circulation::_GetCircControlBranch( $item->unblessed,
182             $patron->unblessed );
183         $letter_params = {
184             tables => {
185                 issues      => $item->itemnumber,
186                 items       => $item->itemnumber,
187                 biblio      => $item->biblionumber,
188                 biblioitems => $item->biblionumber,
189                 issues      => $patron->borrowernumber,
190                 branches    => $branchcode,
191             }
192         };
193         push @messages, { code => 'not_checked_out_yet', type => 'message' };
194     }
195     else {
196         push @messages, { code => 'no_item_or_no_patron', type => 'alert' };
197         $letter_params = {};
198     }
199 }
200 elsif ( $code eq 'HOLD_SLIP' ) {
201     my ( $biblionumber, $borrowernumber ) = split '\|', $data_preview;
202     my $hold = Koha::Holds->find( { borrowernumber => $borrowernumber, biblionumber => $biblionumber } );
203     if ($hold) {
204         $letter_params = {
205             tables => {
206                 reserves    => $hold->unblessed,
207                 branches    => $hold->branchcode,
208                 borrowers   => $hold->borrowernumber,
209                 biblio      => $hold->biblionumber,
210                 biblioitems => $hold->biblionumber,
211                 items       => $hold->itemnumber,
212             }
213         };
214     }
215     else {
216         push @messages, { code => 'no_hold', type => 'alert' };
217         $letter_params = {};
218     }
219 }
220 else {
221     push @messages, { type => 'alert', code => 'preview_not_available', letter_code => $code, };
222 }
223
224 if ( %$letter_params ) {
225     # FIXME Be case here GetPreparedLetter modify $fake_letter
226     $rendered_message = C4::Letters::GetPreparedLetter(
227         letter => $fake_letter,
228         %$letter_params,
229     );
230     if ($tt_content) {
231         $rendered_tt_message = C4::Letters::GetPreparedLetter(
232             letter => $fake_tt_letter,
233             %$letter_params,
234         );
235         $messages_are_similar =
236           $rendered_message->{content} eq $rendered_tt_message->{content};
237     }
238 }
239
240 $template->param(
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,
247 );
248
249 output_html_with_http_headers $input, $cookie, $template->output;