Fixed bug #405
[koha.git] / 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::Search;
41 use Date::Manip;
42 use C4::Date;
43 use C4::Reserves2;
44 use C4::Circulation::Renewals2;
45 use C4::Circulation::Circ2;
46 use C4::Koha;
47 use HTML::Template;
48
49 my $dbh = C4::Context->dbh;
50
51 my $input = new CGI;
52
53 my ($template, $loggedinuser, $cookie)
54     = get_template_and_user({template_name => "members/moremember.tmpl",
55                              query => $input,
56                              type => "intranet",
57                              authnotrequired => 0,
58                              flagsrequired => {borrowers => 1},
59                              debug => 1,
60                              });
61
62 my $bornum=$input->param('bornum');
63
64 #start the page and read in includes
65
66 my $data=borrdata('',$bornum);
67
68 $data->{'dateenrolled'} = format_date($data->{'dateenrolled'});
69 $data->{'expiry'} = format_date($data->{'expiry'});
70 $data->{'dateofbirth'} = format_date($data->{'dateofbirth'});
71 $data->{'IS_ADULT'} = ($data->{'categorycode'} ne 'I');
72
73 $data->{'ethnicity'} = fixEthnicity($data->{'ethnicity'});
74
75 $data->{&expand_sex_into_predicate($data->{'sex'})} = 1;
76
77 if ($data->{'categorycode'} eq 'C'){
78     my $data2=borrdata('',$data->{'guarantor'});
79     $data->{'streetaddress'}=$data2->{'streetaddress'};
80     $data->{'city'}=$data2->{'city'};
81     $data->{'physstreet'}=$data2->{'physstreet'};
82     $data->{'streetcity'}=$data2->{'streetcity'};
83     $data->{'phone'}=$data2->{'phone'};
84     $data->{'phoneday'}=$data2->{'phoneday'};
85 }
86
87
88 if ($data->{'ethnicity'} || $data->{'ethnotes'}) {
89         $template->param(printethnicityline => 1);
90 }
91
92 if ($data->{'categorycode'} ne 'C'){
93   $template->param(isguarantee => 1);
94   # FIXME
95   # It looks like the $i is only being returned to handle walking through
96   # the array, which is probably better done as a foreach loop.
97   #
98   my ($count,$guarantees)=findguarantees($data->{'borrowernumber'});
99   my @guaranteedata;
100   for (my $i=0;$i<$count;$i++){
101     push (@guaranteedata, {borrowernumber => $guarantees->[$i]->{'borrowernumber'},
102                            cardnumber => $guarantees->[$i]->{'cardnumber'},
103                            name => $guarantees->[$i]->{'firstname'} . " " . $guarantees->[$i]->{'surname'}});
104   }
105   $template->param(guaranteeloop => \@guaranteedata);
106
107 } else {
108   my ($guarantor)=findguarantor($data->{'borrowernumber'});
109   unless ($guarantor->{'borrowernumber'} == 0){
110     $template->param(guarantorborrowernumber => $guarantor->{'borrowernumber'}, guarantorcardnumber => $guarantor->{'cardnumber'});
111   }
112 }
113
114 my %bor;
115 $bor{'borrowernumber'}=$bornum;
116
117 # FIXME
118 # it looks like $numaccts is a temp variable and that the
119 # for (my $i;$i<$numaccts;$i++)
120 # can be turned into a foreach loop instead
121 #
122 my ($numaccts,$accts,$total)=getboracctrecord('',\%bor);
123 #if ($numaccts > 10){
124 #  $numaccts=10;
125 #}
126 my @accountdata;
127 for (my$i=0;$i<$numaccts;$i++){
128   my $amount= $accts->[$i]{'amount'} + 0.00;
129   my $amount2= $accts->[$i]{'amountoutstanding'} + 0.00;
130   my %row = %$accts->[$i];
131   if ($amount2 != 0){
132     my $item=" &nbsp; ";
133     $row{'date'} = format_date($accts->[$i]{'date'});
134
135     if ($accts->[$i]{'accounttype'} ne 'Res'){
136       #get item data
137       #$item=
138     }
139
140     # FIXME
141     # why set this variable if it's not going to be used?
142     #
143     my $env;
144     if ($accts->[$i]{'accounttype'} ne 'Res'){
145       my $iteminfo=C4::Circulation::Circ2::getiteminformation($env,$accts->[$i]->{'itemnumber'},'');
146    # FIXME, seems to me $iteminfo gets not defined
147       %row = (%row , %$iteminfo) if $iteminfo;
148     }
149   }
150   push (@accountdata, \%row);
151 }
152
153 my ($count,$issue)=borrissues($bornum);
154 my $today=ParseDate('today');
155 my @issuedata;
156 for (my $i=0;$i<$count;$i++){
157   my $datedue=ParseDate($issue->[$i]{'date_due'});
158   $issue->[$i]{'date_due'} = format_date($issue->[$i]{'date_due'});
159   my %row = %{$issue->[$i]};
160   if ($datedue < $today){
161     $row{'red'}=1; #print "<font color=red>";
162   }
163   #find the charge for an item
164   # FIXME - This is expecting
165   # &C4::Circulation::Renewals2::calc_charges, but it's getting
166   # &C4::Circulation::Circ2::calc_charges, which only returns one
167   # element, so itemtype isn't being set.
168   # But &C4::Circulation::Renewals2::calc_charges doesn't appear to
169   # return the correct item type either (or a properly-formatted
170   # charge, for that matter).
171   my ($charge,$itemtype)=calc_charges(undef,$dbh,$issue->[$i]{'itemnumber'},$bornum);
172   $row{'itemtype'}=$itemtype;
173   $row{'charge'}=$charge;
174
175   #check item is not reserved
176   my ($restype,$reserves)=CheckReserves($issue->[$i]{'itemnumber'});
177   if ($restype){
178     print "<TD><a href=/cgi-bin/koha/request.pl?bib=$issue->[$i]{'biblionumber'}>On Request - no renewals</a></td></tr>";
179 #  } elsif ($issue->[$i]->{'renewals'} > 0) {
180 #      print "<TD>Previously Renewed - no renewals</td></tr>";
181   } else {
182     $row{'norenew'}=0;
183   }
184   push (@issuedata, \%row);
185 }
186
187 my ($rescount,$reserves)=FindReserves('',$bornum); #From C4::Reserves2
188
189 my @reservedata;
190 foreach my $reserveline (@$reserves) {
191         $reserveline->{'reservedate2'} = format_date($reserveline->{'reservedate'});
192         my $restitle;
193         my %row = %$reserveline;
194         if ($reserveline->{'constrainttype'} eq 'o'){
195                 $restitle=getreservetitle($reserveline->{'biblionumber'},$reserveline->{'borrowernumber'},$reserveline->{'reservedate'},$reserveline->{'timestamp'});
196                 %row =  (%row , %$restitle) if $restitle;
197         }
198         push (@reservedata, \%row);
199 }
200
201 $template->param($data);
202 $template->param(
203                  bornum          => $bornum,
204                  accountloop     => \@accountdata,
205                  issueloop       => \@issuedata,
206                  reserveloop     => \@reservedata);
207
208 output_html_with_http_headers $input, $cookie, $template->output;