]> git.koha-community.org Git - koha.git/blob - circ/circulation.pl
Improvements for reserves infos in circulation.pl
[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     warn $borrower->{'expiry'};
142     my $warningdate =
143       DateCalc( $borrower->{'expiry'},
144         "- " . C4::Context->preference('NotifyBorrowerDeparture') . "  days" );
145     my $warning = Date_Cmp( ParseDate("today"), $warningdate );
146     if ( $warning > 0 ) {
147
148         #borrowercard expired
149         $template->param( warndeparture => $warning );
150     }
151     $template->param(
152         overduecount => $od,
153         issuecount   => $issue,
154         finetotal    => $fines
155     );
156     my $htdocs = C4::Context->config('intrahtdocs');
157     $picture = "/borrowerimages/" . $borrowernumber . ".jpg";
158     if ( -e $htdocs . "$picture" ) {
159         $template->param( picture => $picture );
160     }
161 }
162
163 #
164 # STEP 3 : ISSUING
165 #
166 #
167
168 if ($barcode) {
169     $barcode = cuecatbarcodedecode($barcode);
170     my ( $datedue, $invalidduedate ) = fixdate( $year, $month, $day );
171     if ($issueconfirmed) {
172         issuebook( \%env, $borrower, $barcode, $datedue, $cancelreserve );
173     }
174     else {
175         my ( $error, $question ) =
176           canbookbeissued( \%env, $borrower, $barcode, $year, $month, $day );
177         my $noerror    = 1;
178         my $noquestion = 1;
179         foreach my $impossible ( keys %$error ) {
180             $template->param(
181                 $impossible => $$error{$impossible},
182                 IMPOSSIBLE  => 1
183             );
184             $noerror = 0;
185         }
186         foreach my $needsconfirmation ( keys %$question ) {
187             $template->param(
188                 $needsconfirmation => $$question{$needsconfirmation},
189                 NEEDSCONFIRMATION  => 1
190             );
191             $noquestion = 0;
192         }
193         $template->param(
194             day   => $day,
195             month => $month,
196             year  => $year
197         );
198         if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
199             issuebook( \%env, $borrower, $barcode, $datedue );
200         }
201     }
202 }
203
204 # reload the borrower info for the sake of reseting the flags.....
205 if ($borrowernumber) {
206     $borrower = getpatroninformation( \%env, $borrowernumber, 0 );
207 }
208
209 ##################################################################################
210 # BUILD HTML
211 # show all reserves of this borrower, and the position of the reservation ....
212 if ($borrowernumber) {
213 # new op dev
214 # now we show the status of the borrower's reservations
215         my @borrowerreserv = FastFindReserves(0,$borrowernumber);
216         my @reservloop;
217         foreach my $num_res (@borrowerreserv) {
218                 my %getreserv;
219                 my %env;
220                 my $getiteminfo = getiteminformation(\%env,$num_res->{'itemnumber'});
221                 my $itemtypeinfo = getitemtypeinfo($getiteminfo->{'itemtype'});
222                 my ($transfertwhen,$transfertfrom,$transfertto) = checktransferts($num_res->{'itemnumber'});
223
224                 $getreserv{waiting} = 0;
225                 $getreserv{transfered} = 0;
226                 $getreserv{nottransfered} = 0;
227
228                 $getreserv{reservedate} = format_date($num_res->{'reservedate'});
229                 $getreserv{biblionumber} = $getiteminfo->{'biblionumber'};
230                 $getreserv{title} = $getiteminfo->{'title'};
231                 $getreserv{itemtype} = $itemtypeinfo->{'description'};
232                 $getreserv{author} = $getiteminfo->{'author'};
233                 $getreserv{barcodereserv} = $getiteminfo->{'barcode'};
234                 $getreserv{itemcallnumber} = $getiteminfo->{'itemcallnumber'};
235 #               check if we have a waitin status for reservations
236                 if ($num_res->{'found'} eq 'W'){
237                         $getreserv{color} = 'reserved';
238                         $getreserv{waiting} = 1; 
239                 }
240
241 #               check transfers with the itemnumber foud in th reservation loop
242                 if ($transfertwhen){
243                 $getreserv{color} = 'transfered';
244                 $getreserv{transfered} = 1;
245                 $getreserv{datesent} = format_date($transfertwhen);
246                 $getreserv{frombranch} = getbranchname($transfertfrom);
247                 }
248
249                 if (($getiteminfo->{'holdingbranch'} ne $num_res->{'branchcode'}) and not $transfertwhen){
250                 $getreserv{nottransfered} = 1;
251                 $getreserv{nottransferedby} = getbranchname($getiteminfo->{'holdingbranch'});
252                 }
253
254 #               if we don't have a reserv on item, we put the biblio infos and the waiting position     
255                 if ($getiteminfo->{'title'} eq '' ){
256                         my $getbibinfo = bibitemdata($num_res->{'biblionumber'});
257                         my $getbibtype = getitemtypeinfo($getbibinfo->{'itemtype'});
258                         $getreserv{color} = 'inwait';
259                         $getreserv{title} = $getbibinfo->{'title'};
260                         $getreserv{waitingposition} = $num_res->{'priority'};
261                         $getreserv{nottransfered} = 0;
262                         $getreserv{itemtype} = $getbibtype->{'description'};
263                         $getreserv{author} = $getbibinfo->{'author'};
264                         $getreserv{itemcallnumber} = '----------';
265                         
266                 }
267
268                 push(@reservloop, \%getreserv);
269         }
270         # return result to the template
271         $template->param(reservloop => \@reservloop);
272
273 }
274
275
276 # make the issued books table.....
277 my $todaysissues = '';
278 my $previssues   = '';
279 my @realtodayissues;
280 my @realprevissues;
281 my $allowborrow;
282 if ($borrower) {
283
284 # get each issue of the borrower & separate them in todayissues & previous issues
285     my @todaysissues;
286     my @previousissues;
287     my $issueslist = getissues($borrower);
288
289     # split in 2 arrays for today & previous
290     foreach my $it ( keys %$issueslist ) {
291         my $issuedate = $issueslist->{$it}->{'timestamp'};
292         $issuedate =~ s/-//g;
293         $issuedate = substr( $issuedate, 0, 8 );
294         if ( $todaysdate == $issuedate ) {
295             push @todaysissues, $issueslist->{$it};
296         }
297         else {
298             push @previousissues, $issueslist->{$it};
299         }
300     }
301     my $od;    # overdues
302     my $i = 0;
303     my $togglecolor;
304
305     # parses today & build Template array
306     foreach my $book ( sort { $b->{'timestamp'} <=> $a->{'timestamp'} }
307         @todaysissues )
308     {
309         my $dd      = $book->{'date_due'};
310         my $datedue = $book->{'date_due'};
311         $dd = format_date($dd);
312         $datedue =~ s/-//g;
313         if ( $datedue < $todaysdate ) {
314             $od = 1;
315         }
316         else {
317             $od = 0;
318         }
319         if ( $i % 2 ) {
320             $togglecolor = 0;
321         }
322         else {
323             $togglecolor = 1;
324         }
325         $book->{'togglecolor'} = $togglecolor;
326         $book->{'od'}          = $od;
327         $book->{'dd'}          = $dd;
328         if ( $book->{'author'} eq '' ) {
329             $book->{'author'} = ' ';
330         }
331         push @realtodayissues, $book;
332         $i++;
333     }
334
335     # parses previous & build Template array
336     $i = 0;
337     foreach my $book ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
338         @previousissues )
339     {
340         my $dd      = $book->{'date_due'};
341         my $datedue = $book->{'date_due'};
342         $dd = format_date($dd);
343         my $pcolor = '';
344         my $od     = '';
345         $datedue =~ s/-//g;
346         if ( $datedue < $todaysdate ) {
347             $od = 1;
348         }
349         else {
350             $od = 0;
351         }
352         if ( $i % 2 ) {
353             $togglecolor = 0;
354         }
355         else {
356             $togglecolor = 1;
357         }
358         $book->{'togglecolor'} = $togglecolor;
359         $book->{'dd'}          = $dd;
360         $book->{'od'}          = $od;
361         if ( $book->{'author'} eq '' ) {
362             $book->{'author'} = ' ';
363         }
364         push @realprevissues, $book;
365         $i++;
366     }
367 }
368
369 my @values;
370 my %labels;
371 my $CGIselectborrower;
372 if ($borrowerslist) {
373     foreach (
374         sort {
375             $a->{'surname'}
376               . $a->{'firstname'} cmp $b->{'surname'}
377               . $b->{'firstname'}
378         } @$borrowerslist
379       )
380     {
381         push @values, $_->{'borrowernumber'};
382         $labels{ $_->{'borrowernumber'} } =
383 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ...  $_->{'streetaddress'} ";
384     }
385     $CGIselectborrower = CGI::scrolling_list(
386         -name     => 'borrnumber',
387         -values   => \@values,
388         -labels   => \%labels,
389         -size     => 7,
390         -multiple => 0
391     );
392 }
393
394 #title
395
396 my ( $patrontable, $flaginfotable ) = patrontable($borrower);
397 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
398 my @temp = split( /\$/, $amountold );
399
400 my $CGIorganisations;
401 my $member_of_institution;
402 if ( C4::Context->preference("memberofinstitution") ) {
403     my $organisations = get_institutions();
404     my @orgs;
405     my %org_labels;
406     foreach my $organisation ( keys %$organisations ) {
407         push @orgs, $organisation;
408         $org_labels{$organisation} =
409           $organisations->{$organisation}->{'surname'};
410     }
411     $member_of_institution = 1;
412     $CGIorganisations = CGI::popup_menu(
413         -id       => 'organisations',
414         -name     => 'organisations',
415         -labels   => \%org_labels,
416         -values   => \@orgs,
417
418     );
419 }
420
421 $amountold = $temp[1];
422 $template->param(
423     findborrower      => $findborrower,
424     borrower          => $borrower,
425     borrowernumber    => $borrowernumber,
426     branch            => $branch,
427     printer           => $printer,
428     printername       => $printer,
429     firstname         => $borrower->{'firstname'},
430     surname           => $borrower->{'surname'},
431     categorycode      => $borrower->{'categorycode'},
432     streetaddress     => $borrower->{'streetaddress'},
433     emailaddress      => $borrower->{'emailaddress'},
434     borrowernotes     => $borrower->{'borrowernotes'},
435     city              => $borrower->{'city'},
436     phone             => $borrower->{'phone'},
437     cardnumber        => $borrower->{'cardnumber'},
438     amountold         => $amountold,
439     barcode           => $barcode,
440     stickyduedate     => $stickyduedate,
441     message           => $message,
442     CGIselectborrower => $CGIselectborrower,
443     todayissues       => \@realtodayissues,
444     previssues        => \@realprevissues,
445     memberofinstution => $member_of_institution,                                                                 
446     CGIorganisations => $CGIorganisations, 
447 );
448
449 # set return date if stickyduedate
450 if ($stickyduedate) {
451     my $t_year  = "year" . $year;
452     my $t_month = "month" . $month;
453     my $t_day   = "day" . $day;
454     $template->param(
455         $t_year  => 1,
456         $t_month => 1,
457         $t_day   => 1,
458     );
459 }
460
461
462 # if ($branchcookie) {
463 #     $cookie=[$cookie, $branchcookie, $printercookie];
464 # }
465
466 output_html_with_http_headers $query, $cookie, $template->output;
467
468 ####################################################################
469 # Extra subroutines,,,
470
471 sub patrontable {
472     my ($borrower)    = @_;
473     my $flags         = $borrower->{'flags'};
474     my $flaginfotable = '';
475     my $flaginfotext;
476
477     #my $flaginfotext='';
478     my $flag;
479     my $color = '';
480     foreach $flag ( sort keys %$flags ) {
481
482         #       my @itemswaiting='';
483         $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
484         if ( $flags->{$flag}->{'noissues'} ) {
485             $template->param(
486                 flagged  => 1,
487                 noissues => 'true',
488             );
489             if ( $flag eq 'GNA' ) {
490                 $template->param( gna => 'true' );
491             }
492             if ( $flag eq 'LOST' ) {
493                 $template->param( lost => 'true' );
494             }
495             if ( $flag eq 'DBARRED' ) {
496                 $template->param( dbarred => 'true' );
497             }
498             if ( $flag eq 'CHARGES' ) {
499                 $template->param(
500                     charges    => 'true',
501                     chargesmsg => $flags->{'CHARGES'}->{'message'}
502                 );
503             }
504         }
505         else {
506             if ( $flag eq 'CHARGES' ) {
507                 $template->param(
508                     charges    => 'true',
509                     flagged    => 1,
510                     chargesmsg => $flags->{'CHARGES'}->{'message'}
511                 );
512             }
513 # FIXME this part can be removed if we keep new display of reserves "reservloop"
514 #             if ( $flag eq 'WAITING' ) {
515 #                 my $items = $flags->{$flag}->{'itemlist'};
516 #                 my @itemswaiting;
517 #                 foreach my $item (@$items) {
518 #                     my ($iteminformation) =
519 #                       getiteminformation( \%env, $item->{'itemnumber'}, 0 );
520 #                     $iteminformation->{'branchname'} =
521 #                       $branches->{ $iteminformation->{'holdingbranch'} }
522 #                       ->{'branchname'};
523 #                     push @itemswaiting, $iteminformation;
524 #                 }
525 #                 $template->param(
526 #                     flagged      => 1,
527 #                     waiting      => 'true',
528 #                     waitingmsg   => $flags->{'WAITING'}->{'message'},
529 #                     itemswaiting => \@itemswaiting,
530 #                 );
531 #             }
532             if ( $flag eq 'ODUES' ) {
533                 $template->param(
534                     odues    => 'true',
535                     flagged  => 1,
536                     oduesmsg => $flags->{'ODUES'}->{'message'}
537                 );
538
539                 my $items = $flags->{$flag}->{'itemlist'};
540                 {
541                     my @itemswaiting;
542                     foreach my $item (@$items) {
543                         my ($iteminformation) =
544                           getiteminformation( \%env, $item->{'itemnumber'}, 0 );
545                         push @itemswaiting, $iteminformation;
546                     }
547                 }
548                 if ( $query->param('module') ne 'returns' ) {
549                     $template->param( nonreturns => 'true' );
550                 }
551             }
552             if ( $flag eq 'NOTES' ) {
553                 $template->param(
554                     notes    => 'true',
555                     flagged  => 1,
556                     notesmsg => $flags->{'NOTES'}->{'message'}
557                 );
558             }
559         }
560     }
561     return ( $patrontable, $flaginfotext );
562 }
563
564 sub cuecatbarcodedecode {
565     my ($barcode) = @_;
566     chomp($barcode);
567     my @fields = split( /\./, $barcode );
568     my @results = map( decode($_), @fields[ 1 .. $#fields ] );
569     if ( $#results == 2 ) {
570         return $results[2];
571     }
572     else {
573         return $barcode;
574     }
575 }
576
577 # Local Variables:
578 # tab-width: 8
579 # End: