bug fix : for item reservation, now the system don't keep the reserve if the document...
[koha.git] / circ / circulation.pl
1 #!/usr/bin/perl
2
3 # Please use 8-character tabs for this file (indents are every 4 characters)
4
5 # written 8/5/2002 by Finlay
6 # script to execute issuing of books
7
8 # Copyright 2000-2002 Katipo Communications
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA  02111-1307 USA
24
25 use strict;
26 use CGI;
27 use C4::Circulation::Circ2;
28 use C4::Search;
29 use C4::Members;
30 use C4::Output;
31 use C4::Print;
32 use DBI;
33 use C4::Auth;
34 use C4::Interface::CGI::Output;
35 use C4::Koha;
36 use HTML::Template;
37 use C4::Date;
38 use Date::Manip;
39 use C4::Biblio;
40 use C4::Reserves2;
41
42 #
43 # PARAMETERS READING
44 #
45 my $query = new CGI;
46
47 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
48     {
49         template_name   => 'circ/circulation.tmpl',
50         query           => $query,
51         type            => "intranet",
52         authnotrequired => 0,
53         flagsrequired   => { circulate => 1 },
54     }
55 );
56 my $branches = getbranches();
57 # my $printers = getprinters();
58 # my $printer = getprinter($query, $printers);
59
60 my $findborrower = $query->param('findborrower');
61 $findborrower =~ s|,| |g;
62 $findborrower =~ s|'| |g;
63 my $borrowernumber = $query->param('borrnumber');
64 # new op dev the branch and the printer are now defined by the userenv
65 my $branch = C4::Context->userenv->{'branch'};
66 my $printer=C4::Context->userenv->{'branchprinter'};
67
68 my $barcode = $query->param('barcode') || '';
69 my $year=$query->param('year');
70 my $month=$query->param('month');
71 my $day=$query->param('day');
72 my $stickyduedate=$query->param('stickyduedate');
73 my $issueconfirmed = $query->param('issueconfirmed');
74 my $cancelreserve  = $query->param('cancelreserve');
75 my $organisation   = $query->param('organisations');
76 my $print = $query->param('print');
77
78 #set up cookie.....
79 # my $branchcookie;
80 # my $printercookie;
81 # if ($query->param('setcookies')) {
82 #       $branchcookie = $query->cookie(-name=>'branch', -value=>"$branch", -expires=>'+1y');
83 #       $printercookie = $query->cookie(-name=>'printer', -value=>"$printer", -expires=>'+1y');
84 # }
85
86 my %env; # FIXME env is used as an "environment" variable. Could be dropped probably...
87 #
88 my $print; 
89 $env{'branchcode'}= $branch;
90 $env{'printer'}= $printer;
91 # $env{'queue'}=$printer;
92
93 my @datearr = localtime(time());
94 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
95 my $todaysdate =
96     ( 1900 + $datearr[5] )
97   . sprintf( "%0.2d", ( $datearr[4] + 1 ) )
98   . sprintf( "%0.2d", ( $datearr[3] ) );
99
100 # check and see if we should print
101 if ( $barcode eq '' && $print eq 'maybe' ) {
102     $print = 'yes';
103 }
104 if ( $print eq 'yes' && $borrowernumber ne '' ) {
105     printslip( \%env, $borrowernumber );
106     $query->param( 'borrnumber', '' );
107     $borrowernumber = '';
108 }
109
110 #
111 # STEP 2 : FIND BORROWER
112 # if there is a list of find borrowers....
113 #
114 my $borrowerslist;
115 my $message;
116 if ($findborrower) {
117     my ( $count, $borrowers ) =
118       BornameSearch( \%env, $findborrower, 'cardnumber', 'web' );
119     my @borrowers = @$borrowers;
120     if ( $#borrowers == -1 ) {
121         $query->param( 'findborrower', '' );
122         $message = "'$findborrower'";
123     }
124     elsif ( $#borrowers == 0 ) {
125         $query->param( 'borrnumber', $borrowers[0]->{'borrowernumber'} );
126         $query->param( 'barcode',    '' );
127         $borrowernumber = $borrowers[0]->{'borrowernumber'};
128     }
129     else {
130         $borrowerslist = \@borrowers;
131     }
132 }
133
134 # get the borrower information.....
135 my $borrower;
136 my $picture;
137
138 if ($borrowernumber) {
139     $borrower = getpatroninformation( \%env, $borrowernumber, 0 );
140     my ( $od, $issue, $fines ) = borrdata2( \%env, $borrowernumber );
141     my $warningdate =
142       DateCalc( $borrower->{'expiry'},
143         "- " . C4::Context->preference('NotifyBorrowerDeparture') . "  days" );
144     my $warning = Date_Cmp( ParseDate("today"), $warningdate );
145     if ( $warning > 0 ) {
146
147         #borrowercard expired
148         $template->param( warndeparture => $warning );
149     }
150     $template->param(
151         overduecount => $od,
152         issuecount   => $issue,
153         finetotal    => $fines
154     );
155     my $htdocs = C4::Context->config('intrahtdocs');
156     $picture = "/borrowerimages/" . $borrowernumber . ".jpg";
157     if ( -e $htdocs . "$picture" ) {
158         $template->param( picture => $picture );
159     }
160 }
161
162 #
163 # STEP 3 : ISSUING
164 #
165 #
166
167 if ($barcode) {
168     $barcode = cuecatbarcodedecode($barcode);
169     my ( $datedue, $invalidduedate ) = fixdate( $year, $month, $day );
170     if ($issueconfirmed) {
171         issuebook( \%env, $borrower, $barcode, $datedue, $cancelreserve );
172     }
173     else {
174         my ( $error, $question ) =
175           canbookbeissued( \%env, $borrower, $barcode, $year, $month, $day );
176         my $noerror    = 1;
177         my $noquestion = 1;
178         foreach my $impossible ( keys %$error ) {
179             $template->param(
180                 $impossible => $$error{$impossible},
181                 IMPOSSIBLE  => 1
182             );
183             $noerror = 0;
184         }
185         foreach my $needsconfirmation ( keys %$question ) {
186             $template->param(
187                 $needsconfirmation => $$question{$needsconfirmation},
188                 NEEDSCONFIRMATION  => 1
189             );
190             $noquestion = 0;
191         }
192         $template->param(
193             day   => $day,
194             month => $month,
195             year  => $year
196         );
197         if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
198             issuebook( \%env, $borrower, $barcode, $datedue );
199         }
200     }
201 }
202
203 # reload the borrower info for the sake of reseting the flags.....
204 if ($borrowernumber) {
205     $borrower = getpatroninformation( \%env, $borrowernumber, 0 );
206 }
207
208 ##################################################################################
209 # BUILD HTML
210 # show all reserves of this borrower, and the position of the reservation ....
211 if ($borrowernumber) {
212 # new op dev
213 # now we show the status of the borrower's reservations
214         my @borrowerreserv = FastFindReserves(0,$borrowernumber);
215         my @reservloop;
216         foreach my $num_res (@borrowerreserv) {
217                 my %getreserv;
218                 my %env;
219                 my $getiteminfo = getiteminformation(\%env,$num_res->{'itemnumber'});
220                 my $itemtypeinfo = getitemtypeinfo($getiteminfo->{'itemtype'});
221                 my ($transfertwhen,$transfertfrom,$transfertto) = checktransferts($num_res->{'itemnumber'});
222
223                 $getreserv{waiting} = 0;
224                 $getreserv{transfered} = 0;
225                 $getreserv{nottransfered} = 0;
226
227                 $getreserv{reservedate} = format_date($num_res->{'reservedate'});
228                 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
229                 $getreserv{title} = $getiteminfo->{'title'};
230                 $getreserv{itemtype} = $itemtypeinfo->{'description'};
231                 $getreserv{author} = $getiteminfo->{'author'};
232                 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
233                 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
234 #               check if we have a waitin status for reservations
235                 if ($num_res->{'found'} eq 'W'){
236                         $getreserv{color} = 'reserved';
237                         $getreserv{waiting} = 1; 
238                 }
239
240 #               check transfers with the itemnumber foud in th reservation loop
241                 if ($transfertwhen){
242                 $getreserv{color} = 'transfered';
243                 $getreserv{transfered} = 1;
244                 $getreserv{datesent} = format_date($transfertwhen);
245                 $getreserv{frombranch} = getbranchname($transfertfrom);
246                 }
247
248                 if (($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'}) and not $transfertwhen){
249                 $getreserv{nottransfered} = 1;
250                 $getreserv{nottransferedby} = getbranchname($getiteminfo->{'holdingbranch'});
251                 }
252
253 #               if we don't have a reserv on item, we put the biblio infos and the waiting position     
254                 if ($getiteminfo->{'title'} eq '' ){
255                         my $getbibinfo = bibitemdata($num_res->{'biblionumber'});
256                         my $getbibtype = getitemtypeinfo($getbibinfo->{'itemtype'});
257                         $getreserv{color} = 'inwait';
258                         $getreserv{title} = $getbibinfo->{'title'};
259                         $getreserv{waitingposition} = $num_res->{'priority'};
260                         $getreserv{nottransfered} = 0;
261                         $getreserv{itemtype} = $getbibtype->{'description'};
262                         $getreserv{author} = $getbibinfo->{'author'};
263                         $getreserv{itemcallnumber} = '----------';
264                         
265                 }
266
267                 push(@reservloop, \%getreserv);
268         }
269         # return result to the template
270         $template->param(reservloop => \@reservloop);
271
272 }
273
274
275 # make the issued books table.....
276 my $todaysissues = '';
277 my $previssues   = '';
278 my @realtodayissues;
279 my @realprevissues;
280 my $allowborrow;
281 if ($borrower) {
282
283 # get each issue of the borrower & separate them in todayissues & previous issues
284     my @todaysissues;
285     my @previousissues;
286     my $issueslist = getissues($borrower);
287
288     # split in 2 arrays for today & previous
289     foreach my $it ( keys %$issueslist ) {
290         my $issuedate = $issueslist->{$it}->{'timestamp'};
291         $issuedate =~ s/-//g;
292         $issuedate = substr( $issuedate, 0, 8 );
293         if ( $todaysdate == $issuedate ) {
294             push @todaysissues, $issueslist->{$it};
295         }
296         else {
297             push @previousissues, $issueslist->{$it};
298         }
299     }
300     my $od;    # overdues
301     my $i = 0;
302     my $togglecolor;
303
304     # parses today & build Template array
305     foreach my $book ( sort { $b->{'timestamp'} <=> $a->{'timestamp'} }
306         @todaysissues )
307     {
308         my $dd      = $book->{'date_due'};
309         my $datedue = $book->{'date_due'};
310         $dd = format_date($dd);
311         $datedue =~ s/-//g;
312         if ( $datedue < $todaysdate ) {
313             $od = 1;
314         }
315         else {
316             $od = 0;
317         }
318         if ( $i % 2 ) {
319             $togglecolor = 0;
320         }
321         else {
322             $togglecolor = 1;
323         }
324         $book->{'togglecolor'} = $togglecolor;
325         $book->{'od'}          = $od;
326         $book->{'dd'}          = $dd;
327         if ( $book->{'author'} eq '' ) {
328             $book->{'author'} = ' ';
329         }
330         push @realtodayissues, $book;
331         $i++;
332     }
333
334     # parses previous & build Template array
335     $i = 0;
336     foreach my $book ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
337         @previousissues )
338     {
339         my $dd      = $book->{'date_due'};
340         my $datedue = $book->{'date_due'};
341         $dd = format_date($dd);
342         my $pcolor = '';
343         my $od     = '';
344         $datedue =~ s/-//g;
345         if ( $datedue < $todaysdate ) {
346             $od = 1;
347         }
348         else {
349             $od = 0;
350         }
351         if ( $i % 2 ) {
352             $togglecolor = 0;
353         }
354         else {
355             $togglecolor = 1;
356         }
357         $book->{'togglecolor'} = $togglecolor;
358         $book->{'dd'}          = $dd;
359         $book->{'od'}          = $od;
360         if ( $book->{'author'} eq '' ) {
361             $book->{'author'} = ' ';
362         }
363         push @realprevissues, $book;
364         $i++;
365     }
366 }
367
368 my @values;
369 my %labels;
370 my $CGIselectborrower;
371 if ($borrowerslist) {
372     foreach (
373         sort {
374             $a->{'surname'}
375               . $a->{'firstname'} cmp $b->{'surname'}
376               . $b->{'firstname'}
377         } @$borrowerslist
378       )
379     {
380         push @values, $_->{'borrowernumber'};
381         $labels{ $_->{'borrowernumber'} } =
382 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ...  $_->{'streetaddress'} ";
383     }
384     $CGIselectborrower = CGI::scrolling_list(
385         -name     => 'borrnumber',
386         -values   => \@values,
387         -labels   => \%labels,
388         -size     => 7,
389         -multiple => 0
390     );
391 }
392
393 #title
394
395 my ( $patrontable, $flaginfotable ) = patrontable($borrower);
396 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
397 my @temp = split( /\$/, $amountold );
398
399 my $CGIorganisations;
400 my $member_of_institution;
401 if ( C4::Context->preference("memberofinstitution") ) {
402     my $organisations = get_institutions();
403     my @orgs;
404     my %org_labels;
405     foreach my $organisation ( keys %$organisations ) {
406         push @orgs, $organisation;
407         $org_labels{$organisation} =
408           $organisations->{$organisation}->{'surname'};
409     }
410     $member_of_institution = 1;
411     $CGIorganisations = CGI::popup_menu(
412         -id       => 'organisations',
413         -name     => 'organisations',
414         -labels   => \%org_labels,
415         -values   => \@orgs,
416
417     );
418 }
419
420 $amountold = $temp[1];
421 $template->param(
422     findborrower      => $findborrower,
423     borrower          => $borrower,
424     borrowernumber    => $borrowernumber,
425     branch            => $branch,
426     printer           => $printer,
427     printername       => $printer,
428     firstname         => $borrower->{'firstname'},
429     surname           => $borrower->{'surname'},
430     categorycode      => $borrower->{'categorycode'},
431     streetaddress     => $borrower->{'streetaddress'},
432     emailaddress      => $borrower->{'emailaddress'},
433     borrowernotes     => $borrower->{'borrowernotes'},
434     city              => $borrower->{'city'},
435     phone             => $borrower->{'phone'},
436     cardnumber        => $borrower->{'cardnumber'},
437     amountold         => $amountold,
438     barcode           => $barcode,
439     stickyduedate     => $stickyduedate,
440     message           => $message,
441     CGIselectborrower => $CGIselectborrower,
442     todayissues       => \@realtodayissues,
443     previssues        => \@realprevissues,
444     memberofinstution => $member_of_institution,                                                                 
445     CGIorganisations => $CGIorganisations, 
446 );
447
448 # set return date if stickyduedate
449 if ($stickyduedate) {
450     my $t_year  = "year" . $year;
451     my $t_month = "month" . $month;
452     my $t_day   = "day" . $day;
453     $template->param(
454         $t_year  => 1,
455         $t_month => 1,
456         $t_day   => 1,
457     );
458 }
459
460
461 # if ($branchcookie) {
462 #     $cookie=[$cookie, $branchcookie, $printercookie];
463 # }
464
465 output_html_with_http_headers $query, $cookie, $template->output;
466
467 ####################################################################
468 # Extra subroutines,,,
469
470 sub patrontable {
471     my ($borrower)    = @_;
472     my $flags         = $borrower->{'flags'};
473     my $flaginfotable = '';
474     my $flaginfotext;
475
476     #my $flaginfotext='';
477     my $flag;
478     my $color = '';
479     foreach $flag ( sort keys %$flags ) {
480
481         #       my @itemswaiting='';
482         $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
483         if ( $flags->{$flag}->{'noissues'} ) {
484             $template->param(
485                 flagged  => 1,
486                 noissues => 'true',
487             );
488             if ( $flag eq 'GNA' ) {
489                 $template->param( gna => 'true' );
490             }
491             if ( $flag eq 'LOST' ) {
492                 $template->param( lost => 'true' );
493             }
494             if ( $flag eq 'DBARRED' ) {
495                 $template->param( dbarred => 'true' );
496             }
497             if ( $flag eq 'CHARGES' ) {
498                 $template->param(
499                     charges    => 'true',
500                     chargesmsg => $flags->{'CHARGES'}->{'message'}
501                 );
502             }
503         }
504         else {
505             if ( $flag eq 'CHARGES' ) {
506                 $template->param(
507                     charges    => 'true',
508                     flagged    => 1,
509                     chargesmsg => $flags->{'CHARGES'}->{'message'}
510                 );
511             }
512 # FIXME this part can be removed if we keep new display of reserves "reservloop"
513 #             if ( $flag eq 'WAITING' ) {
514 #                 my $items = $flags->{$flag}->{'itemlist'};
515 #                 my @itemswaiting;
516 #                 foreach my $item (@$items) {
517 #                     my ($iteminformation) =
518 #                       getiteminformation( \%env, $item->{'itemnumber'}, 0 );
519 #                     $iteminformation->{'branchname'} =
520 #                       $branches->{ $iteminformation->{'holdingbranch'} }
521 #                       ->{'branchname'};
522 #                     push @itemswaiting, $iteminformation;
523 #                 }
524 #                 $template->param(
525 #                     flagged      => 1,
526 #                     waiting      => 'true',
527 #                     waitingmsg   => $flags->{'WAITING'}->{'message'},
528 #                     itemswaiting => \@itemswaiting,
529 #                 );
530 #             }
531             if ( $flag eq 'ODUES' ) {
532                 $template->param(
533                     odues    => 'true',
534                     flagged  => 1,
535                     oduesmsg => $flags->{'ODUES'}->{'message'}
536                 );
537
538                 my $items = $flags->{$flag}->{'itemlist'};
539                 {
540                     my @itemswaiting;
541                     foreach my $item (@$items) {
542                         my ($iteminformation) =
543                           getiteminformation( \%env, $item->{'itemnumber'}, 0 );
544                         push @itemswaiting, $iteminformation;
545                     }
546                 }
547                 if ( $query->param('module') ne 'returns' ) {
548                     $template->param( nonreturns => 'true' );
549                 }
550             }
551             if ( $flag eq 'NOTES' ) {
552                 $template->param(
553                     notes    => 'true',
554                     flagged  => 1,
555                     notesmsg => $flags->{'NOTES'}->{'message'}
556                 );
557             }
558         }
559     }
560     return ( $patrontable, $flaginfotext );
561 }
562
563 sub cuecatbarcodedecode {
564     my ($barcode) = @_;
565     chomp($barcode);
566     my @fields = split( /\./, $barcode );
567     my @results = map( decode($_), @fields[ 1 .. $#fields ] );
568     if ( $#results == 2 ) {
569         return $results[2];
570     }
571     else {
572         return $barcode;
573     }
574 }
575
576 # Local Variables:
577 # tab-width: 8
578 # End: