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

(-)a/C4/ILSDI/Services.pm (-2 / +10 lines)
Lines 26-32 use C4::Circulation; Link Here
26
use C4::Branch;
26
use C4::Branch;
27
use C4::Accounts;
27
use C4::Accounts;
28
use C4::Biblio;
28
use C4::Biblio;
29
use C4::Reserves qw(AddReserve GetReservesFromBiblionumber GetReservesFromBorrowernumber CanBookBeReserved CanItemBeReserved IsAvailableForItemLevelRequest);
29
use C4::Reserves qw(AddReserve GetReservesFromBiblionumber GetReservesFromBorrowernumber CanBookBeReserved CanItemBeReserved IsAvailableForItemLevelRequest AlreadyReserved ReachedMaxReserves);
30
use C4::Context;
30
use C4::Context;
31
use C4::AuthoritiesMarc;
31
use C4::AuthoritiesMarc;
32
use XML::Simple;
32
use XML::Simple;
Lines 605-613 sub HoldTitle { Link Here
605
    my $biblionumber = $cgi->param('bib_id');
605
    my $biblionumber = $cgi->param('bib_id');
606
    my $biblio = GetBiblio( $biblionumber );
606
    my $biblio = GetBiblio( $biblionumber );
607
    return { code => 'RecordNotFound' } unless $$biblio{biblionumber};
607
    return { code => 'RecordNotFound' } unless $$biblio{biblionumber};
608
    
608
609
    my $title = $$biblio{title};
609
    my $title = $$biblio{title};
610
610
611
    return { code => 'NotHoldable' } if ReachedMaxReserves($borrowernumber);
612
613
    return { code => 'NotHoldable' } if AlreadyReserved($biblionumber, $borrowernumber);
614
611
    # Check if the biblio can be reserved
615
    # Check if the biblio can be reserved
612
    return { code => 'NotHoldable' } unless CanBookBeReserved( $borrowernumber, $biblionumber ) eq 'OK';
616
    return { code => 'NotHoldable' } unless CanBookBeReserved( $borrowernumber, $biblionumber ) eq 'OK';
613
617
Lines 684-689 sub HoldItem { Link Here
684
    # If the biblio does not match the item, return an error code
688
    # If the biblio does not match the item, return an error code
685
    return { code => 'RecordNotFound' } if $$item{biblionumber} ne $$biblio{biblionumber};
689
    return { code => 'RecordNotFound' } if $$item{biblionumber} ne $$biblio{biblionumber};
686
690
691
    return { code => 'NotHoldable' } if ReachedMaxReserves($borrowernumber);
692
693
    return { code => 'NotHoldable' } if AlreadyReserved($biblionumber, $borrowernumber);
694
687
    # Check for item disponibility
695
    # Check for item disponibility
688
    my $canitembereserved = C4::Reserves::CanItemBeReserved( $borrowernumber, $itemnumber );
696
    my $canitembereserved = C4::Reserves::CanItemBeReserved( $borrowernumber, $itemnumber );
689
    my $canbookbereserved = C4::Reserves::CanBookBeReserved( $borrowernumber, $biblionumber );
697
    my $canbookbereserved = C4::Reserves::CanBookBeReserved( $borrowernumber, $biblionumber );
(-)a/C4/Reserves.pm (+50 lines)
Lines 126-131 BEGIN { Link Here
126
        &CancelReserve
126
        &CancelReserve
127
        &CancelExpiredReserves
127
        &CancelExpiredReserves
128
128
129
        &AlreadyReserved
130
        &ReachedMaxReserves
131
129
        &AutoUnsuspendReserves
132
        &AutoUnsuspendReserves
130
133
131
        &IsAvailableForItemLevelRequest
134
        &IsAvailableForItemLevelRequest
Lines 1017-1022 sub CancelExpiredReserves { Link Here
1017
1020
1018
}
1021
}
1019
1022
1023
=head2 AlreadyReserved
1024
1025
    $alreadyreserved = AlreadyReserved($biblionumber, $borrowernumber);
1026
1027
    return 1 if borrower already reserved this biblio.
1028
1029
=cut
1030
1031
sub AlreadyReserved {
1032
    my ($biblionumber, $borrowernumber) = @_;
1033
1034
    return 0 unless $biblionumber && $borrowernumber;
1035
1036
    my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
1037
1038
    return 0 unless scalar( @$reserves );
1039
1040
    foreach my $res (@$reserves) {
1041
        return 1 if $borrowernumber eq $res->{borrowernumber};
1042
    }
1043
1044
    return 0;
1045
}
1046
1047
=head2 ReachedMaxReserves
1048
1049
    $hasmaxreserves = ReachedMaxReserves($borrowernumber);
1050
1051
    return 1 if borrower reached maxreserves.
1052
1053
=cut
1054
1055
sub ReachedMaxReserves {
1056
    my ($borrowernumber, $number_wanted) = @_;
1057
    $number_wanted ||= 1;
1058
1059
    my $number_reserves = GetReserveCount( $borrowernumber );
1060
1061
    return 0 unless C4::Context->preference('maxreserves');
1062
1063
    if ( $number_reserves + $number_wanted >= C4::Context->preference('maxreserves') ) {
1064
        return 1;
1065
    }
1066
1067
    return 0;
1068
}
1069
1020
=head2 AutoUnsuspendReserves
1070
=head2 AutoUnsuspendReserves
1021
1071
1022
  AutoUnsuspendReserves();
1072
  AutoUnsuspendReserves();
(-)a/reserve/request.pl (-12 / +5 lines)
Lines 151-159 if ($borrowernumber_hold && !$action) { Link Here
151
    my $new_reserves_count = scalar( @biblionumbers );
151
    my $new_reserves_count = scalar( @biblionumbers );
152
152
153
    my $maxreserves = C4::Context->preference('maxreserves');
153
    my $maxreserves = C4::Context->preference('maxreserves');
154
    if ( $maxreserves
154
    if ( ReachedMaxReserves($borrowerinfo->{'borrowernumber'}, $new_reserves_count) ) {
155
        && ( $reserves_count + $new_reserves_count > $maxreserves ) )
156
    {
157
        my $new_reserves_allowed =
155
        my $new_reserves_allowed =
158
            $maxreserves - $reserves_count > 0
156
            $maxreserves - $reserves_count > 0
159
          ? $maxreserves - $reserves_count
157
          ? $maxreserves - $reserves_count
Lines 243-265 foreach my $biblionumber (@biblionumbers) { Link Here
243
    my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
241
    my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
244
    my $count = scalar( @$reserves );
242
    my $count = scalar( @$reserves );
245
    my $totalcount = $count;
243
    my $totalcount = $count;
246
    my $holds_count = 0;
247
    my $alreadyreserved = 0;
244
    my $alreadyreserved = 0;
248
245
249
    foreach my $res (@$reserves) {
246
    foreach my $res (@$reserves) {
250
        if ( defined $res->{found} ) { # found can be 'W' or 'T'
247
        if ( defined $res->{found} ) { # found can be 'W' or 'T'
251
            $count--;
248
            $count--;
252
        }
249
        }
253
254
        if ( defined $borrowerinfo && defined($borrowerinfo->{borrowernumber}) && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
255
            $holds_count++;
256
        }
257
    }
250
    }
258
251
259
    if ( $holds_count ) {
252
    if ( AlreadyReserved($biblionumber, $borrowerinfo->{borrowernumber}) ) {
260
            $alreadyreserved = 1;
253
        $alreadyreserved = 1;
261
            $biblioloopiter{warn} = 1;
254
        $biblioloopiter{warn} = 1;
262
            $biblioloopiter{alreadyres} = 1;
255
        $biblioloopiter{alreadyres} = 1;
263
    }
256
    }
264
257
265
    $template->param(
258
    $template->param(
(-)a/t/db_dependent/ILSDI_Services.t (-2 / +74 lines)
Lines 4-12 use Modern::Perl; Link Here
4
4
5
use C4::Members qw/AddMember GetMember GetBorrowercategory/;
5
use C4::Members qw/AddMember GetMember GetBorrowercategory/;
6
use C4::Branch;
6
use C4::Branch;
7
use C4::Biblio;
8
use C4::Items;
9
use C4::Reserves;
7
use CGI qw ( -utf8 );
10
use CGI qw ( -utf8 );
11
use t::lib::Mocks;
8
12
9
use Test::More tests => 15;
13
use Test::More tests => 23;
10
14
11
BEGIN {
15
BEGIN {
12
    use_ok('C4::ILSDI::Services');
16
    use_ok('C4::ILSDI::Services');
Lines 18-23 my $dbh = C4::Context->dbh; Link Here
18
$dbh->{AutoCommit} = 0;
22
$dbh->{AutoCommit} = 0;
19
$dbh->{RaiseError} = 1;
23
$dbh->{RaiseError} = 1;
20
24
25
my $maxallowedreserves = 3;
26
t::lib::Mocks::mock_preference('maxreserves', $maxallowedreserves);
27
21
# Create patron
28
# Create patron
22
my %data = (
29
my %data = (
23
    firstname =>  'my firstname',
30
    firstname =>  'my firstname',
Lines 29-34 my %data = ( Link Here
29
    password => 'ilsdi-password',
36
    password => 'ilsdi-password',
30
);
37
);
31
38
39
my %data2 = (
40
    firstname =>  'Joe',
41
    surname => 'John',
42
    categorycode => 'UT',
43
    branchcode => 'UT',
44
    cardnumber => '99999',
45
    userid => 'jjohn',
46
    password => 'ilsdi-password',
47
);
48
32
# Crate patron category
49
# Crate patron category
33
unless ( GetBorrowercategory('UT') ) {
50
unless ( GetBorrowercategory('UT') ) {
34
    $dbh->do("INSERT INTO categories
51
    $dbh->do("INSERT INTO categories
Lines 44-51 unless ( GetBranchDetail('UT') ) { Link Here
44
61
45
62
46
my $borrowernumber = AddMember(%data);
63
my $borrowernumber = AddMember(%data);
64
my $borrowernumber2 = AddMember(%data2);
47
my $borrower = GetMember( borrowernumber => $borrowernumber );
65
my $borrower = GetMember( borrowernumber => $borrowernumber );
48
66
67
# Create biblio and item
68
my %item_branch_infos = (
69
    homebranch    => 'UT',
70
    holdingbranch => 'UT',
71
);
72
73
my $record = MARC::Record->new;
74
my $title = MARC::Field->new('200','','','a' => 'Hello world.');
75
$record->append_fields($title);
76
77
my ($biblionumber) = AddBiblio($record, '');
78
my ($biblionumber2) = AddBiblio(MARC::Record->new, '');
79
my ($biblionumber3) = AddBiblio(MARC::Record->new, '');
80
my ($biblionumber4) = AddBiblio(MARC::Record->new, '');
81
my $itemnumber = AddItem({ barcode => '0101', %item_branch_infos }, $biblionumber);
82
my $itemnumber2 = AddItem({ barcode => '0201', %item_branch_infos }, $biblionumber2);
83
my $itemnumber3 = AddItem({ barcode => '0301', %item_branch_infos }, $biblionumber3);
84
my $itemnumber4 = AddItem({ barcode => '0401', %item_branch_infos }, $biblionumber4);
85
49
{ # AuthenticatePatron test
86
{ # AuthenticatePatron test
50
87
51
    my $query = new CGI;
88
    my $query = new CGI;
Lines 90-92 my $borrower = GetMember( borrowernumber => $borrowernumber ); Link Here
90
127
91
}
128
}
92
129
93
- 
130
{ # Add reserves tests
131
132
    my $query = new CGI;
133
    $query->param('patron_id',$borrowernumber);
134
    $query->param('bib_id', $biblionumber);
135
    $query->param('request_location','127.0.0.1');
136
    my $reply = C4::ILSDI::Services::HoldTitle($query);
137
    is($reply->{'title'}, 'Hello world.');
138
    is($reply->{'pickup_location'}, 'Unit test library', 'Hold title');
139
140
    $reply = C4::ILSDI::Services::HoldTitle($query);
141
    is($reply->{'code'}, 'NotHoldable', 'Hold same title again - ILSDI');
142
143
    $query->param('bib_id', $biblionumber2);
144
    $reply = C4::ILSDI::Services::HoldTitle($query);
145
    is($reply->{'pickup_location'}, 'Unit test library', 'Hold a second title - ILSDI');
146
147
    $query->param('bib_id', $biblionumber3);
148
    $reply = C4::ILSDI::Services::HoldTitle($query);
149
    is($reply->{'pickup_location'}, 'Unit test library', 'Hold a third title - ILSDI');
150
151
    $query->param('bib_id', $biblionumber4);
152
    $reply = C4::ILSDI::Services::HoldTitle($query);
153
    is($reply->{'code'}, 'NotHoldable', 'Hold a fourth title - ILSDI');
154
155
    $query->param('bib_id', $biblionumber4);
156
    $query->param('item_id', $itemnumber4);
157
    $reply = C4::ILSDI::Services::HoldItem($query);
158
    is($reply->{'code'}, 'NotHoldable', 'Hold a fourth item - ILSDI');
159
160
    $query->param('bib_id', $biblionumber);
161
    $query->param('patron_id',$borrowernumber2);
162
    $query->param('item_id', $itemnumber);
163
    $reply = C4::ILSDI::Services::HoldItem($query);
164
    is($reply->{'pickup_location'}, 'Unit test library', 'Joe John hold an item - ILSDI');
165
}

Return to bug 14155