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