user privacy managing and dealing with AnonymousPatron new syspref
[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::Members::Attributes;
41 use C4::Members::AttributeTypes;
42 use C4::Dates;
43 use C4::Reserves;
44 use C4::Circulation;
45 use C4::Koha;
46 use C4::Letters;
47 use C4::Biblio;
48 use C4::Reserves;
49 use C4::Branch; # GetBranchName
50 use C4::Form::MessagingPreferences;
51
52 #use Smart::Comments;
53 #use Data::Dumper;
54
55 use vars qw($debug);
56
57 BEGIN {
58         $debug = $ENV{DEBUG} || 0;
59 }
60
61 my $dbh = C4::Context->dbh;
62
63 my $input = new CGI;
64 $debug or $debug = $input->param('debug') || 0;
65 my $print = $input->param('print');
66 my $override_limit = $input->param("override_limit") || 0;
67 my @failedrenews = $input->param('failedrenew');
68 my @failedreturns = $input->param('failedreturn');
69 my $error = $input->param('error');
70 my %renew_failed;
71 for my $renew (@failedrenews) { $renew_failed{$renew} = 1; }
72 my %return_failed;
73 for my $failedret (@failedreturns) { $return_failed{$failedret} = 1; }
74
75 my $template_name;
76
77 if    ($print eq "page") { $template_name = "members/moremember-print.tmpl";   }
78 elsif ($print eq "slip") { $template_name = "members/moremember-receipt.tmpl"; }
79 else {                     $template_name = "members/moremember.tmpl";         }
80
81 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
82     {
83         template_name   => $template_name,
84         query           => $input,
85         type            => "intranet",
86         authnotrequired => 0,
87         flagsrequired   => { borrowers => 1 },
88         debug           => 1,
89     }
90 );
91 my $borrowernumber = $input->param('borrowernumber');
92
93 #start the page and read in includes
94 my $data           = GetMember( borrowernumber=>$borrowernumber );
95 my $reregistration = $input->param('reregistration');
96
97 if ( not defined $data ) {
98     $template->param (unknowuser => 1);
99         output_html_with_http_headers $input, $cookie, $template->output;
100     exit;
101 }
102
103 # re-reregistration function to automatic calcul of date expiry
104 if ( $reregistration eq 'y' ) {
105         $data->{'dateexpiry'} = ExtendMemberSubscriptionTo( $borrowernumber );
106 }
107
108 my $category_type = $data->{'category_type'};
109
110 ### $category_type
111
112 # in template <TMPL_IF name="I"> => instutitional (A for Adult& C for children) 
113 $template->param( $data->{'categorycode'} => 1 ); 
114
115 $debug and printf STDERR "dates (enrolled,expiry,birthdate) raw: (%s, %s, %s)\n", map {$data->{$_}} qw(dateenrolled dateexpiry dateofbirth);
116 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
117                 my $userdate = $data->{$_};
118                 unless ($userdate) {
119                         $debug and warn sprintf "Empty \$data{%12s}", $_;
120                         $data->{$_} = '';
121                         next;
122                 }
123                 $userdate = C4::Dates->new($userdate,'iso')->output('syspref');
124                 $data->{$_} = $userdate || '';
125                 $template->param( $_ => $userdate );
126 }
127 $data->{'IS_ADULT'} = ( $data->{'categorycode'} ne 'I' );
128
129 for (qw(debarred gonenoaddress lost borrowernotes)) {
130          $data->{$_} and $template->param(flagged => 1) and last;
131 }
132
133 $data->{'ethnicity'} = fixEthnicity( $data->{'ethnicity'} );
134 $data->{ "sex_".$data->{'sex'}."_p" } = 1;
135
136 my $catcode;
137 if ( $category_type eq 'C') {
138         if ($data->{'guarantorid'} ne '0' ) {
139         my $data2 = GetMember(borrowernumber=> $data->{'guarantorid'} );
140         foreach (qw(address city B_address B_city phone mobile zipcode)) {
141             $data->{$_} = $data2->{$_};
142         }
143    }
144    my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
145    my $cnt = scalar(@$catcodes);
146
147    $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
148    $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
149 }
150
151
152 if ( $data->{'ethnicity'} || $data->{'ethnotes'} ) {
153     $template->param( printethnicityline => 1 );
154 }
155 if ( $category_type eq 'A' ) {
156     $template->param( isguarantee => 1 );
157
158     # FIXME
159     # It looks like the $i is only being returned to handle walking through
160     # the array, which is probably better done as a foreach loop.
161     #
162     my ( $count, $guarantees ) = GetGuarantees( $data->{'borrowernumber'} );
163     my @guaranteedata;
164     for ( my $i = 0 ; $i < $count ; $i++ ) {
165         push(@guaranteedata,
166             {
167                 borrowernumber => $guarantees->[$i]->{'borrowernumber'},
168                 cardnumber     => $guarantees->[$i]->{'cardnumber'},
169                 name           => $guarantees->[$i]->{'firstname'} . " "
170                                 . $guarantees->[$i]->{'surname'}
171             }
172         );
173     }
174     $template->param( guaranteeloop => \@guaranteedata );
175     ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
176 }
177 else {
178     if ($data->{'guarantorid'}){
179             my ($guarantor) = GetMember( borrowernumber=>$data->{'guarantorid'});
180                 $template->param(guarantor => 1);
181                 foreach (qw(borrowernumber cardnumber firstname surname)) {        
182                           $template->param("guarantor$_" => $guarantor->{$_});
183         }
184     }
185         if ($category_type eq 'C'){
186                 $template->param('C' => 1);
187         }
188 }
189
190 #Independant branches management
191 my $unvalidlibrarian =
192   (      ( C4::Context->preference("IndependantBranches") )
193       && ( C4::Context->userenv->{flags} % 2 != 1 )
194       && ( $data->{'branchcode'} ne C4::Context->userenv->{branch} ) );
195
196 my %bor;
197 $bor{'borrowernumber'} = $borrowernumber;
198
199 # Converts the branchcode to the branch name
200 my $samebranch;
201 my $branchdetail = GetBranchDetail( $data->{'branchcode'});
202 $data->{'branchname'} = $branchdetail->{branchname};
203
204
205 my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
206 my $lib1 = &GetSortDetails( "Bsort1", $data->{'sort1'} );
207 my $lib2 = &GetSortDetails( "Bsort2", $data->{'sort2'} );
208 $template->param( lib1 => $lib1 ) if ($lib1);
209 $template->param( lib2 => $lib2 ) if ($lib2);
210
211 # current issues
212 #
213 my $issue = GetPendingIssues($borrowernumber);
214 my $count = scalar(@$issue);
215 my $roaddetails = &GetRoadTypeDetails( $data->{'streettype'} );
216 my $today       = POSIX::strftime("%Y-%m-%d", localtime);       # iso format
217 my @issuedata;
218 my $overdues_exist = 0;
219 my $totalprice = 0;
220 for ( my $i = 0 ; $i < $count ; $i++ ) {
221     my $datedue = $issue->[$i]{'date_due'};
222     $issue->[$i]{'date_due'}  = C4::Dates->new($issue->[$i]{'date_due'}, 'iso')->output('syspref');
223     $issue->[$i]{'issuedate'} = C4::Dates->new($issue->[$i]{'issuedate'},'iso')->output('syspref');
224     my %row = %{ $issue->[$i] };
225     $totalprice += $issue->[$i]{'replacementprice'};
226     $row{'replacementprice'} = $issue->[$i]{'replacementprice'};
227     if ( $datedue lt $today ) {
228         $overdues_exist = 1;
229         $row{'red'} = 1;
230         }
231
232     #find the charge for an item
233     my ( $charge, $itemtype ) =
234       GetIssuingCharges( $issue->[$i]{'itemnumber'}, $borrowernumber );
235
236     my $itemtypeinfo = getitemtypeinfo($itemtype);
237     $row{'itemtype_description'} = $itemtypeinfo->{description};
238     $row{'itemtype_image'}       = $itemtypeinfo->{imageurl};
239
240     $row{'charge'} = sprintf( "%.2f", $charge );
241
242         my ( $renewokay,$renewerror ) = CanBookBeRenewed( $borrowernumber, $issue->[$i]{'itemnumber'}, $override_limit );
243         $row{'norenew'} = !$renewokay;
244         $row{'can_confirm'} = ( !$renewokay && $renewerror ne 'on_reserve' );
245         $row{"norenew_reason_$renewerror"} = 1 if $renewerror;
246         $row{'renew_failed'}  = $renew_failed{ $issue->[$i]{'itemnumber'} };
247         $row{'return_failed'} = $return_failed{$issue->[$i]{'barcode'}};   
248     push( @issuedata, \%row );
249 }
250
251 ### ###############################################################################
252 # BUILD HTML
253 # show all reserves of this borrower, and the position of the reservation ....
254 if ($borrowernumber) {
255
256     # new op dev
257     # now we show the status of the borrower's reservations
258     my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
259     my @reservloop;
260     foreach my $num_res (@borrowerreserv) {
261         my %getreserv;
262         my $getiteminfo  = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
263         my $itemtypeinfo = getitemtypeinfo( $getiteminfo->{'itemtype'} );
264         my ( $transfertwhen, $transfertfrom, $transfertto ) =
265             GetTransfers( $num_res->{'itemnumber'} );
266
267         foreach (qw(waiting transfered nottransfered)) {
268             $getreserv{$_} = 0;
269         }
270         $getreserv{reservedate}  = C4::Dates->new($num_res->{'reservedate'},'iso')->output('syspref');
271         foreach (qw(biblionumber title author itemcallnumber )) {
272             $getreserv{$_} = $getiteminfo->{$_};
273         }
274         $getreserv{barcodereserv}  = $getiteminfo->{'barcode'};
275         $getreserv{itemtype}  = $itemtypeinfo->{'description'};
276
277         #               check if we have a waitin status for reservations
278         if ( $num_res->{'found'} eq 'W' ) {
279             $getreserv{color}   = 'reserved';
280             $getreserv{waiting} = 1;
281         }
282
283         #               check transfers with the itemnumber foud in th reservation loop
284         if ($transfertwhen) {
285             $getreserv{color}      = 'transfered';
286             $getreserv{transfered} = 1;
287             $getreserv{datesent}   = C4::Dates->new($transfertwhen, 'iso')->output('syspref') or die "Cannot get new($transfertwhen, 'iso') from C4::Dates";
288             $getreserv{frombranch} = GetBranchName($transfertfrom);
289         }
290
291         if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
292             and not $transfertwhen )
293         {
294             $getreserv{nottransfered}   = 1;
295             $getreserv{nottransferedby} =
296                 GetBranchName( $getiteminfo->{'holdingbranch'} );
297         }
298
299 #               if we don't have a reserv on item, we put the biblio infos and the waiting position
300         if ( $getiteminfo->{'title'} eq '' ) {
301             my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
302             my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );
303             $getreserv{color}           = 'inwait';
304             $getreserv{title}           = $getbibinfo->{'title'};
305             $getreserv{nottransfered}   = 0;
306             $getreserv{itemtype}        = $getbibtype->{'description'};
307             $getreserv{author}          = $getbibinfo->{'author'};
308             $getreserv{biblionumber}  = $num_res->{'biblionumber'};     
309         }
310         $getreserv{waitingposition} = $num_res->{'priority'};
311
312         push( @reservloop, \%getreserv );
313     }
314
315     # return result to the template
316     $template->param( reservloop => \@reservloop );
317 }
318
319 # current alert subscriptions
320 my $alerts = getalert($borrowernumber);
321 foreach (@$alerts) {
322     $_->{ $_->{type} } = 1;
323     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
324 }
325
326 # check to see if patron's image exists in the database
327 # basically this gives us a template var to condition the display of
328 # patronimage related interface on
329 my ($picture, $dberror) = GetPatronImage($data->{'cardnumber'});
330 $template->param( picture => 1 ) if $picture;
331
332 my $branch=C4::Context->userenv->{'branch'};
333
334 $template->param($data);
335
336 if (C4::Context->preference('ExtendedPatronAttributes')) {
337     $template->param(ExtendedPatronAttributes => 1);
338     $template->param(patron_attributes => C4::Members::Attributes::GetBorrowerAttributes($borrowernumber));
339     my @types = C4::Members::AttributeTypes::GetAttributeTypes();
340     if (scalar(@types) == 0) {
341         $template->param(no_patron_attribute_types => 1);
342     }
343 }
344
345 if (C4::Context->preference('EnhancedMessagingPreferences')) {
346     C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
347     $template->param(messaging_form_inactive => 1);
348     $template->param(SMSSendDriver => C4::Context->preference("SMSSendDriver"));
349     $template->param(SMSnumber     => defined $data->{'smsalertnumber'} ? $data->{'smsalertnumber'} : $data->{'mobile'});
350 }
351
352 $template->param(
353     detailview => 1,
354     AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
355     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
356     roaddetails     => $roaddetails,
357     borrowernumber  => $borrowernumber,
358     categoryname    => $data->{'description'},
359     reregistration  => $reregistration,
360     branch          => $branch,
361     totalprice      => sprintf("%.2f", $totalprice),
362     totaldue        => sprintf("%.2f", $total),
363     totaldue_raw    => $total,
364     issueloop       => \@issuedata,
365     overdues_exist  => $overdues_exist,
366     error           => $error,
367     $error          => 1,
368     StaffMember     => ($category_type eq 'S'),
369     is_child        => ($category_type eq 'C'),
370 #   reserveloop     => \@reservedata,
371     dateformat      => C4::Context->preference("dateformat"),
372     "dateformat_" . (C4::Context->preference("dateformat") || '') => 1,
373     samebranch     => !($unvalidlibrarian),
374 );
375
376 output_html_with_http_headers $input, $cookie, $template->output;