moremember.pl - We will be doing a lot of debugging on Members, this is just the...
[koha.git] / members / moremember.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20
21 =head1 moremember.pl
22
23  script to do a borrower enquiry/bring up borrower details etc
24  Displays all the details about a borrower
25  written 20/12/99 by chris@katipo.co.nz
26  last modified 21/1/2000 by chris@katipo.co.nz
27  modified 31/1/2001 by chris@katipo.co.nz
28    to not allow items on request to be renewed
29
30  needs html removed and to use the C4::Output more, but its tricky
31
32 =cut
33
34 use strict;
35 use CGI;
36 use C4::Context;
37 use C4::Auth;
38 use C4::Output;
39 use C4::Members;
40 use C4::Dates;
41 use C4::Reserves;
42 use C4::Circulation;
43 use C4::Koha;
44 use C4::Letters;
45 use C4::Biblio;
46 use C4::Reserves;
47 use C4::Branch; # GetBranchName
48
49 use vars qw($debug);
50
51 BEGIN {
52         $debug = $ENV{DEBUG} || 0;
53 }
54
55 my $dbh = C4::Context->dbh;
56
57 my $input = new CGI;
58 my $print = $input->param('print');
59 my @failedrenews = $input->param('failedrenew');
60 my $error = $input->param('error');
61 my @renew_failed;
62 for (@failedrenews) { $renew_failed[$_] = 1; }
63
64 my $template_name;
65
66 if    ($print eq "page") { $template_name = "members/moremember-print.tmpl";   }
67 elsif ($print eq "slip") { $template_name = "members/moremember-receipt.tmpl"; }
68 else {                     $template_name = "members/moremember.tmpl";         }
69
70 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
71     {
72         template_name   => $template_name,
73         query           => $input,
74         type            => "intranet",
75         authnotrequired => 0,
76         flagsrequired   => { borrowers => 1 },
77         debug           => 1,
78     }
79 );
80 my $borrowernumber = $input->param('borrowernumber');
81
82 #start the page and read in includes
83 my $data           = GetMember( $borrowernumber ,'borrowernumber');
84 my $reregistration = $input->param('reregistration');
85
86 if ( not defined $data ) {
87     $template->param (unknowuser => 1);
88     exit;
89 }
90
91 # re-reregistration function to automatic calcul of date expiry
92 if ( $reregistration eq 'y' ) {
93         $data->{'dateexpiry'} = ExtendMemberSubscriptionTo( $borrowernumber );
94 }
95
96 my $borrowercategory = GetBorrowercategory( $data->{'categorycode'} );
97 my $category_type = $borrowercategory->{'category_type'};
98
99 # in template <TMPL_IF name="I"> => instutitional (A for Adult& C for children) 
100 $template->param( $data->{'categorycode'} => 1 ); 
101
102 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
103                 my $userdate = $data->{$_};
104                 unless ($userdate) {
105                         $debug and warn sprintf "Empty \$data{%12s}", $_;
106                         $data->{$_} = '';
107                         next;
108                 }
109                 my $tempdate = C4::Dates->new($userdate,'iso')->output('syspref')
110                         or warn ("Invalid $_ = '$userdate'");
111                 $data->{$_} = $tempdate || '';
112 }
113 $data->{'IS_ADULT'} = ( $data->{'categorycode'} ne 'I' );
114
115 for (qw(debarred gonenoaddress lost borrowernotes)) {
116          $data->{$_} and $template->param(flagged => 1) and last;
117 }
118
119 $data->{'ethnicity'} = fixEthnicity( $data->{'ethnicity'} );
120
121 $data->{ "sex_".$data->{'sex'}."_p" } = 1;
122
123 if ( $category_type eq 'C' and $data->{'guarantorid'} ne '0' ) {
124     my $data2 = GetMember( $data->{'guarantorid'} ,'borrowernumber');
125         foreach (qw(address city B_address B_city phone mobilezipcode)) {
126         $data->{$_} = $data2->{$_};
127         }
128 }
129
130 if ( $data->{'ethnicity'} || $data->{'ethnotes'} ) {
131     $template->param( printethnicityline => 1 );
132 }
133 if ( $category_type eq 'A' ) {
134     $template->param( isguarantee => 1 );
135
136     # FIXME
137     # It looks like the $i is only being returned to handle walking through
138     # the array, which is probably better done as a foreach loop.
139     #
140     my ( $count, $guarantees ) = GetGuarantees( $data->{'borrowernumber'} );
141     my @guaranteedata;
142     for ( my $i = 0 ; $i < $count ; $i++ ) {
143         push(@guaranteedata,
144             {
145                 borrowernumber => $guarantees->[$i]->{'borrowernumber'},
146                 cardnumber     => $guarantees->[$i]->{'cardnumber'},
147                 name           => $guarantees->[$i]->{'firstname'} . " "
148                                 . $guarantees->[$i]->{'surname'}
149             }
150         );
151     }
152     $template->param( guaranteeloop => \@guaranteedata );
153     ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
154 }
155 else {
156     if ($data->{'guarantorid'}){
157             my ($guarantor) = GetMember( $data->{'guarantorid'},'biblionumber');
158                 $template->param(guarantor => 1);
159                 foreach (qw(borrowernumber cardnumber firstname surname)) {        
160                           $template->param("guarantor$_" => $guarantor->{$_});
161         }
162     }
163 }
164
165 #Independant branches management
166 my $unvalidlibrarian =
167   (      ( C4::Context->preference("IndependantBranches") )
168       && ( C4::Context->userenv->{flags} != 1 )
169       && ( $data->{'branchcode'} ne C4::Context->userenv->{branch} ) );
170
171 my %bor;
172 $bor{'borrowernumber'} = $borrowernumber;
173
174 # Converts the branchcode to the branch name
175 my $samebranch;
176 if ( C4::Context->preference("IndependantBranches") ) {
177     my $userenv = C4::Context->userenv;
178     unless ( $userenv->{flags} == 1 ) {
179         $samebranch = ( $data->{'branchcode'} eq $userenv->{branch} );
180     }
181     $samebranch = 1 if ( $userenv->{flags} == 1 );
182 }
183 my $branchdetail = GetBranchDetail( $data->{'branchcode'});
184 $data->{'branchname'} = $branchdetail->{branchname};
185
186
187 my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
188 my $lib1 = &GetSortDetails( "Bsort1", $data->{'sort1'} );
189 my $lib2 = &GetSortDetails( "Bsort2", $data->{'sort2'} );
190 ( $template->param( lib1 => $lib1 ) ) if ($lib1);
191 ( $template->param( lib2 => $lib2 ) ) if ($lib2);
192
193 # current issues
194 #
195 my ( $count, $issue ) = GetPendingIssues($borrowernumber);
196 my $roaddetails = &GetRoadTypeDetails( $data->{'streettype'} );
197 my $today       = POSIX::strftime("%Y%m%d", localtime); # iso format
198 my @issuedata;
199 my $totalprice = 0;
200 my $toggle     = 0;
201 for ( my $i = 0 ; $i < $count ; $i++ ) {
202     my $datedue = $issue->[$i]{'date_due'};
203     $issue->[$i]{'date_due'} = C4::Dates->new($issue->[$i]{'date_due'},'iso')->output('syspref');
204     my %row = %{ $issue->[$i] };
205     $totalprice += $issue->[$i]{'replacementprice'};
206     $row{'replacementprice'} = $issue->[$i]{'replacementprice'};
207     if ( $datedue < $today ) {
208         $row{'red'} = 1;    #print "<font color=red>";
209     }
210     $row{toggle} = $toggle++ % 2;
211
212     #find the charge for an item
213     my ( $charge, $itemtype ) =
214       GetIssuingCharges( $issue->[$i]{'itemnumber'}, $borrowernumber );
215
216     my $itemtypeinfo = getitemtypeinfo($itemtype);
217     $row{'itemtype_description'} = $itemtypeinfo->{description};
218     $row{'itemtype_image'}       = $itemtypeinfo->{imageurl};
219
220     $row{'charge'} = sprintf( "%.2f", $charge );
221
222     #check item is not reserved
223     my ( $restype, $reserves ) = CheckReserves( $issue->[$i]{'itemnumber'} );
224     $row{'norenew'} = ($restype) ? 1 : 0;
225         $row{'renew_failed'} = $renew_failed[$issue->[$i]{'itemnumber'}];               
226     push( @issuedata, \%row );
227 }
228
229 ##################################################################################
230 # BUILD HTML
231 # show all reserves of this borrower, and the position of the reservation ....
232 if ($borrowernumber) {
233
234     # new op dev
235     # now we show the status of the borrower's reservations
236     my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
237     my @reservloop;
238     foreach my $num_res (@borrowerreserv) {
239         my %getreserv;
240         my $getiteminfo  = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
241         my $itemtypeinfo = getitemtypeinfo( $getiteminfo->{'itemtype'} );
242         my ( $transfertwhen, $transfertfrom, $transfertto ) =
243             GetTransfers( $num_res->{'itemnumber'} );
244
245                 foreach (qw(waiting transfered nottransfered)) {
246                                 $getreserv{$_} = 0;
247                 }
248         $getreserv{reservedate}  = C4::Dates->new($num_res->{'reservedate'},'iso')->output('syspref');
249                 foreach (qw(biblionumber title author barcodereserv itemcallnumber )) {
250                                 $getreserv{$_} = $getiteminfo->{$_};
251                 }
252         $getreserv{itemtype}  = $itemtypeinfo->{'description'};
253
254         #               check if we have a waitin status for reservations
255         if ( $num_res->{'found'} eq 'W' ) {
256             $getreserv{color}   = 'reserved';
257             $getreserv{waiting} = 1;
258         }
259
260         #               check transfers with the itemnumber foud in th reservation loop
261         if ($transfertwhen) {
262             $getreserv{color}      = 'transfered';
263             $getreserv{transfered} = 1;
264             $getreserv{datesent}   = C4::Dates->new($transfertwhen, 'iso')->output('syspref') or die "Cannot get new($transfertwhen, 'iso') from C4::Dates";
265             $getreserv{frombranch} = GetBranchName($transfertfrom);
266         }
267
268         if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
269             and not $transfertwhen )
270         {
271             $getreserv{nottransfered}   = 1;
272             $getreserv{nottransferedby} =
273                 GetBranchName( $getiteminfo->{'holdingbranch'} );
274         }
275
276 #               if we don't have a reserv on item, we put the biblio infos and the waiting position
277         if ( $getiteminfo->{'title'} eq '' ) {
278             my $getbibinfo = GetBiblioItemData( $num_res->{'biblionumber'} );
279             my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );
280             $getreserv{color}           = 'inwait';
281             $getreserv{title}           = $getbibinfo->{'title'};
282             $getreserv{waitingposition} = $num_res->{'priority'};
283             $getreserv{nottransfered}   = 0;
284             $getreserv{itemtype}        = $getbibtype->{'description'};
285             $getreserv{author}          = $getbibinfo->{'author'};
286             $getreserv{itemcallnumber}  = '----------';
287             $getreserv{biblionumber}  = $num_res->{'biblionumber'};     
288         }
289
290         push( @reservloop, \%getreserv );
291     }
292
293     # return result to the template
294     $template->param( reservloop => \@reservloop );
295 }
296
297 # current alert subscriptions
298 my $alerts = getalert($borrowernumber);
299 foreach (@$alerts) {
300     $_->{ $_->{type} } = 1;
301     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
302 }
303 my $picture;
304 my $htdocs = C4::Context->config('intrahtdocs');
305 $picture = "/borrowerimages/" . $borrowernumber . ".jpg";
306 if ( -e $htdocs . "$picture" ) {
307     $template->param( picture => $picture );
308 }
309 my $branch=C4::Context->userenv->{'branch'};
310
311 $template->param($data);
312
313 $template->param(
314         detailview => 1,
315     roaddetails      => $roaddetails,
316     borrowernumber   => $borrowernumber,
317     reregistration   => $reregistration,
318     branch           => $branch,        
319     totalprice       => sprintf( "%.2f", $totalprice ),
320     totaldue         => sprintf( "%.2f", $total ),
321     issueloop        => \@issuedata,
322     unvalidlibrarian => $unvalidlibrarian,
323         error            => $error,
324         $error                  => 1,
325     StaffMember         => ($category_type eq 'S'),
326         #                reserveloop     => \@reservedata,
327 );
328
329 output_html_with_http_headers $input, $cookie, $template->output;