add control of 'return date' if it 's a special or repeatable holidays , and return...
[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 CGI;
37 use Date::Manip;
38 use C4::Auth;
39 use C4::Context;
40 use C4::Output;
41 use C4::Members;
42 use C4::Date;
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
51 my $dbh = C4::Context->dbh;
52
53 my $input = new CGI;
54 my $print = $input->param('print');
55 my $template_name;
56
57 if ( $print eq "page" ) {
58     $template_name = "members/moremember-print.tmpl";
59 }
60 elsif ( $print eq "slip" ) {
61     $template_name = "members/moremember-receipt.tmpl";
62 }
63 else {
64     $template_name = "members/moremember.tmpl";
65 }
66
67 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
68     {
69         template_name   => $template_name,
70         query           => $input,
71         type            => "intranet",
72         authnotrequired => 0,
73         flagsrequired   => { borrowers => 1 },
74         debug           => 1,
75     }
76 );
77 my $borrowernumber = $input->param('borrowernumber');
78
79 #start the page and read in includes
80 my $data           = GetMember( $borrowernumber ,'borrowernumber');
81 my $reregistration = $input->param('reregistration');
82
83 if ( not defined $data ) {
84     $template->param (
85         unknowuser => 1
86     );
87     output_html_with_http_headers $input, $cookie, $template->output;
88     exit;
89 }
90
91 # re-reregistration function to automatic calcul of date expiry
92 (
93 #     $data->{'dateexpiry'} = GetMembeReregistration(
94  $data->{'dateexpiry'} = ExtendMemberSubscriptionTo(
95         $data->{'categorycode'},
96         $borrowernumber, $data->{'dateenrolled'}
97     )
98 ) if ( $reregistration eq 'y' );
99 my $borrowercategory = GetBorrowercategory( $data->{'categorycode'} );
100 my $category_type = $borrowercategory->{'category_type'};
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->{ "sex_".$data->{'sex'}."_p" } = 1;
121
122 if ( $category_type eq 'C' and $data->{'guarantorid'} ne '0' ) {
123     my $data2 = GetMember( $data->{'guarantorid'} ,'borrowernumber');
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 ) = GetGuarantees( $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     if ($data->{'guarantorid'}){
162       my ($guarantor) = GetMember( $data->{'guarantorid'},'biblionumber');
163       $template->param( 
164               guarantor => 1,
165               guarantorborrowernumber => $guarantor->{'borrowernumber'},
166               guarantorcardnumber     => $guarantor->{'cardnumber'},
167               guarantorfirstname      => $guarantor->{'firstname'},
168               guarantorsurname        => $guarantor->{'surname'}
169           );
170     }
171 }
172
173 #Independant branches management
174 my $unvalidlibrarian =
175   (      ( C4::Context->preference("IndependantBranches") )
176       && ( C4::Context->userenv->{flags} != 1 )
177       && ( $data->{'branchcode'} ne C4::Context->userenv->{branch} ) );
178
179 my %bor;
180 $bor{'borrowernumber'} = $borrowernumber;
181
182 # Converts the branchcode to the branch name
183 my $samebranch;
184 if ( C4::Context->preference("IndependantBranches") ) {
185     my $userenv = C4::Context->userenv;
186     unless ( $userenv->{flags} == 1 ) {
187         $samebranch = ( $data->{'branchcode'} eq $userenv->{branch} );
188     }
189     $samebranch = 1 if ( $userenv->{flags} == 1 );
190 }
191
192 $data->{'branchname'} =
193   ( ( GetBranchDetail( $data->{'branchcode'} ) )->{'branchname'} );
194
195
196 my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
197 my $lib1 = &GetSortDetails( "Bsort1", $data->{'sort1'} );
198 my $lib2 = &GetSortDetails( "Bsort2", $data->{'sort2'} );
199 ( $template->param( lib1 => $lib1 ) ) if ($lib1);
200 ( $template->param( lib2 => $lib2 ) ) if ($lib2);
201
202 # current issues
203 #
204 my ( $count, $issue ) = GetPendingIssues($borrowernumber);
205 my $roaddetails = &GetRoadTypeDetails( $data->{'streettype'} );
206 my $today       = ParseDate('today');
207 my @issuedata;
208 my $totalprice = 0;
209 my $toggle     = 0;
210 for ( my $i = 0 ; $i < $count ; $i++ ) {
211     my $datedue = ParseDate( $issue->[$i]{'date_due'} );
212     $issue->[$i]{'date_due'} = format_date( $issue->[$i]{'date_due'} );
213     my %row = %{ $issue->[$i] };
214     $totalprice += $issue->[$i]{'replacementprice'};
215     $row{'replacementprice'} = $issue->[$i]{'replacementprice'};
216     if ( $datedue < $today ) {
217         $row{'red'} = 1;    #print "<font color=red>";
218     }
219     $row{toggle} = $toggle++ % 2;
220
221     #find the charge for an item
222     my ( $charge, $itemtype ) =
223       GetIssuingCharges( $issue->[$i]{'itemnumber'}, $borrowernumber );
224
225     my $itemtypeinfo = getitemtypeinfo($itemtype);
226     $row{'itemtype_description'} = $itemtypeinfo->{description};
227     $row{'itemtype_image'}       = $itemtypeinfo->{imageurl};
228
229     $row{'charge'} = sprintf( "%.2f", $charge );
230
231     #check item is not reserved
232     my ( $restype, $reserves ) = CheckReserves( $issue->[$i]{'itemnumber'} );
233     if ($restype) {
234         $row{'norenew'} = 1;
235     }
236     else {
237         $row{'norenew'} = 0;
238     }
239     push( @issuedata, \%row );
240 }
241
242 ##################################################################################
243 # BUILD HTML
244 # show all reserves of this borrower, and the position of the reservation ....
245 if ($borrowernumber) {
246
247     # new op dev
248     # now we show the status of the borrower's reservations
249     my @borrowerreserv = GetReservesFromBorrowernumber($borrowernumber );
250     my @reservloop;
251     foreach my $num_res (@borrowerreserv) {
252         eval{
253             scalar @$num_res;
254         };
255         if($@){
256             next;
257         }
258     
259         my %getreserv;
260         
261         my $getiteminfo  = GetBiblioFromItemNumber( $num_res->{'itemnumber'} );
262         my $itemtypeinfo = getitemtypeinfo( $getiteminfo->{'itemtype'} );
263         my ( $transfertwhen, $transfertfrom, $transfertto ) =
264             GetTransfers( $num_res->{'itemnumber'} );
265
266         $getreserv{waiting}       = 0;
267         $getreserv{transfered}    = 0;
268         $getreserv{nottransfered} = 0;
269
270         $getreserv{reservedate}    = format_date( $num_res->{'reservedate'} );
271         $getreserv{biblionumber}   = $getiteminfo->{'biblionumber'};
272         $getreserv{title}          = $getiteminfo->{'title'};
273         $getreserv{itemtype}       = $itemtypeinfo->{'description'};
274         $getreserv{author}         = $getiteminfo->{'author'};
275         $getreserv{barcodereserv}  = $getiteminfo->{'barcode'};
276         $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
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}   = format_date($transfertwhen);
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 = GetBiblioItemData( $num_res->{'biblionumber'} );
303             my $getbibtype = getitemtypeinfo( $getbibinfo->{'itemtype'} );
304             $getreserv{color}           = 'inwait';
305             $getreserv{title}           = $getbibinfo->{'title'};
306             $getreserv{waitingposition} = $num_res->{'priority'};
307             $getreserv{nottransfered}   = 0;
308             $getreserv{itemtype}        = $getbibtype->{'description'};
309             $getreserv{author}          = $getbibinfo->{'author'};
310             $getreserv{itemcallnumber}  = '----------';
311             $getreserv{biblionumber}  = $num_res->{'biblionumber'};     
312         }
313
314         push( @reservloop, \%getreserv );
315     }
316
317     # return result to the template
318     $template->param( reservloop => \@reservloop );
319
320 }
321
322 # current alert subscriptions
323 my $alerts = getalert($borrowernumber);
324 foreach (@$alerts) {
325     $_->{ $_->{type} } = 1;
326     $_->{relatedto} = findrelatedto( $_->{type}, $_->{externalid} );
327 }
328 my $picture;
329 my $htdocs = C4::Context->config('intrahtdocs');
330 $picture = "/borrowerimages/" . $borrowernumber . ".jpg";
331 if ( -e $htdocs . "$picture" ) {
332     $template->param( picture => $picture );
333 }
334 my $branch=C4::Context->userenv->{'branch'};
335
336 $template->param($data);
337
338 $template->param(
339     roaddetails      => $roaddetails,
340     borrowernumber   => $borrowernumber,
341     reregistration   => $reregistration,
342     branch           => $branch,        
343     totalprice       => sprintf( "%.2f", $totalprice ),
344     totaldue         => sprintf( "%.2f", $total ),
345     issueloop        => \@issuedata,
346     unvalidlibrarian => $unvalidlibrarian,
347     
348     #            reserveloop     => \@reservedata,
349 );
350
351 output_html_with_http_headers $input, $cookie, $template->output;