Fix for bug 1538
[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 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") { $template_name = "members/moremember-print.tmpl";   }
56 elsif ($print eq "slip") { $template_name = "members/moremember-receipt.tmpl"; }
57 else {                     $template_name = "members/moremember.tmpl";         }
58
59 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
60     {
61         template_name   => $template_name,
62         query           => $input,
63         type            => "intranet",
64         authnotrequired => 0,
65         flagsrequired   => { borrowers => 1 },
66         debug           => 1,
67     }
68 );
69 my $borrowernumber = $input->param('borrowernumber');
70
71 #start the page and read in includes
72 my $data           = GetMember( $borrowernumber ,'borrowernumber');
73 my $reregistration = $input->param('reregistration');
74
75 if ( not defined $data ) {
76     $template->param (unknowuser => 1);
77     exit;
78 }
79
80 # re-reregistration function to automatic calcul of date expiry
81 (
82 #     $data->{'dateexpiry'} = GetMembeReregistration(
83  $data->{'dateexpiry'} = ExtendMemberSubscriptionTo(
84         $data->{'categorycode'},
85         $borrowernumber, $data->{'dateenrolled'}
86     )
87 ) if ( $reregistration eq 'y' );
88 my $borrowercategory = GetBorrowercategory( $data->{'categorycode'} );
89 my $category_type = $borrowercategory->{'category_type'};
90
91 # in template <TMPL_IF name="I"> => instutitional (A for Adult& C for children) 
92 $template->param( $data->{'categorycode'} => 1 ); 
93
94 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
95                 my $tempdate = C4::Dates->new($data->{$_})->output('iso')
96                         or warn ("Invalid $_ = " . $data->{$_});
97                 $data->{$_} = $tempdate || '';
98 }
99 $data->{'IS_ADULT'} = ( $data->{'categorycode'} ne 'I' );
100
101 for (qw(debarred gonenoaddress lost borrowernotes)) {
102          $data->{$_} and $template->param(flagged => 1) and last;
103 }
104
105 $data->{'ethnicity'} = fixEthnicity( $data->{'ethnicity'} );
106
107 $data->{ "sex_".$data->{'sex'}."_p" } = 1;
108
109 if ( $category_type eq 'C' and $data->{'guarantorid'} ne '0' ) {
110     my $data2 = GetMember( $data->{'guarantorid'} ,'borrowernumber');
111         foreach (qw(address city B_address B_city phone mobilezipcode)) {
112         $data->{$_} = $data2->{$_};
113         }
114 }
115
116 if ( $data->{'ethnicity'} || $data->{'ethnotes'} ) {
117     $template->param( printethnicityline => 1 );
118 }
119 if ( $category_type eq 'A' ) {
120     $template->param( isguarantee => 1 );
121
122     # FIXME
123     # It looks like the $i is only being returned to handle walking through
124     # the array, which is probably better done as a foreach loop.
125     #
126     my ( $count, $guarantees ) = GetGuarantees( $data->{'borrowernumber'} );
127     my @guaranteedata;
128     for ( my $i = 0 ; $i < $count ; $i++ ) {
129         push(@guaranteedata,
130             {
131                 borrowernumber => $guarantees->[$i]->{'borrowernumber'},
132                 cardnumber     => $guarantees->[$i]->{'cardnumber'},
133                 name           => $guarantees->[$i]->{'firstname'} . " "
134                                 . $guarantees->[$i]->{'surname'}
135             }
136         );
137     }
138     $template->param( guaranteeloop => \@guaranteedata );
139     ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
140 }
141 else {
142     if ($data->{'guarantorid'}){
143             my ($guarantor) = GetMember( $data->{'guarantorid'},'biblionumber');
144                 $template->param(guarantor => 1);
145                 foreach (qw(borrowernumber cardnumber firstname surname)) {        
146                           $template->param("guarantor$_" => $guarantor->{$_});
147         }
148     }
149 }
150
151 #Independant branches management
152 my $unvalidlibrarian =
153   (      ( C4::Context->preference("IndependantBranches") )
154       && ( C4::Context->userenv->{flags} != 1 )
155       && ( $data->{'branchcode'} ne C4::Context->userenv->{branch} ) );
156
157 my %bor;
158 $bor{'borrowernumber'} = $borrowernumber;
159
160 # Converts the branchcode to the branch name
161 my $samebranch;
162 if ( C4::Context->preference("IndependantBranches") ) {
163     my $userenv = C4::Context->userenv;
164     unless ( $userenv->{flags} == 1 ) {
165         $samebranch = ( $data->{'branchcode'} eq $userenv->{branch} );
166     }
167     $samebranch = 1 if ( $userenv->{flags} == 1 );
168 }
169 my $branchdetail = GetBranchDetail( $data->{'branchcode'});
170 $data->{'branchname'} = $branchdetail->{branchname};
171
172
173 my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
174 my $lib1 = &GetSortDetails( "Bsort1", $data->{'sort1'} );
175 my $lib2 = &GetSortDetails( "Bsort2", $data->{'sort2'} );
176 ( $template->param( lib1 => $lib1 ) ) if ($lib1);
177 ( $template->param( lib2 => $lib2 ) ) if ($lib2);
178
179 # current issues
180 #
181 my ( $count, $issue ) = GetPendingIssues($borrowernumber);
182 my $roaddetails = &GetRoadTypeDetails( $data->{'streettype'} );
183 my $today       = POSIX::strftime("%Y%m%d", localtime); # iso format
184 my @issuedata;
185 my $totalprice = 0;
186 my $toggle     = 0;
187 for ( my $i = 0 ; $i < $count ; $i++ ) {
188     my $datedue = $issue->[$i]{'date_due'};
189     $issue->[$i]{'date_due'} = C4::Dates->new($issue->[$i]{'date_due'},'iso')->output(C4::Context->preference('dateformat'));
190     my %row = %{ $issue->[$i] };
191     $totalprice += $issue->[$i]{'replacementprice'};
192     $row{'replacementprice'} = $issue->[$i]{'replacementprice'};
193     if ( $datedue < $today ) {
194         $row{'red'} = 1;    #print "<font color=red>";
195     }
196     $row{toggle} = $toggle++ % 2;
197
198     #find the charge for an item
199     my ( $charge, $itemtype ) =
200       GetIssuingCharges( $issue->[$i]{'itemnumber'}, $borrowernumber );
201
202     my $itemtypeinfo = getitemtypeinfo($itemtype);
203     $row{'itemtype_description'} = $itemtypeinfo->{description};
204     $row{'itemtype_image'}       = $itemtypeinfo->{imageurl};
205
206     $row{'charge'} = sprintf( "%.2f", $charge );
207
208     #check item is not reserved
209     my ( $restype, $reserves ) = CheckReserves( $issue->[$i]{'itemnumber'} );
210     $row{'norenew'} = ($restype) ? 1 : 0;
211     push( @issuedata, \%row );
212 }
213
214 ##################################################################################
215 # BUILD HTML
216 # show all reserves of this borrower, and the position of the reservation ....
217 if ($borrowernumber) {
218
219     # new op dev
220     # now we show the status of the borrower's reservations
221     my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
222     my @reservloop;
223     foreach my $num_res (@borrowerreserv) {
224         my %getreserv;
225         my $getiteminfo  = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
226         my $itemtypeinfo = getitemtypeinfo( $getiteminfo->{'itemtype'} );
227         my ( $transfertwhen, $transfertfrom, $transfertto ) =
228             GetTransfers( $num_res->{'itemnumber'} );
229
230                 foreach (qw(waiting transfered nottransfered)) {
231                                 $getreserv{$_} = 0;
232                 }
233 #       Joe this line fails, can you take a look please
234 #        $getreserv{reservedate}  = C4::Dates->new($num_res->{'reservedate'})->output() or die "Cannot get new($num_res->{'reservedate'}) from C4::Dates";
235                 foreach (qw(biblionumber title author barcodereserv itemcallnumber )) {
236                                 $getreserv{$_} = $getiteminfo->{$_};
237                 }
238         $getreserv{itemtype}  = $itemtypeinfo->{'description'};
239
240         #               check if we have a waitin status for reservations
241         if ( $num_res->{'found'} eq 'W' ) {
242             $getreserv{color}   = 'reserved';
243             $getreserv{waiting} = 1;
244         }
245
246         #               check transfers with the itemnumber foud in th reservation loop
247         if ($transfertwhen) {
248             $getreserv{color}      = 'transfered';
249             $getreserv{transfered} = 1;
250             $getreserv{datesent}   = C4::Dates->new($transfertwhen, 'iso')->output() or die "Cannot get new($transfertwhen, 'iso') from C4::Dates";
251             $getreserv{frombranch} = GetBranchName($transfertfrom);
252         }
253
254         if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
255             and not $transfertwhen )
256         {
257             $getreserv{nottransfered}   = 1;
258             $getreserv{nottransferedby} =
259                 GetBranchName( $getiteminfo->{'holdingbranch'} );
260         }
261
262 #               if we don't have a reserv on item, we put the biblio infos and the waiting position
263         if ( $getiteminfo->{'title'} eq '' ) {
264             my $getbibinfo = GetBiblioItemData( $num_res->{'biblionumber'} );
265             my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );
266             $getreserv{color}           = 'inwait';
267             $getreserv{title}           = $getbibinfo->{'title'};
268             $getreserv{waitingposition} = $num_res->{'priority'};
269             $getreserv{nottransfered}   = 0;
270             $getreserv{itemtype}        = $getbibtype->{'description'};
271             $getreserv{author}          = $getbibinfo->{'author'};
272             $getreserv{itemcallnumber}  = '----------';
273             $getreserv{biblionumber}  = $num_res->{'biblionumber'};     
274         }
275
276         push( @reservloop, \%getreserv );
277     }
278
279     # return result to the template
280     $template->param( reservloop => \@reservloop );
281 }
282
283 # current alert subscriptions
284 my $alerts = getalert($borrowernumber);
285 foreach (@$alerts) {
286     $_->{ $_->{type} } = 1;
287     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
288 }
289 my $picture;
290 my $htdocs = C4::Context->config('intrahtdocs');
291 $picture = "/borrowerimages/" . $borrowernumber . ".jpg";
292 if ( -e $htdocs . "$picture" ) {
293     $template->param( picture => $picture );
294 }
295 my $branch=C4::Context->userenv->{'branch'};
296
297 $template->param($data);
298
299 $template->param(
300     roaddetails      => $roaddetails,
301     borrowernumber   => $borrowernumber,
302     reregistration   => $reregistration,
303     branch           => $branch,        
304     totalprice       => sprintf( "%.2f", $totalprice ),
305     totaldue         => sprintf( "%.2f", $total ),
306     issueloop        => \@issuedata,
307     unvalidlibrarian => $unvalidlibrarian,
308     
309     #            reserveloop     => \@reservedata,
310 );
311
312 output_html_with_http_headers $input, $cookie, $template->output;