3 # script to execute issuing of books
5 # Copyright 2000-2002 Katipo Communications
6 # copyright 2010 BibLibre
7 # Copyright 2011 PTFS-Europe Ltd.
8 # Copyright 2012 software.coop and MJ Ray
10 # This file is part of Koha.
12 # Koha is free software; you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 3 of the License, or
15 # (at your option) any later version.
17 # Koha is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with Koha; if not, see <http://www.gnu.org/licenses>.
29 use DateTime
::Duration
;
32 use C4
::Auth qw
/:DEFAULT get_session haspermission/;
33 use C4
::Dates qw
/format_date/;
34 use C4
::Branch
; # GetBranches
35 use C4
::Koha
; # GetPrinter
37 use C4
::Utils
::DataTables
::Members
;
45 use C4
::Members
::Attributes
qw(GetBorrowerAttributes);
46 use Koha
::Borrower
::Debarments
qw(GetDebarments IsDebarred);
56 use List
::MoreUtils qw
/uniq/;
64 my $sessionID = $query->cookie("CGISESSID") ;
65 my $session = get_session
($sessionID);
67 # branch and printer are now defined by the userenv
68 # but first we have to check if someone has tried to change them
70 my $branch = $query->param('branch');
72 # update our session so the userenv is updated
73 $session->param('branch', $branch);
74 $session->param('branchname', GetBranchName
($branch));
77 my $printer = $query->param('printer');
79 # update our session so the userenv is updated
80 $session->param('branchprinter', $printer);
83 if (!C4
::Context
->userenv && !$branch){
84 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
85 # no branch set we can't issue
86 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
91 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
93 template_name
=> 'circ/circulation.tt',
97 flagsrequired
=> { circulate
=> 'circulate_remaining_permissions' },
101 my $branches = GetBranches
();
103 my $force_allow_issue = $query->param('forceallow') || 0;
104 if (!C4
::Auth
::haspermission
( C4
::Context
->userenv->{id
} , { circulate
=> 'force_checkout' } )) {
105 $force_allow_issue = 0;
108 my $onsite_checkout = $query->param('onsite_checkout');
110 my @failedrenews = $query->param('failedrenew'); # expected to be itemnumbers
111 our %renew_failed = ();
112 for (@failedrenews) { $renew_failed{$_} = 1; }
114 my @failedreturns = $query->param('failedreturn');
115 our %return_failed = ();
116 for (@failedreturns) { $return_failed{$_} = 1; }
118 my $findborrower = $query->param('findborrower') || q{};
119 $findborrower =~ s
|,| |g
;
120 my $borrowernumber = $query->param('borrowernumber');
122 $branch = C4
::Context
->userenv->{'branch'};
123 $printer = C4
::Context
->userenv->{'branchprinter'};
126 # If AutoLocation is not activated, we show the Circulation Parameters to chage settings of librarian
127 if (C4
::Context
->preference("AutoLocation") != 1) {
128 $template->param(ManualLocation
=> 1);
131 if (C4
::Context
->preference("DisplayClearScreenButton")) {
132 $template->param(DisplayClearScreenButton
=> 1);
135 my $barcode = $query->param('barcode') || q{};
136 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
138 $barcode = barcodedecode
($barcode) if( $barcode && C4
::Context
->preference('itemBarcodeInputFilter'));
139 my $stickyduedate = $query->param('stickyduedate') || $session->param('stickyduedate');
140 my $duedatespec = $query->param('duedatespec') || $session->param('stickyduedate');
141 my $issueconfirmed = $query->param('issueconfirmed');
142 my $cancelreserve = $query->param('cancelreserve');
143 my $print = $query->param('print') || q{};
144 my $debt_confirmed = $query->param('debt_confirmed') || 0; # Don't show the debt error dialog twice
145 my $charges = $query->param('charges') || q{};
147 # Check if stickyduedate is turned off
149 # was stickyduedate loaded from session?
150 if ( $stickyduedate && ! $query->param("stickyduedate") ) {
151 $session->clear( 'stickyduedate' );
152 $stickyduedate = $query->param('stickyduedate');
153 $duedatespec = $query->param('duedatespec');
155 $session->param('auto_renew', $query->param('auto_renew'));
158 $session->clear('auto_renew');
161 my ($datedue,$invalidduedate);
163 my $duedatespec_allow = C4
::Context
->preference('SpecifyDueDate');
164 if( $onsite_checkout && !$duedatespec_allow ) {
165 $datedue = output_pref
({ dt
=> dt_from_string
, dateonly
=> 1, dateformat
=> 'iso' });
166 $datedue .= ' 23:59:00';
167 } elsif( $duedatespec_allow ) {
169 if ($duedatespec =~ C4
::Dates
->regexp('syspref')) {
170 $datedue = dt_from_string
($duedatespec);
173 $template->param(IMPOSSIBLE
=>1, INVALID_DATE
=>$duedatespec);
178 our $todaysdate = C4
::Dates
->new->output('iso');
180 # check and see if we should print
181 if ( $barcode eq '' && $print eq 'maybe' ) {
185 my $inprocess = ($barcode eq '') ?
'' : $query->param('inprocess');
186 if ( $barcode eq '' && $charges eq 'yes' ) {
189 borrowernumber
=> $borrowernumber
193 if ( $print eq 'yes' && $borrowernumber ne '' ) {
194 if ( C4
::Context
->boolean_preference('printcirculationslips') ) {
195 my $letter = IssueSlip
($branch, $borrowernumber, "QUICK");
196 NetworkPrint
($letter->{content
});
198 $query->param( 'borrowernumber', '' );
199 $borrowernumber = '';
203 # STEP 2 : FIND BORROWER
204 # if there is a list of find borrowers....
208 my $borrower = C4
::Members
::GetMember
( cardnumber
=> $findborrower );
210 $borrowernumber = $borrower->{borrowernumber
};
212 my $dt_params = { iDisplayLength
=> -1 };
213 my $results = C4
::Utils
::DataTables
::Members
::search
(
215 searchmember
=> $findborrower,
216 dt_params
=> $dt_params,
219 my $borrowers = $results->{patrons
};
220 if ( scalar @
$borrowers == 1 ) {
221 $borrowernumber = $borrowers->[0]->{borrowernumber
};
222 $query->param( 'borrowernumber', $borrowernumber );
223 $query->param( 'barcode', '' );
224 } elsif ( @
$borrowers ) {
225 $template->param( borrowers
=> $borrowers );
227 $query->param( 'findborrower', '' );
228 $message = "'$findborrower'";
233 # get the borrower information.....
235 if ($borrowernumber) {
236 $borrower = GetMemberDetails
( $borrowernumber, 0 );
237 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines
( $borrowernumber );
239 # Warningdate is the date that the warning starts appearing
240 my ( $today_year, $today_month, $today_day) = Today
();
241 my ($warning_year, $warning_month, $warning_day) = split /-/, $borrower->{'dateexpiry'};
242 my ( $enrol_year, $enrol_month, $enrol_day) = split /-/, $borrower->{'dateenrolled'};
243 # Renew day is calculated by adding the enrolment period to today
244 my ( $renew_year, $renew_month, $renew_day);
245 if ($enrol_year*$enrol_month*$enrol_day>0) {
246 ( $renew_year, $renew_month, $renew_day) =
247 Add_Delta_YM
( $enrol_year, $enrol_month, $enrol_day,
248 0 , $borrower->{'enrolmentperiod'});
250 # if the expiry date is before today ie they have expired
251 if ( !$borrower->{'dateexpiry'} || $warning_year*$warning_month*$warning_day==0
252 || Date_to_Days
($today_year, $today_month, $today_day )
253 > Date_to_Days
($warning_year, $warning_month, $warning_day) )
255 #borrowercard expired, no issues
258 noissues
=> ($force_allow_issue) ?
0 : "1",
259 forceallow
=> $force_allow_issue,
261 renewaldate
=> format_date
("$renew_year-$renew_month-$renew_day")
264 # check for NotifyBorrowerDeparture
265 elsif ( C4
::Context
->preference('NotifyBorrowerDeparture') &&
266 Date_to_Days
(Add_Delta_Days
($warning_year,$warning_month,$warning_day,- C4
::Context
->preference('NotifyBorrowerDeparture'))) <
267 Date_to_Days
( $today_year, $today_month, $today_day ) )
269 # borrower card soon to expire warn librarian
270 $template->param("warndeparture" => format_date
($borrower->{dateexpiry
}),
272 if (C4
::Context
->preference('ReturnBeforeExpiry')){
273 $template->param("returnbeforeexpiry" => 1);
278 issuecount
=> $issue,
282 if ( IsDebarred
($borrowernumber) ) {
284 'userdebarred' => $borrower->{debarred
},
285 'debarredcomment' => $borrower->{debarredcomment
},
288 if ( $borrower->{debarred
} ne "9999-12-31" ) {
289 $template->param( 'userdebarreddate' =>
290 C4
::Dates
::format_date
( $borrower->{debarred
} ) );
301 # always check for blockers on issuing
302 my ( $error, $question, $alerts ) =
303 CanBookBeIssued
( $borrower, $barcode, $datedue , $inprocess );
304 my $blocker = $invalidduedate ?
1 : 0;
306 $template->param( alert
=> $alerts );
308 # Get the item title for more information
309 my $getmessageiteminfo = GetBiblioFromItemNumber
(undef,$barcode);
311 authvalcode_notforloan
=> C4
::Koha
::GetAuthValCode
('items.notforloan', $getmessageiteminfo->{'frameworkcode'}),
313 # Fix for bug 7494: optional checkout-time fallback search for a book
315 if ( $error->{'UNKNOWN_BARCODE'}
316 && C4
::Context
->preference("itemBarcodeFallbackSearch") )
318 $template->param( FALLBACK
=> 1 );
320 my $query = "kw=" . $barcode;
321 my ( $searcherror, $results, $total_hits ) = SimpleSearch
($query);
323 # if multiple hits, offer options to librarian
324 if ( $total_hits > 0 ) {
326 foreach my $hit ( @
{$results} ) {
328 TransformMarcToKoha
( C4
::Context
->dbh,
329 C4
::Search
::new_record_from_zebra
('biblioserver',$hit) );
331 # offer all barcodes individually
332 if ( $chosen->{barcode
} ) {
333 foreach my $barcode ( sort split(/\s*\|\s*/, $chosen->{barcode
}) ) {
334 my %chosen_single = %{$chosen};
335 $chosen_single{barcode
} = $barcode;
336 push( @options, \
%chosen_single );
340 $template->param( options
=> \
@options );
344 unless( $onsite_checkout and C4
::Context
->preference("OnSiteCheckoutsForce") ) {
345 delete $question->{'DEBT'} if ($debt_confirmed);
346 foreach my $impossible ( keys %$error ) {
348 $impossible => $$error{$impossible},
354 if( !$blocker || $force_allow_issue ){
355 my $confirm_required = 0;
356 unless($issueconfirmed){
357 # Get the item title for more information
358 my $getmessageiteminfo = GetBiblioFromItemNumber
(undef,$barcode);
359 $template->{VARS
}->{'additional_materials'} = $getmessageiteminfo->{'materials'};
360 $template->param( itemhomebranch
=> $getmessageiteminfo->{'homebranch'} );
362 # pass needsconfirmation to template if issuing is possible and user hasn't yet confirmed.
363 foreach my $needsconfirmation ( keys %$question ) {
365 $needsconfirmation => $$question{$needsconfirmation},
366 getTitleMessageIteminfo
=> $getmessageiteminfo->{'title'},
367 getBarcodeMessageIteminfo
=> $getmessageiteminfo->{'barcode'},
368 NEEDSCONFIRMATION
=> 1,
369 onsite_checkout
=> $onsite_checkout,
371 $confirm_required = 1;
374 unless($confirm_required) {
375 my $issue = AddIssue
( $borrower, $barcode, $datedue, $cancelreserve, undef, undef, { onsite_checkout
=> $onsite_checkout, auto_renew
=> $session->param('auto_renew') } );
376 $template->param( issue
=> $issue );
377 $session->clear('auto_renew');
382 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines
($borrowernumber);
383 $template->param( issuecount
=> $issue );
386 # reload the borrower info for the sake of reseting the flags.....
387 if ($borrowernumber) {
388 $borrower = GetMemberDetails
( $borrowernumber, 0 );
391 ##################################################################################
393 # show all reserves of this borrower, and the position of the reservation ....
394 if ($borrowernumber) {
396 holds_count
=> Koha
::Database
->new()->schema()->resultset('Reserve')
397 ->count( { borrowernumber
=> $borrowernumber } ) );
398 my @borrowerreserv = GetReservesFromBorrowernumber
($borrowernumber);
400 my @WaitingReserveLoop;
401 foreach my $num_res (@borrowerreserv) {
402 if ( $num_res->{'found'} && $num_res->{'found'} eq 'W' ) {
403 my $getiteminfo = GetBiblioFromItemNumber
( $num_res->{'itemnumber'} );
404 my $itemtypeinfo = getitemtypeinfo
( (C4
::Context
->preference('item-level_itypes')) ?
$getiteminfo->{'itype'} : $getiteminfo->{'itemtype'} );
405 my %getWaitingReserveInfo;
406 $getWaitingReserveInfo{title
} = $getiteminfo->{'title'};
407 $getWaitingReserveInfo{biblionumber
} =
408 $getiteminfo->{'biblionumber'};
409 $getWaitingReserveInfo{itemtype
} = $itemtypeinfo->{'description'};
410 $getWaitingReserveInfo{author
} = $getiteminfo->{'author'};
411 $getWaitingReserveInfo{itemcallnumber
} =
412 $getiteminfo->{'itemcallnumber'};
413 $getWaitingReserveInfo{reservedate
} =
414 format_date
( $num_res->{'reservedate'} );
415 $getWaitingReserveInfo{waitingat
} =
416 GetBranchName
( $num_res->{'branchcode'} );
417 $getWaitingReserveInfo{waitinghere
} = 1
418 if $num_res->{'branchcode'} eq $branch;
419 push( @WaitingReserveLoop, \
%getWaitingReserveInfo );
422 $template->param( WaitingReserveLoop
=> \
@WaitingReserveLoop );
423 $template->param( adultborrower
=> 1 )
424 if ( $borrower->{'category_type'} eq 'A' );
428 my $flags = $borrower->{'flags'};
429 foreach my $flag ( sort keys %$flags ) {
430 $template->param( flagged
=> 1);
431 $flags->{$flag}->{'message'} =~ s
#\n#<br />#g;
432 if ( $flags->{$flag}->{'noissues'} ) {
434 noissues
=> ($force_allow_issue) ?
0 : 'true',
435 forceallow
=> $force_allow_issue,
437 if ( $flag eq 'GNA' ) {
438 $template->param( gna
=> 'true' );
440 elsif ( $flag eq 'LOST' ) {
441 $template->param( lost
=> 'true' );
443 elsif ( $flag eq 'DBARRED' ) {
444 $template->param( dbarred
=> 'true' );
446 elsif ( $flag eq 'CHARGES' ) {
449 chargesmsg
=> $flags->{'CHARGES'}->{'message'},
450 chargesamount
=> $flags->{'CHARGES'}->{'amount'},
451 charges_is_blocker
=> 1
454 elsif ( $flag eq 'CREDITS' ) {
457 creditsmsg
=> $flags->{'CREDITS'}->{'message'},
458 creditsamount
=> sprintf("%.02f", -($flags->{'CREDITS'}->{'amount'})), # from patron's pov
463 if ( $flag eq 'CHARGES' ) {
466 chargesmsg
=> $flags->{'CHARGES'}->{'message'},
467 chargesamount
=> $flags->{'CHARGES'}->{'amount'},
470 elsif ( $flag eq 'CREDITS' ) {
473 creditsmsg
=> $flags->{'CREDITS'}->{'message'},
474 creditsamount
=> sprintf("%.02f", -($flags->{'CREDITS'}->{'amount'})), # from patron's pov
477 elsif ( $flag eq 'ODUES' ) {
480 oduesmsg
=> $flags->{'ODUES'}->{'message'}
483 my $items = $flags->{$flag}->{'itemlist'};
484 if ( ! $query->param('module') || $query->param('module') ne 'returns' ) {
485 $template->param( nonreturns
=> 'true' );
488 elsif ( $flag eq 'NOTES' ) {
491 notesmsg
=> $flags->{'NOTES'}->{'message'}
497 my $amountold = $borrower->{flags
}->{'CHARGES'}->{'message'} || 0;
498 $amountold =~ s/^.*\$//; # remove upto the $, if any
500 my ( $total, $accts, $numaccts) = GetMemberAccountRecords
( $borrowernumber );
502 if ( $borrowernumber && $borrower->{'category_type'} eq 'C') {
503 my ( $catcodes, $labels ) = GetborCatFromCatType
( 'A', 'WHERE category_type = ?' );
504 my $cnt = scalar(@
$catcodes);
505 $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
506 $template->param( 'catcode' => $catcodes->[0]) if $cnt == 1;
509 my $lib_messages_loop = GetMessages
( $borrowernumber, 'L', $branch );
510 if($lib_messages_loop){ $template->param(flagged
=> 1 ); }
512 my $bor_messages_loop = GetMessages
( $borrowernumber, 'B', $branch );
513 if($bor_messages_loop){ $template->param(flagged
=> 1 ); }
515 my $fast_cataloging = 0;
516 if (defined getframeworkinfo
('FA')) {
520 if (C4
::Context
->preference('ExtendedPatronAttributes')) {
521 my $attributes = GetBorrowerAttributes
($borrowernumber);
523 ExtendedPatronAttributes
=> 1,
524 extendedattributes
=> $attributes
528 my @relatives = GetMemberRelatives
( $borrower->{'borrowernumber'} );
529 my $relatives_issues_count =
530 Koha
::Database
->new()->schema()->resultset('Issue')
531 ->count( { borrowernumber
=> \
@relatives } );
533 my $roadtype = C4
::Koha
::GetAuthorisedValueByCode
( 'ROADTYPE', $borrower->{streettype
} );
535 $template->param(%$borrower);
538 lib_messages_loop
=> $lib_messages_loop,
539 bor_messages_loop
=> $bor_messages_loop,
540 all_messages_del
=> C4
::Context
->preference('AllowAllMessageDeletion'),
541 findborrower
=> $findborrower,
542 borrower
=> $borrower,
543 borrowernumber
=> $borrowernumber,
545 branchname
=> GetBranchName
($borrower->{'branchcode'}),
547 printername
=> $printer,
548 was_renewed
=> $query->param('was_renewed') ?
1 : 0,
549 expiry
=> format_date
($borrower->{'dateexpiry'}),
550 roadtype
=> $roadtype,
551 amountold
=> $amountold,
553 stickyduedate
=> $stickyduedate,
554 duedatespec
=> $duedatespec,
556 totaldue
=> sprintf('%.2f', $total),
557 inprocess
=> $inprocess,
558 is_child
=> ($borrowernumber && $borrower->{'category_type'} eq 'C'),
560 soundon
=> C4
::Context
->preference("SoundOn"),
561 fast_cataloging
=> $fast_cataloging,
562 CircAutoPrintQuickSlip
=> C4
::Context
->preference("CircAutoPrintQuickSlip"),
563 activeBorrowerRelationship
=> (C4
::Context
->preference('borrowerRelationship') ne ''),
564 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
565 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
566 RoutingSerials
=> C4
::Context
->preference('RoutingSerials'),
567 relatives_issues_count
=> $relatives_issues_count,
568 relatives_borrowernumbers
=> \
@relatives,
571 # save stickyduedate to session
572 if ($stickyduedate) {
573 $session->param( 'stickyduedate', $duedatespec );
576 my ($picture, $dberror) = GetPatronImage
($borrower->{'borrowernumber'});
577 $template->param( picture
=> 1 ) if $picture;
579 # get authorised values with type of BOR_NOTES
581 my $canned_notes = GetAuthorisedValues
("BOR_NOTES");
584 debt_confirmed
=> $debt_confirmed,
585 SpecifyDueDate
=> $duedatespec_allow,
586 CircAutocompl
=> C4
::Context
->preference("CircAutocompl"),
587 AllowRenewalLimitOverride
=> C4
::Context
->preference("AllowRenewalLimitOverride"),
588 canned_bor_notes_loop
=> $canned_notes,
589 debarments
=> GetDebarments
({ borrowernumber
=> $borrowernumber }),
590 todaysdate
=> output_pref
( { dt
=> dt_from_string
()->set(hour
=> 23)->set(minute
=> 59), dateformat
=> 'sql' } ),
593 output_html_with_http_headers
$query, $cookie, $template->output;