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>.
25 use DateTime::Duration;
26 use Koha::Item::Transfers;
28 use t::lib::TestBuilder;
30 use Test::More tests => 24;
34 use_ok('C4::Circulation');
39 CreateBranchTransferLimit
40 DeleteBranchTransferLimits
47 my $schema = Koha::Database->schema;
48 $schema->storage->txn_begin;
49 my $builder = t::lib::TestBuilder->new;
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|);
59 ## Create sample datas
61 my $branchcode_1 = $builder->build( { source => 'Branch', } )->{branchcode};
62 my $branchcode_2 = $builder->build( { source => 'Branch', } )->{branchcode};
64 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
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, '', );
72 my @sampleitem1 = C4::Items::AddItem(
74 itemcallnumber => 'callnumber1',
75 homebranch => $branchcode_1,
76 holdingbranch => $branchcode_1,
81 my $item_id1 = $sampleitem1[2];
82 my @sampleitem2 = C4::Items::AddItem(
84 itemcallnumber => 'callnumber2',
85 homebranch => $branchcode_1,
86 holdingbranch => $branchcode_1,
91 my $item_id2 = $sampleitem2[2];
106 #Test CreateBranchTransferLimit
108 CreateBranchTransferLimit(
110 $branchcode_1, 'CODE'
113 "A Branch TransferLimit has been added"
115 is(CreateBranchTransferLimit(),undef,
116 "Without parameters CreateBranchTransferLimit returns undef");
117 is(CreateBranchTransferLimit($branchcode_2),undef,
118 "With only tobranch CreateBranchTransferLimit returns undef");
119 is(CreateBranchTransferLimit(undef,$branchcode_2),undef,
120 "With only frombranch CreateBranchTransferLimit returns undef");
121 #FIXME: Currently, we can add a transferlimit even to nonexistent branches because in the database,
122 #branch_transfer_limits.toBranch and branch_transfer_limits.fromBranch aren't foreign keys
123 #is(CreateBranchTransferLimit(-1,-1,'CODE'),0,"With wrong CreateBranchTransferLimit returns 0 - No transfertlimit added");
126 my @transfers = GetTransfers($item_id1);
129 [ re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'), $branchcode_1, $branchcode_2, re('[0-9]*') ],
130 "Transfers of the item1"
131 ); #NOTE: Only the first transfer is returned
132 @transfers = GetTransfers;
133 is_deeply( \@transfers, [],
134 "GetTransfers without params returns an empty array" );
135 @transfers = GetTransfers(-1);
136 is_deeply( \@transfers, [],
137 "GetTransfers with a wrong item id returns an empty array" );
139 #Test GetTransfersFromTo
140 my @transferfrom1to2 = GetTransfersFromTo( $branchcode_1,
146 branchtransfer_id => re('[0-9]*'),
147 itemnumber => $item_id1,
148 datesent => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
149 frombranch => $branchcode_1
152 branchtransfer_id => re('[0-9]*'),
153 itemnumber => $item_id2,
154 datesent => re('^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'),
155 frombranch => $branchcode_1
158 "Item1 and Item2 has been transferred from branch1 to branch2"
160 my @transferto = GetTransfersFromTo( undef, $branchcode_2 );
161 is_deeply( \@transferto, [],
162 "GetTransfersfromTo without frombranch returns an empty array" );
163 my @transferfrom = GetTransfersFromTo( $branchcode_1 );
164 is_deeply( \@transferfrom, [],
165 "GetTransfersfromTo without tobranch returns an empty array" );
166 @transferfrom = GetTransfersFromTo();
167 is_deeply( \@transferfrom, [],
168 "GetTransfersfromTo without params returns an empty array" );
170 #Test DeleteBranchTransferLimits
172 C4::Circulation::DeleteBranchTransferLimits( $branchcode_1 ),
174 "A Branch TransferLimit has been deleted"
176 is(C4::Circulation::DeleteBranchTransferLimits(),undef,"Without parameters DeleteBranchTransferLimit returns undef");
177 is(C4::Circulation::DeleteBranchTransferLimits('B'),'0E0',"With a wrong id DeleteBranchTransferLimit returns 0E0");
180 is( C4::Circulation::DeleteTransfer($item_id1),
181 1, "A the item1's transfer has been deleted" );
182 is(C4::Circulation::DeleteTransfer(),undef,"Without itemid DeleteTransfer returns undef");
183 is(C4::Circulation::DeleteTransfer(-1),'0E0',"with a wrong itemid DeleteTranfer returns 0E0");
186 is( C4::Circulation::TransferSlip($branchcode_1, undef, 5, $branchcode_2),
187 undef, "No tranferslip if invalid or undef itemnumber or barcode" );
188 is( C4::Circulation::TransferSlip($branchcode_1, $item_id1, 1, $branchcode_2)->{'code'},
189 'TRANSFERSLIP', "Get a transferslip on valid itemnumber and/or barcode" );
191 C4::Circulation::TransferSlip($branchcode_1, $item_id1, undef, $branchcode_2),
192 C4::Circulation::TransferSlip($branchcode_1, undef, 1, $branchcode_2),
193 "Barcode and itemnumber for same item both generate same TransferSlip"
196 $dbh->do("DELETE FROM branchtransfers");
202 my $transfer = Koha::Item::Transfers->search()->next();
208 $transfer->{_result}->discard_changes;
209 ok( $transfer->datearrived, 'Date arrived is set when new transfer is initiated' );
210 is( $transfer->comments, "Canceled, new transfer from $branchcode_1 to $branchcode_2 created", 'Transfer comment is set as expected when new transfer is initiated' );
212 $schema->storage->txn_rollback;