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