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

(-)a/C4/Reserves.pm (+28 lines)
Lines 48-53 use Koha::IssuingRules; Link Here
48
use Koha::Items;
48
use Koha::Items;
49
use Koha::ItemTypes;
49
use Koha::ItemTypes;
50
use Koha::Patrons;
50
use Koha::Patrons;
51
use Koha::CirculationRules;
51
52
52
use List::MoreUtils qw( firstidx any );
53
use List::MoreUtils qw( firstidx any );
53
use Carp;
54
use Carp;
Lines 398-403 sub CanItemBeReserved { Link Here
398
        return 'tooManyReserves';
399
        return 'tooManyReserves';
399
    }
400
    }
400
401
402
    # Now we need to check hold limits by patron category
403
    my $rule = Koha::CirculationRules->find(
404
        {
405
            categorycode => $borrower->{categorycode},
406
            branchcode   => $borrower->{branchcode},
407
            itemtype     => undef,
408
            rule_name    => 'max_holds',
409
        }
410
    );
411
    $rule ||= Koha::CirculationRules->find(
412
        {
413
            categorycode => $borrower->{categorycode},
414
            branchcode   => undef,,
415
            itemtype     => undef,
416
            rule_name    => 'max_holds',
417
        }
418
    );
419
    if ( $rule ) {
420
        my $total_holds_count = Koha::Holds->search(
421
            {
422
                borrowernumber => $borrower->{borrowernumber}
423
            }
424
        )->count();
425
426
        return 'tooManyReserves' if $total_holds_count >= $rule->rule_value;
427
    }
428
401
    my $circ_control_branch =
429
    my $circ_control_branch =
402
      C4::Circulation::_GetCircControlBranch( $item->unblessed(), $borrower );
430
      C4::Circulation::_GetCircControlBranch( $item->unblessed(), $borrower );
403
    my $branchitemrule =
431
    my $branchitemrule =
(-)a/admin/smart-rules.pl (-4 / +44 lines)
Lines 32-37 use Koha::Logger; Link Here
32
use Koha::RefundLostItemFeeRule;
32
use Koha::RefundLostItemFeeRule;
33
use Koha::RefundLostItemFeeRules;
33
use Koha::RefundLostItemFeeRules;
34
use Koha::Libraries;
34
use Koha::Libraries;
35
use Koha::CirculationRules;
35
use Koha::Patron::Categories;
36
use Koha::Patron::Categories;
36
37
37
my $input = CGI->new;
38
my $input = CGI->new;
Lines 93-98 elsif ($op eq 'delete-branch-cat') { Link Here
93
                                        AND categorycode = ?");
94
                                        AND categorycode = ?");
94
        $sth_delete->execute($branch, $categorycode);
95
        $sth_delete->execute($branch, $categorycode);
95
    }
96
    }
97
    Koha::CirculationRules->set_rule(
98
        {
99
            branchcode   => $branch,
100
            categorycode => $categorycode,
101
            itemtype     => undef,
102
            rule_name    => 'max_holds',
103
            rule_value   => undef,
104
        }
105
    );
