Cleanup - moremember.pl and .tmpl
[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( $data->{'guarantorid'} ,'borrowernumber');
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( $data->{'guarantorid'},'biblionumber');
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 my %bor;
191 $bor{'borrowernumber'} = $borrowernumber;
192
193 # Converts the branchcode to the branch name
194 my $samebranch;
195 if ( C4::Context->preference("IndependantBranches") ) {
196     my $userenv = C4::Context->userenv;
197     unless ( $userenv->{flags} % 2 == 1 ) {
198         $samebranch = ( $data->{'branchcode'} eq $userenv->{branch} );
199     }
200     $samebranch = 1 if ( $userenv->{flags} % 2 == 1 );
201 }
202 my $branchdetail = GetBranchDetail( $data->{'branchcode'});
203 $data->{'branchname'} = $branchdetail->{branchname};
204
205
206 my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
207 my $lib1 = &GetSortDetails( "Bsort1", $data->{'sort1'} );
208 my $lib2 = &GetSortDetails( "Bsort2", $data->{'sort2'} );
209 $template->param( lib1 => $lib1 ) if ($lib1);
210 $template->param( lib2 => $lib2 ) if ($lib2);
211
212 # current issues
213 #
214 my $issue = GetPendingIssues($borrowernumber);
215 my $count = scalar(@$issue);
216 my $roaddetails = &GetRoadTypeDetails( $data->{'streettype'} );
217 my $today       = POSIX::strftime("%Y-%m-%d", localtime);       # iso format
218 my @issuedata;
219 my $overdues_exist = 0;
220 my $totalprice = 0;
221 for ( my $i = 0 ; $i < $count ; $i++ ) {
222     my $datedue = $issue->[$i]{'date_due'};
223     $issue->[$i]{'date_due'}  = C4::Dates->new($issue->[$i]{'date_due'}, 'iso')->output('syspref');
224     $issue->[$i]{'issuedate'} = C4::Dates->new($issue->[$i]{'issuedate'},'iso')->output('syspref');
225     my %row = %{ $issue->[$i] };
226     $totalprice += $issue->[$i]{'replacementprice'};
227     $row{'replacementprice'} = $issue->[$i]{'replacementprice'};
228     if ( $datedue lt $today ) {
229         $overdues_exist = 1;
230         $row{'red'} = 1;
231         }
232
233     #find the charge for an item
234     my ( $charge, $itemtype ) =
235       GetIssuingCharges( $issue->[$i]{'itemnumber'}, $borrowernumber );
236
237     my $itemtypeinfo = getitemtypeinfo($itemtype);
238     $row{'itemtype_description'} = $itemtypeinfo->{description};
239     $row{'itemtype_image'}       = $itemtypeinfo->{imageurl};
240
241     $row{'charge'} = sprintf( "%.2f", $charge );
242
243         my ( $renewokay,$renewerror ) = CanBookBeRenewed( $borrowernumber, $issue->[$i]{'itemnumber'}, $override_limit );
244         $row{'norenew'} = !$renewokay;
245         $row{'can_confirm'} = ( !$renewokay && $renewerror ne 'on_reserve' );
246         $row{"norenew_reason_$renewerror"} = 1 if $renewerror;
247         $row{'renew_failed'}  = $renew_failed{ $issue->[$i]{'itemnumber'} };
248         $row{'return_failed'} = $return_failed{$issue->[$i]{'barcode'}};   
249     push( @issuedata, \%row );
250 }
251
252 ### ###############################################################################
253 # BUILD HTML
254 # show all reserves of this borrower, and the position of the reservation ....
255 if ($borrowernumber) {
256
257     # new op dev
258     # now we show the status of the borrower's reservations
259     my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
260     my @reservloop;
261     foreach my $num_res (@borrowerreserv) {
262         my %getreserv;
263         my $getiteminfo  = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
264         my $itemtypeinfo = getitemtypeinfo( $getiteminfo->{'itemtype'} );
265         my ( $transfertwhen, $transfertfrom, $transfertto ) =
266             GetTransfers( $num_res->{'itemnumber'} );
267
268         foreach (qw(waiting transfered nottransfered)) {
269             $getreserv{$_} = 0;
270         }
271         $getreserv{reservedate}  = C4::Dates->new($num_res->{'reservedate'},'iso')->output('syspref');
272         foreach (qw(biblionumber title author itemcallnumber )) {
273             $getreserv{$_} = $getiteminfo->{$_};
274         }
275         $getreserv{barcodereserv}  = $getiteminfo->{'barcode'};
276         $getreserv{itemtype}  = $itemtypeinfo->{'description'};
277
278         #               check if we have a waitin status for reservations
279         if ( $num_res->{'found'} eq 'W' ) {
280             $getreserv{color}   = 'reserved';
281             $getreserv{waiting} = 1;
282         }
283
284         #               check transfers with the itemnumber foud in th reservation loop
285         if ($transfertwhen) {
286             $getreserv{color}      = 'transfered';
287             $getreserv{transfered} = 1;
288             $getreserv{datesent}   = C4::Dates->new($transfertwhen, 'iso')->output('syspref') or die "Cannot get new($transfertwhen, 'iso') from C4::Dates";
289             $getreserv{frombranch} = GetBranchName($transfertfrom);
290         }
291
292         if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
293             and not $transfertwhen )
294         {
295             $getreserv{nottransfered}   = 1;
296             $getreserv{nottransferedby} =
297                 GetBranchName( $getiteminfo->{'holdingbranch'} );
298         }
299
300 #               if we don't have a reserv on item, we put the biblio infos and the waiting position
301         if ( $getiteminfo->{'title'} eq '' ) {
302             my $getbibinfo = GetBiblioData( $num_res->{'biblionumber'} );
303             my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );
304             $getreserv{color}           = 'inwait';
305             $getreserv{title}           = $getbibinfo->{'title'};
306             $getreserv{nottransfered}   = 0;
307             $getreserv{itemtype}        = $getbibtype->{'description'};
308             $getreserv{author}          = $getbibinfo->{'author'};
309             $getreserv{biblionumber}  = $num_res->{'biblionumber'};     
310         }
311         $getreserv{waitingposition} = $num_res->{'priority'};
312
313         push( @reservloop, \%getreserv );
314     }
315
316     # return result to the template
317     $template->param( reservloop => \@reservloop );
318 }
319
320 # current alert subscriptions
321 my $alerts = getalert($borrowernumber);
322 foreach (@$alerts) {
323     $_->{ $_->{type} } = 1;
324     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
325 }
326
327 # check to see if patron's image exists in the database
328 # basically this gives us a template var to condition the display of
329 # patronimage related interface on
330 my ($picture, $dberror) = GetPatronImage($data->{'cardnumber'});
331 $template->param( picture => 1 ) if $picture;
332
333 my $branch=C4::Context->userenv->{'branch'};
334
335 $template->param($data);
336
337 if (C4::Context->preference('ExtendedPatronAttributes')) {
338     $template->param(ExtendedPatronAttributes => 1);
339     $template->param(patron_attributes => C4::Members::Attributes::GetBorrowerAttributes($borrowernumber));
340     my @types = C4::Members::AttributeTypes::GetAttributeTypes();
341     if (scalar(@types) == 0) {
342         $template->param(no_patron_attribute_types => 1);
343     }
344 }
345
346 if (C4::Context->preference('EnhancedMessagingPreferences')) {
347     C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
348     $template->param(messaging_form_inactive => 1);
349     $template->param(SMSSendDriver => C4::Context->preference("SMSSendDriver"));
350     $template->param(SMSnumber     => defined $data->{'smsalertnumber'} ? $data->{'smsalertnumber'} : $data->{'mobile'});
351 }
352
353 $template->param(
354     detailview => 1,
355     AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
356     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
357     roaddetails     => $roaddetails,
358     borrowernumber  => $borrowernumber,
359     categoryname    => $data->{'description'},
360     reregistration  => $reregistration,
361     branch          => $branch,
362     totalprice      => sprintf("%.2f", $totalprice),
363     totaldue        => sprintf("%.2f", $total),
364     totaldue_raw    => $total,
365     issueloop       => \@issuedata,
366     overdues_exist  => $overdues_exist,
367     error           => $error,
368     $error          => 1,
369     StaffMember     => ($category_type eq 'S'),
370     is_child        => ($category_type eq 'C'),
371 #   reserveloop     => \@reservedata,
372     dateformat      => C4::Context->preference("dateformat"),
373     "dateformat_" . (C4::Context->preference("dateformat") || '') => 1,
374 );
375
376 output_html_with_http_headers $input, $cookie, $template->output;