5 use DateTime::Duration;
13 use Test::More tests => 24;
16 use_ok('C4::Circulation');
36 my $dbh = C4::Context->dbh;
37 $dbh->{RaiseError} = 1;
38 $dbh->{AutoCommit} = 0;
40 $dbh->do(q|DELETE FROM issues|);
41 $dbh->do(q|DELETE FROM items|);
42 $dbh->do(q|DELETE FROM borrowers|);
43 $dbh->do(q|DELETE FROM branches|);
44 $dbh->do(q|DELETE FROM categories|);
45 $dbh->do(q|DELETE FROM accountlines|);
46 $dbh->do(q|DELETE FROM issuingrules|);
52 my $dt_today = dt_from_string;
53 my $today = output_pref( $dt_today, 'iso', '24hr', 1 );
55 my $dt_today2 = dt_from_string;
56 my $dur10 = DateTime::Duration->new( days => -10 );
57 $dt_today2->add_duration($dur10);
58 my $daysago10 = output_pref( $dt_today2, 'iso', '24hr', 1 );
60 #Add branch and category
64 branchname => 'Sample Branch',
65 branchaddress1 => 'sample adr1',
66 branchaddress2 => 'sample adr2',
67 branchaddress3 => 'sample adr3',
68 branchzip => 'sample zip',
69 branchcity => 'sample city',
70 branchstate => 'sample state',
71 branchcountry => 'sample country',
72 branchphone => 'sample phone',
73 branchfax => 'sample fax',
74 branchemail => 'sample email',
75 branchurl => 'sample url',
76 branchip => 'sample ip',
77 branchprinter => undef,
78 opac_info => 'sample opac',
83 branchname => 'Sample Branch2',
84 branchaddress1 => 'sample adr1_2',
85 branchaddress2 => 'sample adr2_2',
86 branchaddress3 => 'sample adr3_2',
87 branchzip => 'sample zip2',
88 branchcity => 'sample city2',
89 branchstate => 'sample state2',
90 branchcountry => 'sample country2',
91 branchphone => 'sample phone2',
92 branchfax => 'sample fax2',
93 branchemail => 'sample email2',
94 branchurl => 'sample url2',
95 branchip => 'sample ip2',
96 branchprinter => undef,
97 opac_info => 'sample opac2',
99 ModBranch($samplebranch1);
100 ModBranch($samplebranch2);
103 categorycode => 'CAT1',
104 description => 'Description1',
105 enrolmentperiod => 'Null',
106 enrolmentperioddate => 'Null',
107 dateofbirthrequired => 'Null',
110 enrolmentfee => 'Null',
111 overduenoticerequired => 'Null',
112 issuelimit => 'Null',
113 reservefee => 'Null',
115 category_type => 'Null'
118 "INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,dateofbirthrequired ,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit ,reservefee ,hidelostitems ,category_type) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
121 $samplecat->{categorycode}, $samplecat->{description},
122 $samplecat->{enrolmentperiod}, $samplecat->{enrolmentperioddate},
123 $samplecat->{dateofbirthrequired}, $samplecat->{finetype},
124 $samplecat->{bulk}, $samplecat->{enrolmentfee},
125 $samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
126 $samplecat->{reservefee}, $samplecat->{hidelostitems},
127 $samplecat->{category_type}
131 my $record = MARC::Record->new();
132 $record->append_fields(
133 MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
134 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
136 my @sampleitem1 = C4::Items::AddItem(
138 barcode => 'barcode_1',
139 itemcallnumber => 'callnumber1',
140 homebranch => $samplebranch1->{branchcode},
141 holdingbranch => $samplebranch1->{branchcode},
147 my $item_id1 = $sampleitem1[2];
148 my @sampleitem2 = C4::Items::AddItem(
150 barcode => 'barcode_2',
151 itemcallnumber => 'callnumber2',
152 homebranch => $samplebranch2->{branchcode},
153 holdingbranch => $samplebranch2->{branchcode},
159 my $item_id2 = $sampleitem2[2];
162 my $borrower_id1 = C4::Members::AddMember(
163 firstname => 'firstname1',
164 surname => 'surname1 ',
165 categorycode => $samplecat->{categorycode},
166 branchcode => $samplebranch1->{branchcode},
168 my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
169 my $borrower_id2 = C4::Members::AddMember(
170 firstname => 'firstname2',
171 surname => 'surname2 ',
172 categorycode => $samplecat->{categorycode},
173 branchcode => $samplebranch2->{branchcode},
175 my $borrower_2 = C4::Members::GetMember(borrowernumber => $borrower_id2);
177 # NEED TO BE FIXED !!!
178 # The first parameter for set_userenv is the class ref
179 #my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'username', 'CPL', 'CPL', 'email@example.org' );
180 my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'CPL', 'CPL', 'email@example.org' );
182 C4::Context->_new_userenv('DUMMY_SESSION_ID');
183 C4::Context->set_userenv(@USERENV);
185 my $userenv = C4::Context->userenv
186 or BAIL_OUT("No userenv");
191 $query = " SELECT count(*) FROM issues";
192 my $sth = $dbh->prepare($query);
194 my $countissue = $sth -> fetchrow_array;
195 is ($countissue ,0, "there is no issue");
196 my $datedue1 = C4::Circulation::AddIssue( $borrower_1, "code", $daysago10,0, $today, '' );
199 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
200 "AddRenewal returns a date"
202 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef );
204 my $datedue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
205 is( $datedue2, undef, "AddIssue returns undef if no datedue is specified" );
206 my $issue_id2 = $dbh->last_insert_id( undef, undef, 'issues', undef );
209 $countissue = $sth -> fetchrow_array;
210 #FIXME: Currently AddIssue doesn't add correctly issues
211 #is ($countissue,2,"2 issues have been added");
213 #Test AddIssuingCharge
214 $query = " SELECT count(*) FROM accountlines";
215 $sth = $dbh->prepare($query);
217 my $countaccount = $sth -> fetchrow_array;
218 is ($countaccount,0,"0 accountline exists");
219 is( C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, 10 ),
220 1, "An issuing charge has been added" );
221 my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
223 $countaccount = $sth -> fetchrow_array;
224 is ($countaccount,1,"1 accountline has been added");
228 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
229 $datedue1, $daysago10 );
232 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
233 "AddRenewal returns a date"
236 #Test GetBiblioIssues
237 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
240 #FIXME : As the issues are not correctly added in the database, these tests don't work correctly
241 is(GetItemIssue,undef,"Without parameter GetItemIssue returns undef");
242 #is(GetItemIssue($item_id1),{},"Item1's issues");
245 #FIXME: this routine currently doesn't work be
246 #is_deeply (GetItemIssues,{},"Without parameter, GetItemIssue returns all the issues");
249 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
250 is( GetOpenIssue(-1), undef,
251 "With wrong parameter GetOpenIssue returns undef" );
252 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
256 $datedue2 = C4::Circulation::AddIssue( $borrower_1, 'barcode_1' );
257 isnt( $datedue2, undef, "AddIssue does not return undef if datedue is specified" );
258 #Without anything in DB
259 @renewcount = C4::Circulation::GetRenewCount();
262 [ 0, undef, 0 ], # FIXME Need to be fixed
263 "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
265 @renewcount = C4::Circulation::GetRenewCount(-1);
268 [ 0, undef, 0 ], # FIXME Need to be fixed
269 "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
271 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
274 [ 0, undef, 0 ], # FIXME Need to be fixed
275 "Without issuing rules and with a valid parameter, renewcount = 0, renewsallowed = undef, renewsleft = 0"
278 #With something in DB
279 # Add a default rule: No renewal allowed
281 INSERT INTO issuingrules( categorycode, itemtype, branchcode, issuelength, renewalsallowed )
282 VALUES ( '*', '*', '*', 10, 0 )
284 @renewcount = C4::Circulation::GetRenewCount();
288 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
290 @renewcount = C4::Circulation::GetRenewCount(-1);
294 "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
296 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
300 "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
303 # Add a default rule: renewal is allowed
305 UPDATE issuingrules SET renewalsallowed = 3
307 @renewcount = C4::Circulation::GetRenewCount();
311 "With issuing rules (renewal allowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
313 @renewcount = C4::Circulation::GetRenewCount(-1);
317 "With issuing rules (renewal allowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
319 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
323 "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
326 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
327 $datedue3, $daysago10 );
328 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
332 "With issuing rules (renewal allowed, 2 remaining) and with a valid parameter, Getrenewcount of item1 returns 2 renews left"