3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 6;
21 use t::lib::TestBuilder;
24 use C4::Circulation qw( transferbook AddIssue );
25 use C4::Reserves qw( AddReserve );
26 use Koha::DateUtils qw( dt_from_string );
27 use Koha::Item::Transfers;
29 my $builder = t::lib::TestBuilder->new;
30 my $schema = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
34 subtest 'transfer a non-existant item' => sub {
37 my $library = $builder->build( { source => 'Branch' } );
39 #Transfert on unknown barcode
40 my $item = $builder->build_sample_item();
41 my $badbc = $item->barcode;
44 my $trigger = "Manual";
45 my ( $dotransfer, $messages ) =
46 C4::Circulation::transferbook({
47 from_branch => $item->homebranch,
48 to_branch => $library->{branchcode},
52 is( $dotransfer, 0, "Can't transfer a bad barcode" );
55 { BadBarcode => $badbc },
56 "We got the expected barcode"
60 subtest 'field population tests' => sub {
63 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
64 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
66 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } )->store;
68 my $patron = $builder->build_object(
70 class => 'Koha::Patrons',
71 value => { branchcode => $library->branchcode }
75 my $item = $builder->build_sample_item(
77 library => $library->branchcode,
81 my $trigger = "Manual";
82 my ($dotransfer, $messages ) = transferbook({
83 from_branch => $item->homebranch,
84 to_branch => $library2->branchcode,
85 barcode => $item->barcode,
88 is( $dotransfer, 1, 'Transfer succeeded' );
91 { 'WasTransfered' => $library2->branchcode },
92 "WasTransfered was set correctly"
95 my $transfers = Koha::Item::Transfers->search({ itemnumber => $item->itemnumber, datearrived => undef });
96 is( $transfers->count, 1, 'One transfer created');
98 my $transfer = $transfers->next;
99 is ($transfer->frombranch, $library->branchcode, 'frombranch set correctly');
100 is ($transfer->tobranch, $library2->branchcode, 'tobranch set correctly');
101 is ($transfer->reason, $trigger, 'reason set if passed');
104 #FIXME:'UseBranchTransferLimits tests missing
106 subtest 'transfer already at destination' => sub {
109 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
110 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
112 my $patron = $builder->build_object(
114 class => 'Koha::Patrons',
115 value => { branchcode => $library->branchcode }
119 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' })->store->itemtype;
120 my $item = $builder->build_sample_item(
122 library => $library->branchcode,
127 my ($dotransfer, $messages ) = transferbook({
128 from_branch => $item->homebranch,
129 to_branch => $library->branchcode,
130 barcode => $item->barcode,
133 is( $dotransfer, 0, 'Transfer of item failed when destination equals holding branch' );
136 { 'DestinationEqualsHolding' => 1 },
137 "We got the expected failure message: DestinationEqualsHolding"
140 # We are making sure there is no regression, feel free to change the behavior if needed.
141 # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
142 # found will override all other measures that may prevent transfer and force a transfer.
144 branchcode => $item->homebranch,
145 borrowernumber => $patron->borrowernumber,
146 biblionumber => $item->biblionumber,
147 itemnumber => $item->itemnumber,
150 ($dotransfer, $messages ) = transferbook({
151 from_branch => $item->homebranch,
152 to_branch => $library->branchcode,
153 barcode => $item->barcode,
156 is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
157 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
158 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
161 t::lib::Mocks::mock_preference('UseRecalls', 1);
162 my $recall = Koha::Recall->new(
163 { biblio_id => $item->biblionumber,
164 item_id => $item->itemnumber,
166 patron_id => $patron->borrowernumber,
167 pickup_library_id => $library->branchcode,
170 ( $recall, $dotransfer, $messages ) = $recall->start_transfer;
171 is( $dotransfer, 0, 'Do not transfer recalled item, it has already arrived' );
172 is( $messages->{RecallPlacedAtHoldingBranch}, 1, "We found the recall");
174 $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' })->store->itemtype;
175 my $item2 = $builder->build_object({ class => 'Koha::Items', value => { itype => $itemtype } }); # this item will have a different holding branch to the pickup branch
176 $recall = Koha::Recall->new(
177 { biblio_id => $item2->biblionumber,
178 item_id => $item2->itemnumber,
180 patron_id => $patron->borrowernumber,
181 pickup_library_id => $library->branchcode,
184 ( $recall, $dotransfer, $messages ) = $recall->start_transfer;
185 is( $dotransfer, 1, 'Transfer of recalled item succeeded' );
186 is( $messages->{RecallFound}->id, $recall->id, "We found the recall");
189 subtest 'transfer an issued item' => sub {
192 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
193 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
195 my $patron = $builder->build_object(
197 class => 'Koha::Patrons',
198 value => { branchcode => $library->branchcode }
202 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' })->store->itemtype;
203 my $item = $builder->build_sample_item(
205 library => $library->branchcode,
210 my $dt_to = dt_from_string();
211 my $issue = AddIssue( $patron, $item->barcode, $dt_to );
213 # We are making sure there is no regression, feel free to change the behavior if needed.
214 # * WasReturned does not seem like a variable that should contain a borrowernumber
215 # * Should we return even if the transfer did not happen? (same branches)
216 my ($dotransfer, $messages) = transferbook({
217 from_branch => $item->homebranch,
218 to_branch => $library->branchcode,
219 barcode => $item->barcode,
222 is( $messages->{WasReturned}, $patron->borrowernumber, 'transferbook should have return a WasReturned flag is the item was issued before the transferbook call');
225 $issue = AddIssue( $patron, $item->barcode, $dt_to );
227 # We are making sure there is no regression, feel free to change the behavior if needed.
228 # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
229 # found will override all other measures that may prevent transfer and force a transfer.
231 branchcode => $item->homebranch,
232 borrowernumber => $patron->borrowernumber,
233 biblionumber => $item->biblionumber,
234 itemnumber => $item->itemnumber,
237 ($dotransfer, $messages ) = transferbook({
238 from_branch => $item->homebranch,
239 to_branch => $library->branchcode,
240 barcode => $item->barcode,
243 is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
244 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
245 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
246 is( $messages->{WasReturned}, $patron->borrowernumber, "We got the return info");
249 subtest 'ignore_reserves flag' => sub {
251 my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
252 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } )->store;
253 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
255 my $patron = $builder->build_object(
257 class => 'Koha::Patrons',
258 value => { branchcode => $library->branchcode }
262 my $item = $builder->build_sample_item(
264 library => $library->branchcode,
269 branchcode => $item->homebranch,
270 borrowernumber => $patron->borrowernumber,
271 biblionumber => $item->biblionumber,
272 itemnumber => $item->itemnumber,
275 # We are making sure there is no regression, feel free to change the behavior if needed.
276 # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
277 # found will override all other measures that may prevent transfer and force a transfer.
278 my ($dotransfer, $messages ) = transferbook({
279 from_branch => $item->homebranch,
280 to_branch => $library2->branchcode,
281 barcode => $item->barcode,
284 is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
285 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
286 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
288 my $ignore_reserves = 0;
289 ($dotransfer, $messages ) = transferbook({
290 from_branch => $item->homebranch,
291 to_branch => $library2->branchcode,
292 barcode => $item->barcode,
293 ignore_reserves => $ignore_reserves,
296 is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
297 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
298 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
300 $ignore_reserves = 1;
301 ($dotransfer, $messages ) = transferbook({
302 from_branch => $item->homebranch,
303 to_branch => $library2->branchcode,
304 barcode => $item->barcode,
305 ignore_reserves => $ignore_reserves,
308 is( $dotransfer, 1, 'Transfer of reserved item succeed with ignore reserves: true' );
309 is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
310 is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
313 subtest 'transferbook test from branch' => sub {
316 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
317 my $item = $builder->build_sample_item();
318 ok( $item->holdingbranch ne $library->branchcode && $item->homebranch ne $library->branchcode, "Item is not held or owned by library");
319 C4::Circulation::transferbook({
320 from_branch => $library->branchcode,
321 to_branch => $item->homebranch,
322 barcode => $item->barcode,
325 my $transfer = $item->get_transfer;
326 is( $transfer->frombranch, $library->branchcode, 'The transfer is initiated from the specified branch, not the items home or holdingbranch');
327 is( $transfer->tobranch, $item->homebranch, 'The transfer is initiated to the specified branch');
328 C4::Circulation::transferbook({
329 from_branch => $item->homebranch,
330 to_branch => $library->branchcode,
331 barcode => $item->barcode,
334 $transfer = $item->get_transfer;
335 is( $transfer->frombranch, $item->homebranch, 'The transfer is initiated from the specified branch');
336 is( $transfer->tobranch, $library->branchcode, 'The transfer is initiated to the specified branch');
339 $schema->storage->txn_rollback;