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