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