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 C4::Circulation qw( CreateBranchTransferLimit DeleteBranchTransferLimits GetTransfers GetTransfersFromTo TransferSlip );
21 use C4::Biblio qw( AddBiblio );
22 use C4::Items qw( ModItemTransfer );
24 use Koha::DateUtils qw( dt_from_string );
25 use DateTime::Duration;
26 use Koha::Item::Transfers;
28 use t::lib::TestBuilder;
30 use Test::More tests => 22;
34 use_ok('C4::Circulation', qw( CreateBranchTransferLimit DeleteBranchTransferLimits GetTransfers GetTransfersFromTo TransferSlip ));
39 CreateBranchTransferLimit
40 DeleteBranchTransferLimits
46 my $schema = Koha::Database->schema;
47 $schema->storage->txn_begin;
48 my $builder = t::lib::TestBuilder->new;
50 my $dbh = C4::Context->dbh;
51 $dbh->do(q|DELETE FROM issues|);
52 $dbh->do(q|DELETE FROM borrowers|);
53 $dbh->do(q|DELETE FROM items|);
54 $dbh->do(q|DELETE FROM branches|);
55 $dbh->do(q|DELETE FROM branch_transfer_limits|);
56 $dbh->do(q|DELETE FROM branchtransfers|);
58 ## Create sample datas
60 my $branchcode_1 = $builder->build( { source => 'Branch', } )->{branchcode};
61 my $branchcode_2 = $builder->build( { source => 'Branch', } )->{branchcode};
63 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
66 my $record = MARC::Record->new();
67 $record->append_fields(
68 MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
69 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '', );
71 my $item_id1 = Koha::Item->new(
73 biblionumber => $biblionumber,
75 itemcallnumber => 'callnumber1',
76 homebranch => $branchcode_1,
77 holdingbranch => $branchcode_1,
81 my $item_id2 = Koha::Item->new(
83 biblionumber => $biblionumber,
85 itemcallnumber => 'callnumber2',
86 homebranch => $branchcode_1,
87 holdingbranch => $branchcode_1,
91 my $item_id3 = Koha::Item->new(
93 biblionumber => $biblionumber,
95 itemcallnumber => 'callnumber3',
96 homebranch => $branchcode_1,
97 holdingbranch => $branchcode_1,
100 )->store->itemnumber;
101 my $item_id4 = Koha::Item->new(
103 biblionumber => $biblionumber,
105 itemcallnumber => 'callnumber4',
106 homebranch => $branchcode_1,
107 holdingbranch => $branchcode_1,
110 )->store->itemnumber;
113 my $trigger = 'Manual';
121 my $item_obj = Koha::Items->find({ itemnumber => $item_id1 });
122 is( $item_obj->holdingbranch, $branchcode_1, "Item should be held at branch that initiates transfer");
131 # Add an "unsent" transfer for tests
138 my $transfer_requested = Koha::Item::Transfers->search( { itemnumber => $item_id3 }, { rows => 1 })->single;
139 $transfer_requested->set({ daterequested => dt_from_string, datesent => undef })->store;
141 # Add a "cancelled" transfer for tests
148 my $transfer_cancelled = Koha::Item::Transfers->search( { itemnumber => $item_id4 }, { rows => 1 })->single;
149 $transfer_cancelled->set( { daterequested => dt_from_string, datesent => undef, datecancelled => dt_from_string } )->store;
152 #Test CreateBranchTransferLimit
154 CreateBranchTransferLimit(
156 $branchcode_1, 'CODE'
159 "A Branch TransferLimit has been added"
161 is(CreateBranchTransferLimit(),undef,
162 "Without parameters CreateBranchTransferLimit returns undef");
163 is(CreateBranchTransferLimit($branchcode_2),undef,
164 "With only tobranch CreateBranchTransferLimit returns undef");
165 is(CreateBranchTransferLimit(undef,$branchcode_2),undef,
166 "With only frombranch CreateBranchTransferLimit returns undef");
167 #FIXME: Currently, we can add a transferlimit even to nonexistent branches because in the database,
168 #branch_transfer_limits.toBranch and branch_transfer_limits.fromBranch aren't foreign keys
169 #is(CreateBranchTransferLimit(-1,-1,'CODE'),0,"With wrong CreateBranchTransferLimit returns 0 - No transfertlimit added");
172 my @transfers = GetTransfers($item_id1);
175 [ 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}$'), 'Manual' ],
176 "Transfers of the item1"
177 ); #NOTE: Only the first transfer is returned
178 @transfers = GetTransfers;
179 is_deeply( \@transfers, [],
180 "GetTransfers without params returns an empty array" );
181 @transfers = GetTransfers(-1);
182 is_deeply( \@transfers, [],
183 "GetTransfers with a wrong item id returns an empty array" );
185 #Test GetTransfersFromTo
186 my @transferfrom1to2 = GetTransfersFromTo( $branchcode_1,
192 branchtransfer_id => re('[0-9]*'),
193 itemnumber => $item_id1,
194 datesent => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
195 frombranch => $branchcode_1
198 branchtransfer_id => re('[0-9]*'),
199 itemnumber => $item_id2,
200 datesent => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
201 frombranch => $branchcode_1
204 "Item1 and Item2 has been transferred from branch1 to branch2"
206 my @transferto = GetTransfersFromTo( undef, $branchcode_2 );
207 is_deeply( \@transferto, [],
208 "GetTransfersfromTo without frombranch returns an empty array" );
209 my @transferfrom = GetTransfersFromTo( $branchcode_1 );
210 is_deeply( \@transferfrom, [],
211 "GetTransfersfromTo without tobranch returns an empty array" );
212 @transferfrom = GetTransfersFromTo();
213 is_deeply( \@transferfrom, [],
214 "GetTransfersfromTo without params returns an empty array" );
216 #Test DeleteBranchTransferLimits
218 C4::Circulation::DeleteBranchTransferLimits( $branchcode_1 ),
220 "A Branch TransferLimit has been deleted"
222 is(C4::Circulation::DeleteBranchTransferLimits(),undef,"Without parameters DeleteBranchTransferLimit returns undef");
223 is(C4::Circulation::DeleteBranchTransferLimits('B'),'0E0',"With a wrong id DeleteBranchTransferLimit returns 0E0");
226 is( C4::Circulation::TransferSlip($branchcode_1, undef, 5, $branchcode_2),
227 undef, "No tranferslip if invalid or undef itemnumber or barcode" );
228 is( C4::Circulation::TransferSlip($branchcode_1, $item_id1, 1, $branchcode_2)->{'code'},
229 'TRANSFERSLIP', "Get a transferslip on valid itemnumber and/or barcode" );
231 C4::Circulation::TransferSlip($branchcode_1, $item_id1, undef, $branchcode_2),
232 C4::Circulation::TransferSlip($branchcode_1, undef, 1, $branchcode_2),
233 "Barcode and itemnumber for same item both generate same TransferSlip"
236 $dbh->do("DELETE FROM branchtransfers");
243 my $transfer = Koha::Item::Transfers->search()->next();
250 $transfer->{_result}->discard_changes;
251 ok( $transfer->datecancelled, 'Date cancelled is set when new transfer is initiated' );
252 is( $transfer->cancellation_reason, "Manual", 'Cancellation reason is set correctly when new transfer is initiated' );
254 $schema->storage->txn_rollback;