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>.
21 use DateTime
::Duration
;
29 use Test
::More tests
=> 30;
32 use_ok
('C4::Circulation');
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
|);
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
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',
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);
119 categorycode
=> 'CAT1',
120 description
=> 'Description1',
121 enrolmentperiod
=> 'Null',
122 enrolmentperioddate
=> 'Null',
123 dateofbirthrequired
=> 'Null',
126 enrolmentfee
=> 'Null',
127 overduenoticerequired
=> 'Null',
128 issuelimit
=> 'Null',
129 reservefee
=> 'Null',
131 category_type
=> 'Null'
134 "INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,dateofbirthrequired ,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit ,reservefee ,hidelostitems ,category_type) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
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
}
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
},
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
},
175 my $item_id2 = $sampleitem2[2];
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");
207 $query = " SELECT count(*) FROM issues";
208 my $sth = $dbh->prepare($query);
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() );
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 );
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);
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 );
241 $countaccount = $sth -> fetchrow_array
;
242 is
($countaccount,1,"1 accountline has been added");
246 AddRenewal
( $borrower_id1, $item_id1, $samplebranch1->{branchcode
},
247 $datedue1, $daysago10 );
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" );
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");
263 #FIXME: this routine currently doesn't work be
264 #is_deeply (GetItemIssues,{},"Without parameter, GetItemIssue returns all the issues");
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);
274 my $issue3 = C4
::Circulation
::AddIssue
( $borrower_1, 'barcode_1' );
275 #Without anything in DB
276 @renewcount = C4
::Circulation
::GetRenewCount
();
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);
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);
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
298 INSERT INTO issuingrules
( categorycode
, itemtype
, branchcode
, issuelength
, renewalsallowed
)
299 VALUES
( '*', '*', '*', 10, 0 )
301 @renewcount = C4
::Circulation
::GetRenewCount
();
305 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
307 @renewcount = C4
::Circulation
::GetRenewCount
(-1);
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);
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
322 UPDATE issuingrules SET renewalsallowed
= 3
324 @renewcount = C4
::Circulation
::GetRenewCount
();
328 "With issuing rules (renewal allowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
330 @renewcount = C4
::Circulation
::GetRenewCount
(-1);
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);
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);
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" );
364 ($biblionumber, $biblioitemnumber, $itemnumber) = C4
::Items
::AddItem
(
366 barcode
=> 'barcode_3',
367 itemcallnumber
=> 'callnumber3',
368 homebranch
=> $samplebranch1->{branchcode
},
369 holdingbranch
=> $samplebranch1->{branchcode
},
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"} );