Allows a person to choose an associated borrower (orgranisation) when Issuing.
[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::Members;
29 use C4::Output;
30 use C4::Print;
31 use DBI;
32 use C4::Auth;
33 use C4::Interface::CGI::Output;
34 use C4::Koha;
35 use HTML::Template;
36 use C4::Date;
37 use Date::Manip;
38
39 #
40 # PARAMETERS READING
41 #
42 my $query = new CGI;
43
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45     {
46         template_name   => 'circ/circulation.tmpl',
47         query           => $query,
48         type            => "intranet",
49         authnotrequired => 0,
50         flagsrequired   => { circulate => 1 },
51     }
52 );
53 my $branches = getbranches();
54 my $printers = getprinters();
55 my $branch   = getbranch( $query, $branches );
56 my $printer  = getprinter( $query, $printers );
57
58 my $findborrower = $query->param('findborrower');
59 $findborrower =~ s|,| |g;
60 $findborrower =~ s|'| |g;
61 my $borrowernumber = $query->param('borrnumber');
62 my $print          = $query->param('print') || '';
63 my $barcode        = $query->param('barcode') || '';
64 my $year           = $query->param('year');
65 my $month          = $query->param('month');
66 my $day            = $query->param('day');
67 my $stickyduedate  = $query->param('stickyduedate');
68 my $issueconfirmed = $query->param('issueconfirmed');
69 my $cancelreserve  = $query->param('cancelreserve');
70 my $organisation   = $query->param('organisations');
71
72 #set up cookie.....
73 my $branchcookie;
74 my $printercookie;
75 if ( $query->param('setcookies') ) {
76     $branchcookie = $query->cookie(
77         -name    => 'branch',
78         -value   => "$branch",
79         -expires => '+1y'
80     );
81     $printercookie = $query->cookie(
82         -name    => 'printer',
83         -value   => "$printer",
84         -expires => '+1y'
85     );
86 }
87
88 my %env
89   ; # FIXME env is used as an "environment" variable. Could be dropped probably...
90
91 $env{'branchcode'} = $branch;
92 $env{'printer'}    = $printer;
93 $env{'queue'}      = $printer;
94 $env{'organisation'} = $organisation;
95
96 my @datearr = localtime( time() );
97
98 # FIXME - Could just use POSIX::strftime("%Y%m%d", localtime);
99 my $todaysdate =
100     ( 1900 + $datearr[5] )
101   . sprintf( "%0.2d", ( $datearr[4] + 1 ) )
102   . sprintf( "%0.2d", ( $datearr[3] ) );
103
104 # check and see if we should print
105 if ( $barcode eq '' && $print eq 'maybe' ) {
106     $print = 'yes';
107 }
108 if ( $print eq 'yes' && $borrowernumber ne '' ) {
109     printslip( \%env, $borrowernumber );
110     $query->param( 'borrnumber', '' );
111     $borrowernumber = '';
112 }
113
114 #
115 # STEP 2 : FIND BORROWER
116 # if there is a list of find borrowers....
117 #
118 my $borrowerslist;
119 my $message;
120 if ($findborrower) {
121     my ( $count, $borrowers ) =
122       BornameSearch( \%env, $findborrower, 'cardnumber', 'web' );
123     my @borrowers = @$borrowers;
124     if ( $#borrowers == -1 ) {
125         $query->param( 'findborrower', '' );
126         $message = "'$findborrower'";
127     }
128     elsif ( $#borrowers == 0 ) {
129         $query->param( 'borrnumber', $borrowers[0]->{'borrowernumber'} );
130         $query->param( 'barcode',    '' );
131         $borrowernumber = $borrowers[0]->{'borrowernumber'};
132     }
133     else {
134         $borrowerslist = \@borrowers;
135     }
136 }
137
138 # get the borrower information.....
139 my $borrower;
140 my $picture;
141
142 if ($borrowernumber) {
143     $borrower = getpatroninformation( \%env, $borrowernumber, 0 );
144     my ( $od, $issue, $fines ) = borrdata2( \%env, $borrowernumber );
145     warn $borrower->{'expiry'};
146     my $warningdate =
147       DateCalc( $borrower->{'expiry'},
148         "- " . C4::Context->preference('NotifyBorrowerDeparture') . "  days" );
149     my $warning = Date_Cmp( ParseDate("today"), $warningdate );
150     if ( $warning > 0 ) {
151
152         #borrowercard expired
153         $template->param( warndeparture => $warning );
154     }
155     $template->param(
156         overduecount => $od,
157         issuecount   => $issue,
158         finetotal    => $fines
159     );
160     my $htdocs = C4::Context->config('intrahtdocs');
161     $picture = "/borrowerimages/" . $borrowernumber . ".jpg";
162     if ( -e $htdocs . "$picture" ) {
163         $template->param( picture => $picture );
164     }
165 }
166
167 #
168 # STEP 3 : ISSUING
169 #
170 #
171
172 if ($barcode) {
173     $barcode = cuecatbarcodedecode($barcode);
174     my ( $datedue, $invalidduedate ) = fixdate( $year, $month, $day );
175     if ($issueconfirmed) {
176         issuebook( \%env, $borrower, $barcode, $datedue, $cancelreserve );
177     }
178     else {
179         my ( $error, $question ) =
180           canbookbeissued( \%env, $borrower, $barcode, $year, $month, $day );
181         my $noerror    = 1;
182         my $noquestion = 1;
183         foreach my $impossible ( keys %$error ) {
184             $template->param(
185                 $impossible => $$error{$impossible},
186                 IMPOSSIBLE  => 1
187             );
188             $noerror = 0;
189         }
190         foreach my $needsconfirmation ( keys %$question ) {
191             $template->param(
192                 $needsconfirmation => $$question{$needsconfirmation},
193                 NEEDSCONFIRMATION  => 1
194             );
195             $noquestion = 0;
196         }
197         $template->param(
198             day   => $day,
199             month => $month,
200             year  => $year
201         );
202         if ( $noerror && ( $noquestion || $issueconfirmed ) ) {
203             issuebook( \%env, $borrower, $barcode, $datedue );
204         }
205     }
206 }
207
208 # reload the borrower info for the sake of reseting the flags.....
209 if ($borrowernumber) {
210     $borrower = getpatroninformation( \%env, $borrowernumber, 0 );
211 }
212
213 ##################################################################################
214 # BUILD HTML
215
216 # make the issued books table.....
217 my $todaysissues = '';
218 my $previssues   = '';
219 my @realtodayissues;
220 my @realprevissues;
221 my $allowborrow;
222 if ($borrower) {
223
224 # get each issue of the borrower & separate them in todayissues & previous issues
225     my @todaysissues;
226     my @previousissues;
227     my $issueslist = getissues($borrower);
228
229     # split in 2 arrays for today & previous
230     foreach my $it ( keys %$issueslist ) {
231         my $issuedate = $issueslist->{$it}->{'timestamp'};
232         $issuedate =~ s/-//g;
233         $issuedate = substr( $issuedate, 0, 8 );
234         if ( $todaysdate == $issuedate ) {
235             push @todaysissues, $issueslist->{$it};
236         }
237         else {
238             push @previousissues, $issueslist->{$it};
239         }
240     }
241     my $od;    # overdues
242     my $i = 0;
243     my $togglecolor;
244
245     # parses today & build Template array
246     foreach my $book ( sort { $b->{'timestamp'} <=> $a->{'timestamp'} }
247         @todaysissues )
248     {
249         my $dd      = $book->{'date_due'};
250         my $datedue = $book->{'date_due'};
251         $dd = format_date($dd);
252         $datedue =~ s/-//g;
253         if ( $datedue < $todaysdate ) {
254             $od = 1;
255         }
256         else {
257             $od = 0;
258         }
259         if ( $i % 2 ) {
260             $togglecolor = 0;
261         }
262         else {
263             $togglecolor = 1;
264         }
265         $book->{'togglecolor'} = $togglecolor;
266         $book->{'od'}          = $od;
267         $book->{'dd'}          = $dd;
268         if ( $book->{'author'} eq '' ) {
269             $book->{'author'} = ' ';
270         }
271         push @realtodayissues, $book;
272         $i++;
273     }
274
275     # parses previous & build Template array
276     $i = 0;
277     foreach my $book ( sort { $a->{'date_due'} cmp $b->{'date_due'} }
278         @previousissues )
279     {
280         my $dd      = $book->{'date_due'};
281         my $datedue = $book->{'date_due'};
282         $dd = format_date($dd);
283         my $pcolor = '';
284         my $od     = '';
285         $datedue =~ s/-//g;
286         if ( $datedue < $todaysdate ) {
287             $od = 1;
288         }
289         else {
290             $od = 0;
291         }
292         if ( $i % 2 ) {
293             $togglecolor = 0;
294         }
295         else {
296             $togglecolor = 1;
297         }
298         $book->{'togglecolor'} = $togglecolor;
299         $book->{'dd'}          = $dd;
300         $book->{'od'}          = $od;
301         if ( $book->{'author'} eq '' ) {
302             $book->{'author'} = ' ';
303         }
304         push @realprevissues, $book;
305         $i++;
306     }
307 }
308
309 my @values;
310 my %labels;
311 my $CGIselectborrower;
312 if ($borrowerslist) {
313     foreach (
314         sort {
315             $a->{'surname'}
316               . $a->{'firstname'} cmp $b->{'surname'}
317               . $b->{'firstname'}
318         } @$borrowerslist
319       )
320     {
321         push @values, $_->{'borrowernumber'};
322         $labels{ $_->{'borrowernumber'} } =
323 "$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'}) ...  $_->{'streetaddress'} ";
324     }
325     $CGIselectborrower = CGI::scrolling_list(
326         -name     => 'borrnumber',
327         -values   => \@values,
328         -labels   => \%labels,
329         -size     => 7,
330         -multiple => 0
331     );
332 }
333
334 #title
335
336 my ( $patrontable, $flaginfotable ) = patrontable($borrower);
337 my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
338 my @temp = split( /\$/, $amountold );
339
340 my $CGIorganisations;
341 my $member_of_institution;
342 if ( C4::Context->preference("memberofinstitution") ) {
343     my $organisations = get_institutions();
344     my @orgs;
345     my %org_labels;
346     foreach my $organisation ( keys %$organisations ) {
347         push @orgs, $organisation;
348         $org_labels{$organisation} =
349           $organisations->{$organisation}->{'surname'};
350     }
351     $member_of_institution = 1;
352     $CGIorganisations = CGI::popup_menu(
353         -id       => 'organisations',
354         -name     => 'organisations',
355         -labels   => \%org_labels,
356         -values   => \@orgs,
357
358     );
359 }
360
361 $amountold = $temp[1];
362 $template->param(
363     findborrower      => $findborrower,
364     borrower          => $borrower,
365     borrowernumber    => $borrowernumber,
366     branch            => $branch,
367     printer           => $printer,
368     branchname        => $branches->{$branch}->{'branchname'},
369     printername       => $printers->{$printer}->{'printername'},
370     firstname         => $borrower->{'firstname'},
371     surname           => $borrower->{'surname'},
372     categorycode      => $borrower->{'categorycode'},
373     streetaddress     => $borrower->{'streetaddress'},
374     emailaddress      => $borrower->{'emailaddress'},
375     borrowernotes     => $borrower->{'borrowernotes'},
376     city              => $borrower->{'city'},
377     phone             => $borrower->{'phone'},
378     cardnumber        => $borrower->{'cardnumber'},
379     amountold         => $amountold,
380     barcode           => $barcode,
381     stickyduedate     => $stickyduedate,
382     message           => $message,
383     CGIselectborrower => $CGIselectborrower,
384     todayissues       => \@realtodayissues,
385     previssues        => \@realprevissues,
386     memberofinstution => $member_of_institution,                                                                 
387     CGIorganisations => $CGIorganisations, 
388 );
389
390 # set return date if stickyduedate
391 if ($stickyduedate) {
392     my $t_year  = "year" . $year;
393     my $t_month = "month" . $month;
394     my $t_day   = "day" . $day;
395     $template->param(
396         $t_year  => 1,
397         $t_month => 1,
398         $t_day   => 1,
399     );
400 }
401
402 if ($branchcookie) {
403     $cookie = [ $cookie, $branchcookie, $printercookie ];
404 }
405
406 output_html_with_http_headers $query, $cookie, $template->output;
407
408 ####################################################################
409 # Extra subroutines,,,
410
411 sub patrontable {
412     my ($borrower)    = @_;
413     my $flags         = $borrower->{'flags'};
414     my $flaginfotable = '';
415     my $flaginfotext;
416
417     #my $flaginfotext='';
418     my $flag;
419     my $color = '';
420     foreach $flag ( sort keys %$flags ) {
421
422         #       my @itemswaiting='';
423         $flags->{$flag}->{'message'} =~ s/\n/<br>/g;
424         if ( $flags->{$flag}->{'noissues'} ) {
425             $template->param(
426                 flagged  => 1,
427                 noissues => 'true',
428             );
429             if ( $flag eq 'GNA' ) {
430                 $template->param( gna => 'true' );
431             }
432             if ( $flag eq 'LOST' ) {
433                 $template->param( lost => 'true' );
434             }
435             if ( $flag eq 'DBARRED' ) {
436                 $template->param( dbarred => 'true' );
437             }
438             if ( $flag eq 'CHARGES' ) {
439                 $template->param(
440                     charges    => 'true',
441                     chargesmsg => $flags->{'CHARGES'}->{'message'}
442                 );
443             }
444         }
445         else {
446             if ( $flag eq 'CHARGES' ) {
447                 $template->param(
448                     charges    => 'true',
449                     flagged    => 1,
450                     chargesmsg => $flags->{'CHARGES'}->{'message'}
451                 );
452             }
453             if ( $flag eq 'WAITING' ) {
454                 my $items = $flags->{$flag}->{'itemlist'};
455                 my @itemswaiting;
456                 foreach my $item (@$items) {
457                     my ($iteminformation) =
458                       getiteminformation( \%env, $item->{'itemnumber'}, 0 );
459                     $iteminformation->{'branchname'} =
460                       $branches->{ $iteminformation->{'holdingbranch'} }
461                       ->{'branchname'};
462                     push @itemswaiting, $iteminformation;
463                 }
464                 $template->param(
465                     flagged      => 1,
466                     waiting      => 'true',
467                     waitingmsg   => $flags->{'WAITING'}->{'message'},
468                     itemswaiting => \@itemswaiting,
469                 );
470             }
471             if ( $flag eq 'ODUES' ) {
472                 $template->param(
473                     odues    => 'true',
474                     flagged  => 1,
475                     oduesmsg => $flags->{'ODUES'}->{'message'}
476                 );
477
478                 my $items = $flags->{$flag}->{'itemlist'};
479                 {
480                     my @itemswaiting;
481                     foreach my $item (@$items) {
482                         my ($iteminformation) =
483                           getiteminformation( \%env, $item->{'itemnumber'}, 0 );
484                         push @itemswaiting, $iteminformation;
485                     }
486                 }
487                 if ( $query->param('module') ne 'returns' ) {
488                     $template->param( nonreturns => 'true' );
489                 }
490             }
491             if ( $flag eq 'NOTES' ) {
492                 $template->param(
493                     notes    => 'true',
494                     flagged  => 1,
495                     notesmsg => $flags->{'NOTES'}->{'message'}
496                 );
497             }
498         }
499     }
500     return ( $patrontable, $flaginfotext );
501 }
502
503 sub cuecatbarcodedecode {
504     my ($barcode) = @_;
505     chomp($barcode);
506     my @fields = split( /\./, $barcode );
507     my @results = map( decode($_), @fields[ 1 .. $#fields ] );
508     if ( $#results == 2 ) {
509         return $results[2];
510     }
511     else {
512         return $barcode;
513     }
514 }
515
516 # Local Variables:
517 # tab-width: 8
518 # End: