improvement: in member details page, issue and reserve lines are toggled.
[koha.git] / members / moremember.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # script to do a borrower enquiry/bring up borrower details etc
6 # Displays all the details about a borrower
7 # written 20/12/99 by chris@katipo.co.nz
8 # last modified 21/1/2000 by chris@katipo.co.nz
9 # modified 31/1/2001 by chris@katipo.co.nz
10 #   to not allow items on request to be renewed
11 #
12 # needs html removed and to use the C4::Output more, but its tricky
13 #
14
15
16 # Copyright 2000-2002 Katipo Communications
17 #
18 # This file is part of Koha.
19 #
20 # Koha is free software; you can redistribute it and/or modify it under the
21 # terms of the GNU General Public License as published by the Free Software
22 # Foundation; either version 2 of the License, or (at your option) any later
23 # version.
24 #
25 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
26 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
27 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
28 #
29 # You should have received a copy of the GNU General Public License along with
30 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
31 # Suite 330, Boston, MA  02111-1307 USA
32
33 use strict;
34 use C4::Auth;
35 use C4::Context;
36 use C4::Output;
37 use C4::Interface::CGI::Output;
38 use C4::Interface::CGI::Template;
39 use CGI;
40 use C4::Members;
41 use Date::Manip;
42 use C4::Date;
43 use C4::Reserves2;
44 use C4::Circulation::Circ2;
45 use C4::Koha;
46 use C4::Letters;
47 use HTML::Template;
48
49 my $dbh = C4::Context->dbh;
50
51 my $input = new CGI;
52 my $print = $input->param('print');
53 my $template_name;
54
55 if($print eq "page"){
56         $template_name = "members/moremember-print.tmpl";
57 } elsif($print eq "slip"){
58         $template_name = "members/moremember-receipt.tmpl";
59 } else {
60         $template_name = "members/moremember.tmpl";
61 }
62
63 my ($template, $loggedinuser, $cookie)
64     = get_template_and_user({template_name => $template_name,
65                              query => $input,
66                              type => "intranet",
67                              authnotrequired => 0,
68                              flagsrequired => {borrowers => 1},
69                              debug => 1,
70                              });
71
72 my $bornum=$input->param('bornum');
73
74 #start the page and read in includes
75
76 my $data=borrdata('',$bornum);
77
78 $template->param($data->{'categorycode'} => 1); # in template <TMPL_IF name="I"> => instutitional (A for Adult & C for children)
79
80 $data->{'dateenrolled'} = format_date($data->{'dateenrolled'});
81 $data->{'dateexpiry'} = format_date($data->{'dateexpiry'});
82 $data->{'dateofbirth'} = format_date($data->{'dateofbirth'});
83 $data->{'IS_ADULT'} = ($data->{'categorycode'} ne 'I');
84
85 if($data->{'debarred'} || $data->{'gonenoaddress'} || $data->{'lost'} || $data->{'borrowernotes'}){
86         $template->param(flagged =>1);
87 }
88
89 $data->{'ethnicity'} = fixEthnicity($data->{'ethnicity'});
90
91 $data->{&expand_sex_into_predicate($data->{'sex'})} = 1;
92
93 if ($data->{'categorycode'} eq 'C'){
94         my $data2=borrdata('',$data->{'guarantorid'});
95         $data->{'address'}=$data2->{'address'};
96         $data->{'city'}=$data2->{'city'};
97         $data->{'B_address'}=$data2->{'B_address'};
98         $data->{'B_city'}=$data2->{'B_city'};
99         $data->{'phone'}=$data2->{'phone'};
100         $data->{'mobile'}=$data2->{'mobile'};
101         $data->{'zipcode'} = $data2->{'zipcode'};
102 }
103
104
105 if ($data->{'ethnicity'} || $data->{'ethnotes'}) {
106         $template->param(printethnicityline => 1);
107 }
108
109 if ($data->{'category_type'} ne 'C'){
110         $template->param(isguarantee => 1);
111         # FIXME
112         # It looks like the $i is only being returned to handle walking through
113         # the array, which is probably better done as a foreach loop.
114         #
115         my ($count,$guarantees)=findguarantees($data->{'borrowernumber'});
116         my @guaranteedata;
117         for (my $i=0;$i<$count;$i++){
118                 push (@guaranteedata, {borrowernumber => $guarantees->[$i]->{'borrowernumber'},
119                                         cardnumber => $guarantees->[$i]->{'cardnumber'},
120                                         name => $guarantees->[$i]->{'firstname'} . " " . $guarantees->[$i]->{'surname'}});
121         }
122         $template->param(guaranteeloop => \@guaranteedata);
123
124 } else {
125         my ($guarantorid)=findguarantor($data->{guarantorid});
126         if ($guarantorid->{'borrowernumber'}){
127                 $template->param(guarantorborrowernumber => $guarantorid->{'borrowernumber'}, guarantorcardnumber => $guarantorid->{'cardnumber'});
128         }
129 }
130
131 #Independant branches management
132 my $unvalidlibrarian = ((C4::Context->preference("IndependantBranches")) && (C4::Context->userenv->{flags}!=1) && ($data->{'branchcode'} ne C4::Context->userenv->{branch}));
133
134 my %bor;
135 $bor{'borrowernumber'}=$bornum;
136
137 # Converts the branchcode to the branch name
138 my $samebranch;
139 if (C4::Context->preference("IndependantBranches")) {
140         my $userenv = C4::Context->userenv;
141         unless ($userenv->{flags} == 1){
142                 $samebranch = ($data->{'branchcode'} eq $userenv->{branch});
143         }
144         $samebranch =1 if ($userenv->{flags} == 1);
145 }
146
147 $data->{'branchname'} = &getbranchdetail($data->{'branchcode'});
148
149 # Converts the categorycode to the description
150 $data->{'categorycode'} = &getborrowercategory($data->{'categorycode'});
151
152 my ($numaccts,$accts,$total)=getboracctrecord('',\%bor);
153
154 #
155 # current issues
156 #
157 my ($count,$issue)=borrissues($bornum);
158
159 my $today=ParseDate('today');
160 my @issuedata;
161 my $totalprice = 0;
162 my $toggle = 0;
163 for (my $i=0;$i<$count;$i++){
164         my $datedue=ParseDate($issue->[$i]{'date_due'});
165         $issue->[$i]{'date_due'} = format_date($issue->[$i]{'date_due'});
166         my %row = %{$issue->[$i]};
167         $totalprice += $issue->[$i]{'replacementprice'};
168         $row{'replacementprice'}=$issue->[$i]{'replacementprice'};
169         if ($datedue < $today){
170                 $row{'red'}=1; #print "<font color=red>";
171         }
172         $row{toggle} = $toggle++%2;
173         #find the charge for an item
174         # FIXME - This is expecting
175         # &C4::Circulation::Renewals2::calc_charges, but it's getting
176         # &C4::Circulation::Circ2::calc_charges, which only returns one
177         # element, so itemtype isn't being set.
178         # But &C4::Circulation::Renewals2::calc_charges doesn't appear to
179         # return the correct item type either (or a properly-formatted
180         # charge, for that matter).
181         my ($charge,$itemtype)=calc_charges($dbh,$issue->[$i]{'itemnumber'},$bornum);
182
183         my $itemtypeinfo = getitemtypeinfo($itemtype);
184         $row{'itemtype_description'} = $itemtypeinfo->{description};
185         $row{'itemtype_image'} = $itemtypeinfo->{imageurl};
186
187         $row{'charge'}= sprintf("%.2f",$charge);
188
189         #check item is not reserved
190         my ($restype,$reserves)=CheckReserves($issue->[$i]{'itemnumber'});
191         if ($restype){
192 #               print "<TD><a href=/cgi-bin/koha/request.pl?bib=$issue->[$i]{'biblionumber'}>On Request - no renewals</a></td></tr>";
193                 #  } elsif ($issue->[$i]->{'renewals'} > 0) {
194                 #      print "<TD>Previously Renewed - no renewals</td></tr>";
195                         $row{'norenew'}=1;
196         } else {
197                 $row{'norenew'}=0;
198         }
199         push (@issuedata, \%row);
200 }
201
202 #
203 # find reserves
204 #
205 my ($rescount,$reserves)=FindReserves('',$bornum); #From C4::Reserves2
206 my @reservedata;
207 $toggle = 0;
208 foreach my $reserveline (@$reserves) {
209         $reserveline->{'reservedate2'} = format_date($reserveline->{'reservedate'});
210         my $restitle;
211         my %row = %$reserveline;
212         $row{toggle} = $toggle++%2;
213         if ($reserveline->{'constrainttype'} eq 'o'){
214                 $restitle=getreservetitle($reserveline->{'biblionumber'},$reserveline->{'borrowernumber'},$reserveline->{'reservedate'},$reserveline->{'rtimestamp'});
215                 %row =  (%row , %$restitle) if $restitle;
216         }
217         push (@reservedata, \%row);
218 }
219
220 # current alert subscriptions
221 my $alerts = getalert($bornum);
222 foreach (@$alerts) {
223         $_->{$_->{type}}=1;
224         $_->{relatedto} = findrelatedto($_->{type},$_->{externalid});
225 }
226 my $picture;
227 my $htdocs = C4::Context->config('intrahtdocs');
228 $picture = "/borrowerimages/".$bornum.".jpg";
229 if (-e $htdocs."$picture")
230
231   $template->param(picture => $picture)
232 };
233 $template->param($data);
234 $template->param(
235                  bornum          => $bornum,
236                  totalprice =>$totalprice,
237                  totaldue =>$total,
238                  issueloop       => \@issuedata,
239                  unvalidlibrarian => $unvalidlibrarian,
240                  reserveloop     => \@reservedata);
241
242 output_html_with_http_headers $input, $cookie, $template->output;