6 use Koha::CirculationRules;
8 use Test::More tests => 11;
10 use t::lib::TestBuilder;
15 use_ok('C4::Reserves');
18 my $schema = Koha::Database->schema;
19 $schema->storage->txn_begin;
20 my $dbh = C4::Context->dbh;
22 my $builder = t::lib::TestBuilder->new;
24 my $library1 = $builder->build({
27 my $library2 = $builder->build({
30 my $library3 = $builder->build({
33 my $itemtype = $builder->build({ source => 'Item' })->{itype};
35 my $bib_title = "Test Title";
37 my $borrower = $builder->build({
40 branchcode => $library1->{branchcode},
44 # Test hold_fulfillment_policy
45 my $borrowernumber = $borrower->{borrowernumber};
46 my $library_A = $library1->{branchcode};
47 my $library_B = $library2->{branchcode};
48 my $library_C = $library3->{branchcode};
49 $dbh->do("DELETE FROM transport_cost");
50 $dbh->do("DELETE FROM tmp_holdsqueue");
51 $dbh->do("DELETE FROM hold_fill_targets");
52 $dbh->do("DELETE FROM circulation_rules");
54 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$bib_title', '2011-02-01')");
56 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$bib_title'")
57 or BAIL_OUT("Cannot find newly created biblio record");
59 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
61 my $biblioitemnumber =
62 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
63 or BAIL_OUT("Cannot find newly created biblioitems record");
66 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
67 VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
71 $dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber")
72 or BAIL_OUT("Cannot find newly created item");
74 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
75 $dbh->do("DELETE FROM circulation_rules");
76 Koha::CirculationRules->set_rules(
82 hold_fulfillment_policy => 'homebranch',
87 # Home branch matches pickup branch
88 my $reserve_id = AddReserve(
90 branchcode => $library_A,
91 borrowernumber => $borrowernumber,
92 biblionumber => $biblionumber,
96 my ( $status ) = CheckReserves($itemnumber);
97 is( $status, 'Reserved', "Hold where pickup branch matches home branch targeted" );
98 Koha::Holds->find( $reserve_id )->cancel;
100 # Holding branch matches pickup branch
101 $reserve_id = AddReserve(
103 branchcode => $library_B,
104 borrowernumber => $borrowernumber,
105 biblionumber => $biblionumber,
109 ( $status ) = CheckReserves($itemnumber);
110 is($status, q{}, "Hold where pickup ne home, pickup eq home not targeted" );
111 Koha::Holds->find( $reserve_id )->cancel;
113 # Neither branch matches pickup branch
114 $reserve_id = AddReserve(
116 branchcode => $library_C,
117 borrowernumber => $borrowernumber,
118 biblionumber => $biblionumber,
122 ( $status ) = CheckReserves($itemnumber);
123 is( $status, q{}, "Hold where pickup ne home, pickup ne holding not targeted" );
124 Koha::Holds->find( $reserve_id )->cancel;
126 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
127 $dbh->do("DELETE FROM circulation_rules");
128 Koha::CirculationRules->set_rules(
134 hold_fulfillment_policy => 'holdingbranch',
139 # Home branch matches pickup branch
140 $reserve_id = AddReserve(
142 branchcode => $library_A,
143 borrowernumber => $borrowernumber,
144 biblionumber => $biblionumber,
148 ( $status ) = CheckReserves($itemnumber);
149 is( $status, q{}, "Hold where pickup eq home, pickup ne holding not targeted" );
150 Koha::Holds->find( $reserve_id )->cancel;
152 # Holding branch matches pickup branch
153 $reserve_id = AddReserve(
155 branchcode => $library_B,
156 borrowernumber => $borrowernumber,
157 biblionumber => $biblionumber,
161 ( $status ) = CheckReserves($itemnumber);
162 is( $status, 'Reserved', "Hold where pickup ne home, pickup eq holding targeted" );
163 Koha::Holds->find( $reserve_id )->cancel;
165 # Neither branch matches pickup branch
166 $reserve_id = AddReserve(
168 branchcode => $library_C,
169 borrowernumber => $borrowernumber,
170 biblionumber => $biblionumber,
174 ( $status ) = CheckReserves($itemnumber);
175 is( $status, q{}, "Hold where pickup ne home, pickup ne holding not targeted" );
176 Koha::Holds->find( $reserve_id )->cancel;
178 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
179 $dbh->do("DELETE FROM circulation_rules");
180 Koha::CirculationRules->set_rules(
186 hold_fulfillment_policy => 'any',
191 # Home branch matches pickup branch
192 $reserve_id = AddReserve(
194 branchcode => $library_A,
195 borrowernumber => $borrowernumber,
196 biblionumber => $biblionumber,
200 ( $status ) = CheckReserves($itemnumber);
201 is( $status, 'Reserved', "Hold where pickup eq home, pickup ne holding targeted" );
202 Koha::Holds->find( $reserve_id )->cancel;
204 # Holding branch matches pickup branch
205 $reserve_id = AddReserve(
207 branchcode => $library_B,
208 borrowernumber => $borrowernumber,
209 biblionumber => $biblionumber,
213 ( $status ) = CheckReserves($itemnumber);
214 is( $status, 'Reserved', "Hold where pickup ne home, pickup eq holding targeted" );
215 Koha::Holds->find( $reserve_id )->cancel;
217 # Neither branch matches pickup branch
218 $reserve_id = AddReserve(
220 branchcode => $library_C,
221 borrowernumber => $borrowernumber,
222 biblionumber => $biblionumber,
226 ( $status ) = CheckReserves($itemnumber);
227 is( $status, 'Reserved', "Hold where pickup ne home, pickup ne holding targeted" );
228 Koha::Holds->find( $reserve_id )->cancel;
230 # Test enforement of branch transfer limits
231 t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
232 t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
233 Koha::Holds->search()->delete();
234 my ($item) = Koha::Biblios->find($biblionumber)->items->as_list;
235 my $limit = Koha::Item::Transfer::Limit->new(
237 toBranch => $library_C,
238 fromBranch => $item->holdingbranch,
239 itemtype => $item->effective_itemtype,
242 $reserve_id = AddReserve(
244 branchcode => $library_C,
245 borrowernumber => $borrowernumber,
246 biblionumber => $biblionumber,
250 ($status) = CheckReserves($itemnumber);
251 is( $status, '', "No hold where branch transfer is not allowed" );
252 Koha::Holds->find($reserve_id)->cancel;