Bug 30770: (QA follow-up) Fixing the last occurrence of reserve in returns.tt
[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 qw( get_template_and_user );
23 use C4::Context;
24 use C4::Output qw( output_html_with_http_headers );
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         aqorders               => 'orders',
68         reserves               => 'hold',
69         serial                 => 'serial',
70         subscription           => 'subscription',
71         suggestions            => 'suggestion',
72         issues                 => 'checkout',
73         old_issues             => 'old_checkout',
74         overdues               => 'overdue',
75         borrower_modifications => 'patron_modification',
76     };
77
78     # Today
79     $tt_content =~ s#<<today>>#[% today| \$KohaDates with_hours => 1 %]#sg;
80
81
82     for my $date_field ( qw(
83             borrowers.dateofbirth
84             borrowers.dateenrolled
85             borrowers.dateexpiry
86             borrowers.debarred
87             items.dateaccessioned
88             items.datelastborrowed
89             items.datelastseen
90             items.onloan
91             serials.planneddate
92             serials.publisheddate
93             serials.claimdate
94             reserves.reservedate
95             reserves.waitingdate
96             reserves.expirationdate
97             suggestions.suggesteddate
98             suggestions.manageddate
99             suggestions.accepteddate
100             suggestions.rejecteddate
101             aqorders.entrydate
102             aqorders.datereceived
103             aqorders.datecancellationprinted
104             aqorders.budgetdate
105             aqorders.claimed_date
106     ) ) {
107         my ( $table, $field ) = split '\.', $date_field;
108         my $new_field =
109           exists $table_mapping->{$table}
110           ? $table_mapping->{$table} . ".$field"
111           : "$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;
114     }
115
116     for my $datetime_field ( qw(
117             items.itemlost_on
118             items.withdrawn_on
119             issues.date_due
120             issues.returndate
121             issues.lastreneweddate
122             issues.issuedate
123             reserves.suspend_until
124     ) ) {
125         my ( $table, $field ) = split '\.', $datetime_field;
126         my $new_field =
127           exists $table_mapping->{$table}
128           ? $table_mapping->{$table} . ".$field"
129           : "$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;
132     }
133
134
135
136     while ( my ( $key, $value ) = each %$table_mapping ) {
137         $tt_content =~ s|<<$key\.|<<$value.|sg;
138     }
139
140     $tt_content =~ s|<<|[% |sg;
141     $tt_content =~ s|>>| %]|sg;
142     $fake_tt_letter =
143       { content => $tt_content, title => $title, is_html => $is_html };
144 }
145
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 } );
152     if ($checkout) {
153         my $patron = Koha::Patrons->find( $checkout->borrowernumber );
154         my $branchcode =
155           C4::Circulation::_GetCircControlBranch( $item->unblessed,
156             $patron->unblessed );
157         $letter_params = {
158             tables => {
159                 issues      => $item->itemnumber,
160                 items       => $item->itemnumber,
161                 biblio      => $item->biblionumber,
162                 biblioitems => $item->biblionumber,
163                 issues      => $patron->borrowernumber,
164                 branches    => $branchcode,
165             }
166         };
167         push @messages, { code => 'not_checked_in_yet', type => 'message' };
168     }
169     else {
170         push @messages, { code => 'no_checkout', type => 'alert' };
171         $letter_params = {};
172     }
173 }
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) {
179         my $branchcode =
180           C4::Circulation::_GetCircControlBranch( $item->unblessed,
181             $patron->unblessed );
182         $letter_params = {
183             tables => {
184                 issues      => $item->itemnumber,
185                 items       => $item->itemnumber,
186                 biblio      => $item->biblionumber,
187                 biblioitems => $item->biblionumber,
188                 issues      => $patron->borrowernumber,
189                 branches    => $branchcode,
190             }
191         };
192         push @messages, { code => 'not_checked_out_yet', type => 'message' };
193     }
194     else {
195         push @messages, { code => 'no_item_or_no_patron', type => 'alert' };
196         $letter_params = {};
197     }
198 }
199 elsif ( $code eq 'HOLD_SLIP' ) {
200     my ( $biblionumber, $borrowernumber ) = split '\|', $data_preview;
201     my $hold = Koha::Holds->find( { borrowernumber => $borrowernumber, biblionumber => $biblionumber } );
202     if ($hold) {
203         $letter_params = {
204             tables => {
205                 reserves    => $hold->unblessed,
206                 branches    => $hold->branchcode,
207                 borrowers   => $hold->borrowernumber,
208                 biblio      => $hold->biblionumber,
209                 biblioitems => $hold->biblionumber,
210                 items       => $hold->itemnumber,
211             }
212         };
213     }
214     else {
215         push @messages, { code => 'no_hold', type => 'alert' };
216         $letter_params = {};
217     }
218 }
219 else {
220     push @messages, { type => 'alert', code => 'preview_not_available', letter_code => $code, };
221 }
222
223 if ( %$letter_params ) {
224     # FIXME Be case here GetPreparedLetter modify $fake_letter
225     $rendered_message = C4::Letters::GetPreparedLetter(
226         letter => $fake_letter,
227         %$letter_params,
228     );
229     if ($tt_content) {
230         $rendered_tt_message = C4::Letters::GetPreparedLetter(
231             letter => $fake_tt_letter,
232             %$letter_params,
233         );
234         $messages_are_similar =
235           $rendered_message->{content} eq $rendered_tt_message->{content};
236     }
237 }
238
239 $template->param(
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,
246 );
247
248 output_html_with_http_headers $input, $cookie, $template->output;