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