fix number two for bug 1204: Lists of issues and overdues on opac-user.pl
[koha.git] / opac / opac-user.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18
19 use strict;
20 require Exporter;
21 use CGI;
22
23 use C4::Auth;
24 use C4::Koha;
25 use C4::Circulation;
26 use C4::Reserves;
27 use C4::Members;
28 use C4::Output;
29 use C4::Biblio;
30 use C4::Items;
31 use C4::Dates qw/format_date/;
32 use C4::Letters;
33 use C4::Branch; # GetBranches
34
35 my $query = new CGI;
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
37     {
38         template_name   => "opac-user.tmpl",
39         query           => $query,
40         type            => "opac",
41         authnotrequired => 0,
42         flagsrequired   => { borrow => 1 },
43         debug           => 1,
44     }
45 );
46
47 my $patronupdate = $query->param('patronupdate');
48
49 # get borrower information ....
50 my ( $borr ) = GetMemberDetails( $borrowernumber );
51
52 $borr->{'dateenrolled'} = format_date( $borr->{'dateenrolled'} );
53 $borr->{'expiry'}       = format_date( $borr->{'expiry'} );
54 $borr->{'dateofbirth'}  = format_date( $borr->{'dateofbirth'} );
55 $borr->{'ethnicity'}    = fixEthnicity( $borr->{'ethnicity'} );
56
57 if ( $borr->{'debarred'} || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
58     $borr->{'flagged'} = 1;
59 }
60 # $make flagged available everywhere in the template
61 my $patron_flagged = $borr->{'flagged'};
62 if ( $borr->{'amountoutstanding'} > 5 ) {
63     $borr->{'amountoverfive'} = 1;
64 }
65 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
66     $borr->{'amountoverzero'} = 1;
67 }
68 if ( $borr->{'amountoutstanding'} < 0 ) {
69     $borr->{'amountlessthanzero'} = 1;
70     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
71 }
72
73 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
74
75 my @bordat;
76 $bordat[0] = $borr;
77
78 $template->param(   BORROWER_INFO  => \@bordat,
79                     borrowernumber => $borrowernumber,
80                     patron_flagged => $patron_flagged,
81                 );
82
83 #get issued items ....
84 my ($countissues,$issues) = GetPendingIssues($borrowernumber);
85 my @issue_list = sort { $b->{'date_due'} cmp $a->{'date_due'} } @$issues;
86
87 my $count          = 0;
88 my $toggle = 0;
89 my $overdues_count = 0;
90 my @overdues;
91 my @issuedat;
92 my $itemtypes = GetItemTypes();
93 foreach my $issue ( @issue_list ) {
94         if($count%2 eq 0){ $issue->{'toggle'} = 1; } else { $issue->{'toggle'} = 0; }
95     # check for reserves
96     my ( $restype, $res ) = CheckReserves( $issue->{'itemnumber'} );
97     if ( $restype ) {
98         $issue->{'reserved'} = 1;
99     }
100     
101     my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
102     my $charges = 0;
103     foreach my $ac (@$accts) {
104         if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
105             $charges += $ac->{'amountoutstanding'}
106               if $ac->{'accounttype'} eq 'F';
107             $charges += $ac->{'amountoutstanding'}
108               if $ac->{'accounttype'} eq 'L';
109         }
110     }
111     $issue->{'charges'} = $charges;
112
113     # get publictype for icon
114
115     my $publictype = $issue->{'publictype'};
116     $issue->{$publictype} = 1;
117
118     # check if item is renewable
119     my ($status,$renewerror) = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
120         ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
121
122     $issue->{'status'} = $status;
123
124     if ( $issue->{'overdue'} ) {
125         push @overdues, $issue;
126         $overdues_count++;
127         $issue->{'overdue'} = 1;
128     }
129     else {
130         $issue->{'issued'} = 1;
131     }
132     # imageurl:
133     my $itemtype = $issue->{'itemtype'};
134     if ( $itemtype ) {
135         $issue->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
136         $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
137     }
138     $issue->{date_due} = format_date($issue->{date_due});
139     push @issuedat, $issue;
140     $count++;
141         
142                 # XISBN Stuff
143         my $xisbn=$issue->{'isbn'};
144         $xisbn =~ /(\d*[X]*)/;
145         $issue->{amazonisbn} = $1;              # FIXME: so it is OK if the ISBN = 'XXXXX' ?
146         my ($clean, $amazonisbn);
147         $amazonisbn = $1;
148         # these might be overkill, but they are better than the regexp above.
149         if (
150                 $amazonisbn =~ /\b(\d{13})\b/ or
151                 $amazonisbn =~ /\b(\d{10})\b/ or 
152                 $amazonisbn =~ /\b(\d{9}X)\b/i
153         ) {
154                 $clean = $1;
155                 $issue->{clean_isbn} = $1;
156         }
157         
158 }
159
160 $template->param( ISSUES       => \@issuedat );
161 $template->param( issues_count => $count );
162
163 $template->param( OVERDUES       => \@overdues );
164 $template->param( overdues_count => $overdues_count );
165
166 # load the branches
167 my $branches = GetBranches();
168 my @branch_loop;
169 for my $branch_hash (sort keys %$branches ) {
170     my $selected=(C4::Context->userenv && ($branch_hash eq C4::Context->userenv->{branch})) if (C4::Context->preference('SearchMyLibraryFirst'));
171     push @branch_loop,
172       {
173         value      => "branch: $branch_hash",
174         branchname => $branches->{$branch_hash}->{'branchname'},
175         selected => $selected
176       };
177 }
178 $template->param( branchloop => \@branch_loop, "mylibraryfirst"=>C4::Context->preference("SearchMyLibraryFirst"));
179
180 # now the reserved items....
181 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
182 foreach my $res (@reserves) {
183     $res->{'reservedate'} = format_date( $res->{'reservedate'} );
184     my $publictype = $res->{'publictype'};
185     $res->{$publictype} = 1;
186     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
187     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
188     my $biblioData = GetBiblioData($res->{'biblionumber'});
189     $res->{'reserves_title'} = $biblioData->{'title'};
190 }
191
192 # use Data::Dumper;
193 # warn Dumper(@reserves);
194
195 $template->param( RESERVES       => \@reserves );
196 $template->param( reserves_count => $#reserves+1 );
197
198 my @waiting;
199 my $wcount = 0;
200 foreach my $res (@reserves) {
201     if ( $res->{'itemnumber'} ) {
202         my $item = GetItem( $res->{'itemnumber'});
203         $res->{'holdingbranch'} =
204           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
205         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
206         # get document reserve status
207         my $biblioData = GetBiblioData($res->{'biblionumber'});
208         $res->{'waiting_title'} = $biblioData->{'title'};
209         if ( ( $res->{'found'} eq 'W' ) ) {
210             my $item = $res->{'itemnumber'};
211             $item = GetBiblioFromItemNumber($item,undef);
212             $res->{'wait'}= 1; 
213             $res->{'holdingbranch'}=$item->{'holdingbranch'};
214             $res->{'biblionumber'}=$item->{'biblionumber'};
215             $res->{'barcodenumber'}     = $item->{'barcode'};
216             $res->{'wbrcode'} = $res->{'branchcode'};
217             $res->{'itemnumber'}        = $res->{'itemnumber'};
218             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
219             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
220                 $res->{'atdestination'} = 1;
221             }
222             # set found to 1 if reserve is waiting for patron pickup
223             $res->{'found'} = 1 if $res->{'found'} eq 'W';
224         } else {
225             my ($transfertwhen, $transfertfrom, $transfertto) = GetTransfers( $res->{'itemnumber'} );
226             if ($transfertwhen) {
227                 $res->{intransit} = 1;
228                 $res->{datesent}   = format_date($transfertwhen);
229                 $res->{frombranch} = GetBranchName($transfertfrom);
230             }
231         }
232         push @waiting, $res;
233         $wcount++;
234     }
235 }
236
237 $template->param( WAITING => \@waiting );
238
239 # current alert subscriptions
240 my $alerts = getalert($borrowernumber);
241 foreach ( @$alerts ) {
242     $_->{ $_->{type} } = 1;
243     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
244 }
245 if (C4::Context->preference("AmazonContent"     ) or 
246         C4::Context->preference("GoogleJackets"     ) or
247         C4::Context->preference("BakerTaylorEnabled")   ) {
248                 $template->param(JacketImages=>1);
249 }
250
251 $template->param(
252     waiting_count      => $wcount,
253     textmessaging      => $borr->{textmessaging},
254         patronupdate => $patronupdate,
255         OpacRenewalAllowed => C4::Context->preference("OpacRenewalAllowed"),
256         userview => 1,
257         dateformat    => C4::Context->preference("dateformat"),
258 );
259
260 output_html_with_http_headers $query, $cookie, $template->output;
261