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 => 4;
25 use t::lib::TestBuilder;
32 use Koha::Account::Lines;
40 # Mock userenv, used by AddIssue
42 my $context = Test::MockModule->new('C4::Context');
43 $context->mock( 'userenv', sub {
44 return { branch => $branch }
47 my $schema = Koha::Database->schema;
48 $schema->storage->txn_begin;
50 my $builder = t::lib::TestBuilder->new();
51 Koha::IssuingRules->search->delete;
52 my $rule = Koha::IssuingRule->new(
63 subtest "InProcessingToShelvingCart tests" => sub {
67 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
68 my $permanent_location = 'TEST';
69 my $location = 'PROC';
71 # Create a biblio record with biblio-level itemtype
72 my $record = MARC::Record->new();
73 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
74 my $built_item = $builder->build({
77 biblionumber => $biblionumber,
78 homebranch => $branch,
79 holdingbranch => $branch,
80 location => $location,
81 permanent_location => $permanent_location
84 my $barcode = $built_item->{ barcode };
85 my $itemnumber = $built_item->{ itemnumber };
88 t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 1 );
89 AddReturn( $barcode, $branch );
90 $item = GetItem( $itemnumber );
91 is( $item->{location}, 'CART',
92 "InProcessingToShelvingCart functions as intended" );
94 $item->{location} = $location;
95 ModItem( $item, undef, $itemnumber );
97 t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 0 );
98 AddReturn( $barcode, $branch );
99 $item = GetItem( $itemnumber );
100 is( $item->{location}, $permanent_location,
101 "InProcessingToShelvingCart functions as intended" );
105 subtest "AddReturn logging on statistics table (item-level_itypes=1)" => sub {
109 # Set item-level item types
110 t::lib::Mocks::mock_preference( "item-level_itypes", 1 );
112 # Make sure logging is enabled
113 t::lib::Mocks::mock_preference( "IssueLog", 1 );
114 t::lib::Mocks::mock_preference( "ReturnLog", 1 );
116 # Create an itemtype for biblio-level item type
117 my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
118 # Create an itemtype for item-level item type
119 my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
121 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
123 my $borrowernumber = $builder->build({
124 source => 'Borrower',
125 value => { branchcode => $branch }
126 })->{ borrowernumber };
127 # Look for the defined MARC field for biblio-level itemtype
128 my $rs = $schema->resultset('MarcSubfieldStructure')->search({
130 kohafield => 'biblioitems.itemtype'
132 my $tagfield = $rs->first->tagfield;
133 my $tagsubfield = $rs->first->tagsubfield;
135 # Create a biblio record with biblio-level itemtype
136 my $record = MARC::Record->new();
137 $record->append_fields(
138 MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
140 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
141 my $item_with_itemtype = $builder->build(
145 biblionumber => $biblionumber,
146 biblioitemnumber => $biblioitemnumber,
147 homebranch => $branch,
148 holdingbranch => $branch,
149 itype => $ilevel_itemtype
153 my $item_without_itemtype = $builder->build(
157 biblionumber => $biblionumber,
158 biblioitemnumber => $biblioitemnumber,
159 homebranch => $branch,
160 holdingbranch => $branch,
166 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
167 AddIssue( $borrower, $item_with_itemtype->{ barcode } );
168 AddReturn( $item_with_itemtype->{ barcode }, $branch );
169 # Test item-level itemtype was recorded on the 'statistics' table
170 my $stat = $schema->resultset('Statistic')->search({
173 itemnumber => $item_with_itemtype->{ itemnumber }
174 }, { order_by => { -asc => 'datetime' } })->next();
176 is( $stat->itemtype, $ilevel_itemtype,
177 "item-level itype recorded on statistics for return");
178 warning_like { AddIssue( $borrower, $item_without_itemtype->{ barcode } ) }
179 [qr/^item-level_itypes set but no itemtype set for item/,
180 qr/^item-level_itypes set but no itemtype set for item/],
181 'Item without itemtype set raises warning on AddIssue';
182 warning_like { AddReturn( $item_without_itemtype->{ barcode }, $branch ) }
183 qr/^item-level_itypes set but no itemtype set for item/,
184 'Item without itemtype set raises warning on AddReturn';
185 # Test biblio-level itemtype was recorded on the 'statistics' table
186 $stat = $schema->resultset('Statistic')->search({
189 itemnumber => $item_without_itemtype->{ itemnumber }
190 }, { order_by => { -asc => 'datetime' } })->next();
192 is( $stat->itemtype, $blevel_itemtype,
193 "biblio-level itype recorded on statistics for return as a fallback for null item-level itype");
197 subtest "AddReturn logging on statistics table (item-level_itypes=0)" => sub {
201 # Make sure logging is enabled
202 t::lib::Mocks::mock_preference( "IssueLog", 1 );
203 t::lib::Mocks::mock_preference( "ReturnLog", 1 );
205 # Set item-level item types
206 t::lib::Mocks::mock_preference( "item-level_itypes", 0 );
208 # Create an itemtype for biblio-level item type
209 my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
210 # Create an itemtype for item-level item type
211 my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
213 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
215 my $borrowernumber = $builder->build({
216 source => 'Borrower',
217 value => { branchcode => $branch }
218 })->{ borrowernumber };
219 # Look for the defined MARC field for biblio-level itemtype
220 my $rs = $schema->resultset('MarcSubfieldStructure')->search({
222 kohafield => 'biblioitems.itemtype'
224 my $tagfield = $rs->first->tagfield;
225 my $tagsubfield = $rs->first->tagsubfield;
227 # Create a biblio record with biblio-level itemtype
228 my $record = MARC::Record->new();
229 $record->append_fields(
230 MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
232 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
233 my $item_with_itemtype = $builder->build({
236 biblionumber => $biblionumber,
237 homebranch => $branch,
238 holdingbranch => $branch,
239 itype => $ilevel_itemtype
242 my $item_without_itemtype = $builder->build({
245 biblionumber => $biblionumber,
246 homebranch => $branch,
247 holdingbranch => $branch,
252 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
254 AddIssue( $borrower, $item_with_itemtype->{ barcode } );
255 AddReturn( $item_with_itemtype->{ barcode }, $branch );
256 # Test item-level itemtype was recorded on the 'statistics' table
257 my $stat = $schema->resultset('Statistic')->search({
260 itemnumber => $item_with_itemtype->{ itemnumber }
261 }, { order_by => { -asc => 'datetime' } })->next();
263 is( $stat->itemtype, $blevel_itemtype,
264 "biblio-level itype recorded on statistics for return");
266 AddIssue( $borrower, $item_without_itemtype->{ barcode } );
267 AddReturn( $item_without_itemtype->{ barcode }, $branch );
268 # Test biblio-level itemtype was recorded on the 'statistics' table
269 $stat = $schema->resultset('Statistic')->search({
272 itemnumber => $item_without_itemtype->{ itemnumber }
273 }, { order_by => { -asc => 'datetime' } })->next();
275 is( $stat->itemtype, $blevel_itemtype,
276 "biblio-level itype recorded on statistics for return");
279 subtest 'Handle ids duplication' => sub {
282 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
283 t::lib::Mocks::mock_preference( 'CalculateFinesOnReturn', 1 );
284 t::lib::Mocks::mock_preference( 'finesMode', 'production' );
285 Koha::IssuingRules->search->update({ chargeperiod => 1, fine => 1, firstremind => 1, });
287 my $biblio = $builder->build( { source => 'Biblio' } );
288 my $itemtype = $builder->build( { source => 'Itemtype', value => { rentalcharge => 5 } } );
289 my $item = $builder->build(
293 biblionumber => $biblio->{biblionumber},
297 itype => $itemtype->{itemtype},
301 my $patron = $builder->build({source => 'Borrower'});
302 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
304 my $original_checkout = AddIssue( $patron->unblessed, $item->{barcode}, dt_from_string->subtract( days => 50 ) );
306 my $issue_id = $original_checkout->issue_id;
307 # Create an existing entry in old_issue
308 $builder->build({ source => 'OldIssue', value => { issue_id => $issue_id } });
310 my $old_checkout = Koha::Old::Checkouts->find( $issue_id );
312 my ($doreturn, $messages, $new_checkout, $borrower);
314 ( $doreturn, $messages, $new_checkout, $borrower ) =
315 AddReturn( $item->{barcode}, undef, undef, undef, dt_from_string );
318 qr{.*DBD::mysql::st execute failed: Duplicate entry.*},
319 { carped => qr{The checkin for the following issue failed.*DBIx::Class::Storage::DBI::_dbh_execute.*} }
321 'DBD should have raised an error about dup primary key';
323 is( $doreturn, 0, 'Return should not have been done' );
324 is( $messages->{WasReturned}, 0, 'messages should have the WasReturned flag set to 0' );
325 is( $messages->{DataCorrupted}, 1, 'messages should have the DataCorrupted flag set to 1' );
327 my $account_lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber, issue_id => $issue_id });
328 is( $account_lines->count, 0, 'No account lines should exist for this issue_id, patron should not have been charged' );
330 is( Koha::Checkouts->find( $issue_id )->issue_id, $issue_id, 'The issues entry should not have been removed' );