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-2492
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 |
if ( ( $data1->{renewalsallowed} && $data1->{renewalsallowed} > $data1->{renewals} ) || $override_limit ) { |
2477 |
$renewokay = 1; |
2478 |
} |
2479 |
else { |
2480 |
$error = "too_many"; |
2481 |
} |
2482 |
|
2446 |
|
2483 |
my $resstatus = C4::Reserves::GetReserveStatus($itemnumber); |
2447 |
my $issuingrule = GetIssuingRule($borrower->{categorycode}, $item->{itype}, $branchcode); |
2484 |
if ( $resstatus eq "Waiting" or $resstatus eq "Reserved" ) { |
2448 |
|
2485 |
$renewokay = 0; |
2449 |
if ( ( $issuingrule->{renewalsallowed} > $itemissue->{renewals} ) || $override_limit ) { |
2486 |
$error = "on_reserve"; |
2450 |
$renewokay = 1; |
2487 |
} |
2451 |
} else { |
|
|
2452 |
$error = "too_many"; |
2488 |
} |
2453 |
} |
2489 |
return ($renewokay,$error); |
2454 |
|
|
|
2455 |
my $resstatus = C4::Reserves::GetReserveStatus($itemnumber); |
2456 |
if ( $resstatus eq "Waiting" or $resstatus eq "Reserved" ) { |
2457 |
$renewokay = 0; |
2458 |
$error = "on_reserve"; |
2459 |
} |
2460 |
|
2461 |
return ( $renewokay, $error ); |
2490 |
} |
2462 |
} |
2491 |
|
2463 |
|
2492 |
=head2 AddRenewal |
2464 |
=head2 AddRenewal |
Lines 2547-2553
sub AddRenewal {
Link Here
|
2547 |
$datedue = (C4::Context->preference('RenewalPeriodBase') eq 'date_due') ? |
2519 |
$datedue = (C4::Context->preference('RenewalPeriodBase') eq 'date_due') ? |
2548 |
$issuedata->{date_due} : |
2520 |
$issuedata->{date_due} : |
2549 |
DateTime->now( time_zone => C4::Context->tz()); |
2521 |
DateTime->now( time_zone => C4::Context->tz()); |
2550 |
$datedue = CalcDateDue($datedue,$itemtype,$issuedata->{'branchcode'},$borrower); |
2522 |
$datedue = CalcDateDue($datedue, $itemtype, $issuedata->{'branchcode'}, $borrower, 'is a renewal'); |
2551 |
} |
2523 |
} |
2552 |
|
2524 |
|
2553 |
# 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 3016-3073
C<$borrower> = Borrower object
Link Here
|
3016 |
=cut |
2988 |
=cut |
3017 |
|
2989 |
|
3018 |
sub CalcDateDue { |
2990 |
sub CalcDateDue { |
3019 |
my ( $startdate, $itemtype, $branch, $borrower ) = @_; |
2991 |
my ( $startdate, $itemtype, $branch, $borrower, $isrenewal ) = @_; |
|
|
2992 |
|
2993 |
$isrenewal ||= 0; |
3020 |
|
2994 |
|
3021 |
# loanlength now a href |
2995 |
# loanlength now a href |
3022 |
my $loanlength = |
2996 |
my $loanlength = |
3023 |
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}; |
3024 |
|
3002 |
|
3025 |
my $datedue; |
3003 |
my $datedue; |
3026 |
|
3004 |
|
3027 |
# if globalDueDate ON the datedue is set to that date |
3005 |
# calculate the datedue as normal |
3028 |
if (C4::Context->preference('globalDueDate') |
3006 |
if ( C4::Context->preference('useDaysMode') eq 'Days' ) |
3029 |
&& ( C4::Context->preference('globalDueDate') =~ |
3007 |
{ # ignoring calendar |
3030 |
C4::Dates->regexp('syspref') ) |
3008 |
my $dt = |
3031 |
) { |
3009 |
DateTime->now( time_zone => C4::Context->tz() ) |
3032 |
$datedue = dt_from_string( |
3010 |
->truncate( to => 'minute' ); |
3033 |
C4::Context->preference('globalDueDate'), |
3011 |
if ( $loanlength->{lengthunit} eq 'hours' ) { |
3034 |
C4::Context->preference('dateformat') |
3012 |
$dt->add( hours => $loanlength->{$length_key} ); |
3035 |
); |
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; |
3036 |
} else { |
3020 |
} else { |
3037 |
|
3021 |
my $dur; |
3038 |
# otherwise, calculate the datedue as normal |
3022 |
if ($loanlength->{lengthunit} eq 'hours') { |
3039 |
if ( C4::Context->preference('useDaysMode') eq 'Days' ) |
3023 |
$dur = DateTime::Duration->new( hours => $loanlength->{$length_key}); |
3040 |
{ # ignoring calendar |
3024 |
} |
3041 |
my $dt = |
3025 |
else { # days |
3042 |
DateTime->now( time_zone => C4::Context->tz() ) |
3026 |
$dur = DateTime::Duration->new( days => $loanlength->{$length_key}); |
3043 |
->truncate( to => 'minute' ); |
3027 |
} |
3044 |
if ( $loanlength->{lengthunit} eq 'hours' ) { |
3028 |
if (ref $startdate ne 'DateTime' ) { |
3045 |
$dt->add( hours => $loanlength->{issuelength} ); |
3029 |
$startdate = dt_from_string($startdate); |
3046 |
} else { # days |
3030 |
} |
3047 |
$dt->add( days => $loanlength->{issuelength} ); |
3031 |
my $calendar = Koha::Calendar->new( branchcode => $branch ); |
3048 |
$dt->set_hour(23); |
3032 |
$datedue = $calendar->addDate( $startdate, $dur, $loanlength->{lengthunit} ); |
3049 |
$dt->set_minute(59); |
3033 |
if ($loanlength->{lengthunit} eq 'days') { |
3050 |
} |
3034 |
$datedue->set_hour(23); |
3051 |
# break |
3035 |
$datedue->set_minute(59); |
3052 |
return $dt; |
|
|
3053 |
|
3054 |
} else { |
3055 |
my $dur; |
3056 |
if ($loanlength->{lengthunit} eq 'hours') { |
3057 |
$dur = DateTime::Duration->new( hours => $loanlength->{issuelength}); |
3058 |
} |
3059 |
else { # days |
3060 |
$dur = DateTime::Duration->new( days => $loanlength->{issuelength}); |
3061 |
} |
3062 |
if (ref $startdate ne 'DateTime' ) { |
3063 |
$startdate = dt_from_string($startdate); |
3064 |
} |
3065 |
my $calendar = Koha::Calendar->new( branchcode => $branch ); |
3066 |
$datedue = $calendar->addDate( $startdate, $dur, $loanlength->{lengthunit} ); |
3067 |
if ($loanlength->{lengthunit} eq 'days') { |
3068 |
$datedue->set_hour(23); |
3069 |
$datedue->set_minute(59); |
3070 |
} |
3071 |
} |
3036 |
} |
3072 |
} |
3037 |
} |
3073 |
|
3038 |
|