96
}
106
}
97
elsif ($op eq 'delete-branch-item') {
107
elsif ($op eq 'delete-branch-item') {
98
    my $itemtype  = $input->param('itemtype');
108
    my $itemtype  = $input->param('itemtype');
Lines 277-286 elsif ($op eq "add-branch-cat") { Link Here
277
            $sth_search->execute();
287
            $sth_search->execute();
278
            my $res = $sth_search->fetchrow_hashref();
288
            my $res = $sth_search->fetchrow_hashref();
279
            if ($res->{total}) {
289
            if ($res->{total}) {
280
                $sth_update->execute($maxissueqty, $maxonsiteissueqty);
290
                $sth_update->execute( $maxissueqty, $maxonsiteissueqty );
281
            } else {
291
            } else {
282
                $sth_insert->execute($maxissueqty, $maxonsiteissueqty);
292
                $sth_insert->execute( $maxissueqty, $maxonsiteissueqty );
283
            }
293
            }
294
295
            Koha::CirculationRules->set_rule(
296
                {
297
                    branchcode   => undef,
298
                    categorycode => undef,
299
                    itemtype     => undef,
300
                    rule_name    => 'max_holds',
301
                    rule_value   => $max_holds,
302
                }
303
            );
284
        } else {
304
        } else {
285
            my $sth_search = $dbh->prepare("SELECT count(*) AS total
305
            my $sth_search = $dbh->prepare("SELECT count(*) AS total
286
                                            FROM default_borrower_circ_rules
306
                                            FROM default_borrower_circ_rules
Lines 299-308 elsif ($op eq "add-branch-cat") { Link Here
299
            $sth_search->execute($categorycode);
319
            $sth_search->execute($categorycode);
300
            my $res = $sth_search->fetchrow_hashref();
320
            my $res = $sth_search->fetchrow_hashref();
301
            if ($res->{total}) {
321
            if ($res->{total}) {
302
                $sth_update->execute($maxissueqty, $maxonsiteissueqty, $categorycode);
322
                $sth_update->execute( $maxissueqty, $maxonsiteissueqty, $categorycode );
303
            } else {
323
            } else {
304
                $sth_insert->execute($categorycode, $maxissueqty, $maxonsiteissueqty);
324
                $sth_insert->execute( $categorycode, $maxissueqty, $maxonsiteissueqty );
305
            }
325
            }
326
327
            Koha::CirculationRules->set_rule(
328
                {
329
                    branchcode   => undef,
330
                    categorycode => $categorycode,
331
                    itemtype     => undef,
332
                    rule_name    => 'max_holds',
333
                    rule_value   => $max_holds,
334
                }
335
            );
306
        }
336
        }
307
    } elsif ($categorycode eq "*") {
337
    } elsif ($categorycode eq "*") {
308
        my $sth_search = $dbh->prepare("SELECT count(*) AS total
338
        my $sth_search = $dbh->prepare("SELECT count(*) AS total
Lines 351-356 elsif ($op eq "add-branch-cat") { Link Here
351
        } else {
381
        } else {
352
            $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
382
            $sth_insert->execute($branch, $categorycode, $maxissueqty, $maxonsiteissueqty);
353
        }
383
        }
384
385
        Koha::CirculationRules->set_rule(
386
            {
387
                branchcode   => $branch,
388
                categorycode => $categorycode,
389
                itemtype     => undef,
390
                rule_name    => 'max_holds',
391
                rule_value   => $max_holds,
392
            }
393
        );
354
    }
394
    }
355
}
395
}
356
elsif ($op eq "add-branch-item") {
396
elsif ($op eq "add-branch-item") {
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt (+9 lines)
Lines 1-4 Link Here
1
[% USE Branches %]
1
[% USE Branches %]
2
[% USE CirculationRules %]
2
[% SET footerjs = 1 %]
3
[% SET footerjs = 1 %]
3
[% INCLUDE 'doc-head-open.inc' %]
4
[% INCLUDE 'doc-head-open.inc' %]
4
<title>Koha &rsaquo; Administration &rsaquo; Circulation and fine rules</title>
5
<title>Koha &rsaquo; Administration &rsaquo; Circulation and fine rules</title>
Lines 490-495 Link Here
490
                                [% branch_cat_rule_loo.maxonsiteissueqty %]
491
                                [% branch_cat_rule_loo.maxonsiteissueqty %]
491
                            [% END %]
492
                            [% END %]
492
                        </td>
493
                        </td>
494
                        <td>
495
                            [% SET rule_value = CirculationRules.Get( branch_cat_rule_loo.branchcode, branch_cat_rule_loo.categorycode, branch_cat_rule_loo.itemtype, 'max_holds' ) %]
496
                            [% IF rule_value  %]
497
                                [% rule_value %]
498
                            [% ELSE %]
499
                                Unlimited
500
                            [% END %]
501
                        </td>
493
502
494
                        <td class="actions">
503
                        <td class="actions">
495
                            <a class="btn btn-default btn-xs delete" href="/cgi-bin/koha/admin/smart-rules.pl?op=delete-branch-cat&amp;categorycode=[% branch_cat_rule_loo.categorycode %]&amp;branch=[% current_branch %]"><i class="fa fa-trash"></i> Delete</a>
504
                            <a class="btn btn-default btn-xs delete" href="/cgi-bin/koha/admin/smart-rules.pl?op=delete-branch-cat&amp;categorycode=[% branch_cat_rule_loo.categorycode %]&amp;branch=[% current_branch %]"><i class="fa fa-trash"></i> Delete</a>
(-)a/t/db_dependent/Holds.t (-2 / +75 lines)
Lines 7-13 use t::lib::TestBuilder; Link Here
7
7
8
use C4::Context;
8
use C4::Context;
9
9
10
use Test::More tests => 55;
10
use Test::More tests => 56;
11
use MARC::Record;
11
use MARC::Record;
12
use C4::Biblio;
12
use C4::Biblio;
13
use C4::Items;
13
use C4::Items;
Lines 18-23 use Koha::DateUtils qw( dt_from_string output_pref ); Link Here
18
use Koha::Biblios;
18
use Koha::Biblios;
19
use Koha::Holds;
19
use Koha::Holds;
20
use Koha::Patrons;
20
use Koha::Patrons;
21
use Koha::CirculationRules;
21
22
22
BEGIN {
23
BEGIN {
23
    use FindBin;
24
    use FindBin;
Lines 411-416 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, ); Link Here
411
is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
412
is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
412
    'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
413
    'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
413
414
415
subtest 'Test max_holds per library/patron category' => sub {
416
    plan tests => 6;
417
418
    $dbh->do('DELETE FROM reserves');
419
    $dbh->do('DELETE FROM issuingrules');
420
    $dbh->do('DELETE FROM circulation_rules');
421
422
    ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('TEST');
423
    ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
424
      AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
425
        $bibnum );
426
    $dbh->do(
427
        q{
428
            INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
429
            VALUES (?, ?, ?, ?, ?)
430
        },
431
        {},
432
        '*', '*', 'TEST', 99, 99
433
    );
434
    AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
435
    AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
436
    AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
437
438
    my $count =
439
      Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
440
    is( $count, 3, 'Patron now has 3 holds' );
441
442
    my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
443
    is( $ret, 'OK', 'Patron can place hold with no borrower circ rules' );
444
445
    my $rule_all = Koha::CirculationRules->set_rule(
446
        {
447
            categorycode => $category->{categorycode},
448
            branchcode   => undef,
449
            itemtype     => undef,
450
            rule_name    => 'max_holds',
451
            rule_value   => 3,
452
        }
453
    );
454
455
    my $rule_branch = Koha::CirculationRules->set_rule(
456
        {
457
            branchcode   => $branch_1,
458
            categorycode => $category->{categorycode},
459
            itemtype     => undef,
460
            rule_name    => 'max_holds',
461
            rule_value   => 5,
462
        }
463
    );
464
465
    $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
466
    is( $ret, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
467
468
    $rule_branch->delete();
469
470
    $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
471
    is( $ret, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
472
473
    $rule_all->delete();
474
    $rule_branch->rule_value(3);
475
    $rule_branch->store();
476
477
    $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
478
    is( $ret, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
479
480
    $rule_branch->rule_value(5);
481
    $rule_branch->update();
482
    $rule_branch->rule_value(5);
483
    $rule_branch->store();
484
485
    $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
486
    is( $ret, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
487
};
414
488
415
# Helper method to set up a Biblio.
489
# Helper method to set up a Biblio.
416
sub create_helper_biblio {
490
sub create_helper_biblio {
417
- 

Return to bug 18887