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