Fix for Bug 1726 (re-selecting another 'status' value should hide the 'add serials...
[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 # get borrower information ....
48 my ( $borr, $flags ) = GetMemberDetails( $borrowernumber );
49
50 $borr->{'dateenrolled'} = format_date( $borr->{'dateenrolled'} );
51 $borr->{'expiry'}       = format_date( $borr->{'expiry'} );
52 $borr->{'dateofbirth'}  = format_date( $borr->{'dateofbirth'} );
53 $borr->{'ethnicity'}    = fixEthnicity( $borr->{'ethnicity'} );
54
55 if ( $borr->{'debarred'} || $borr->{'gonenoaddress'} || $borr->{'lost'} ) {
56     $borr->{'flagged'} = 1;
57 }
58 # $make flagged available everywhere in the template
59 my $patron_flagged = $borr->{'flagged'};
60 if ( $borr->{'amountoutstanding'} > 5 ) {
61     $borr->{'amountoverfive'} = 1;
62 }
63 if ( 5 >= $borr->{'amountoutstanding'} && $borr->{'amountoutstanding'} > 0 ) {
64     $borr->{'amountoverzero'} = 1;
65 }
66 if ( $borr->{'amountoutstanding'} < 0 ) {
67     $borr->{'amountlessthanzero'} = 1;
68     $borr->{'amountoutstanding'} = -1 * ( $borr->{'amountoutstanding'} );
69 }
70
71 $borr->{'amountoutstanding'} = sprintf "%.02f", $borr->{'amountoutstanding'};
72
73 my @bordat;
74 $bordat[0] = $borr;
75
76 $template->param(   BORROWER_INFO  => \@bordat,
77                     borrowernumber => $borrowernumber,
78                     patron_flagged => $patron_flagged,
79                 );
80
81 #get issued items ....
82 my ($countissues,$issues) = GetPendingIssues($borrowernumber);
83
84 my $count          = 0;
85 my $toggle = 0;
86 my $overdues_count = 0;
87 my @overdues;
88 my @issuedat;
89 my $imgdir = getitemtypeimagesrc();
90 my $itemtypes = GetItemTypes();
91 foreach my $issue ( @$issues ) {
92         if($count%2 eq 0){ $issue->{'toggle'} = 1; } else { $issue->{'toggle'} = 0; }
93     # check for reserves
94     my ( $restype, $res ) = CheckReserves( $issue->{'itemnumber'} );
95     if ( $restype ) {
96         $issue->{'reserved'} = 1;
97     }
98     
99     my ( $total , $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
100     my $charges = 0;
101     foreach my $ac (@$accts) {
102         if ( $ac->{'itemnumber'} == $issue->{'itemnumber'} ) {
103             $charges += $ac->{'amountoutstanding'}
104               if $ac->{'accounttype'} eq 'F';
105             $charges += $ac->{'amountoutstanding'}
106               if $ac->{'accounttype'} eq 'L';
107         }
108     }
109     $issue->{'charges'} = $charges;
110
111     # get publictype for icon
112
113     my $publictype = $issue->{'publictype'};
114     $issue->{$publictype} = 1;
115
116     # check if item is renewable
117     my $status = CanBookBeRenewed( $borrowernumber, $issue->{'itemnumber'} );
118         ($issue->{'renewcount'},$issue->{'renewsallowed'},$issue->{'renewsleft'}) = GetRenewCount($borrowernumber, $issue->{'itemnumber'});
119
120     $issue->{'status'} = $status;
121
122     if ( $issue->{'overdue'} ) {
123         push @overdues, $issue;
124         $overdues_count++;
125         $issue->{'overdue'} = 1;
126     }
127     else {
128         $issue->{'issued'} = 1;
129     }
130     # imageurl:
131     my $itemtype = $issue->{'itemtype'};
132     if ( $itemtype ) {
133         $issue->{'imageurl'}    = $imgdir."/".$itemtypes->{$itemtype}->{'imageurl'};
134         $issue->{'description'} = $itemtypes->{$itemtype}->{'description'};
135     }
136     push @issuedat, $issue;
137     $count++;
138 }
139
140 $template->param( ISSUES       => \@issuedat );
141 $template->param( issues_count => $count );
142
143 $template->param( OVERDUES       => \@overdues );
144 $template->param( overdues_count => $overdues_count );
145
146 # load the branches
147 my $branches = GetBranches();
148 my @branch_loop;
149 for my $branch_hash (sort keys %$branches ) {
150     my $selected=(C4::Context->userenv && ($branch_hash eq C4::Context->userenv->{branch})) if (C4::Context->preference('SearchMyLibraryFirst'));
151     push @branch_loop,
152       {
153         value      => "branch: $branch_hash",
154         branchname => $branches->{$branch_hash}->{'branchname'},
155         selected => $selected
156       };
157 }
158 $template->param( branchloop => \@branch_loop, "mylibraryfirst"=>C4::Context->preference("SearchMyLibraryFirst"));
159
160 # now the reserved items....
161 my @reserves  = GetReservesFromBorrowernumber( $borrowernumber );
162 foreach my $res (@reserves) {
163     $res->{'reservedate'} = format_date( $res->{'reservedate'} );
164     my $publictype = $res->{'publictype'};
165     $res->{$publictype} = 1;
166     $res->{'waiting'} = 1 if $res->{'found'} eq 'W';
167     $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
168     my $biblioData = GetBiblioData($res->{'biblionumber'});
169     $res->{'reserves_title'} = $biblioData->{'title'};
170 }
171
172 # use Data::Dumper;
173 # warn Dumper(@reserves);
174
175 $template->param( RESERVES       => \@reserves );
176 $template->param( reserves_count => $#reserves+1 );
177
178 my @waiting;
179 my $wcount = 0;
180 foreach my $res (@reserves) {
181     if ( $res->{'itemnumber'} ) {
182         my $item = GetItem( $res->{'itemnumber'});
183         $res->{'holdingbranch'} =
184           $branches->{ $item->{'holdingbranch'} }->{'branchname'};
185         $res->{'branch'} = $branches->{ $res->{'branchcode'} }->{'branchname'};
186         # get document reserve status
187         my $biblioData = GetBiblioData($res->{'biblionumber'});
188         $res->{'waiting_title'} = $biblioData->{'title'};
189         if ( ( $res->{'found'} eq 'W' ) ) {
190             my $item = $res->{'itemnumber'};
191             $item = GetBiblioFromItemNumber($item,undef);
192             $res->{'wait'}= 1; 
193             $res->{'holdingbranch'}=$item->{'holdingbranch'};
194             $res->{'biblionumber'}=$item->{'biblionumber'};
195             $res->{'barcodenumber'}     = $item->{'barcode'};
196             $res->{'wbrcode'} = $res->{'branchcode'};
197             $res->{'itemnumber'}        = $res->{'itemnumber'};
198             $res->{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
199             if($res->{'holdingbranch'} eq $res->{'wbrcode'}){
200                 $res->{'atdestination'} = 1;
201             }
202             # set found to 1 if reserve is waiting for patron pickup
203             $res->{'found'} = 1 if $res->{'found'} eq 'W';
204         }
205         push @waiting, $res;
206         $wcount++;
207     }
208 }
209
210 $template->param( WAITING => \@waiting );
211
212 # current alert subscriptions
213 my $alerts = getalert($borrowernumber);
214 foreach ( @$alerts ) {
215     $_->{ $_->{type} } = 1;
216     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
217 }
218
219 $template->param(
220     waiting_count      => $wcount,
221     textmessaging      => $borr->{textmessaging},
222 );
223
224 output_html_with_http_headers $query, $cookie, $template->output;
225