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
;
46 use C4
::Members
::Attributes
qw(GetBorrowerAttributes);
47 use Koha
::Borrower
::Debarments
qw(GetDebarments IsDebarred);
57 use List
::MoreUtils qw
/uniq/;
65 my $sessionID = $query->cookie("CGISESSID") ;
66 my $session = get_session
($sessionID);
68 # branch and printer are now defined by the userenv
69 # but first we have to check if someone has tried to change them
71 my $branch = $query->param('branch');
73 # update our session so the userenv is updated
74 $session->param('branch', $branch);
75 $session->param('branchname', GetBranchName
($branch));
78 my $printer = $query->param('printer');
80 # update our session so the userenv is updated
81 $session->param('branchprinter', $printer);
84 if (!C4
::Context
->userenv && !$branch){
85 if ($session->param('branch') eq 'NO_LIBRARY_SET'){
86 # no branch set we can't issue
87 print $query->redirect("/cgi-bin/koha/circ/selectbranchprinter.pl");
92 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
94 template_name
=> 'circ/circulation.tt',
98 flagsrequired
=> { circulate
=> 'circulate_remaining_permissions' },
102 my $branches = GetBranches
();
104 my $force_allow_issue = $query->param('forceallow') || 0;
105 if (!C4
::Auth
::haspermission
( C4
::Context
->userenv->{id
} , { circulate
=> 'force_checkout' } )) {
106 $force_allow_issue = 0;
109 my $onsite_checkout = $query->param('onsite_checkout');
111 my @failedrenews = $query->param('failedrenew'); # expected to be itemnumbers
112 our %renew_failed = ();
113 for (@failedrenews) { $renew_failed{$_} = 1; }
115 my @failedreturns = $query->param('failedreturn');
116 our %return_failed = ();
117 for (@failedreturns) { $return_failed{$_} = 1; }
119 my $findborrower = $query->param('findborrower') || q{};
120 $findborrower =~ s
|,| |g
;
121 my $borrowernumber = $query->param('borrowernumber');
123 $branch = C4
::Context
->userenv->{'branch'};
124 $printer = C4
::Context
->userenv->{'branchprinter'};
127 # If AutoLocation is not activated, we show the Circulation Parameters to chage settings of librarian
128 if (C4
::Context
->preference("AutoLocation") != 1) {
129 $template->param(ManualLocation
=> 1);
132 if (C4
::Context
->preference("DisplayClearScreenButton")) {
133 $template->param(DisplayClearScreenButton
=> 1);
136 my $barcode = $query->param('barcode') || q{};
137 $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace
139 $barcode = barcodedecode
($barcode) if( $barcode && C4
::Context
->preference('itemBarcodeInputFilter'));
140 my $stickyduedate = $query->param('stickyduedate') || $session->param('stickyduedate');
141 my $duedatespec = $query->param('duedatespec') || $session->param('stickyduedate');
142 my $issueconfirmed = $query->param('issueconfirmed');
143 my $cancelreserve = $query->param('cancelreserve');
144 my $print = $query->param('print') || q{};
145 my $debt_confirmed = $query->param('debt_confirmed') || 0; # Don't show the debt error dialog twice
146 my $charges = $query->param('charges') || q{};
148 # Check if stickyduedate is turned off
150 # was stickyduedate loaded from session?
151 if ( $stickyduedate && ! $query->param("stickyduedate") ) {
152 $session->clear( 'stickyduedate' );
153 $stickyduedate = $query->param('stickyduedate');
154 $duedatespec = $query->param('duedatespec');
156 $session->param('auto_renew', $query->param('auto_renew'));
159 $session->clear('auto_renew');
162 my ($datedue,$invalidduedate);
164 my $duedatespec_allow = C4
::Context
->preference('SpecifyDueDate');
165 if( $onsite_checkout && !$duedatespec_allow ) {
166 $datedue = output_pref
({ dt
=> dt_from_string
, dateonly
=> 1, dateformat
=> 'iso' });
167 $datedue .= ' 23:59:00';
168 } elsif( $duedatespec_allow ) {
170 if ($duedatespec =~ C4
::Dates
->regexp('syspref')) {
171 $datedue = dt_from_string
($duedatespec);
174 $template->param(IMPOSSIBLE
=>1, INVALID_DATE
=>$duedatespec);
179 our $todaysdate = C4
::Dates
->new->output('iso');
181 # check and see if we should print
182 if ( $barcode eq '' && $print eq 'maybe' ) {
186 my $inprocess = ($barcode eq '') ?
'' : $query->param('inprocess');
187 if ( $barcode eq '' && $charges eq 'yes' ) {
190 borrowernumber
=> $borrowernumber
194 if ( $print eq 'yes' && $borrowernumber ne '' ) {
195 if ( C4
::Context
->boolean_preference('printcirculationslips') ) {
196 my $letter = IssueSlip
($branch, $borrowernumber, "QUICK");
197 NetworkPrint
($letter->{content
});
199 $query->param( 'borrowernumber', '' );
200 $borrowernumber = '';
204 # STEP 2 : FIND BORROWER
205 # if there is a list of find borrowers....
209 my $borrower = C4
::Members
::GetMember
( cardnumber
=> $findborrower );
211 $borrowernumber = $borrower->{borrowernumber
};
213 my $dt_params = { iDisplayLength
=> -1 };
214 my $results = C4
::Utils
::DataTables
::Members
::search
(
216 searchmember
=> $findborrower,
217 dt_params
=> $dt_params,
220 my $borrowers = $results->{patrons
};
221 if ( scalar @
$borrowers == 1 ) {
222 $borrowernumber = $borrowers->[0]->{borrowernumber
};
223 $query->param( 'borrowernumber', $borrowernumber );
224 $query->param( 'barcode', '' );
225 } elsif ( @
$borrowers ) {
226 $template->param( borrowers
=> $borrowers );
228 $query->param( 'findborrower', '' );
229 $message = "'$findborrower'";
234 # get the borrower information.....
236 if ($borrowernumber) {
237 $borrower = GetMemberDetails
( $borrowernumber, 0 );
238 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines
( $borrowernumber );
240 # Warningdate is the date that the warning starts appearing
241 my ( $today_year, $today_month, $today_day) = Today
();
242 my ($warning_year, $warning_month, $warning_day) = split /-/, $borrower->{'dateexpiry'};
243 my ( $enrol_year, $enrol_month, $enrol_day) = split /-/, $borrower->{'dateenrolled'};
244 # Renew day is calculated by adding the enrolment period to today
245 my ( $renew_year, $renew_month, $renew_day);
246 if ($enrol_year*$enrol_month*$enrol_day>0) {
247 ( $renew_year, $renew_month, $renew_day) =
248 Add_Delta_YM
( $enrol_year, $enrol_month, $enrol_day,
249 0 , $borrower->{'enrolmentperiod'});
251 # if the expiry date is before today ie they have expired
252 if ( !$borrower->{'dateexpiry'} || $warning_year*$warning_month*$warning_day==0
253 || Date_to_Days
($today_year, $today_month, $today_day )
254 > Date_to_Days
($warning_year, $warning_month, $warning_day) )
256 #borrowercard expired, no issues
259 noissues
=> ($force_allow_issue) ?
0 : "1",
260 forceallow
=> $force_allow_issue,
262 renewaldate
=> format_date
("$renew_year-$renew_month-$renew_day")
265 # check for NotifyBorrowerDeparture
266 elsif ( C4
::Context
->preference('NotifyBorrowerDeparture') &&
267 Date_to_Days
(Add_Delta_Days
($warning_year,$warning_month,$warning_day,- C4
::Context
->preference('NotifyBorrowerDeparture'))) <
268 Date_to_Days
( $today_year, $today_month, $today_day ) )
270 # borrower card soon to expire warn librarian
271 $template->param("warndeparture" => format_date
($borrower->{dateexpiry
}),
273 if (C4
::Context
->preference('ReturnBeforeExpiry')){
274 $template->param("returnbeforeexpiry" => 1);
279 issuecount
=> $issue,
283 if ( IsDebarred
($borrowernumber) ) {
285 'userdebarred' => $borrower->{debarred
},
286 'debarredcomment' => $borrower->{debarredcomment
},
289 if ( $borrower->{debarred
} ne "9999-12-31" ) {
290 $template->param( 'userdebarreddate' =>
291 C4
::Dates
::format_date
( $borrower->{debarred
} ) );
302 # always check for blockers on issuing
303 my ( $error, $question, $alerts ) =
304 CanBookBeIssued
( $borrower, $barcode, $datedue , $inprocess );
305 my $blocker = $invalidduedate ?
1 : 0;
307 $template->param( alert
=> $alerts );
309 # Get the item title for more information
310 my $getmessageiteminfo = GetBiblioFromItemNumber
(undef,$barcode);
312 authvalcode_notforloan
=> C4
::Koha
::GetAuthValCode
('items.notforloan', $getmessageiteminfo->{'frameworkcode'}),
314 # Fix for bug 7494: optional checkout-time fallback search for a book
316 if ( $error->{'UNKNOWN_BARCODE'}
317 && C4
::Context
->preference("itemBarcodeFallbackSearch") )
319 $template->param( FALLBACK
=> 1 );
321 my $query = "kw=" . $barcode;
322 my ( $searcherror, $results, $total_hits ) = SimpleSearch
($query);
324 # if multiple hits, offer options to librarian
325 if ( $total_hits > 0 ) {
327 foreach my $hit ( @
{$results} ) {
329 TransformMarcToKoha
( C4
::Context
->dbh,
330 C4
::Search
::new_record_from_zebra
('biblioserver',$hit) );
332 # offer all barcodes individually
333 if ( $chosen->{barcode
} ) {
334 foreach my $barcode ( sort split(/\s*\|\s*/, $chosen->{barcode
}) ) {
335 my %chosen_single = %{$chosen};
336 $chosen_single{barcode
} = $barcode;
337 push( @options, \
%chosen_single );
341 $template->param( options
=> \
@options );
345 unless( $onsite_checkout and C4
::Context
->preference("OnSiteCheckoutsForce") ) {
346 delete $question->{'DEBT'} if ($debt_confirmed);
347 foreach my $impossible ( keys %$error ) {
349 $impossible => $$error{$impossible},
355 if( !$blocker || $force_allow_issue ){
356 my $confirm_required = 0;
357 unless($issueconfirmed){
358 # Get the item title for more information
359 my $getmessageiteminfo = GetBiblioFromItemNumber
(undef,$barcode);
360 $template->{VARS
}->{'additional_materials'} = $getmessageiteminfo->{'materials'};
361 $template->param( itemhomebranch
=> $getmessageiteminfo->{'homebranch'} );
363 # pass needsconfirmation to template if issuing is possible and user hasn't yet confirmed.
364 foreach my $needsconfirmation ( keys %$question ) {
366 $needsconfirmation => $$question{$needsconfirmation},
367 getTitleMessageIteminfo
=> $getmessageiteminfo->{'title'},
368 getBarcodeMessageIteminfo
=> $getmessageiteminfo->{'barcode'},
369 NEEDSCONFIRMATION
=> 1,
370 onsite_checkout
=> $onsite_checkout,
372 $confirm_required = 1;
375 unless($confirm_required) {
376 my $issue = AddIssue
( $borrower, $barcode, $datedue, $cancelreserve, undef, undef, { onsite_checkout
=> $onsite_checkout, auto_renew
=> $session->param('auto_renew') } );
377 $template->param( issue
=> $issue );
378 $session->clear('auto_renew');
383 my ( $od, $issue, $fines ) = GetMemberIssuesAndFines
($borrowernumber);
384 $template->param( issuecount
=> $issue );
386 if ($question->{RESERVE_WAITING
} or $question->{RESERVED
}){
388 reserveborrowernumber
=> $question->{'resborrowernumber'},
389 itembiblionumber
=> $getmessageiteminfo->{'biblionumber'}
394 # reload the borrower info for the sake of reseting the flags.....
395 if ($borrowernumber) {
396 $borrower = GetMemberDetails
( $borrowernumber, 0 );
399 ##################################################################################
401 # show all reserves of this borrower, and the position of the reservation ....
402 if ($borrowernumber) {
403 my $holds = Koha
::Holds
->search( { borrowernumber
=> $borrowernumber } );
405 holds_count
=> $holds->count(),
406 WaitingHolds
=> scalar $holds->waiting(),
409 $template->param( adultborrower
=> 1 ) if ( $borrower->{category_type
} eq 'A' || $borrower->{category_type
} eq 'I' );
413 my $flags = $borrower->{'flags'};
414 foreach my $flag ( sort keys %$flags ) {
415 $template->param( flagged
=> 1);
416 $flags->{$flag}->{'message'} =~ s
#\n#<br />#g;
417 if ( $flags->{$flag}->{'noissues'} ) {
419 noissues
=> ($force_allow_issue) ?
0 : 'true',
420 forceallow
=> $force_allow_issue,
422 if ( $flag eq 'GNA' ) {
423 $template->param( gna
=> 'true' );
425 elsif ( $flag eq 'LOST' ) {
426 $template->param( lost
=> 'true' );
428 elsif ( $flag eq 'DBARRED' ) {
429 $template->param( dbarred
=> 'true' );
431 elsif ( $flag eq 'CHARGES' ) {
434 chargesmsg
=> $flags->{'CHARGES'}->{'message'},
435 chargesamount
=> $flags->{'CHARGES'}->{'amount'},
436 charges_is_blocker
=> 1
439 elsif ( $flag eq 'CREDITS' ) {
442 creditsmsg
=> $flags->{'CREDITS'}->{'message'},
443 creditsamount
=> sprintf("%.02f", -($flags->{'CREDITS'}->{'amount'})), # from patron's pov
448 if ( $flag eq 'CHARGES' ) {
451 chargesmsg
=> $flags->{'CHARGES'}->{'message'},
452 chargesamount
=> $flags->{'CHARGES'}->{'amount'},
455 elsif ( $flag eq 'CREDITS' ) {
458 creditsmsg
=> $flags->{'CREDITS'}->{'message'},
459 creditsamount
=> sprintf("%.02f", -($flags->{'CREDITS'}->{'amount'})), # from patron's pov
462 elsif ( $flag eq 'ODUES' ) {
465 oduesmsg
=> $flags->{'ODUES'}->{'message'}
468 my $items = $flags->{$flag}->{'itemlist'};
469 if ( ! $query->param('module') || $query->param('module') ne 'returns' ) {
470 $template->param( nonreturns
=> 'true' );
473 elsif ( $flag eq 'NOTES' ) {
476 notesmsg
=> $flags->{'NOTES'}->{'message'}
482 my $amountold = $borrower->{flags
}->{'CHARGES'}->{'message'} || 0;
483 $amountold =~ s/^.*\$//; # remove upto the $, if any
485 my ( $total, $accts, $numaccts) = GetMemberAccountRecords
( $borrowernumber );
487 if ( $borrowernumber && $borrower->{'category_type'} eq 'C') {
488 my ( $catcodes, $labels ) = GetborCatFromCatType
( 'A', 'WHERE category_type = ?' );
489 my $cnt = scalar(@
$catcodes);
490 $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
491 $template->param( 'catcode' => $catcodes->[0]) if $cnt == 1;
494 my $lib_messages_loop = GetMessages
( $borrowernumber, 'L', $branch );
495 if($lib_messages_loop){ $template->param(flagged
=> 1 ); }
497 my $bor_messages_loop = GetMessages
( $borrowernumber, 'B', $branch );
498 if($bor_messages_loop){ $template->param(flagged
=> 1 ); }
500 my $fast_cataloging = 0;
501 if (defined getframeworkinfo
('FA')) {
505 if (C4
::Context
->preference('ExtendedPatronAttributes')) {
506 my $attributes = GetBorrowerAttributes
($borrowernumber);
508 ExtendedPatronAttributes
=> 1,
509 extendedattributes
=> $attributes
513 my @relatives = GetMemberRelatives
( $borrower->{'borrowernumber'} );
514 my $relatives_issues_count =
515 Koha
::Database
->new()->schema()->resultset('Issue')
516 ->count( { borrowernumber
=> \
@relatives } );
518 my $roadtype = C4
::Koha
::GetAuthorisedValueByCode
( 'ROADTYPE', $borrower->{streettype
} );
520 $template->param(%$borrower);
523 lib_messages_loop
=> $lib_messages_loop,
524 bor_messages_loop
=> $bor_messages_loop,
525 all_messages_del
=> C4
::Context
->preference('AllowAllMessageDeletion'),
526 findborrower
=> $findborrower,
527 borrower
=> $borrower,
528 borrowernumber
=> $borrowernumber,
530 branchname
=> GetBranchName
($borrower->{'branchcode'}),
532 printername
=> $printer,
533 was_renewed
=> $query->param('was_renewed') ?
1 : 0,
534 expiry
=> format_date
($borrower->{'dateexpiry'}),
535 roadtype
=> $roadtype,
536 amountold
=> $amountold,
538 stickyduedate
=> $stickyduedate,
539 duedatespec
=> $duedatespec,
541 totaldue
=> sprintf('%.2f', $total),
542 inprocess
=> $inprocess,
543 is_child
=> ($borrowernumber && $borrower->{'category_type'} eq 'C'),
545 soundon
=> C4
::Context
->preference("SoundOn"),
546 fast_cataloging
=> $fast_cataloging,
547 CircAutoPrintQuickSlip
=> C4
::Context
->preference("CircAutoPrintQuickSlip"),
548 activeBorrowerRelationship
=> (C4
::Context
->preference('borrowerRelationship') ne ''),
549 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
550 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
551 RoutingSerials
=> C4
::Context
->preference('RoutingSerials'),
552 relatives_issues_count
=> $relatives_issues_count,
553 relatives_borrowernumbers
=> \
@relatives,
556 # save stickyduedate to session
557 if ($stickyduedate) {
558 $session->param( 'stickyduedate', $duedatespec );
561 my ($picture, $dberror) = GetPatronImage
($borrower->{'borrowernumber'});
562 $template->param( picture
=> 1 ) if $picture;
564 # get authorised values with type of BOR_NOTES
566 my $canned_notes = GetAuthorisedValues
("BOR_NOTES");
569 debt_confirmed
=> $debt_confirmed,
570 SpecifyDueDate
=> $duedatespec_allow,
571 CircAutocompl
=> C4
::Context
->preference("CircAutocompl"),
572 AllowRenewalLimitOverride
=> C4
::Context
->preference("AllowRenewalLimitOverride"),
573 canned_bor_notes_loop
=> $canned_notes,
574 debarments
=> GetDebarments
({ borrowernumber
=> $borrowernumber }),
575 todaysdate
=> output_pref
( { dt
=> dt_from_string
()->set(hour
=> 23)->set(minute
=> 59), dateformat
=> 'sql' } ),
578 output_html_with_http_headers
$query, $cookie, $template->output;