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