Bug 17829: Move GetMember to Koha::Patron
[koha.git] / t / db_dependent / Circulation / issue.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 => 30;
21 use DateTime::Duration;
22
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
25
26 use C4::Biblio;
27 use C4::Circulation;
28 use C4::Context;
29 use C4::Items;
30 use C4::Members;
31 use C4::Reserves;
32 use Koha::Database;
33 use Koha::DateUtils;
34 use Koha::Library;
35 use Koha::Patrons;
36
37 BEGIN {
38     require_ok('C4::Circulation');
39 }
40
41 can_ok(
42     'C4::Circulation',
43     qw(AddIssue
44       AddIssuingCharge
45       AddRenewal
46       AddReturn
47       GetBiblioIssues
48       GetIssuingCharges
49       GetOpenIssue
50       GetRenewCount
51       GetUpcomingDueIssues
52       )
53 );
54
55 #Start transaction
56 my $schema = Koha::Database->schema;
57 $schema->storage->txn_begin;
58 my $dbh = C4::Context->dbh;
59
60 my $builder = t::lib::TestBuilder->new();
61
62 $dbh->do(q|DELETE FROM issues|);
63 $dbh->do(q|DELETE FROM items|);
64 $dbh->do(q|DELETE FROM borrowers|);
65 $dbh->do(q|DELETE FROM branches|);
66 $dbh->do(q|DELETE FROM categories|);
67 $dbh->do(q|DELETE FROM accountlines|);
68 $dbh->do(q|DELETE FROM issuingrules|);
69 $dbh->do(q|DELETE FROM reserves|);
70 $dbh->do(q|DELETE FROM old_reserves|);
71 $dbh->do(q|DELETE FROM statistics|);
72
73 # Generate sample datas
74 my $itemtype = $builder->build(
75     {   source => 'Itemtype',
76         value  => { notforloan => undef, rentalcharge => 0 }
77     }
78 )->{itemtype};
79 my $branchcode_1 = $builder->build({ source => 'Branch' })->{branchcode};
80 my $branchcode_2 = $builder->build({ source => 'Branch' })->{branchcode};
81 my $branchcode_3 = $builder->build({ source => 'Branch' })->{branchcode};
82 my $categorycode = $builder->build({
83         source => 'Category',
84         value => { enrolmentfee => undef }
85     })->{categorycode};
86
87 # Add Dates
88 my $dt_today = dt_from_string;
89 my $today    = output_pref(
90     {   dt         => $dt_today,
91         dateformat => 'iso',
92         timeformat => '24hr',
93         dateonly   => 1
94     }
95 );
96
97 my $dt_today2 = dt_from_string;
98 my $dur10 = DateTime::Duration->new( days => -10 );
99 $dt_today2->add_duration($dur10);
100 my $daysago10 = output_pref(
101     {   dt         => $dt_today2,
102         dateformat => 'iso',
103         timeformat => '24hr',
104         dateonly   => 1
105     }
106 );
107
108 # Add biblio and item
109 my $record = MARC::Record->new();
110 $record->append_fields(
111     MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
112
113 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
114
115 my $barcode_1 = 'barcode_1';
116 my $barcode_2 = 'barcode_2';
117 my @sampleitem1 = C4::Items::AddItem(
118     {
119         barcode        => $barcode_1,
120         itemcallnumber => 'callnumber1',
121         homebranch     => $branchcode_1,
122         holdingbranch  => $branchcode_1,
123         issue          => 1,
124         reserve        => 1,
125         itype          => $itemtype
126     },
127     $biblionumber
128 );
129 my $item_id1    = $sampleitem1[2];
130 my @sampleitem2 = C4::Items::AddItem(
131     {
132         barcode        => $barcode_2,
133         itemcallnumber => 'callnumber2',
134         homebranch     => $branchcode_2,
135         holdingbranch  => $branchcode_2,
136         notforloan     => 1,
137         issue          => 1,
138         itype          => $itemtype
139     },
140     $biblionumber
141 );
142 my $item_id2 = $sampleitem2[2];
143
144 #Add borrower
145 my $borrower_id1 = C4::Members::AddMember(
146     firstname    => 'firstname1',
147     surname      => 'surname1 ',
148     categorycode => $categorycode,
149     branchcode   => $branchcode_1
150 );
151 my $borrower_1 = Koha::Patrons->find( $borrower_id1 )->unblessed;
152 my $borrower_id2 = C4::Members::AddMember(
153     firstname    => 'firstname2',
154     surname      => 'surname2 ',
155     categorycode => $categorycode,
156     branchcode   => $branchcode_2,
157 );
158 my $borrower_2 = Koha::Patrons->find( $borrower_id2 )->unblessed;
159
160 my @USERENV = (
161     $borrower_id1, 'test', 'MASTERTEST', 'firstname', $branchcode_1,
162     $branchcode_1, 'email@example.org'
163 );
164
165 my @USERENV_DIFFERENT_LIBRARY = (
166     $borrower_id1, 'test', 'MASTERTEST', 'firstname', $branchcode_3,
167     $branchcode_3, 'email@example.org'
168 );
169
170
171 C4::Context->_new_userenv('DUMMY_SESSION_ID');
172 C4::Context->set_userenv(@USERENV);
173
174 my $userenv = C4::Context->userenv
175   or BAIL_OUT("No userenv");
176
177 #Begin Tests
178
179 #Test AddIssue
180 my $query = " SELECT count(*) FROM issues";
181 my $sth = $dbh->prepare($query);
182 $sth->execute;
183 my $countissue = $sth -> fetchrow_array;
184 is ($countissue ,0, "there is no issue");
185 my $issue1 = C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10,0, $today, '' );
186 is( ref $issue1, 'Koha::Schema::Result::Issue',
187        'AddIssue returns a Koha::Schema::Result::Issue object' );
188 my $datedue1 = dt_from_string( $issue1->date_due() );
189 like(
190     $datedue1,
191     qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
192     "Koha::Schema::Result::Issue->date_due() returns a date"
193 );
194 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef );
195
196 my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
197 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
198 my $issue_id2 = $dbh->last_insert_id( undef, undef, 'issues', undef );
199
200 $sth->execute;
201 $countissue = $sth -> fetchrow_array;
202 is ($countissue,1,"1 issues have been added");
203
204 #Test AddIssuingCharge
205 $query = " SELECT count(*) FROM accountlines";
206 $sth = $dbh->prepare($query);
207 $sth->execute;
208 my $countaccount = $sth -> fetchrow_array;
209 is ($countaccount,0,"0 accountline exists");
210 is( C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, 10 ),
211     1, "An issuing charge has been added" );
212 my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
213 $sth->execute;
214 $countaccount = $sth -> fetchrow_array;
215 is ($countaccount,1,"1 accountline has been added");
216
217 # Test AddRenewal
218
219 # Let's renew this one at a different library for statistical purposes to test Bug 17781
220 C4::Context->set_userenv(@USERENV_DIFFERENT_LIBRARY);
221 my $datedue3 = AddRenewal( $borrower_id1, $item_id1, $branchcode_1, $datedue1, $daysago10 );
222 C4::Context->set_userenv(@USERENV);
223
224 like(
225     $datedue3,
226     qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
227     "AddRenewal returns a date"
228 );
229
230 my $stat = $dbh->selectrow_hashref("SELECT * FROM statistics WHERE type = 'renew' AND borrowernumber = ? AND itemnumber = ? AND branch = ?", undef, $borrower_id1, $item_id1, $branchcode_3 );
231 ok( $stat, "Bug 17781 - 'Improper branchcode set during renewal' still fixed" );
232
233
234 #Test GetBiblioIssues
235 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
236
237 #Test GetOpenIssue
238 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
239 is( GetOpenIssue(-1), undef,
240     "With wrong parameter GetOpenIssue returns undef" );
241 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
242
243 my @renewcount;
244 #Test GetRenewCount
245 my $issue3 = C4::Circulation::AddIssue( $borrower_1, $barcode_1 );
246 #Without anything in DB
247 @renewcount = C4::Circulation::GetRenewCount();
248 is_deeply(
249     \@renewcount,
250     [ 0, 0, 0 ], # FIXME Need to be fixed, see FIXME in GetRenewCount
251     "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
252 );
253 @renewcount = C4::Circulation::GetRenewCount(-1);
254 is_deeply(
255     \@renewcount,
256     [ 0, 0, 0 ], # FIXME Need to be fixed
257     "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
258 );
259 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
260 is_deeply(
261     \@renewcount,
262     [ 2, 0, 0 ],
263     "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
264 );
265
266 #With something in DB
267 # Add a default rule: No renewal allowed
268 $dbh->do(q|
269     INSERT INTO issuingrules( categorycode, itemtype, branchcode, issuelength, renewalsallowed )
270     VALUES ( '*', '*', '*', 10, 0 )
271 |);
272 @renewcount = C4::Circulation::GetRenewCount();
273 is_deeply(
274     \@renewcount,
275     [ 0, 0, 0 ],
276     "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
277 );
278 @renewcount = C4::Circulation::GetRenewCount(-1);
279 is_deeply(
280     \@renewcount,
281     [ 0, 0, 0 ],
282     "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
283 );
284 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
285 is_deeply(
286     \@renewcount,
287     [ 2, 0, 0 ],
288     "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
289 );
290
291 # Add a default rule: renewal is allowed
292 $dbh->do(q|
293     UPDATE issuingrules SET renewalsallowed = 3
294 |);
295 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
296 is_deeply(
297     \@renewcount,
298     [ 2, 3, 1 ],
299     "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
300 );
301
302 AddRenewal( $borrower_id1, $item_id1, $branchcode_1,
303     $datedue3, $daysago10 );
304 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
305 is_deeply(
306     \@renewcount,
307     [ 3, 3, 0 ],
308     "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
309 );
310
311 $dbh->do("DELETE FROM old_issues");
312 AddReturn($barcode_1);
313 my $return = $dbh->selectrow_hashref("SELECT DATE(returndate) AS return_date, CURRENT_DATE() AS today FROM old_issues LIMIT 1" );
314 ok( $return->{return_date} eq $return->{today}, "Item returned with no return date specified has todays date" );
315
316 $dbh->do("DELETE FROM old_issues");
317 C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10, 0, $today );
318 AddReturn($barcode_1, undef, undef, undef, '2014-04-01 23:42');
319 $return = $dbh->selectrow_hashref("SELECT * FROM old_issues LIMIT 1" );
320 ok( $return->{returndate} eq '2014-04-01 23:42:00', "Item returned with a return date of '2014-04-01 23:42' has that return date" );
321
322 my $itemnumber;
323 ($biblionumber, $biblioitemnumber, $itemnumber) = C4::Items::AddItem(
324     {
325         barcode        => 'barcode_3',
326         itemcallnumber => 'callnumber3',
327         homebranch     => $branchcode_1,
328         holdingbranch  => $branchcode_1,
329         notforloan     => 1,
330         itype          => $itemtype
331     },
332     $biblionumber
333 );
334
335 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
336 AddReturn( 'barcode_3', $branchcode_1 );
337 my $item = GetItem( $itemnumber );
338 ok( $item->{notforloan} eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
339
340 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
341 AddReturn( 'barcode_3', $branchcode_1 );
342 $item = GetItem( $itemnumber );
343 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
344
345 AddReturn( 'barcode_3', $branchcode_1 );
346 $item = GetItem( $itemnumber );
347 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
348
349 # Bug 14640 - Cancel the hold on checking out if asked
350 my $reserve_id = AddReserve($branchcode_1, $borrower_id1, $biblionumber,
351     undef,  1, undef, undef, "a note", "a title", undef, '');
352 ok( $reserve_id, 'The reserve should have been inserted' );
353 AddIssue( $borrower_2, $barcode_1, dt_from_string, 'cancel' );
354 my $reserve = GetReserve( $reserve_id );
355 is( $reserve, undef, 'The reserve should have been correctly cancelled' );
356
357 #End transaction
358 $schema->storage->txn_rollback;