Bug 24446: Add datesent/datecancelled handling to GetTransfersFromTo
[koha.git] / t / db_dependent / Circulation / transfers.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 use C4::Context;
20 use C4::Circulation;
21 use C4::Biblio;
22 use C4::Items;
23 use Koha::Database;
24 use Koha::DateUtils;
25 use DateTime::Duration;
26 use Koha::Item::Transfers;
27
28 use t::lib::TestBuilder;
29
30 use Test::More tests => 25;
31 use Test::Deep;
32
33 BEGIN {
34     use_ok('C4::Circulation');
35 }
36 can_ok(
37     'C4::Circulation',
38     qw(
39       CreateBranchTransferLimit
40       DeleteBranchTransferLimits
41       DeleteTransfer
42       GetTransfers
43       GetTransfersFromTo
44       )
45 );
46
47 my $schema = Koha::Database->schema;
48 $schema->storage->txn_begin;
49 my $builder = t::lib::TestBuilder->new;
50
51 my $dbh = C4::Context->dbh;
52 $dbh->do(q|DELETE FROM issues|);
53 $dbh->do(q|DELETE FROM borrowers|);
54 $dbh->do(q|DELETE FROM items|);
55 $dbh->do(q|DELETE FROM branches|);
56 $dbh->do(q|DELETE FROM branch_transfer_limits|);
57 $dbh->do(q|DELETE FROM branchtransfers|);
58
59 ## Create sample datas
60 # Add branches
61 my $branchcode_1 = $builder->build( { source => 'Branch', } )->{branchcode};
62 my $branchcode_2 = $builder->build( { source => 'Branch', } )->{branchcode};
63 # Add itemtype
64 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
65
66 #Add biblio and items
67 my $record = MARC::Record->new();
68 $record->append_fields(
69     MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
70 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '', );
71
72 my $item_id1 = Koha::Item->new(
73     {
74         biblionumber   => $biblionumber,
75         barcode        => 1,
76         itemcallnumber => 'callnumber1',
77         homebranch     => $branchcode_1,
78         holdingbranch  => $branchcode_1,
79         itype          => $itemtype
80     },
81 )->store->itemnumber;
82 my $item_id2 = Koha::Item->new(
83     {
84         biblionumber   => $biblionumber,
85         barcode        => 2,
86         itemcallnumber => 'callnumber2',
87         homebranch     => $branchcode_1,
88         holdingbranch  => $branchcode_1,
89         itype          => $itemtype
90     },
91 )->store->itemnumber;
92 my $item_id3 = Koha::Item->new(
93     {
94         biblionumber   => $biblionumber,
95         barcode        => 3,
96         itemcallnumber => 'callnumber3',
97         homebranch     => $branchcode_1,
98         holdingbranch  => $branchcode_1,
99         itype          => $itemtype
100     },
101 )->store->itemnumber;
102 my $item_id4 = Koha::Item->new(
103     {
104         biblionumber   => $biblionumber,
105         barcode        => 4,
106         itemcallnumber => 'callnumber4',
107         homebranch     => $branchcode_1,
108         holdingbranch  => $branchcode_1,
109         itype          => $itemtype
110     },
111 )->store->itemnumber;
112
113 #Add transfers
114 ModItemTransfer(
115     $item_id1,
116     $branchcode_1,
117     $branchcode_2
118 );
119
120 my $item_obj = Koha::Items->find({ itemnumber => $item_id1 });
121 is( $item_obj->holdingbranch, $branchcode_1, "Item should be held at branch that initiates transfer");
122
123 ModItemTransfer(
124     $item_id2,
125     $branchcode_1,
126     $branchcode_2
127 );
128
129 # Add an "unsent" transfer for tests
130 ModItemTransfer(
131     $item_id3,
132     $branchcode_1,
133     $branchcode_2
134 );
135 my $transfer_requested = Koha::Item::Transfers->search( { itemnumber => $item_id3 }, { rows => 1 })->single;
136 $transfer_requested->set({ daterequested => dt_from_string, datesent => undef })->store;
137
138 # Add a "cancelled" transfer for tests
139 ModItemTransfer(
140     $item_id4,
141     $branchcode_1,
142     $branchcode_2
143 );
144 my $transfer_cancelled = Koha::Item::Transfers->search( { itemnumber => $item_id4 }, { rows => 1 })->single;
145 $transfer_cancelled->set( { daterequested => dt_from_string, datesent => undef, datecancelled => dt_from_string } )->store;
146
147 #Begin Tests
148 #Test CreateBranchTransferLimit
149 is(
150     CreateBranchTransferLimit(
151         $branchcode_2,
152         $branchcode_1, 'CODE'
153     ),
154     1,
155     "A Branch TransferLimit has been added"
156 );
157 is(CreateBranchTransferLimit(),undef,
158     "Without parameters CreateBranchTransferLimit returns undef");
159 is(CreateBranchTransferLimit($branchcode_2),undef,
160     "With only tobranch CreateBranchTransferLimit returns undef");
161 is(CreateBranchTransferLimit(undef,$branchcode_2),undef,
162     "With only frombranch CreateBranchTransferLimit returns undef");
163 #FIXME: Currently, we can add a transferlimit even to nonexistent branches because in the database,
164 #branch_transfer_limits.toBranch and branch_transfer_limits.fromBranch aren't foreign keys
165 #is(CreateBranchTransferLimit(-1,-1,'CODE'),0,"With wrong CreateBranchTransferLimit returns 0 - No transfertlimit added");
166
167 #Test GetTransfers
168 my @transfers = GetTransfers($item_id1);
169 cmp_deeply(
170     \@transfers,
171     [ re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'), $branchcode_1, $branchcode_2, re('[0-9]*'), re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'), undef ],
172     "Transfers of the item1"
173 );    #NOTE: Only the first transfer is returned
174 @transfers = GetTransfers;
175 is_deeply( \@transfers, [],
176     "GetTransfers without params returns an empty array" );
177 @transfers = GetTransfers(-1);
178 is_deeply( \@transfers, [],
179     "GetTransfers with a wrong item id returns an empty array" );
180
181 #Test GetTransfersFromTo
182 my @transferfrom1to2 = GetTransfersFromTo( $branchcode_1,
183     $branchcode_2 );
184 cmp_deeply(
185     \@transferfrom1to2,
186     [
187         {
188             branchtransfer_id => re('[0-9]*'),
189             itemnumber        => $item_id1,
190             datesent          => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
191             frombranch        => $branchcode_1
192         },
193         {
194             branchtransfer_id => re('[0-9]*'),
195             itemnumber        => $item_id2,
196             datesent          => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
197             frombranch        => $branchcode_1
198         }
199     ],
200     "Item1 and Item2 has been transferred from branch1 to branch2"
201 );
202 my @transferto = GetTransfersFromTo( undef, $branchcode_2 );
203 is_deeply( \@transferto, [],
204     "GetTransfersfromTo without frombranch returns an empty array" );
205 my @transferfrom = GetTransfersFromTo( $branchcode_1 );
206 is_deeply( \@transferfrom, [],
207     "GetTransfersfromTo without tobranch returns an empty array" );
208 @transferfrom = GetTransfersFromTo();
209 is_deeply( \@transferfrom, [],
210     "GetTransfersfromTo without params returns an empty array" );
211
212 #Test DeleteBranchTransferLimits
213 is(
214     C4::Circulation::DeleteBranchTransferLimits( $branchcode_1 ),
215     1,
216     "A Branch TransferLimit has been deleted"
217 );
218 is(C4::Circulation::DeleteBranchTransferLimits(),undef,"Without parameters DeleteBranchTransferLimit returns undef");
219 is(C4::Circulation::DeleteBranchTransferLimits('B'),'0E0',"With a wrong id DeleteBranchTransferLimit returns 0E0");
220
221 #Test DeleteTransfer
222 is( C4::Circulation::DeleteTransfer($item_id1),
223     1, "A the item1's transfer has been deleted" );
224 is(C4::Circulation::DeleteTransfer(),undef,"Without itemid DeleteTransfer returns undef");
225 is(C4::Circulation::DeleteTransfer(-1),'0E0',"with a wrong itemid DeleteTranfer returns 0E0");
226
227 #Test TransferSlip
228 is( C4::Circulation::TransferSlip($branchcode_1, undef, 5, $branchcode_2),
229     undef, "No tranferslip if invalid or undef itemnumber or barcode" );
230 is( C4::Circulation::TransferSlip($branchcode_1, $item_id1, 1, $branchcode_2)->{'code'},
231     'TRANSFERSLIP', "Get a transferslip on valid itemnumber and/or barcode" );
232 cmp_deeply(
233     C4::Circulation::TransferSlip($branchcode_1, $item_id1, undef, $branchcode_2),
234     C4::Circulation::TransferSlip($branchcode_1, undef, 1, $branchcode_2),
235     "Barcode and itemnumber for same item both generate same TransferSlip"
236     );
237
238 $dbh->do("DELETE FROM branchtransfers");
239 ModItemTransfer(
240     $item_id1,
241     $branchcode_1,
242     $branchcode_2
243 );
244 my $transfer = Koha::Item::Transfers->search()->next();
245 ModItemTransfer(
246     $item_id1,
247     $branchcode_1,
248     $branchcode_2
249 );
250 $transfer->{_result}->discard_changes;
251 ok( $transfer->datearrived, 'Date arrived is set when new transfer is initiated' );
252 is( $transfer->comments, "Canceled, new transfer from $branchcode_1 to $branchcode_2 created", 'Transfer comment is set as expected when new transfer is initiated' );
253
254 $schema->storage->txn_rollback;
255