2f09606de560decd701f6f1aa9dee1446490c45b
[koha.git] / t / db_dependent / Circulation / Returns.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 => 5;
21 use Test::MockModule;
22 use Test::Warn;
23
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use C4::Members;
28 use C4::Circulation;
29 use C4::Items;
30 use C4::Biblio;
31 use Koha::Database;
32 use Koha::Account::Lines;
33 use Koha::DateUtils;
34 use Koha::Items;
35 use Koha::Patrons;
36
37 use MARC::Record;
38 use MARC::Field;
39
40 # Mock userenv, used by AddIssue
41 my $branch;
42 my $manager_id;
43 my $context = Test::MockModule->new('C4::Context');
44 $context->mock(
45     'userenv',
46     sub {
47         return {
48             branch    => $branch,
49             number    => $manager_id,
50             firstname => "Adam",
51             surname   => "Smaith"
52         };
53     }
54 );
55
56 my $schema = Koha::Database->schema;
57 $schema->storage->txn_begin;
58
59 my $builder = t::lib::TestBuilder->new();
60 Koha::IssuingRules->search->delete;
61 my $rule = Koha::IssuingRule->new(
62     {
63         categorycode => '*',
64         itemtype     => '*',
65         branchcode   => '*',
66         issuelength  => 1,
67     }
68 );
69 $rule->store();
70
71 my $manager = $builder->build({source => 'Borrower'});
72 $manager_id = $manager->{borrowernumber};
73
74 subtest "InProcessingToShelvingCart tests" => sub {
75
76     plan tests => 2;
77
78     $branch = $builder->build({ source => 'Branch' })->{ branchcode };
79     my $permanent_location = 'TEST';
80     my $location           = 'PROC';
81
82     # Create a biblio record with biblio-level itemtype
83     my $record = MARC::Record->new();
84     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
85     my $built_item = $builder->build({
86         source => 'Item',
87         value  => {
88             biblionumber  => $biblionumber,
89             homebranch    => $branch,
90             holdingbranch => $branch,
91             location      => $location,
92             permanent_location => $permanent_location
93         }
94     });
95     my $barcode = $built_item->{ barcode };
96     my $itemnumber = $built_item->{ itemnumber };
97     my $item;
98
99     t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 1 );
100     AddReturn( $barcode, $branch );
101     $item = Koha::Items->find( $itemnumber );
102     is( $item->location, 'CART',
103         "InProcessingToShelvingCart functions as intended" );
104
105     ModItem( {location => $location}, undef, $itemnumber );
106
107     t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 0 );
108     AddReturn( $barcode, $branch );
109     $item = Koha::Items->find( $itemnumber );
110     is( $item->location, $permanent_location,
111         "InProcessingToShelvingCart functions as intended" );
112 };
113
114
115 subtest "AddReturn logging on statistics table (item-level_itypes=1)" => sub {
116
117     plan tests => 4;
118
119     # Set item-level item types
120     t::lib::Mocks::mock_preference( "item-level_itypes", 1 );
121
122     # Make sure logging is enabled
123     t::lib::Mocks::mock_preference( "IssueLog", 1 );
124     t::lib::Mocks::mock_preference( "ReturnLog", 1 );
125
126     # Create an itemtype for biblio-level item type
127     my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
128     # Create an itemtype for item-level item type
129     my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
130     # Create a branch
131     $branch = $builder->build({ source => 'Branch' })->{ branchcode };
132     # Create a borrower
133     my $borrowernumber = $builder->build({
134         source => 'Borrower',
135         value => { branchcode => $branch }
136     })->{ borrowernumber };
137     # Look for the defined MARC field for biblio-level itemtype
138     my $rs = $schema->resultset('MarcSubfieldStructure')->search({
139         frameworkcode => '',
140         kohafield     => 'biblioitems.itemtype'
141     });
142     my $tagfield    = $rs->first->tagfield;
143     my $tagsubfield = $rs->first->tagsubfield;
144
145     # Create a biblio record with biblio-level itemtype
146     my $record = MARC::Record->new();
147     $record->append_fields(
148         MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
149     );
150     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
151     my $item_with_itemtype = $builder->build(
152         {
153             source => 'Item',
154             value  => {
155                 biblionumber     => $biblionumber,
156                 biblioitemnumber => $biblioitemnumber,
157                 homebranch       => $branch,
158                 holdingbranch    => $branch,
159                 itype            => $ilevel_itemtype
160             }
161         }
162     );
163     my $item_without_itemtype = $builder->build(
164         {
165             source => 'Item',
166             value  => {
167                 biblionumber     => $biblionumber,
168                 biblioitemnumber => $biblioitemnumber,
169                 homebranch       => $branch,
170                 holdingbranch    => $branch,
171                 itype            => undef
172             }
173         }
174     );
175
176     my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
177     AddIssue( $borrower, $item_with_itemtype->{ barcode } );
178     AddReturn( $item_with_itemtype->{ barcode }, $branch );
179     # Test item-level itemtype was recorded on the 'statistics' table
180     my $stat = $schema->resultset('Statistic')->search({
181         branch     => $branch,
182         type       => 'return',
183         itemnumber => $item_with_itemtype->{ itemnumber }
184     }, { order_by => { -asc => 'datetime' } })->next();
185
186     is( $stat->itemtype, $ilevel_itemtype,
187         "item-level itype recorded on statistics for return");
188     warning_like { AddIssue( $borrower, $item_without_itemtype->{ barcode } ) }
189                  [qr/^item-level_itypes set but no itemtype set for item/,
190                  qr/^item-level_itypes set but no itemtype set for item/,
191                  qr/^item-level_itypes set but no itemtype set for item/,
192                  qr/^item-level_itypes set but no itemtype set for item/,
193                  qr/^item-level_itypes set but no itemtype set for item/,
194                  qr/^item-level_itypes set but no itemtype set for item/],
195                  'Item without itemtype set raises warning on AddIssue';
196     warning_like { AddReturn( $item_without_itemtype->{ barcode }, $branch ) }
197                  qr/^item-level_itypes set but no itemtype set for item/,
198                  'Item without itemtype set raises warning on AddReturn';
199     # Test biblio-level itemtype was recorded on the 'statistics' table
200     $stat = $schema->resultset('Statistic')->search({
201         branch     => $branch,
202         type       => 'return',
203         itemnumber => $item_without_itemtype->{ itemnumber }
204     }, { order_by => { -asc => 'datetime' } })->next();
205
206     is( $stat->itemtype, $blevel_itemtype,
207         "biblio-level itype recorded on statistics for return as a fallback for null item-level itype");
208
209 };
210
211 subtest "AddReturn logging on statistics table (item-level_itypes=0)" => sub {
212
213     plan tests => 2;
214
215     # Make sure logging is enabled
216     t::lib::Mocks::mock_preference( "IssueLog", 1 );
217     t::lib::Mocks::mock_preference( "ReturnLog", 1 );
218
219     # Set biblio level item types
220     t::lib::Mocks::mock_preference( "item-level_itypes", 0 );
221
222     # Create an itemtype for biblio-level item type
223     my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
224     # Create an itemtype for item-level item type
225     my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
226     # Create a branch
227     $branch = $builder->build({ source => 'Branch' })->{ branchcode };
228     # Create a borrower
229     my $borrowernumber = $builder->build({
230         source => 'Borrower',
231         value => { branchcode => $branch }
232     })->{ borrowernumber };
233     # Look for the defined MARC field for biblio-level itemtype
234     my $rs = $schema->resultset('MarcSubfieldStructure')->search({
235         frameworkcode => '',
236         kohafield     => 'biblioitems.itemtype'
237     });
238     my $tagfield    = $rs->first->tagfield;
239     my $tagsubfield = $rs->first->tagsubfield;
240
241     # Create a biblio record with biblio-level itemtype
242     my $record = MARC::Record->new();
243     $record->append_fields(
244         MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
245     );
246     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
247     my $item_with_itemtype = $builder->build({
248         source => 'Item',
249         value  => {
250             biblionumber  => $biblionumber,
251             biblioitemnumber => $biblioitemnumber,
252             homebranch    => $branch,
253             holdingbranch => $branch,
254             itype         => $ilevel_itemtype
255         }
256     });
257     my $item_without_itemtype = $builder->build({
258         source => 'Item',
259         value  => {
260             biblionumber  => $biblionumber,
261             biblioitemnumber => $biblioitemnumber,
262             homebranch    => $branch,
263             holdingbranch => $branch,
264             itype         => undef
265         }
266     });
267
268     my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
269
270     AddIssue( $borrower, $item_with_itemtype->{ barcode } );
271     AddReturn( $item_with_itemtype->{ barcode }, $branch );
272     # Test item-level itemtype was recorded on the 'statistics' table
273     my $stat = $schema->resultset('Statistic')->search({
274         branch     => $branch,
275         type       => 'return',
276         itemnumber => $item_with_itemtype->{ itemnumber }
277     }, { order_by => { -asc => 'datetime' } })->next();
278
279     is( $stat->itemtype, $blevel_itemtype,
280         "biblio-level itype recorded on statistics for return");
281
282     AddIssue( $borrower, $item_without_itemtype->{ barcode } );
283     AddReturn( $item_without_itemtype->{ barcode }, $branch );
284     # Test biblio-level itemtype was recorded on the 'statistics' table
285     $stat = $schema->resultset('Statistic')->search({
286         branch     => $branch,
287         type       => 'return',
288         itemnumber => $item_without_itemtype->{ itemnumber }
289     }, { order_by => { -asc => 'datetime' } })->next();
290
291     is( $stat->itemtype, $blevel_itemtype,
292         "biblio-level itype recorded on statistics for return");
293 };
294
295 subtest 'Handle ids duplication' => sub {
296     plan tests => 8;
297
298     t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
299     t::lib::Mocks::mock_preference( 'CalculateFinesOnReturn', 1 );
300     t::lib::Mocks::mock_preference( 'finesMode', 'production' );
301     Koha::IssuingRules->search->update({ chargeperiod => 1, fine => 1, firstremind => 1, });
302
303     my $biblio = $builder->build( { source => 'Biblio' } );
304     my $itemtype = $builder->build( { source => 'Itemtype', value => { rentalcharge => 5 } } );
305     my $item = $builder->build(
306         {
307             source => 'Item',
308             value  => {
309                 biblionumber => $biblio->{biblionumber},
310                 notforloan => 0,
311                 itemlost   => 0,
312                 withdrawn  => 0,
313                 itype      => $itemtype->{itemtype},
314             }
315         }
316     );
317     my $patron = $builder->build({source => 'Borrower'});
318     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
319
320     my $original_checkout = AddIssue( $patron->unblessed, $item->{barcode}, dt_from_string->subtract( days => 50 ) );
321     my $issue_id = $original_checkout->issue_id;
322     my $account_lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber, issue_id => $issue_id });
323     is( $account_lines->count, 1, '1 account line should exist for this issue_id' );
324     is( $account_lines->next->description, 'Rental', 'patron has been charged the rentalcharge' );
325     $account_lines->delete;
326
327     # Create an existing entry in old_issue
328     $builder->build({ source => 'OldIssue', value => { issue_id => $issue_id } });
329
330     my $old_checkout = Koha::Old::Checkouts->find( $issue_id );
331
332     my ($doreturn, $messages, $new_checkout, $borrower);
333     warning_like {
334         ( $doreturn, $messages, $new_checkout, $borrower ) =
335           AddReturn( $item->{barcode}, undef, undef, undef, dt_from_string );
336     }
337     [
338         qr{.*DBD::mysql::st execute failed: Duplicate entry.*},
339         { carped => qr{The checkin for the following issue failed.*Duplicate ID.*} }
340     ],
341     'DBD should have raised an error about dup primary key';
342
343     is( $doreturn, 0, 'Return should not have been done' );
344     is( $messages->{WasReturned}, 0, 'messages should have the WasReturned flag set to 0' );
345     is( $messages->{DataCorrupted}, 1, 'messages should have the DataCorrupted flag set to 1' );
346
347     $account_lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber, issue_id => $issue_id });
348     is( $account_lines->count, 0, 'No account lines should exist for this issue_id, patron should not have been charged' );
349
350     is( Koha::Checkouts->find( $issue_id )->issue_id, $issue_id, 'The issues entry should not have been removed' );
351 };
352
353 subtest 'BlockReturnOfLostItems' => sub {
354     plan tests => 4;
355     my $biblio = $builder->build_object( { class => 'Koha::Biblios' } );
356     my $item = $builder->build_object(
357         {
358             class  => 'Koha::Items',
359             value  => {
360                 biblionumber => $biblio->biblionumber,
361                 notforloan => 0,
362                 itemlost   => 0,
363                 withdrawn  => 0,
364         }
365     }
366     );
367     my $patron = $builder->build_object({class => 'Koha::Patrons'});
368     my $checkout = AddIssue( $patron->unblessed, $item->barcode );
369
370     # Mark the item as lost
371     ModItem({itemlost => 1}, $biblio->biblionumber, $item->itemnumber);
372
373     t::lib::Mocks::mock_preference('BlockReturnOfLostItems', 1);
374     my ( $doreturn, $messages, $issue ) = AddReturn($item->barcode);
375     is( $doreturn, 0, "With BlockReturnOfLostItems, a checkin of a lost item should be blocked");
376     is( $messages->{WasLost}, 1, "... and the WasLost flag should be set");
377
378     $item->discard_changes;
379     is( $item->itemlost, 1, "Item remains lost" );
380
381     t::lib::Mocks::mock_preference('BlockReturnOfLostItems', 0);
382     ( $doreturn, $messages, $issue ) = AddReturn($item->barcode);
383     is( $doreturn, 1, "Without BlockReturnOfLostItems, a checkin of a lost item should not be blocked");
384 };