Bug 7939: Separate po files for different MARC dialects
[koha.git] / t / db_dependent / Circulation_issue.t
blob11669c3c41dfe49cc1eb0f96a2d1d3c017d5662c
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Koha::DateUtils;
21 use DateTime::Duration;
22 use C4::Biblio;
23 use C4::Members;
24 use C4::Branch;
25 use C4::Circulation;
26 use C4::Items;
27 use C4::Context;
29 use Test::More tests => 30;
31 BEGIN {
32 use_ok('C4::Circulation');
34 can_ok(
35 'C4::Circulation',
36 qw(AddIssue
37 AddIssuingCharge
38 AddRenewal
39 AddReturn
40 GetBiblioIssues
41 GetIssuingCharges
42 GetIssuingRule
43 GetItemIssue
44 GetItemIssues
45 GetOpenIssue
46 GetRenewCount
47 GetUpcomingDueIssues
51 #Start transaction
52 my $dbh = C4::Context->dbh;
53 $dbh->{RaiseError} = 1;
54 $dbh->{AutoCommit} = 0;
56 $dbh->do(q|DELETE FROM issues|);
57 $dbh->do(q|DELETE FROM items|);
58 $dbh->do(q|DELETE FROM borrowers|);
59 $dbh->do(q|DELETE FROM branches|);
60 $dbh->do(q|DELETE FROM categories|);
61 $dbh->do(q|DELETE FROM accountlines|);
62 $dbh->do(q|DELETE FROM issuingrules|);
64 #Add sample datas
66 #Add Dates
68 my $dt_today = dt_from_string;
69 my $today = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
71 my $dt_today2 = dt_from_string;
72 my $dur10 = DateTime::Duration->new( days => -10 );
73 $dt_today2->add_duration($dur10);
74 my $daysago10 = output_pref({ dt => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
76 #Add branch and category
77 my $samplebranch1 = {
78 add => 1,
79 branchcode => 'CPL',
80 branchname => 'Sample Branch',
81 branchaddress1 => 'sample adr1',
82 branchaddress2 => 'sample adr2',
83 branchaddress3 => 'sample adr3',
84 branchzip => 'sample zip',
85 branchcity => 'sample city',
86 branchstate => 'sample state',
87 branchcountry => 'sample country',
88 branchphone => 'sample phone',
89 branchfax => 'sample fax',
90 branchemail => 'sample email',
91 branchurl => 'sample url',
92 branchip => 'sample ip',
93 branchprinter => undef,
94 opac_info => 'sample opac',
96 my $samplebranch2 = {
97 add => 1,
98 branchcode => 'MPL',
99 branchname => 'Sample Branch2',
100 branchaddress1 => 'sample adr1_2',
101 branchaddress2 => 'sample adr2_2',
102 branchaddress3 => 'sample adr3_2',
103 branchzip => 'sample zip2',
104 branchcity => 'sample city2',
105 branchstate => 'sample state2',
106 branchcountry => 'sample country2',
107 branchphone => 'sample phone2',
108 branchfax => 'sample fax2',
109 branchemail => 'sample email2',
110 branchurl => 'sample url2',
111 branchip => 'sample ip2',
112 branchprinter => undef,
113 opac_info => 'sample opac2',
115 ModBranch($samplebranch1);
116 ModBranch($samplebranch2);
118 my $samplecat = {
119 categorycode => 'CAT1',
120 description => 'Description1',
121 enrolmentperiod => 'Null',
122 enrolmentperioddate => 'Null',
123 dateofbirthrequired => 'Null',
124 finetype => 'Null',
125 bulk => 'Null',
126 enrolmentfee => 'Null',
127 overduenoticerequired => 'Null',
128 issuelimit => 'Null',
129 reservefee => 'Null',
130 hidelostitems => 0,
131 category_type => 'Null'
133 my $query =
134 "INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,dateofbirthrequired ,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit ,reservefee ,hidelostitems ,category_type) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
135 $dbh->do(
136 $query, {},
137 $samplecat->{categorycode}, $samplecat->{description},
138 $samplecat->{enrolmentperiod}, $samplecat->{enrolmentperioddate},
139 $samplecat->{dateofbirthrequired}, $samplecat->{finetype},
140 $samplecat->{bulk}, $samplecat->{enrolmentfee},
141 $samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
142 $samplecat->{reservefee}, $samplecat->{hidelostitems},
143 $samplecat->{category_type}
146 #Add biblio and item
147 my $record = MARC::Record->new();
148 $record->append_fields(
149 MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
150 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
152 my @sampleitem1 = C4::Items::AddItem(
154 barcode => 'barcode_1',
155 itemcallnumber => 'callnumber1',
156 homebranch => $samplebranch1->{branchcode},
157 holdingbranch => $samplebranch1->{branchcode},
158 issue => 1,
159 reserve => 1
161 $biblionumber
163 my $item_id1 = $sampleitem1[2];
164 my @sampleitem2 = C4::Items::AddItem(
166 barcode => 'barcode_2',
167 itemcallnumber => 'callnumber2',
168 homebranch => $samplebranch2->{branchcode},
169 holdingbranch => $samplebranch2->{branchcode},
170 notforloan => 1,
171 issue => 1
173 $biblionumber
175 my $item_id2 = $sampleitem2[2];
177 #Add borrower
178 my $borrower_id1 = C4::Members::AddMember(
179 firstname => 'firstname1',
180 surname => 'surname1 ',
181 categorycode => $samplecat->{categorycode},
182 branchcode => $samplebranch1->{branchcode},
184 my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
185 my $borrower_id2 = C4::Members::AddMember(
186 firstname => 'firstname2',
187 surname => 'surname2 ',
188 categorycode => $samplecat->{categorycode},
189 branchcode => $samplebranch2->{branchcode},
191 my $borrower_2 = C4::Members::GetMember(borrowernumber => $borrower_id2);
193 # NEED TO BE FIXED !!!
194 # The first parameter for set_userenv is the class ref
195 #my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'username', 'CPL', 'CPL', 'email@example.org' );
196 my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'CPL', 'CPL', 'email@example.org' );
198 C4::Context->_new_userenv('DUMMY_SESSION_ID');
199 C4::Context->set_userenv(@USERENV);
201 my $userenv = C4::Context->userenv
202 or BAIL_OUT("No userenv");
204 #Begin Tests
206 #Test AddIssue
207 $query = " SELECT count(*) FROM issues";
208 my $sth = $dbh->prepare($query);
209 $sth->execute;
210 my $countissue = $sth -> fetchrow_array;
211 is ($countissue ,0, "there is no issue");
212 my $issue1 = C4::Circulation::AddIssue( $borrower_1, 'barcode_1', $daysago10,0, $today, '' );
213 is( ref $issue1, 'Koha::Schema::Result::Issue',
214 'AddIssue returns a Koha::Schema::Result::Issue object' );
215 my $datedue1 = dt_from_string( $issue1->date_due() );
216 like(
217 $datedue1,
218 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
219 "Koha::Schema::Result::Issue->date_due() returns a date"
221 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef );
223 my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
224 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
225 my $issue_id2 = $dbh->last_insert_id( undef, undef, 'issues', undef );
227 $sth->execute;
228 $countissue = $sth -> fetchrow_array;
229 is ($countissue,1,"1 issues have been added");
231 #Test AddIssuingCharge
232 $query = " SELECT count(*) FROM accountlines";
233 $sth = $dbh->prepare($query);
234 $sth->execute;
235 my $countaccount = $sth -> fetchrow_array;
236 is ($countaccount,0,"0 accountline exists");
237 is( C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, 10 ),
238 1, "An issuing charge has been added" );
239 my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
240 $sth->execute;
241 $countaccount = $sth -> fetchrow_array;
242 is ($countaccount,1,"1 accountline has been added");
244 #Test AddRenewal
245 my $datedue3 =
246 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
247 $datedue1, $daysago10 );
248 like(
249 $datedue3,
250 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
251 "AddRenewal returns a date"
254 #Test GetBiblioIssues
255 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
257 #Test GetItemIssue
258 #FIXME : As the issues are not correctly added in the database, these tests don't work correctly
259 is(GetItemIssue,undef,"Without parameter GetItemIssue returns undef");
260 #is(GetItemIssue($item_id1),{},"Item1's issues");
262 #Test GetItemIssues
263 #FIXME: this routine currently doesn't work be
264 #is_deeply (GetItemIssues,{},"Without parameter, GetItemIssue returns all the issues");
266 #Test GetOpenIssue
267 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
268 is( GetOpenIssue(-1), undef,
269 "With wrong parameter GetOpenIssue returns undef" );
270 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
272 my @renewcount;
273 #Test GetRenewCount
274 my $issue3 = C4::Circulation::AddIssue( $borrower_1, 'barcode_1' );
275 #Without anything in DB
276 @renewcount = C4::Circulation::GetRenewCount();
277 is_deeply(
278 \@renewcount,
279 [ 0, undef, 0 ], # FIXME Need to be fixed
280 "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
282 @renewcount = C4::Circulation::GetRenewCount(-1);
283 is_deeply(
284 \@renewcount,
285 [ 0, undef, 0 ], # FIXME Need to be fixed
286 "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
288 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
289 is_deeply(
290 \@renewcount,
291 [ 2, undef, 0 ],
292 "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
295 #With something in DB
296 # Add a default rule: No renewal allowed
297 $dbh->do(q|
298 INSERT INTO issuingrules( categorycode, itemtype, branchcode, issuelength, renewalsallowed )
299 VALUES ( '*', '*', '*', 10, 0 )
301 @renewcount = C4::Circulation::GetRenewCount();
302 is_deeply(
303 \@renewcount,
304 [ 0, 0, 0 ],
305 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
307 @renewcount = C4::Circulation::GetRenewCount(-1);
308 is_deeply(
309 \@renewcount,
310 [ 0, 0, 0 ],
311 "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
313 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
314 is_deeply(
315 \@renewcount,
316 [ 2, 0, 0 ],
317 "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
320 # Add a default rule: renewal is allowed
321 $dbh->do(q|
322 UPDATE issuingrules SET renewalsallowed = 3
324 @renewcount = C4::Circulation::GetRenewCount();
325 is_deeply(
326 \@renewcount,
327 [ 0, 3, 3 ],
328 "With issuing rules (renewal allowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
330 @renewcount = C4::Circulation::GetRenewCount(-1);
331 is_deeply(
332 \@renewcount,
333 [ 0, 3, 3 ],
334 "With issuing rules (renewal allowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
336 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
337 is_deeply(
338 \@renewcount,
339 [ 2, 3, 1 ],
340 "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
343 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
344 $datedue3, $daysago10 );
345 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
346 is_deeply(
347 \@renewcount,
348 [ 3, 3, 0 ],
349 "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
352 $dbh->do("DELETE FROM old_issues");
353 AddReturn('barcode_1');
354 my $return = $dbh->selectrow_hashref("SELECT DATE(returndate) AS return_date, CURRENT_DATE() AS today FROM old_issues LIMIT 1" );
355 ok( $return->{return_date} eq $return->{today}, "Item returned with no return date specified has todays date" );
357 $dbh->do("DELETE FROM old_issues");
358 C4::Circulation::AddIssue( $borrower_1, 'barcode_1', $daysago10, 0, $today );
359 AddReturn('barcode_1', undef, undef, undef, '2014-04-01 23:42');
360 $return = $dbh->selectrow_hashref("SELECT * FROM old_issues LIMIT 1" );
361 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" );
363 my $itemnumber;
364 ($biblionumber, $biblioitemnumber, $itemnumber) = C4::Items::AddItem(
366 barcode => 'barcode_3',
367 itemcallnumber => 'callnumber3',
368 homebranch => $samplebranch1->{branchcode},
369 holdingbranch => $samplebranch1->{branchcode},
370 notforloan => 1,
372 $biblionumber
375 C4::Context->set_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
376 AddReturn( 'barcode_3', $samplebranch1->{branchcode} );
377 my $item = GetItem( $itemnumber );
378 ok( $item->{notforloan} eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
380 C4::Context->set_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
381 AddReturn( 'barcode_3', $samplebranch1->{branchcode} );
382 $item = GetItem( $itemnumber );
383 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
385 AddReturn( 'barcode_3', $samplebranch1->{branchcode} );
386 $item = GetItem( $itemnumber );
387 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
390 #End transaction
391 $dbh->rollback;