12 use Test::More tests => 14;
15 use_ok('C4::Circulation');
18 can_ok( 'C4::Circulation', qw(
21 GetBranchBorrowerCircRule
28 my $dbh = C4::Context->dbh;
29 $dbh->{RaiseError} = 1;
30 $dbh->{AutoCommit} = 0;
32 $dbh->do(q|DELETE FROM issues|);
33 $dbh->do(q|DELETE FROM items|);
34 $dbh->do(q|DELETE FROM borrowers|);
35 $dbh->do(q|DELETE FROM branches|);
36 $dbh->do(q|DELETE FROM categories|);
37 $dbh->do(q|DELETE FROM accountlines|);
38 $dbh->do(q|DELETE FROM itemtypes|);
39 $dbh->do(q|DELETE FROM branch_item_rules|);
40 $dbh->do(q|DELETE FROM branch_borrower_circ_rules|);
41 $dbh->do(q|DELETE FROM default_branch_circ_rules|);
42 $dbh->do(q|DELETE FROM default_circ_rules|);
43 $dbh->do(q|DELETE FROM default_branch_item_rules|);
45 #Add branch and category
48 branchname => 'Sample Branch',
49 branchaddress1 => 'sample adr1',
50 branchaddress2 => 'sample adr2',
51 branchaddress3 => 'sample adr3',
52 branchzip => 'sample zip',
53 branchcity => 'sample city',
54 branchstate => 'sample state',
55 branchcountry => 'sample country',
56 branchphone => 'sample phone',
57 branchfax => 'sample fax',
58 branchemail => 'sample email',
59 branchurl => 'sample url',
60 branchip => 'sample ip',
61 branchprinter => undef,
62 opac_info => 'sample opac',
66 branchname => 'Sample Branch2',
67 branchaddress1 => 'sample adr1_2',
68 branchaddress2 => 'sample adr2_2',
69 branchaddress3 => 'sample adr3_2',
70 branchzip => 'sample zip2',
71 branchcity => 'sample city2',
72 branchstate => 'sample state2',
73 branchcountry => 'sample country2',
74 branchphone => 'sample phone2',
75 branchfax => 'sample fax2',
76 branchemail => 'sample email2',
77 branchurl => 'sample url2',
78 branchip => 'sample ip2',
79 branchprinter => undef,
80 opac_info => 'sample opac2',
82 Koha::Library->new($samplebranch1)->store;
83 Koha::Library->new($samplebranch2)->store;
86 categorycode => 'CAT1',
87 description => 'Description1',
88 enrolmentperiod => 'Null',
89 enrolmentperioddate => 'Null',
90 dateofbirthrequired => 'Null',
93 enrolmentfee => 'Null',
94 overduenoticerequired => 'Null',
98 category_type => 'Null'
101 "INSERT INTO categories (categorycode,
105 dateofbirthrequired ,
109 overduenoticerequired,
115 VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
118 $samplecat->{categorycode}, $samplecat->{description},
119 $samplecat->{enrolmentperiod}, $samplecat->{enrolmentperioddate},
120 $samplecat->{dateofbirthrequired}, $samplecat->{finetype},
121 $samplecat->{bulk}, $samplecat->{enrolmentfee},
122 $samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
123 $samplecat->{reservefee}, $samplecat->{hidelostitems},
124 $samplecat->{category_type}
128 my $sampleitemtype1 = {
130 description => 'BookDescription',
131 rentalcharge => '10.0',
134 summary => 'BookSummary'
136 my $sampleitemtype2 = {
138 description => 'DvdDescription',
139 rentalcharge => '5.0',
142 summary => 'DvdSummary'
145 "INSERT INTO itemtypes (itemtype,
152 VALUES( ?,?,?,?,?,?)";
153 my $sth = $dbh->prepare($query);
155 $sampleitemtype1->{itemtype}, $sampleitemtype1->{description},
156 $sampleitemtype1->{rentalcharge}, $sampleitemtype1->{notforloan},
157 $sampleitemtype1->{imageurl}, $sampleitemtype1->{summary}
160 $sampleitemtype2->{itemtype}, $sampleitemtype2->{description},
161 $sampleitemtype2->{rentalcharge}, $sampleitemtype2->{notforloan},
162 $sampleitemtype2->{imageurl}, $sampleitemtype2->{summary}
166 my $record = MARC::Record->new();
167 $record->append_fields(
168 MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
169 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
171 # item 2 has home branch and holding branch samplebranch1
172 my @sampleitem1 = C4::Items::AddItem(
174 barcode => 'barcode_1',
175 itemcallnumber => 'callnumber1',
176 homebranch => $samplebranch1->{branchcode},
177 holdingbranch => $samplebranch1->{branchcode}
181 my $item_id1 = $sampleitem1[2];
183 # item 2 has holding branch samplebranch2
184 my @sampleitem2 = C4::Items::AddItem(
186 barcode => 'barcode_2',
187 itemcallnumber => 'callnumber2',
188 homebranch => $samplebranch2->{branchcode},
189 holdingbranch => $samplebranch1->{branchcode}
193 my $item_id2 = $sampleitem2[2];
195 # item 3 has item type sampleitemtype2 with noreturn policy
196 my @sampleitem3 = C4::Items::AddItem(
198 barcode => 'barcode_3',
199 itemcallnumber => 'callnumber3',
200 homebranch => $samplebranch2->{branchcode},
201 holdingbranch => $samplebranch2->{branchcode},
202 itype => $sampleitemtype2->{itemtype}
206 my $item_id3 = $sampleitem3[2];
209 my $borrower_id1 = C4::Members::AddMember(
210 firstname => 'firstname1',
211 surname => 'surname1 ',
212 categorycode => $samplecat->{categorycode},
213 branchcode => $samplebranch1->{branchcode},
215 my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
218 GetBranchBorrowerCircRule(),
219 { maxissueqty => undef, maxonsiteissueqty => undef },
220 "Without parameter, GetBranchBorrower returns undef (unilimited) for maxissueqty and maxonsiteissueqty if no rules defined"
224 INSERT INTO branch_borrower_circ_rules
225 (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
231 $samplebranch1->{branchcode},
232 $samplecat->{categorycode}, 5, 6
236 INSERT INTO default_branch_circ_rules
237 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
238 VALUES( ?, ?, ?, ?, ? )
240 $dbh->do( $query, {}, $samplebranch2->{branchcode},
241 3, 2, 1, 'holdingbranch' );
243 INSERT INTO default_circ_rules
244 (singleton, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
245 VALUES( ?, ?, ?, ?, ? )
247 $dbh->do( $query, {}, 'singleton', 4, 5, 3, 'homebranch' );
250 "INSERT INTO branch_item_rules (branchcode,itemtype,holdallowed,returnbranch) VALUES( ?,?,?,?)";
251 $sth = $dbh->prepare($query);
253 $samplebranch1->{branchcode},
254 $sampleitemtype1->{itemtype},
258 $samplebranch2->{branchcode},
259 $sampleitemtype1->{itemtype},
263 $samplebranch2->{branchcode},
264 $sampleitemtype2->{itemtype},
268 #Test GetBranchBorrowerCircRule
270 GetBranchBorrowerCircRule(),
271 { maxissueqty => 4, maxonsiteissueqty => 5 },
272 "Without parameter, GetBranchBorrower returns the maxissueqty and maxonsiteissueqty of default_circ_rules"
275 GetBranchBorrowerCircRule( $samplebranch2->{branchcode} ),
276 { maxissueqty => 3, maxonsiteissueqty => 2 },
277 "Without only the branchcode specified, GetBranchBorrower returns the maxissueqty and maxonsiteissueqty corresponding"
280 GetBranchBorrowerCircRule(
281 $samplebranch1->{branchcode},
282 $samplecat->{categorycode}
284 { maxissueqty => 5, maxonsiteissueqty => 6 },
285 "GetBranchBorrower returns the maxissueqty and maxonsiteissueqty of the branch1 and the category1"
288 GetBranchBorrowerCircRule( -1, -1 ),
289 { maxissueqty => 4, maxonsiteissueqty => 5 },
290 "GetBranchBorrower with wrong parameters returns the maxissueqty and maxonsiteissueqty of default_circ_rules"
293 #Test GetBranchItemRule
296 $samplebranch1->{branchcode},
297 $sampleitemtype1->{itemtype}
299 { returnbranch => 'homebranch', holdallowed => 5 },
300 "GetBranchitem returns holdallowed and return branch"
304 { returnbranch => 'homebranch', holdallowed => 3 },
305 "Without parameters GetBranchItemRule returns the values in default_circ_rules"
308 GetBranchItemRule( $samplebranch2->{branchcode} ),
309 { returnbranch => 'holdingbranch', holdallowed => 1 },
310 "With only a branchcode GetBranchItemRule returns values in default_branch_circ_rules"
313 GetBranchItemRule( -1, -1 ),
314 { returnbranch => 'homebranch', holdallowed => 3 },
315 "With only one parametern GetBranchItemRule returns default values"
318 # Test return policies
319 t::lib::Mocks::mock_preference('AutomaticItemReturn','0');
321 # item1 returned at branch2 should trigger transfer to homebranch
323 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
324 $dbh->do( $query, {}, $borrower_id1, $item_id1, $samplebranch1->{branchcode} );
326 my ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_1',
327 $samplebranch2->{branchcode});
328 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects default return policy - return to homebranch" );
330 # item2 returned at branch2 should trigger transfer to holding branch
332 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
333 $dbh->do( $query, {}, $borrower_id1, $item_id2, $samplebranch2->{branchcode} );
334 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_2',
335 $samplebranch2->{branchcode});
336 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects branch return policy - item2->homebranch policy = 'holdingbranch'" );
338 # item3 should not trigger transfer - floating collection
340 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
341 $dbh->do( $query, {}, $borrower_id1, $item_id3, $samplebranch1->{branchcode} );
342 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_3',
343 $samplebranch1->{branchcode});
344 is($messages->{NeedsTransfer},undef,"AddReturn respects branch item return policy - noreturn");