Adding Version variable to systempreferences.
[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 # $Id$
21
22 =head1 moremember.pl
23
24  script to do a borrower enquiry/bring up borrower details etc
25  Displays all the details about a borrower
26  written 20/12/99 by chris@katipo.co.nz
27  last modified 21/1/2000 by chris@katipo.co.nz
28  modified 31/1/2001 by chris@katipo.co.nz
29    to not allow items on request to be renewed
30
31  needs html removed and to use the C4::Output more, but its tricky
32
33 =cut
34
35 use strict;
36 use C4::Auth;
37 use C4::Context;
38 use C4::Output;
39 use C4::Interface::CGI::Output;
40 use C4::Interface::CGI::Template;
41 use CGI;
42 use C4::Members;
43 use Date::Manip;
44 use C4::Date;
45 use C4::Reserves2;
46 use C4::Circulation;
47 use C4::Koha;
48 use C4::Letters;
49 use C4::Biblio;
50 use C4::Branch; # GetBranchName
51
52 my $dbh = C4::Context->dbh;
53
54 my $input = new CGI;
55 my $print = $input->param('print');
56 my $template_name;
57
58 if ( $print eq "page" ) {
59     $template_name = "members/moremember-print.tmpl";
60 }
61 elsif ( $print eq "slip" ) {
62     $template_name = "members/moremember-receipt.tmpl";
63 }
64 else {
65     $template_name = "members/moremember.tmpl";
66 }
67
68 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
69     {
70         template_name   => $template_name,
71         query           => $input,
72         type            => "intranet",
73         authnotrequired => 0,
74         flagsrequired   => { borrowers => 1 },
75         debug           => 1,
76     }
77 );
78 my $borrowernumber = $input->param('borrowernumber');
79
80 #start the page and read in includes
81 my $data           = borrdata( '', $borrowernumber );
82 my $reregistration = $input->param('reregistration');
83
84 if ( not defined $data ) {
85     $template->param (
86         unknowuser => 1
87     );
88     output_html_with_http_headers $input, $cookie, $template->output;
89     exit;
90 }
91
92 # re-reregistration function to automatic calcul of date expiry
93 (
94     $data->{'dateexpiry'} = GetMembeReregistration(
95         $data->{'categorycode'},
96         $borrowernumber, $data->{'dateenrolled'}
97     )
98 ) if ( $reregistration eq 'y' );
99 my ( undef, undef, undef, $category_type ) =
100   getborrowercategory( $data->{'categorycode'} );
101   
102 # in template <TMPL_IF name="I"> => instutitional (A for Adult& C for children) 
103 $template->param( $data->{'categorycode'} => 1 ); 
104
105 $data->{'dateenrolled'} = format_date( $data->{'dateenrolled'} );
106 $data->{'dateexpiry'}   = format_date( $data->{'dateexpiry'} );
107 $data->{'dateofbirth'}  = format_date( $data->{'dateofbirth'} );
108 $data->{'IS_ADULT'}     = ( $data->{'categorycode'} ne 'I' );
109
110 if (   $data->{'debarred'}
111     || $data->{'gonenoaddress'}
112     || $data->{'lost'}
113     || $data->{'borrowernotes'} )
114 {
115     $template->param( flagged => 1 );
116 }
117
118 $data->{'ethnicity'} = fixEthnicity( $data->{'ethnicity'} );
119
120 $data->{ &expand_sex_into_predicate( $data->{'sex'} ) } = 1;
121
122 if ( $category_type eq 'C' and $data->{'guarantorid'} ne '0' ) {
123     my $data2 = borrdata( '', $data->{'guarantorid'} );
124     $data->{'address'}   = $data2->{'address'};
125     $data->{'city'}      = $data2->{'city'};
126     $data->{'B_address'} = $data2->{'B_address'};
127     $data->{'B_city'}    = $data2->{'B_city'};
128     $data->{'phone'}     = $data2->{'phone'};
129     $data->{'mobile'}    = $data2->{'mobile'};
130     $data->{'zipcode'}   = $data2->{'zipcode'};
131 }
132
133 if ( $data->{'ethnicity'} || $data->{'ethnotes'} ) {
134     $template->param( printethnicityline => 1 );
135 }
136 if ( $category_type eq 'A' ) {
137     $template->param( isguarantee => 1 );
138
139     # FIXME
140     # It looks like the $i is only being returned to handle walking through
141     # the array, which is probably better done as a foreach loop.
142     #
143     my ( $count, $guarantees ) = findguarantees( $data->{'borrowernumber'} );
144     my @guaranteedata;
145     for ( my $i = 0 ; $i < $count ; $i++ ) {
146         push(
147             @guaranteedata,
148             {
149                 borrowernumber => $guarantees->[$i]->{'borrowernumber'},
150                 cardnumber     => $guarantees->[$i]->{'cardnumber'},
151                 name           => $guarantees->[$i]->{'firstname'} . " "
152                   . $guarantees->[$i]->{'surname'}
153             }
154         );
155     }
156     $template->param( guaranteeloop => \@guaranteedata );
157     ( $template->param( adultborrower => 1 ) ) if ( $category_type eq 'A' );
158
159 }
160 else {
161     my ($guarantorid) = findguarantor( $data->{'borrowernumber'} );
162     ( $template->param( guarantor => 1 ) )
163       if ( ( $data->{'guarantorid'} > '0' ) );
164     if ( $guarantorid->{'borrowernumber'} ) {
165         $template->param(
166             guarantorborrowernumber => $guarantorid->{'borrowernumber'},
167             guarantorcardnumber     => $guarantorid->{'cardnumber'},
168             guarantorfirstname      => $guarantorid->{'firstname'},
169             guarantorsurname        => $guarantorid->{'surname'}
170         );
171     }
172 }
173
174 #Independant branches management
175 my $unvalidlibrarian =
176   (      ( C4::Context->preference("IndependantBranches") )
177       && ( C4::Context->userenv->{flags} != 1 )
178       && ( $data->{'branchcode'} ne C4::Context->userenv->{branch} ) );
179
180 my %bor;
181 $bor{'borrowernumber'} = $borrowernumber;
182
183 # Converts the branchcode to the branch name
184 my $samebranch;
185 if ( C4::Context->preference("IndependantBranches") ) {
186     my $userenv = C4::Context->userenv;
187     unless ( $userenv->{flags} == 1 ) {
188         $samebranch = ( $data->{'branchcode'} eq $userenv->{branch} );
189     }
190     $samebranch = 1 if ( $userenv->{flags} == 1 );
191 }
192
193 $data->{'branchname'} =
194   ( ( GetBranchDetail( $data->{'branchcode'} ) )->{'branchname'} );
195
196 # Converts the categorycode to the description
197 ( $data->{'categorycode'}, undef, undef ) =
198   &getborrowercategory( $data->{'categorycode'} );
199
200 my ( $numaccts, $accts, $total ) = getboracctrecord( '', \%bor );
201 my $lib1 = &GetSortDetails( "Bsort1", $data->{'sort1'} );
202 my $lib2 = &GetSortDetails( "Bsort2", $data->{'sort2'} );
203 ( $template->param( lib1 => $lib1 ) ) if ($lib1);
204 ( $template->param( lib2 => $lib2 ) ) if ($lib2);
205
206 # current issues
207 #
208 my ( $count, $issue ) = borrissues($borrowernumber);
209 my $roaddetails = &GetRoadTypeDetails( $data->{'streettype'} );
210 my $today       = ParseDate('today');
211 my @issuedata;
212 my $totalprice = 0;
213 my $toggle     = 0;
214 for ( my $i = 0 ; $i < $count ; $i++ ) {
215     my $datedue = ParseDate( $issue->[$i]{'date_due'} );
216     $issue->[$i]{'date_due'} = format_date( $issue->[$i]{'date_due'} );
217     my %row = %{ $issue->[$i] };
218     $totalprice += $issue->[$i]{'replacementprice'};
219     $row{'replacementprice'} = $issue->[$i]{'replacementprice'};
220     if ( $datedue < $today ) {
221         $row{'red'} = 1;    #print "<font color=red>";
222     }
223     $row{toggle} = $toggle++ % 2;
224
225     #find the charge for an item
226     my ( $charge, $itemtype ) =
227       GetIssuingCharges( $issue->[$i]{'itemnumber'}, $borrowernumber );
228
229     my $itemtypeinfo = getitemtypeinfo($itemtype);
230     $row{'itemtype_description'} = $itemtypeinfo->{description};
231     $row{'itemtype_image'}       = $itemtypeinfo->{imageurl};
232
233     $row{'charge'} = sprintf( "%.2f", $charge );
234
235     #check item is not reserved
236     my ( $restype, $reserves ) = CheckReserves( $issue->[$i]{'itemnumber'} );
237     if ($restype) {
238
239 #               print "<TD><a href=/cgi-bin/koha/reserve/request.pl?biblionumber=$issue->[$i]{'biblionumber'}>On Request - no renewals</a></td></tr>";
240 #  } elsif ($issue->[$i]->{'renewals'} > 0) {
241 #      print "<TD>Previously Renewed - no renewals</td></tr>";
242         $row{'norenew'} = 1;
243     }
244     else {
245         $row{'norenew'} = 0;
246     }
247     push( @issuedata, \%row );
248 }
249
250 #
251 # find reserves
252 #
253 # my ($rescount,$reserves)=FindReserves('',$borrowernumber); #From C4::Reserves2
254 # my @reservedata;
255 # $toggle = 0;
256 # foreach my $reserveline (@$reserves) {
257 #       $reserveline->{'reservedate2'} = format_date($reserveline->{'reservedate'});
258 #       my $restitle;
259 #       my %row = %$reserveline;
260 #         $row{toggle} = $toggle++%2;
261 #       if ($reserveline->{'constrainttype'} eq 'o'){
262 #               $restitle=GetReserveTitle($reserveline->{'biblionumber'},$reserveline->{'borrowernumber'},$reserveline->{'reservedate'},$reserveline->{'rtimestamp'});
263 #               %row =  (%row , %$restitle) if $restitle;
264 #       }
265 #       push (@reservedata, \%row);
266 # }
267
268 ##################################################################################
269 # BUILD HTML
270 # show all reserves of this borrower, and the position of the reservation ....
271 if ($borrowernumber) {
272
273     # new op dev
274     # now we show the status of the borrower's reservations
275     my @borrowerreserv = GetReservations( 0, $borrowernumber );
276     my @reservloop;
277     foreach my $num_res (@borrowerreserv) {
278         my %getreserv;
279         my %env;
280         my $getiteminfo  = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
281         my $itemtypeinfo = getitemtypeinfo( $getiteminfo->{'itemtype'} );
282         my ( $transfertwhen, $transfertfrom, $transfertto ) =
283           GetTransfers( $num_res->{'itemnumber'} );
284
285         $getreserv{waiting}       = 0;
286         $getreserv{transfered}    = 0;
287         $getreserv{nottransfered} = 0;
288
289         $getreserv{reservedate}    = format_date( $num_res->{'reservedate'} );
290         $getreserv{biblionumber}   = $getiteminfo->{'biblionumber'};
291         $getreserv{title}          = $getiteminfo->{'title'};
292         $getreserv{itemtype}       = $itemtypeinfo->{'description'};
293         $getreserv{author}         = $getiteminfo->{'author'};
294         $getreserv{barcodereserv}  = $getiteminfo->{'barcode'};
295         $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
296
297         #               check if we have a waitin status for reservations
298         if ( $num_res->{'found'} eq 'W' ) {
299             $getreserv{color}   = 'reserved';
300             $getreserv{waiting} = 1;
301         }
302
303         #               check transfers with the itemnumber foud in th reservation loop
304         if ($transfertwhen) {
305             $getreserv{color}      = 'transfered';
306             $getreserv{transfered} = 1;
307             $getreserv{datesent}   = format_date($transfertwhen);
308             $getreserv{frombranch} = GetBranchName($transfertfrom);
309         }
310
311         if ( ( $getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'} )
312             and not $transfertwhen )
313         {
314             $getreserv{nottransfered}   = 1;
315             $getreserv{nottransferedby} =
316               GetBranchName( $getiteminfo->{'holdingbranch'} );
317         }
318
319 #               if we don't have a reserv on item, we put the biblio infos and the waiting position
320         if ( $getiteminfo->{'title'} eq '' ) {
321             my $getbibinfo = GetBiblioItemData( $num_res->{'biblionumber'} );
322             my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );
323             $getreserv{color}           = 'inwait';
324             $getreserv{title}           = $getbibinfo->{'title'};
325             $getreserv{waitingposition} = $num_res->{'priority'};
326             $getreserv{nottransfered}   = 0;
327             $getreserv{itemtype}        = $getbibtype->{'description'};
328             $getreserv{author}          = $getbibinfo->{'author'};
329             $getreserv{itemcallnumber}  = '----------';
330              $getreserv{biblionumber}  = $num_res->{'biblionumber'};    
331         }
332
333         push( @reservloop, \%getreserv );
334     }
335
336     # return result to the template
337     $template->param( reservloop => \@reservloop );
338
339 }
340
341 # current alert subscriptions
342 my $alerts = getalert($borrowernumber);
343 foreach (@$alerts) {
344     $_->{ $_->{type} } = 1;
345     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
346 }
347 my $picture;
348 my $htdocs = C4::Context->config('intrahtdocs');
349 $picture = "/borrowerimages/" . $borrowernumber . ".jpg";
350 if ( -e $htdocs . "$picture" ) {
351     $template->param( picture => $picture );
352 }
353
354
355 $template->param($data);
356
357 $template->param(
358     roaddetails      => $roaddetails,
359     borrowernumber   => $borrowernumber,
360     reregistration   => $reregistration,
361     totalprice       => sprintf( "%.2f", $totalprice ),
362     totaldue         => sprintf( "%.2f", $total ),
363     issueloop        => \@issuedata,
364     unvalidlibrarian => $unvalidlibrarian,
365     
366     #            reserveloop     => \@reservedata,
367 );
368
369 output_html_with_http_headers $input, $cookie, $template->output;