Lines 1321-1333
Get loan length for an itemtype, a borrower type and a branch
Link Here
|
1321 |
sub GetLoanLength { |
1321 |
sub GetLoanLength { |
1322 |
my ( $borrowertype, $itemtype, $branchcode ) = @_; |
1322 |
my ( $borrowertype, $itemtype, $branchcode ) = @_; |
1323 |
my $dbh = C4::Context->dbh; |
1323 |
my $dbh = C4::Context->dbh; |
1324 |
my $sth = |
1324 |
my $sth = $dbh->prepare(qq{ |
1325 |
$dbh->prepare( |
1325 |
SELECT issuelength, lengthunit, renewalperiod |
1326 |
'select issuelength, lengthunit from issuingrules where categorycode=? and itemtype=? and branchcode=? and issuelength is not null' |
1326 |
FROM issuingrules |
1327 |
); |
1327 |
WHERE categorycode=? |
1328 |
# warn "in get loan lenght $borrowertype $itemtype $branchcode "; |
1328 |
AND itemtype=? |
1329 |
# try to find issuelength & return the 1st available. |
1329 |
AND branchcode=? |
1330 |
# check with borrowertype, itemtype and branchcode, then without one of those parameters |
1330 |
AND issuelength IS NOT NULL |
|
|
1331 |
}); |
1332 |
|
1333 |
# try to find issuelength & return the 1st available. |
1334 |
# check with borrowertype, itemtype and branchcode, then without one of those parameters |
1335 |
|
1331 |
$sth->execute( $borrowertype, $itemtype, $branchcode ); |
1336 |
$sth->execute( $borrowertype, $itemtype, $branchcode ); |
1332 |
my $loanlength = $sth->fetchrow_hashref; |
1337 |
my $loanlength = $sth->fetchrow_hashref; |
1333 |
return $loanlength |
1338 |
return $loanlength |
Lines 1371-1376
sub GetLoanLength {
Link Here
|
1371 |
# if no rule is set => 21 days (hardcoded) |
1376 |
# if no rule is set => 21 days (hardcoded) |
1372 |
return { |
1377 |
return { |
1373 |
issuelength => 21, |
1378 |
issuelength => 21, |
|
|
1379 |
renewalperiod => 21, |
1374 |
lengthunit => 'days', |
1380 |
lengthunit => 'days', |
1375 |
}; |
1381 |
}; |
1376 |
|
1382 |
|
Lines 1405-1411
sub GetHardDueDate {
Link Here
|
1405 |
|
1411 |
|
1406 |
FIXME - This is a copy-paste of GetLoanLength |
1412 |
FIXME - This is a copy-paste of GetLoanLength |
1407 |
as a stop-gap. Do not wish to change API for GetLoanLength |
1413 |
as a stop-gap. Do not wish to change API for GetLoanLength |
1408 |
this close to release, however, Overdues::GetIssuingRules is broken. |
1414 |
this close to release. |
1409 |
|
1415 |
|
1410 |
Get the issuing rule for an itemtype, a borrower type and a branch |
1416 |
Get the issuing rule for an itemtype, a borrower type and a branch |
1411 |
Returns a hashref from the issuingrules table. |
1417 |
Returns a hashref from the issuingrules table. |
Lines 2427-2491
sub CanBookBeRenewed {
Link Here
|
2427 |
my $dbh = C4::Context->dbh; |
2433 |
my $dbh = C4::Context->dbh; |
2428 |
my $renews = 1; |
2434 |
my $renews = 1; |
2429 |
my $renewokay = 0; |
2435 |
my $renewokay = 0; |
2430 |
my $error; |
2436 |
my $error; |
2431 |
|
2437 |
|
2432 |
# Look in the issues table for this item, lent to this borrower, |
2438 |
my $borrower = C4::Members::GetMemberDetails( $borrowernumber, 0 ) or return; |
2433 |
# and not yet returned. |
2439 |
my $item = GetItem($itemnumber) or return; |
|
|
2440 |
my $itemissue = GetItemIssue($itemnumber) or return; |
2434 |
|
2441 |
|
2435 |
# Look in the issues table for this item, lent to this borrower, |
2442 |
my $branchcode = _GetCircControlBranch($item, $borrower); |
2436 |
# and not yet returned. |
|
|
2437 |
my %branch = ( |
2438 |
'ItemHomeLibrary' => 'items.homebranch', |
2439 |
'PickupLibrary' => 'items.holdingbranch', |
2440 |
'PatronLibrary' => 'borrowers.branchcode' |
2441 |
); |
2442 |
my $controlbranch = $branch{C4::Context->preference('CircControl')}; |
2443 |
my $itype = C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype'; |
2444 |
|
2445 |
my $sthcount = $dbh->prepare(" |
2446 |
SELECT |
2447 |
borrowers.categorycode, biblioitems.itemtype, issues.renewals, renewalsallowed, $controlbranch |
2448 |
FROM issuingrules, |
2449 |
issues |
2450 |
LEFT JOIN items USING (itemnumber) |
2451 |
LEFT JOIN borrowers USING (borrowernumber) |
2452 |
LEFT JOIN biblioitems USING (biblioitemnumber) |
2453 |
|
2454 |
WHERE |
2455 |
(issuingrules.categorycode = borrowers.categorycode OR issuingrules.categorycode = '*') |
2456 |
AND |
2457 |
(issuingrules.itemtype = $itype OR issuingrules.itemtype = '*') |
2458 |
AND |
2459 |
(issuingrules.branchcode = $controlbranch OR issuingrules.branchcode = '*') |
2460 |
AND |
2461 |
borrowernumber = ? |
2462 |
AND |
2463 |
itemnumber = ? |
2464 |
ORDER BY |
2465 |
issuingrules.categorycode desc, |
2466 |
issuingrules.itemtype desc, |
2467 |
issuingrules.branchcode desc |
2468 |
LIMIT 1; |
2469 |
"); |
2470 |
|
2471 |
$sthcount->execute( $borrowernumber, $itemnumber ); |
2472 |
if ( my $data1 = $sthcount->fetchrow_hashref ) { |
2473 |
|
2474 |
if ( ( $data1->{renewalsallowed} && $data1->{renewalsallowed} > $data1->{renewals} ) || $override_limit ) { |
2475 |
$renewokay = 1; |
2476 |
} |
2477 |
else { |
2478 |
$error="too_many"; |
2479 |
} |
2480 |
|
2481 |
my ( $resfound, $resrec, undef ) = C4::Reserves::CheckReserves($itemnumber); |
2482 |
if ($resfound) { |
2483 |
$renewokay = 0; |
2484 |
$error="on_reserve" |
2485 |
} |
2486 |
|
2443 |
|
|
|
2444 |
my $issuingrule = GetIssuingRule($borrower->{categorycode}, $item->{itype}, $branchcode); |
2445 |
|
2446 |
if ( ( $issuingrule->{renewalsallowed} > $itemissue->{renewals} ) || $override_limit ) { |
2447 |
$renewokay = 1; |
2448 |
} else { |
2449 |
$error = "too_many"; |
2487 |
} |
2450 |
} |
2488 |
return ($renewokay,$error); |
2451 |
|
|
|
2452 |
my ( $resfound, $resrec ) = C4::Reserves::CheckReserves($itemnumber); |
2453 |
if ( $resfound ) { |
2454 |
$renewokay = 0; |
2455 |
$error = "on_reserve"; |
2456 |
} |
2457 |
|
2458 |
return ( $renewokay, $error ); |
2489 |
} |
2459 |
} |
2490 |
|
2460 |
|
2491 |
=head2 AddRenewal |
2461 |
=head2 AddRenewal |
Lines 2546-2552
sub AddRenewal {
Link Here
|
2546 |
$datedue = (C4::Context->preference('RenewalPeriodBase') eq 'date_due') ? |
2516 |
$datedue = (C4::Context->preference('RenewalPeriodBase') eq 'date_due') ? |
2547 |
$issuedata->{date_due} : |
2517 |
$issuedata->{date_due} : |
2548 |
DateTime->now( time_zone => C4::Context->tz()); |
2518 |
DateTime->now( time_zone => C4::Context->tz()); |
2549 |
$datedue = CalcDateDue($datedue,$itemtype,$issuedata->{'branchcode'},$borrower); |
2519 |
$datedue = CalcDateDue($datedue, $itemtype, $issuedata->{'branchcode'}, $borrower, 'is a renewal'); |
2550 |
} |
2520 |
} |
2551 |
|
2521 |
|
2552 |
# Update the issues record to have the new due date, and a new count |
2522 |
# Update the issues record to have the new due date, and a new count |
Lines 2993-3050
C<$borrower> = Borrower object
Link Here
|
2993 |
=cut |
2963 |
=cut |
2994 |
|
2964 |
|
2995 |
sub CalcDateDue { |
2965 |
sub CalcDateDue { |
2996 |
my ( $startdate, $itemtype, $branch, $borrower ) = @_; |
2966 |
my ( $startdate, $itemtype, $branch, $borrower, $isrenewal ) = @_; |
|
|
2967 |
|
2968 |
$isrenewal ||= 0; |
2997 |
|
2969 |
|
2998 |
# loanlength now a href |
2970 |
# loanlength now a href |
2999 |
my $loanlength = |
2971 |
my $loanlength = |
3000 |
GetLoanLength( $borrower->{'categorycode'}, $itemtype, $branch ); |
2972 |
GetLoanLength( $borrower->{'categorycode'}, $itemtype, $branch ); |
|
|
2973 |
|
2974 |
my $length_key = ( $isrenewal and defined $loanlength->{renewalperiod} ) |
2975 |
? qq{renewalperiod} |
2976 |
: qq{issuelength}; |
3001 |
|
2977 |
|
3002 |
my $datedue; |
2978 |
my $datedue; |
3003 |
|
2979 |
|
3004 |
# if globalDueDate ON the datedue is set to that date |
2980 |
# calculate the datedue as normal |
3005 |
if (C4::Context->preference('globalDueDate') |
2981 |
if ( C4::Context->preference('useDaysMode') eq 'Days' ) |
3006 |
&& ( C4::Context->preference('globalDueDate') =~ |
2982 |
{ # ignoring calendar |
3007 |
C4::Dates->regexp('syspref') ) |
2983 |
my $dt = |
3008 |
) { |
2984 |
DateTime->now( time_zone => C4::Context->tz() ) |
3009 |
$datedue = dt_from_string( |
2985 |
->truncate( to => 'minute' ); |
3010 |
C4::Context->preference('globalDueDate'), |
2986 |
if ( $loanlength->{lengthunit} eq 'hours' ) { |
3011 |
C4::Context->preference('dateformat') |
2987 |
$dt->add( hours => $loanlength->{$length_key} ); |
3012 |
); |
2988 |
} else { # days |
|
|
2989 |
$dt->add( days => $loanlength->{$length_key} ); |
2990 |
$dt->set_hour(23); |
2991 |
$dt->set_minute(59); |
2992 |
} |
2993 |
# break |
2994 |
return $dt; |
3013 |
} else { |
2995 |
} else { |
3014 |
|
2996 |
my $dur; |
3015 |
# otherwise, calculate the datedue as normal |
2997 |
if ($loanlength->{lengthunit} eq 'hours') { |
3016 |
if ( C4::Context->preference('useDaysMode') eq 'Days' ) |
2998 |
$dur = DateTime::Duration->new( hours => $loanlength->{$length_key}); |
3017 |
{ # ignoring calendar |
2999 |
} |
3018 |
my $dt = |
3000 |
else { # days |
3019 |
DateTime->now( time_zone => C4::Context->tz() ) |
3001 |
$dur = DateTime::Duration->new( days => $loanlength->{$length_key}); |
3020 |
->truncate( to => 'minute' ); |
3002 |
} |
3021 |
if ( $loanlength->{lengthunit} eq 'hours' ) { |
3003 |
if (ref $startdate ne 'DateTime' ) { |
3022 |
$dt->add( hours => $loanlength->{issuelength} ); |
3004 |
$startdate = dt_from_string($startdate); |
3023 |
} else { # days |
3005 |
} |
3024 |
$dt->add( days => $loanlength->{issuelength} ); |
3006 |
my $calendar = Koha::Calendar->new( branchcode => $branch ); |
3025 |
$dt->set_hour(23); |
3007 |
$datedue = $calendar->addDate( $startdate, $dur, $loanlength->{lengthunit} ); |
3026 |
$dt->set_minute(59); |
3008 |
if ($loanlength->{lengthunit} eq 'days') { |
3027 |
} |
3009 |
$datedue->set_hour(23); |
3028 |
# break |
3010 |
$datedue->set_minute(59); |
3029 |
return $dt; |
|
|
3030 |
|
3031 |
} else { |
3032 |
my $dur; |
3033 |
if ($loanlength->{lengthunit} eq 'hours') { |
3034 |
$dur = DateTime::Duration->new( hours => $loanlength->{issuelength}); |
3035 |
} |
3036 |
else { # days |
3037 |
$dur = DateTime::Duration->new( days => $loanlength->{issuelength}); |
3038 |
} |
3039 |
if (ref $startdate ne 'DateTime' ) { |
3040 |
$startdate = dt_from_string($startdate); |
3041 |
} |
3042 |
my $calendar = Koha::Calendar->new( branchcode => $branch ); |
3043 |
$datedue = $calendar->addDate( $startdate, $dur, $loanlength->{lengthunit} ); |
3044 |
if ($loanlength->{lengthunit} eq 'days') { |
3045 |
$datedue->set_hour(23); |
3046 |
$datedue->set_minute(59); |
3047 |
} |
3048 |
} |
3011 |
} |
3049 |
} |
3012 |
} |
3050 |
|
3013 |
|