View | Details | Raw Unified | Return to bug 14695
Collapse All | Expand All

(-)a/C4/Reserves.pm (-57 / +64 lines)
Lines 446-519 sub CanBookBeReserved{ Link Here
446
446
447
=cut
447
=cut
448
448
449
sub CanItemBeReserved{
449
sub CanItemBeReserved {
450
    my ($borrowernumber, $itemnumber) = @_;
450
    my ( $borrowernumber, $itemnumber ) = @_;
451
451
452
    my $dbh             = C4::Context->dbh;
452
    my $dbh = C4::Context->dbh;
453
    my $ruleitemtype; # itemtype of the matching issuing rule
453
    my $ruleitemtype;    # itemtype of the matching issuing rule
454
    my $allowedreserves = 0;
454
    my $allowedreserves = 0;
455
            
455
456
    # we retrieve borrowers and items informations #
456
    # we retrieve borrowers and items informations #
457
    # item->{itype} will come for biblioitems if necessery
457
    # item->{itype} will come for biblioitems if necessery
458
    my $item = GetItem($itemnumber);
458
    my $item       = GetItem($itemnumber);
459
    my $biblioData = C4::Biblio::GetBiblioData( $item->{biblionumber} );
459
    my $biblioData = C4::Biblio::GetBiblioData( $item->{biblionumber} );
460
    my $borrower = C4::Members::GetMember('borrowernumber'=>$borrowernumber);
460
    my $borrower   = C4::Members::GetMember( 'borrowernumber' => $borrowernumber );
461
461
462
    # If an item is damaged and we don't allow holds on damaged items, we can stop right here
462
    # If an item is damaged and we don't allow holds on damaged items, we can stop right here
463
    return 'damaged' if ( $item->{damaged} && !C4::Context->preference('AllowHoldsOnDamagedItems') );
463
    return 'damaged'
464
      if ( $item->{damaged}
465
        && !C4::Context->preference('AllowHoldsOnDamagedItems') );
464
466
465
    #Check for the age restriction
467
    #Check for the age restriction
466
    my ($ageRestriction, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction( $biblioData->{agerestriction}, $borrower );
468
    my ( $ageRestriction, $daysToAgeRestriction ) =
469
      C4::Circulation::GetAgeRestriction( $biblioData->{agerestriction}, $borrower );
467
    return 'ageRestricted' if $daysToAgeRestriction && $daysToAgeRestriction > 0;
470
    return 'ageRestricted' if $daysToAgeRestriction && $daysToAgeRestriction > 0;
468
471
469
    my $controlbranch = C4::Context->preference('ReservesControlBranch');
472
    my $controlbranch = C4::Context->preference('ReservesControlBranch');
470
473
471
    # we retrieve user rights on this itemtype and branchcode
474
    # we retrieve user rights on this itemtype and branchcode
472
    my $sth = $dbh->prepare("SELECT categorycode, itemtype, branchcode, reservesallowed
475
    my $sth = $dbh->prepare(
473
                             FROM issuingrules
476
        q{
474
                             WHERE (categorycode in (?,'*') )
477
         SELECT categorycode, itemtype, branchcode, reservesallowed
475
                             AND (itemtype IN (?,'*'))
478
           FROM issuingrules
476
                             AND (branchcode IN (?,'*'))
479
          WHERE (categorycode in (?,'*') )
477
                             ORDER BY
480
            AND (itemtype IN (?,'*'))
478
                               categorycode DESC,
481
            AND (branchcode IN (?,'*'))
479
                               itemtype     DESC,
482
       ORDER BY categorycode DESC,
480
                               branchcode   DESC;"
483
                itemtype     DESC,
481
                           );
484
                branchcode   DESC
482
485
        }
483
    my $querycount ="SELECT
486
    );
484
                            count(*) as count
487
485
                            FROM reserves
488
    my $querycount = q{
486
                                LEFT JOIN items USING (itemnumber)
489
        SELECT count(*) AS count
487
                                LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
490
          FROM reserves
488
                                LEFT JOIN borrowers USING (borrowernumber)
491
     LEFT JOIN items USING (itemnumber)
489
                            WHERE borrowernumber = ?
492
     LEFT JOIN biblioitems ON (reserves.biblionumber=biblioitems.biblionumber)
490
                                ";
493
     LEFT JOIN borrowers USING (borrowernumber)
491
    
494
         WHERE borrowernumber = ?
492
    
495
    };
493
    my $branchcode   = "";
494
    my $branchfield  = "reserves.branchcode";
495
496
496
    if( $controlbranch eq "ItemHomeLibrary" ){
497
    my $branchcode  = "";
498
    my $branchfield = "reserves.branchcode";
499
500
    if ( $controlbranch eq "ItemHomeLibrary" ) {
497
        $branchfield = "items.homebranch";
501
        $branchfield = "items.homebranch";
498
        $branchcode = $item->{homebranch};
502
        $branchcode  = $item->{homebranch};
499
    }elsif( $controlbranch eq "PatronLibrary" ){
503
    }
504
    elsif ( $controlbranch eq "PatronLibrary" ) {
500
        $branchfield = "borrowers.branchcode";
505
        $branchfield = "borrowers.branchcode";
501
        $branchcode = $borrower->{branchcode};
506
        $branchcode  = $borrower->{branchcode};
502
    }
507
    }
503
    
508
504
    # we retrieve rights 
509
    # we retrieve rights
505
    $sth->execute($borrower->{'categorycode'}, $item->{'itype'}, $branchcode);
510
    $sth->execute( $borrower->{'categorycode'}, $item->{'itype'}, $branchcode );
506
    if(my $rights = $sth->fetchrow_hashref()){
511
    if ( my $rights = $sth->fetchrow_hashref() ) {
507
        $ruleitemtype    = $rights->{itemtype};
512
        $ruleitemtype    = $rights->{itemtype};
508
        $allowedreserves = $rights->{reservesallowed}; 
513
        $allowedreserves = $rights->{reservesallowed};
509
    }else{
514
    }
515
    else {
510
        $ruleitemtype = '*';
516
        $ruleitemtype = '*';
511
    }
517
    }
512
518
513
    # we retrieve count
519
    # we retrieve count
514
520
515
    $querycount .= "AND $branchfield = ?";
521
    $querycount .= "AND $branchfield = ?";
516
    
522
517
    # If using item-level itypes, fall back to the record
523
    # If using item-level itypes, fall back to the record
518
    # level itemtype if the hold has no associated item
524
    # level itemtype if the hold has no associated item
519
    $querycount .=
525
    $querycount .=
Lines 523-548 sub CanItemBeReserved{ Link Here
523
      if ( $ruleitemtype ne "*" );
529
      if ( $ruleitemtype ne "*" );
524
530
525
    my $sthcount = $dbh->prepare($querycount);
531
    my $sthcount = $dbh->prepare($querycount);
526
    
532
527
    if($ruleitemtype eq "*"){
533
    if ( $ruleitemtype eq "*" ) {
528
        $sthcount->execute($borrowernumber, $branchcode);
534
        $sthcount->execute( $borrowernumber, $branchcode );
529
    }else{
535
    }
530
        $sthcount->execute($borrowernumber, $branchcode, $ruleitemtype);
536
    else {
537
        $sthcount->execute( $borrowernumber, $branchcode, $ruleitemtype );
531
    }
538
    }
532
539
533
    my $reservecount = "0";
540
    my $reservecount = "0";
534
    if(my $rowcount = $sthcount->fetchrow_hashref()){
541
    if ( my $rowcount = $sthcount->fetchrow_hashref() ) {
535
        $reservecount = $rowcount->{count};
542
        $reservecount = $rowcount->{count};
536
    }
543
    }
544
537
    # we check if it's ok or not
545
    # we check if it's ok or not
538
    if( $reservecount >= $allowedreserves ){
546
    if ( $reservecount >= $allowedreserves ) {
539
        return 'tooManyReserves';
547
        return 'tooManyReserves';
540
    }
548
    }
541
549
542
    my $circ_control_branch = C4::Circulation::_GetCircControlBranch($item,
550
    my $circ_control_branch =
543
        $borrower);
551
      C4::Circulation::_GetCircControlBranch( $item, $borrower );
544
    my $branchitemrule = C4::Circulation::GetBranchItemRule($circ_control_branch,
552
    my $branchitemrule =
545
        $item->{itype});
553
      C4::Circulation::GetBranchItemRule( $circ_control_branch, $item->{itype} );
546
554
547
    if ( $branchitemrule->{holdallowed} == 0 ) {
555
    if ( $branchitemrule->{holdallowed} == 0 ) {
548
        return 'notReservable';
556
        return 'notReservable';
Lines 551-557 sub CanItemBeReserved{ Link Here
551
    if (   $branchitemrule->{holdallowed} == 1
559
    if (   $branchitemrule->{holdallowed} == 1
552
        && $borrower->{branchcode} ne $item->{homebranch} )
560
        && $borrower->{branchcode} ne $item->{homebranch} )
553
    {
561
    {
554
          return 'cannotReserveFromOtherBranches';
562
        return 'cannotReserveFromOtherBranches';
555
    }
563
    }
556
564
557
    # If reservecount is ok, we check item branch if IndependentBranches is ON
565
    # If reservecount is ok, we check item branch if IndependentBranches is ON
Lines 560-566 sub CanItemBeReserved{ Link Here
560
        and !C4::Context->preference('canreservefromotherbranches') )
568
        and !C4::Context->preference('canreservefromotherbranches') )
561
    {
569
    {
562
        my $itembranch = $item->{homebranch};
570
        my $itembranch = $item->{homebranch};
563
        if ($itembranch ne $borrower->{branchcode}) {
571
        if ( $itembranch ne $borrower->{branchcode} ) {
564
            return 'cannotReserveFromOtherBranches';
572
            return 'cannotReserveFromOtherBranches';
565
        }
573
        }
566
    }
574
    }
567
- 

Return to bug 14695