Bug 19532: Other objects used in recalls feature
[koha.git] / t / db_dependent / Circulation / transferbook.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 6;
21 use t::lib::TestBuilder;
22 use t::lib::Mocks;
23
24 use C4::Circulation qw( transferbook AddIssue GetTransfers );
25 use C4::Reserves qw( AddReserve );
26 use Koha::DateUtils qw( dt_from_string );
27 use Koha::Item::Transfers;
28
29 my $builder = t::lib::TestBuilder->new;
30 my $schema = Koha::Database->new->schema;
31
32 $schema->storage->txn_begin;
33
34 subtest 'transfer a non-existant item' => sub {
35     plan tests => 2;
36
37     my $library = $builder->build( { source => 'Branch' } );
38
39     #Transfert on unknown barcode
40     my $item  = $builder->build_sample_item();
41     my $badbc = $item->barcode;
42     $item->delete;
43
44     my $trigger = "Manual";
45     my ( $dotransfer, $messages ) =
46       C4::Circulation::transferbook({
47           from_branch => $item->homebranch,
48           to_branch => $library->{branchcode},
49           barcode => $badbc,
50           trigger => $trigger
51       });
52     is( $dotransfer, 0, "Can't transfer a bad barcode" );
53     is_deeply(
54         $messages,
55         { BadBarcode => $badbc },
56         "We got the expected barcode"
57     );
58 };
59
60 subtest 'field population tests' => sub {
61     plan tests => 6;
62
63     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
64     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
65
66     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } )->store;
67
68     my $patron = $builder->build_object(
69         {
70             class => 'Koha::Patrons',
71             value => { branchcode => $library->branchcode }
72         }
73     );
74
75     my $item = $builder->build_sample_item(
76         {
77             library => $library->branchcode,
78         }
79     );
80
81     my $trigger = "Manual";
82     my ($dotransfer, $messages ) = transferbook({
83         from_branch => $item->homebranch,
84         to_branch => $library2->branchcode,
85         barcode => $item->barcode,
86         trigger => $trigger
87     });
88     is( $dotransfer, 1, 'Transfer succeeded' );
89     is_deeply(
90         $messages,
91         { 'WasTransfered' => $library2->branchcode },
92         "WasTransfered was set correctly"
93     );
94
95     my $transfers = Koha::Item::Transfers->search({ itemnumber => $item->itemnumber, datearrived => undef });
96     is( $transfers->count, 1, 'One transfer created');
97
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');
102 };
103
104 #FIXME:'UseBranchTransferLimits tests missing
105
106 subtest 'transfer already at destination' => sub {
107     plan tests => 9;
108
109     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
110     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
111
112     my $patron = $builder->build_object(
113         {
114             class => 'Koha::Patrons',
115             value => { branchcode => $library->branchcode }
116         }
117     );
118
119     my $item = $builder->build_sample_item(
120         {
121             library => $library->branchcode,
122         }
123     );
124
125     my ($dotransfer, $messages ) = transferbook({
126         from_branch => $item->homebranch,
127         to_branch => $library->branchcode,
128         barcode => $item->barcode,
129         trigger => "Manual"
130     });
131     is( $dotransfer, 0, 'Transfer of item failed when destination equals holding branch' );
132     is_deeply(
133         $messages,
134         { 'DestinationEqualsHolding' => 1 },
135         "We got the expected failure message: DestinationEqualsHolding"
136     );
137
138     # We are making sure there is no regression, feel free to change the behavior if needed.
139     # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
140     #   found will override all other measures that may prevent transfer and force a transfer.
141     AddReserve({
142         branchcode     => $item->homebranch,
143         borrowernumber => $patron->borrowernumber,
144         biblionumber   => $item->biblionumber,
145         itemnumber     => $item->itemnumber,
146     });
147
148     ($dotransfer, $messages ) = transferbook({
149         from_branch => $item->homebranch,
150         to_branch => $library->branchcode,
151         barcode => $item->barcode,
152         trigger => "Manual"
153     });
154     is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
155     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
156     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
157
158     # recalls
159     t::lib::Mocks::mock_preference('UseRecalls', 1);
160     my $recall = Koha::Recall->new({
161         biblionumber => $item->biblionumber,
162         itemnumber => $item->itemnumber,
163         item_level_recall => 1,
164         borrowernumber => $patron->borrowernumber,
165         branchcode => $library->branchcode,
166         status => 'R',
167     })->store;
168     ( $recall, $dotransfer, $messages ) = $recall->start_transfer;
169     is( $dotransfer, 0, 'Do not transfer recalled item, it has already arrived' );
170     is( $messages->{RecallPlacedAtHoldingBranch}, 1, "We found the recall");
171
172     my $item2 = $builder->build_object({ class => 'Koha::Items' }); # this item will have a different holding branch to the pickup branch
173     $recall = Koha::Recall->new({
174         biblionumber => $item2->biblionumber,
175         itemnumber => $item2->itemnumber,
176         item_level_recall => 1,
177         borrowernumber => $patron->borrowernumber,
178         branchcode => $library->branchcode,
179         status => 'R',
180     })->store;
181     ( $recall, $dotransfer, $messages ) = $recall->start_transfer;
182     is( $dotransfer, 1, 'Transfer of recalled item succeeded' );
183     is( $messages->{RecallFound}->recall_id, $recall->recall_id, "We found the recall");
184 };
185
186 subtest 'transfer an issued item' => sub {
187     plan tests => 5;
188
189     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
190     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
191
192     my $patron = $builder->build_object(
193         {
194             class => 'Koha::Patrons',
195             value => { branchcode => $library->branchcode }
196         }
197     );
198
199     my $item = $builder->build_sample_item(
200         {
201             library => $library->branchcode,
202         }
203     );
204
205     my $dt_to = dt_from_string();
206     my $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
207
208     # We are making sure there is no regression, feel free to change the behavior if needed.
209     # * WasReturned does not seem like a variable that should contain a borrowernumber
210     # * Should we return even if the transfer did not happen? (same branches)
211     my ($dotransfer, $messages) = transferbook({
212         from_branch => $item->homebranch,
213         to_branch => $library->branchcode,
214         barcode => $item->barcode,
215         trigger => "Manual"
216     });
217     is( $messages->{WasReturned}, $patron->borrowernumber, 'transferbook should have return a WasReturned flag is the item was issued before the transferbook call');
218
219     # Reset issue
220     $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to );
221
222     # We are making sure there is no regression, feel free to change the behavior if needed.
223     # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
224     #   found will override all other measures that may prevent transfer and force a transfer.
225     AddReserve({
226         branchcode     => $item->homebranch,
227         borrowernumber => $patron->borrowernumber,
228         biblionumber   => $item->biblionumber,
229         itemnumber     => $item->itemnumber,
230     });
231
232     ($dotransfer, $messages ) = transferbook({
233         from_branch => $item->homebranch,
234         to_branch => $library->branchcode,
235         barcode => $item->barcode,
236         trigger => "Manual"
237     });
238     is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
239     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
240     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
241     is( $messages->{WasReturned}, $patron->borrowernumber, "We got the return info");
242 };
243
244 subtest 'ignore_reserves flag' => sub {
245     plan tests => 9;
246     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
247     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } )->store;
248     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
249
250     my $patron = $builder->build_object(
251         {
252             class => 'Koha::Patrons',
253             value => { branchcode => $library->branchcode }
254         }
255     );
256
257     my $item = $builder->build_sample_item(
258         {
259             library => $library->branchcode,
260         }
261     );
262
263     AddReserve({
264         branchcode     => $item->homebranch,
265         borrowernumber => $patron->borrowernumber,
266         biblionumber   => $item->biblionumber,
267         itemnumber     => $item->itemnumber,
268     });
269
270     # We are making sure there is no regression, feel free to change the behavior if needed.
271     # * Contrary to the POD, if ignore_reserves is not passed (or is false), any item reserve
272     #   found will override all other measures that may prevent transfer and force a transfer.
273     my ($dotransfer, $messages ) = transferbook({
274         from_branch => $item->homebranch,
275         to_branch => $library2->branchcode,
276         barcode => $item->barcode,
277         trigger => "Manual"
278     });
279     is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
280     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
281     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
282
283     my $ignore_reserves = 0;
284     ($dotransfer, $messages ) = transferbook({
285         from_branch => $item->homebranch,
286         to_branch => $library2->branchcode,
287         barcode => $item->barcode,
288         ignore_reserves => $ignore_reserves,
289         trigger => "Manual"
290     });
291     is( $dotransfer, 0, 'Transfer of reserved item doesn\'t succeed without ignore_reserves' );
292     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
293     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
294
295     $ignore_reserves = 1;
296     ($dotransfer, $messages ) = transferbook({
297         from_branch => $item->homebranch,
298         to_branch => $library2->branchcode,
299         barcode => $item->barcode,
300         ignore_reserves => $ignore_reserves,
301         trigger => "Manual"
302     });
303     is( $dotransfer, 1, 'Transfer of reserved item succeed with ignore reserves: true' );
304     is( $messages->{ResFound}->{ResFound}, 'Reserved', "We found the reserve");
305     is( $messages->{ResFound}->{itemnumber}, $item->itemnumber, "We got the reserve info");
306 };
307
308 subtest 'transferbook test from branch' => sub {
309     plan tests => 5;
310
311     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
312     my $item = $builder->build_sample_item();
313     ok( $item->holdingbranch ne $library->branchcode && $item->homebranch ne $library->branchcode, "Item is not held or owned by library");
314     C4::Circulation::transferbook({
315         from_branch => $library->branchcode,
316         to_branch => $item->homebranch,
317         barcode   => $item->barcode,
318         trigger => "Manual"
319     });
320     my ($datesent,$from_branch,$to_branch) = GetTransfers($item->itemnumber);
321     is( $from_branch, $library->branchcode, 'The transfer is initiated from the specified branch, not the items home or holdingbranch');
322     is( $to_branch, $item->homebranch, 'The transfer is initiated to the specified branch');
323     C4::Circulation::transferbook({
324         from_branch => $item->homebranch,
325         to_branch => $library->branchcode,
326         barcode   => $item->barcode,
327         trigger => "Manual"
328     });
329     ($datesent,$from_branch,$to_branch) = GetTransfers($item->itemnumber);
330     is( $from_branch, $item->homebranch, 'The transfer is initiated from the specified branch');
331     is( $to_branch, $library->branchcode, 'The transfer is initiated to the specified branch');
332
333 };
334 $schema->storage->txn_rollback;