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