Adding warnings to request/ files
[koha.git] / reserve / request.pl
1 #!/usr/bin/perl
2
3
4 #writen 2/1/00 by chris@katipo.oc.nz
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 =head1 request.pl
23
24 script to place reserves/requests
25
26 =cut
27
28 use strict;
29 use warnings;
30 use C4::Branch; # GetBranches get_branchinfos_of
31 use CGI;
32 use List::MoreUtils qw/uniq/;
33 use Date::Calc qw/Date_to_Days/;
34 use C4::Output;
35 use C4::Auth;
36 use C4::Reserves;
37 use C4::Biblio;
38 use C4::Items;
39 use C4::Koha;
40 use C4::Circulation;
41 use C4::Dates qw/format_date/;
42 use C4::Members;
43
44 my $dbh = C4::Context->dbh;
45 my $sth;
46 my $input = new CGI;
47 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
48     {
49         template_name   => "reserve/request.tmpl",
50         query           => $input,
51         type            => "intranet",
52         authnotrequired => 0,
53         flagsrequired   => { reserveforothers => 1 },
54     }
55 );
56
57 # get Branches and Itemtypes
58 my $branches = GetBranches();
59 my $itemtypes = GetItemTypes();
60
61 # get biblio information....
62 my $biblionumber = $input->param('biblionumber');
63 my $dat          = GetBiblioData($biblionumber);
64
65 # Select borrowers infos
66 my $findborrower = $input->param('findborrower');
67 $findborrower =~ s|,| |g;
68 my $cardnumber = $input->param('cardnumber');
69 my $borrowerslist;
70 my $messageborrower;
71 my $warnings;
72 my $messages;
73
74 my $date = C4::Dates->today('iso');
75
76 if ($findborrower) {
77     my ( $count, $borrowers ) =
78       SearchMember($findborrower, 'cardnumber', 'web' );
79
80     my @borrowers = @$borrowers;
81
82     if ( $#borrowers == -1 ) {
83         $input->param( 'findborrower', '' );
84         $messageborrower = "'$findborrower'";
85     }
86     elsif ( $#borrowers == 0 ) {
87         $input->param( 'cardnumber', $borrowers[0]->{'cardnumber'} );
88         $cardnumber = $borrowers[0]->{'cardnumber'};
89     }
90     else {
91         $borrowerslist = \@borrowers;
92     }
93 }
94
95 if ($cardnumber) {
96     my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
97     my $diffbranch;
98     my @getreservloop;
99     my $count_reserv = 0;
100     my $maxreserves;
101
102 #   we check the reserves of the borrower, and if he can reserv a document
103 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
104
105     my $number_reserves =
106       GetReserveCount( $borrowerinfo->{'borrowernumber'} );
107
108     if ( $number_reserves > C4::Context->preference('maxreserves') ) {
109                 $warnings = 1;
110         $maxreserves = 1;
111     }
112
113     # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
114     my $expiry_date = $borrowerinfo->{dateexpiry};
115     my $expiry = 0; # flag set if patron account has expired
116     if ($expiry_date and $expiry_date ne '0000-00-00' and
117             Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
118                 $messages = $expiry = 1;
119     }else{
120         $expiry = 0;
121     }
122
123
124     # check if the borrower make the reserv in a different branch
125     if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
126                 $messages = 1;
127         $diffbranch = 1;
128     }
129
130     $template->param(
131                 borrowernumber => $borrowerinfo->{'borrowernumber'},
132                 borrowersurname   => $borrowerinfo->{'surname'},
133                 borrowerfirstname => $borrowerinfo->{'firstname'},
134                 borrowerstreetaddress => $borrowerinfo->{'address'},
135                 borrowercity => $borrowerinfo->{'city'},
136                 borrowerphone => $borrowerinfo->{'phone'},
137                 borrowermobile => $borrowerinfo->{'mobile'},
138                 borrowerfax => $borrowerinfo->{'fax'},
139                 borrowerphonepro => $borrowerinfo->{'phonepro'},
140                 borroweremail => $borrowerinfo->{'email'},
141                 borroweremailpro => $borrowerinfo->{'emailpro'},
142                 borrowercategory => $borrowerinfo->{'category'},
143                 borrowerreservs   => $count_reserv,
144                 maxreserves       => $maxreserves,
145                 expiry            => $expiry,
146                 diffbranch        => $diffbranch,
147                                 messages => $messages,
148                                 warnings => $warnings
149     );
150 }
151
152 $template->param( messageborrower => $messageborrower );
153
154 my $CGIselectborrower;
155 if ($borrowerslist) {
156     my @values;
157     my %labels;
158
159     foreach my $borrower (
160         sort {
161                 $a->{surname}
162               . $a->{firstname} cmp $b->{surname}
163               . $b->{firstname}
164         } @{$borrowerslist}
165       )
166     {
167         push @values, $borrower->{cardnumber};
168
169         $labels{ $borrower->{cardnumber} } = sprintf(
170             '%s, %s ... (%s - %s) ... %s',
171             $borrower->{surname},    $borrower->{firstname},
172             $borrower->{cardnumber}, $borrower->{categorycode},
173             $borrower->{address},
174         );
175     }
176
177     $CGIselectborrower = CGI::scrolling_list(
178         -name     => 'cardnumber',
179         -values   => \@values,
180         -labels   => \%labels,
181         -size     => 7,
182         -multiple => 0,
183     );
184 }
185
186 # get existing reserves .....
187 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
188 my $totalcount = $count;
189 my $alreadyreserved;
190
191 # FIXME launch another time GetMemberDetails perhaps until
192 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
193
194 foreach my $res (@$reserves) {
195     if ( ( $res->{found} eq 'W' ) ) {
196         $count--;
197     }
198
199     if ( $borrowerinfo->{borrowernumber} eq $res->{borrowernumber} ) {
200                 $warnings = 1;
201         $alreadyreserved = 1;
202     }
203 }
204
205 $template->param( alreadyreserved => $alreadyreserved,
206                                 messages => $messages,
207                                 warnings => $warnings );
208
209 # FIXME think @optionloop, is maybe obsolete, or  must be switchable by a systeme preference fixed rank or not
210 # make priorities options
211
212 my @optionloop;
213 for ( 1 .. $count + 1 ) {
214     push(
215         @optionloop,
216         {
217             num      => $_,
218             selected => ( $_ == $count + 1 ),
219         }
220     );
221 }
222 # adding a fixed value for priority options
223 my $fixedRank = $count+1;
224
225 my @branchcodes;
226 my %itemnumbers_of_biblioitem;
227 my @itemnumbers;
228
229 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
230         @itemnumbers  = @$items;
231 }
232 else {
233         $template->param('noitems' => 1);
234 }
235         
236 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
237
238 foreach my $itemnumber (@itemnumbers) {
239     my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
240     push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
241 }
242
243 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
244
245 my $notforloan_label_of = get_notforloan_label_of();
246 my $biblioiteminfos_of  = GetBiblioItemInfosOf(@biblioitemnumbers);
247
248 my @bibitemloop;
249
250 foreach my $biblioitemnumber (@biblioitemnumbers) {
251     my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
252
253     $biblioitem->{description} =
254       $itemtypes->{ $biblioitem->{itemtype} }{description};
255
256     foreach
257       my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
258     {
259         my $item = $iteminfos_of->{$itemnumber};
260         unless (C4::Context->preference('item-level_itypes')) {
261             $item->{itype} = $biblioitem->{itemtype};
262         }
263
264         $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
265         $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
266         $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
267
268         # if the holdingbranch is different than the homebranch, we show the
269         # holdingbranch of the document too
270         if ( $item->{homebranch} ne $item->{holdingbranch} ) {
271             $item->{holdingbranchname} =
272               $branches->{ $item->{holdingbranch} }{branchname};
273         }
274         
275 #   add information
276     $item->{itemcallnumber} = $item->{itemcallnumber};
277     
278         # if the item is currently on loan, we display its return date and
279         # change the background color
280         my $issues= GetItemIssue($itemnumber);
281         if ( $issues->{'date_due'} ) {
282             $item->{date_due} = format_date($issues->{'date_due'});
283             $item->{backgroundcolor} = 'onloan';
284         }
285
286         # checking reserve
287         my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
288         my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
289
290         if ( defined $reservedate ) {
291             $item->{backgroundcolor} = 'reserved';
292             $item->{reservedate}     = format_date($reservedate);
293             $item->{ReservedForBorrowernumber}     = $reservedfor;
294             $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
295             $item->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
296             $item->{ExpectedAtLibrary}     = $branches->{$expectedAt}{branchname};
297             
298         }
299
300         # Management of the notforloan document
301         if ( $item->{notforloan} ) {
302             $item->{backgroundcolor} = 'other';
303             $item->{notforloanvalue} =
304               $notforloan_label_of->{ $item->{notforloan} };
305         }
306
307         # Management of lost or long overdue items
308         if ( $item->{itemlost} ) {
309
310             # FIXME localized strings should never be in Perl code
311             $item->{message} =
312                 $item->{itemlost} == 1 ? "(lost)"
313               : $item->{itemlost} == 2 ? "(long overdue)"
314               : "";
315             $item->{backgroundcolor} = 'other';
316         }
317
318         # Check the transit status
319         my ( $transfertwhen, $transfertfrom, $transfertto ) =
320           GetTransfers($itemnumber);
321
322         if ( $transfertwhen ne '' ) {
323             $item->{transfertwhen} = format_date($transfertwhen);
324             $item->{transfertfrom} =
325               $branches->{$transfertfrom}{branchname};
326             $item->{transfertto} = $branches->{$transfertto}{branchname};
327         $item->{nocancel} = 1;
328         }
329
330         # If there is no loan, return and transfer, we show a checkbox.
331         $item->{notforloan} = $item->{notforloan} || 0;
332     
333     # if independent branches is on we need to check if the person can reserve
334     # for branches they arent logged in to
335     if ( C4::Context->preference("IndependantBranches") ) { 
336         if (! C4::Context->preference("canreservefromotherbranches")){
337         # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
338         my $userenv = C4::Context->userenv; 
339         if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
340             $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
341         } 
342         }
343     }
344
345     if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve}) {
346         $item->{available} = 1;
347     }
348
349     # FIXME: move this to a pm
350     my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
351     $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
352     while (my $wait_hashref = $sth2->fetchrow_hashref) {
353         $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
354     }
355         push @{ $biblioitem->{itemloop} }, $item;
356     }
357
358     push @bibitemloop, $biblioitem;
359 }
360
361 # existingreserves building
362 my @reserveloop;
363 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
364 foreach my $res ( sort { $a->{found} cmp $b->{found} } @$reserves ) {
365     my %reserve;
366     my @optionloop;
367     for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
368         push(
369             @optionloop,
370             {
371                 num      => $i,
372                 selected => ( $i == $res->{priority} ),
373             }
374         );
375     }
376     my @branchloop;
377     foreach my $br ( keys %$branches ) {
378         my %abranch;
379         $abranch{'selected'}   = ( $br eq $res->{'branchcode'} );
380         $abranch{'branch'}     = $br;
381         $abranch{'branchname'} = $branches->{$br}->{'branchname'};
382         push( @branchloop, \%abranch );
383     }
384
385     if ( ( $res->{'found'} eq 'W' ) ) {
386         my $item = $res->{'itemnumber'};
387         $item = GetBiblioFromItemNumber($item,undef);
388         $reserve{'wait'}= 1; 
389         $reserve{'holdingbranch'}=$item->{'holdingbranch'};
390         $reserve{'biblionumber'}=$item->{'biblionumber'};
391         $reserve{'barcodenumber'}   = $item->{'barcode'};
392         $reserve{'wbrcode'} = $res->{'branchcode'};
393         $reserve{'itemnumber'}  = $res->{'itemnumber'};
394         $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
395         if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
396             $reserve{'atdestination'} = 1;
397         }
398         # set found to 1 if reserve is waiting for patron pickup
399         $reserve{'found'} = 1 if $res->{'found'} eq 'W';
400     } elsif ($res->{priority} > 0) {
401         if (defined($res->{itemnumber})) {
402             my $item = GetItem($res->{itemnumber});
403             $reserve{'itemnumber'}  = $res->{'itemnumber'};
404             $reserve{'barcodenumber'}   = $item->{'barcode'};
405             $reserve{'item_level_hold'} = 1;
406         }
407     }
408     
409 #     get borrowers reserve info
410 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
411
412     $reserve{'date'}           = format_date( $res->{'reservedate'} );
413     $reserve{'borrowernumber'} = $res->{'borrowernumber'};
414     $reserve{'biblionumber'}   = $res->{'biblionumber'};
415     $reserve{'borrowernumber'} = $res->{'borrowernumber'};
416     $reserve{'firstname'}      = $reserveborrowerinfo->{'firstname'};
417     $reserve{'surname'}        = $reserveborrowerinfo->{'surname'};
418     $reserve{'notes'}          = $res->{'reservenotes'};
419     $reserve{'wait'}           =
420       ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
421     $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
422     $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
423     $reserve{'voldesc'}         = $res->{'volumeddesc'};
424     $reserve{'ccode'}           = $res->{'ccode'};
425     $reserve{'barcode'}         = $res->{'barcode'};
426     $reserve{'priority'}    = $res->{'priority'};
427     $reserve{'branchloop'} = \@branchloop;
428     $reserve{'optionloop'} = \@optionloop;
429
430     push( @reserveloop, \%reserve );
431 }
432
433 my $default = C4::Context->userenv->{branch};
434 my @values;
435 my %label_of;
436
437 foreach my $branchcode (sort keys %{$branches} ) {
438     push @values, $branchcode;
439     $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
440 }
441 my $CGIbranch = CGI::scrolling_list(
442     -name     => 'pickup',
443     -id          => 'pickup',
444     -values   => \@values,
445     -default  => $default,
446     -labels   => \%label_of,
447     -size     => 1,
448     -multiple => 0,
449 );
450
451 # get the time for the form name...
452 my $time = time();
453
454 $template->param(
455     CGIbranch   => $CGIbranch,
456     reserveloop => \@reserveloop,
457     time        => $time,
458     fixedRank   => $fixedRank,
459 );
460
461 # display infos
462 $template->param(
463     optionloop        => \@optionloop,
464     bibitemloop       => \@bibitemloop,
465     date              => $date,
466     biblionumber      => $biblionumber,
467     findborrower      => $findborrower,
468     cardnumber        => $cardnumber,
469     CGIselectborrower => $CGIselectborrower,
470     title             => $dat->{title},
471     author            => $dat->{author},
472         holdsview => 1,
473         borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
474         borrower_branchcode => $borrowerinfo->{'branchcode'},
475 );
476
477 # printout the page
478 output_html_with_http_headers $input, $cookie, $template->output;