Bug 5549 : GetPendingIssues now returns DateTime objects
[koha.git] / opac / opac-user.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 # parts copyright 2010 BibLibre
5 #
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
10 #
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along with
16 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
17 # Suite 330, Boston, MA  02111-1307 USA
18
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22
23 use CGI;
24
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Circulation;
28 use C4::Reserves;
29 use C4::Members;
30 use C4::Members::AttributeTypes;
31 use C4::Members::Attributes qw/GetBorrowerAttributeValue/;
32 use C4::Output;
33 use C4::Overdues qw/CheckBorrowerDebarred/;
34 use C4::Biblio;
35 use C4::Items;
36 use C4::Dates qw/format_date/;
37 use C4::Letters;
38 use C4::Branch; # GetBranches
39 use Koha::DateUtils;
40
41 use constant ATTRIBUTE_SHOW_BARCODE => 'SHOW_BCODE';
42
43 use Date::Calc qw(
44   Today
45   Add_Delta_Days
46   Date_to_Days
47 );
48
49 my $query = new CGI;
50
51 BEGIN {
52     if (C4::Context->preference('BakerTaylorEnabled')) {
53         require C4::External::BakerTaylor;
54         import C4::External::BakerTaylor qw(&image_url &link_url);
55     }
56 }
57
58 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
59     {
60         template_name   => "opac-user.tmpl",
61         query           => $query,
62         type            => "opac",
63         authnotrequired => 0,
64         flagsrequired   => { borrow => 1 },
65         debug           => 1,
66     }
67 );
68
69 my $OPACDisplayRequestPriority = (C4::Context->preference("OPACDisplayRequestPriority")) ? 1 : 0;
70 my $patronupdate = $query->param('patronupdate');
71 my $canrenew = 1;
72
73 # get borrower information ....
74 my ( $borr ) = GetMemberDetails( $borrowernumber );
75
76 my (  $today_year,   $today_month,   $today_day) = Today();
77 my ($warning_year, $warning_month, $warning_day) = split /-/, $borr->{'dateexpiry'};
78
79 for (qw(dateenrolled dateexpiry dateofbirth)) {
80     ($borr->{$_}) and $borr->{$_} = format_date($borr->{$_});
81 }
82 $borr->{'ethnicity'} = fixEthnicity( $borr->{'ethnicity'} );
83
84 my $debar = CheckBorrowerDebarred($borrowernumber);
85 my $userdebarred;
86
87 if ($debar) {
88     $userdebarred = 1;
89     $template->param( 'userdebarred' => $userdebarred );
90     if ( $debar ne "9999-12-31" ) {
91         $borr->{'userdebarreddate'} = C4::Dates::format_date($debar);
92     }
93 }
94
95 if ( $userdebarred || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
96     $borr->{'flagged'} = 1;
97     $canrenew = 0;
98 }
99
100 if ( $borr->{'amountoutstanding'} > 5 ) {
101     $borr->{'amountoverfive'} = 1;
102 }
103 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
104     $borr->{'amountoverzero'} = 1;
105 }
106 my $no_renewal_amt = C4::Context->preference( 'OPACFineNoRenewals' );
107 $no_renewal_amt ||= 0;
108
109 if ( $borr->{amountoutstanding} > $no_renewal_amt ) {
110     $borr->{'flagged'} = 1;
111     $canrenew = 0;
112     $template->param(
113         renewal_blocked_fines => sprintf( '%.02f', $no_renewal_amt ),
114     );
115 }
116
117 if ( $borr->{'amountoutstanding'} < 0 ) {
118     $borr->{'amountlessthanzero'} = 1;
119     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
120 }
121
122 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
123
124 my @bordat;
125 $bordat[0] = $borr;
126
127 # Warningdate is the date that the warning starts appearing
128 if ( $borr->{dateexpiry} && C4::Context->preference('NotifyBorrowerDeparture') &&
129     Date_to_Days(Add_Delta_Days($warning_year,$warning_month,$warning_day,- C4::Context->preference('NotifyBorrowerDeparture'))) <
130     Date_to_Days( $today_year, $today_month, $today_day ) ) 
131 {
132     # borrower card soon to expire, warn the borrower
133     $borr->{'warndeparture'} = $borr->{dateexpiry};
134     if (C4::Context->preference('ReturnBeforeExpiry')){
135         $borr->{'returnbeforeexpiry'} = 1;
136     }
137 }
138
139 $template->param(   BORROWER_INFO     => \@bordat,
140                     borrowernumber    => $borrowernumber,
141                     patron_flagged    => $borr->{flagged},
142                     OPACMySummaryHTML => (C4::Context->preference("OPACMySummaryHTML")) ? 1 : 0,
143                     surname           => $borr->{surname},
144                     showname          => $borr->{showname},
145
146                 );
147
148 #get issued items ....
149
150 my $count          = 0;
151 my $overdues_count = 0;
152 my @overdues;
153 my @issuedat;
154 my $itemtypes = GetItemTypes();
155 my $issues = GetPendingIssues($borrowernumber);
156 if ($issues){
157         foreach my $issue ( sort { $b->{date_due}->datetime() cmp $a->{date_due}->datetime() } @{$issues} ) {
158                 # check for reserves
159                 my ( $restype, $res, undef ) = CheckReserves( $issue->{'itemnumber'} );
160                 if ( $restype ) {
161                         $issue->{'reserved'} = 1;
162                 }
163                 
164                 my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
165                 my $charges = 0;
166                 foreach my $ac (@$accts) {
167                         if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
168                                 $charges += $ac->{'amountoutstanding'}
169                                   if $ac->{'accounttype'} eq 'F';
170                                 $charges += $ac->{'amountoutstanding'}
171                                   if $ac->{'accounttype'} eq 'L';
172                         }
173                 }
174                 $issue->{'charges'} = $charges;
175
176                 # get publictype for icon
177
178                 my $publictype = $issue->{'publictype'};
179                 $issue->{$publictype} = 1;
180
181                 # check if item is renewable
182                 my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
183                 ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
184         if($status && C4::Context->preference("OpacRenewalAllowed")){
185             $issue->{'status'} = $status;
186         }
187                 $issue->{'too_many'} = 1 if $renewerror and $renewerror eq 'too_many';
188                 $issue->{'on_reserve'} = 1 if $renewerror and $renewerror eq 'on_reserve';
189
190                 if ( $issue->{'overdue'} ) {
191                         push @overdues, $issue;
192                         $overdues_count++;
193                         $issue->{'overdue'} = 1;
194                 }
195                 else {
196                         $issue->{'issued'} = 1;
197                 }
198                 # imageurl:
199                 my $itemtype = $issue->{'itemtype'};
200                 if ( $itemtype ) {
201                         $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
202                         $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
203                 }
204                 $issue->{date_due} = output_pref($issue->{date_due});
205                 push @issuedat, $issue;
206                 $count++;
207                 
208                 my $isbn = GetNormalizedISBN($issue->{'isbn'});
209                 $issue->{normalized_isbn} = $isbn;
210
211                 # My Summary HTML
212                 if (my $my_summary_html = C4::Context->preference('OPACMySummaryHTML')){
213                     $issue->{author} ? $my_summary_html =~ s/{AUTHOR}/$issue->{author}/g : $my_summary_html =~ s/{AUTHOR}//g;
214                     $issue->{title} =~ s/\/+$//; # remove trailing slash
215                     $issue->{title} =~ s/\s+$//; # remove trailing space
216                     $issue->{title} ? $my_summary_html =~ s/{TITLE}/$issue->{title}/g : $my_summary_html =~ s/{TITLE}//g;
217                     $issue->{isbn} ? $my_summary_html =~ s/{ISBN}/$isbn/g : $my_summary_html =~ s/{ISBN}//g;
218                     $issue->{biblionumber} ? $my_summary_html =~ s/{BIBLIONUMBER}/$issue->{biblionumber}/g : $my_summary_html =~ s/{BIBLIONUMBER}//g;
219                     $issue->{MySummaryHTML} = $my_summary_html;
220                 }
221         }
222 }
223 $template->param( ISSUES       => \@issuedat );
224 $template->param( issues_count => $count );
225 $template->param( canrenew     => $canrenew );
226 $template->param( OVERDUES       => \@overdues );
227 $template->param( overdues_count => $overdues_count );
228
229 my $show_barcode = C4::Members::AttributeTypes::AttributeTypeExists( ATTRIBUTE_SHOW_BARCODE );
230 if ($show_barcode) {
231     my $patron_show_barcode = GetBorrowerAttributeValue($borrowernumber, ATTRIBUTE_SHOW_BARCODE);
232     undef $show_barcode if defined($patron_show_barcode) && !$patron_show_barcode;
233 }
234 $template->param( show_barcode => 1 ) if $show_barcode;
235
236 # load the branches
237 my $branches = GetBranches();
238 my @branch_loop;
239 for my $branch_hash ( sort keys %{$branches} ) {
240     my $selected;
241     if ( C4::Context->preference('SearchMyLibraryFirst') ) {
242         $selected =
243           ( C4::Context->userenv
244               && ( $branch_hash eq C4::Context->userenv->{branch} ) );
245     }
246     push @branch_loop,
247       { value      => "branch: $branch_hash",
248         branchname => $branches->{$branch_hash}->{'branchname'},
249         selected   => $selected,
250       };
251 }
252 $template->param( branchloop => \@branch_loop );
253
254 # now the reserved items....
255 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
256 foreach my $res (@reserves) {
257     $res->{'reservedate'} = format_date( $res->{'reservedate'} );
258
259     if ( $res->{'expirationdate'} ne '0000-00-00' ) {
260       $res->{'expirationdate'} = format_date( $res->{'expirationdate'} ) 
261     } else {
262       $res->{'expirationdate'} = '';
263     }
264     
265     my $publictype = $res->{'publictype'};
266     $res->{$publictype} = 1;
267     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
268     $res->{'formattedwaitingdate'} = format_date($res->{'waitingdate'});
269     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
270     my $biblioData = GetBiblioData($res->{'biblionumber'});
271     $res->{'reserves_title'} = $biblioData->{'title'};
272     if ($OPACDisplayRequestPriority) {
273         $res->{'priority'} = '' if $res->{'priority'} eq '0';
274     }
275 }
276
277 # use Data::Dumper;
278 # warn Dumper(@reserves);
279
280 $template->param( RESERVES       => \@reserves );
281 $template->param( reserves_count => $#reserves+1 );
282 $template->param( showpriority=>1 ) if $OPACDisplayRequestPriority;
283
284 my @waiting;
285 my $wcount = 0;
286 foreach my $res (@reserves) {
287     if ( $res->{'itemnumber'} ) {
288         my $item = GetItem( $res->{'itemnumber'});
289         $res->{'holdingbranch'} =
290           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
291         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
292         # get document reserve status
293         my $biblioData = GetBiblioData($res->{'biblionumber'});
294         $res->{'waiting_title'} = $biblioData->{'title'};
295         if ( ( $res->{'found'} eq 'W' ) ) {
296             my $item = $res->{'itemnumber'};
297             $item = GetBiblioFromItemNumber($item,undef);
298             $res->{'wait'}= 1; 
299             $res->{'holdingbranch'}=$item->{'holdingbranch'};
300             $res->{'biblionumber'}=$item->{'biblionumber'};
301             $res->{'barcode'} = $item->{'barcode'};
302             $res->{'wbrcode'} = $res->{'branchcode'};
303             $res->{'itemnumber'}    = $res->{'itemnumber'};
304             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
305             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
306                 $res->{'atdestination'} = 1;
307             }
308             # set found to 1 if reserve is waiting for patron pickup
309             $res->{'found'} = 1 if $res->{'found'} eq 'W';
310         } else {
311             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
312             if ($transfertwhen) {
313                 $res->{intransit} = 1;
314                 $res->{datesent}   = format_date($transfertwhen);
315                 $res->{frombranch} = GetBranchName($transfertfrom);
316             }
317         }
318         push @waiting, $res;
319         $wcount++;
320     }
321     # can be cancelled
322     #$res->{'cancelable'} = 1 if ($res->{'wait'} && $res->{'atdestination'} && $res->{'found'} ne "1");
323     $res->{'cancelable'} = 1 if    ($res->{wait} and not $res->{found}) or (not $res->{wait} and not $res->{intransit});
324     
325 }
326
327 $template->param( WAITING => \@waiting );
328
329 # current alert subscriptions
330 my $alerts = getalert($borrowernumber);
331 foreach ( @$alerts ) {
332     $_->{ $_->{type} } = 1;
333     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
334 }
335
336 if (C4::Context->preference('BakerTaylorEnabled')) {
337     $template->param(
338         BakerTaylorEnabled  => 1,
339         BakerTaylorImageURL => &image_url(),
340         BakerTaylorLinkURL  => &link_url(),
341         BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
342     );
343 }
344
345 if (C4::Context->preference("OPACAmazonCoverImages") or 
346     C4::Context->preference("GoogleJackets") or
347     C4::Context->preference("BakerTaylorEnabled") or
348         C4::Context->preference("SyndeticsCoverImages")) {
349         $template->param(JacketImages=>1);
350 }
351
352 if ( GetMessagesCount( $borrowernumber, 'B' ) ) {
353         $template->param( bor_messages => 1 );
354 }
355
356 if ( $borr->{'opacnote'} ) {
357   $template->param( 
358     bor_messages => 1,
359     opacnote => $borr->{'opacnote'},
360   );
361 }
362
363 $template->param(
364     bor_messages_loop   => GetMessages( $borrowernumber, 'B', 'NONE' ),
365     waiting_count      => $wcount,
366     textmessaging      => $borr->{textmessaging},
367     patronupdate => $patronupdate,
368     OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
369     userview => 1,
370     dateformat    => C4::Context->preference("dateformat"),
371 );
372
373 output_html_with_http_headers $query, $cookie, $template->output;
374