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