11 use Test::More tests => 14;
14 use_ok('C4::Circulation');
17 can_ok( 'C4::Circulation', qw(
20 GetBranchBorrowerCircRule
27 my $dbh = C4::Context->dbh;
28 $dbh->{RaiseError} = 1;
29 $dbh->{AutoCommit} = 0;
31 $dbh->do(q|DELETE FROM issues|);
32 $dbh->do(q|DELETE FROM items|);
33 $dbh->do(q|DELETE FROM borrowers|);
34 $dbh->do(q|DELETE FROM branches|);
35 $dbh->do(q|DELETE FROM categories|);
36 $dbh->do(q|DELETE FROM accountlines|);
37 $dbh->do(q|DELETE FROM itemtypes|);
38 $dbh->do(q|DELETE FROM branch_item_rules|);
39 $dbh->do(q|DELETE FROM branch_borrower_circ_rules|);
40 $dbh->do(q|DELETE FROM default_branch_circ_rules|);
41 $dbh->do(q|DELETE FROM default_circ_rules|);
42 $dbh->do(q|DELETE FROM default_branch_item_rules|);
44 #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',
67 branchname => 'Sample Branch2',
68 branchaddress1 => 'sample adr1_2',
69 branchaddress2 => 'sample adr2_2',
70 branchaddress3 => 'sample adr3_2',
71 branchzip => 'sample zip2',
72 branchcity => 'sample city2',
73 branchstate => 'sample state2',
74 branchcountry => 'sample country2',
75 branchphone => 'sample phone2',
76 branchfax => 'sample fax2',
77 branchemail => 'sample email2',
78 branchurl => 'sample url2',
79 branchip => 'sample ip2',
80 branchprinter => undef,
81 opac_info => 'sample opac2',
83 ModBranch($samplebranch1);
84 ModBranch($samplebranch2);
87 categorycode => 'CAT1',
88 description => 'Description1',
89 enrolmentperiod => 'Null',
90 enrolmentperioddate => 'Null',
91 dateofbirthrequired => 'Null',
94 enrolmentfee => 'Null',
95 overduenoticerequired => 'Null',
99 category_type => 'Null'
102 "INSERT INTO categories (categorycode,
106 dateofbirthrequired ,
110 overduenoticerequired,
116 VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
119 $samplecat->{categorycode}, $samplecat->{description},
120 $samplecat->{enrolmentperiod}, $samplecat->{enrolmentperioddate},
121 $samplecat->{dateofbirthrequired}, $samplecat->{finetype},
122 $samplecat->{bulk}, $samplecat->{enrolmentfee},
123 $samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
124 $samplecat->{reservefee}, $samplecat->{hidelostitems},
125 $samplecat->{category_type}
129 my $sampleitemtype1 = {
131 description => 'BookDescription',
132 rentalcharge => '10.0',
135 summary => 'BookSummary'
137 my $sampleitemtype2 = {
139 description => 'DvdDescription',
140 rentalcharge => '5.0',
143 summary => 'DvdSummary'
146 "INSERT INTO itemtypes (itemtype,
153 VALUES( ?,?,?,?,?,?)";
154 my $sth = $dbh->prepare($query);
156 $sampleitemtype1->{itemtype}, $sampleitemtype1->{description},
157 $sampleitemtype1->{rentalcharge}, $sampleitemtype1->{notforloan},
158 $sampleitemtype1->{imageurl}, $sampleitemtype1->{summary}
161 $sampleitemtype2->{itemtype}, $sampleitemtype2->{description},
162 $sampleitemtype2->{rentalcharge}, $sampleitemtype2->{notforloan},
163 $sampleitemtype2->{imageurl}, $sampleitemtype2->{summary}
167 my $record = MARC::Record->new();
168 $record->append_fields(
169 MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
170 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
172 # item 2 has home branch and holding branch samplebranch1
173 my @sampleitem1 = C4::Items::AddItem(
175 barcode => 'barcode_1',
176 itemcallnumber => 'callnumber1',
177 homebranch => $samplebranch1->{branchcode},
178 holdingbranch => $samplebranch1->{branchcode}
182 my $item_id1 = $sampleitem1[2];
184 # item 2 has holding branch samplebranch2
185 my @sampleitem2 = C4::Items::AddItem(
187 barcode => 'barcode_2',
188 itemcallnumber => 'callnumber2',
189 homebranch => $samplebranch2->{branchcode},
190 holdingbranch => $samplebranch1->{branchcode}
194 my $item_id2 = $sampleitem2[2];
196 # item 3 has item type sampleitemtype2 with noreturn policy
197 my @sampleitem3 = C4::Items::AddItem(
199 barcode => 'barcode_3',
200 itemcallnumber => 'callnumber3',
201 homebranch => $samplebranch2->{branchcode},
202 holdingbranch => $samplebranch2->{branchcode},
203 itype => $sampleitemtype2->{itemtype}
207 my $item_id3 = $sampleitem3[2];
210 my $borrower_id1 = C4::Members::AddMember(
211 firstname => 'firstname1',
212 surname => 'surname1 ',
213 categorycode => $samplecat->{categorycode},
214 branchcode => $samplebranch1->{branchcode},
216 my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
219 GetBranchBorrowerCircRule(),
220 { maxissueqty => undef, maxonsiteissueqty => undef },
221 "Without parameter, GetBranchBorrower returns undef (unilimited) for maxissueqty and maxonsiteissueqty if no rules defined"
225 INSERT INTO branch_borrower_circ_rules
226 (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
232 $samplebranch1->{branchcode},
233 $samplecat->{categorycode}, 5, 6
237 INSERT INTO default_branch_circ_rules
238 (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
239 VALUES( ?, ?, ?, ?, ? )
241 $dbh->do( $query, {}, $samplebranch2->{branchcode},
242 3, 2, 1, 'holdingbranch' );
244 INSERT INTO default_circ_rules
245 (singleton, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
246 VALUES( ?, ?, ?, ?, ? )
248 $dbh->do( $query, {}, 'singleton', 4, 5, 3, 'homebranch' );
251 "INSERT INTO branch_item_rules (branchcode,itemtype,holdallowed,returnbranch) VALUES( ?,?,?,?)";
252 $sth = $dbh->prepare($query);
254 $samplebranch1->{branchcode},
255 $sampleitemtype1->{itemtype},
259 $samplebranch2->{branchcode},
260 $sampleitemtype1->{itemtype},
264 $samplebranch2->{branchcode},
265 $sampleitemtype2->{itemtype},
269 #Test GetBranchBorrowerCircRule
271 GetBranchBorrowerCircRule(),
272 { maxissueqty => 4, maxonsiteissueqty => 5 },
273 "Without parameter, GetBranchBorrower returns the maxissueqty and maxonsiteissueqty of default_circ_rules"
276 GetBranchBorrowerCircRule( $samplebranch2->{branchcode} ),
277 { maxissueqty => 3, maxonsiteissueqty => 2 },
278 "Without only the branchcode specified, GetBranchBorrower returns the maxissueqty and maxonsiteissueqty corresponding"
281 GetBranchBorrowerCircRule(
282 $samplebranch1->{branchcode},
283 $samplecat->{categorycode}
285 { maxissueqty => 5, maxonsiteissueqty => 6 },
286 "GetBranchBorrower returns the maxissueqty and maxonsiteissueqty of the branch1 and the category1"
289 GetBranchBorrowerCircRule( -1, -1 ),
290 { maxissueqty => 4, maxonsiteissueqty => 5 },
291 "GetBranchBorrower with wrong parameters returns the maxissueqty and maxonsiteissueqty of default_circ_rules"
294 #Test GetBranchItemRule
297 $samplebranch1->{branchcode},
298 $sampleitemtype1->{itemtype}
300 { returnbranch => 'homebranch', holdallowed => 5 },
301 "GetBranchitem returns holdallowed and return branch"
305 { returnbranch => 'homebranch', holdallowed => 3 },
306 "Without parameters GetBranchItemRule returns the values in default_circ_rules"
309 GetBranchItemRule( $samplebranch2->{branchcode} ),
310 { returnbranch => 'holdingbranch', holdallowed => 1 },
311 "With only a branchcode GetBranchItemRule returns values in default_branch_circ_rules"
314 GetBranchItemRule( -1, -1 ),
315 { returnbranch => 'homebranch', holdallowed => 3 },
316 "With only one parametern GetBranchItemRule returns default values"
319 # Test return policies
320 C4::Context->set_preference('AutomaticItemReturn','0');
322 # item1 returned at branch2 should trigger transfer to homebranch
324 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
325 $dbh->do( $query, {}, $borrower_id1, $item_id1, $samplebranch1->{branchcode} );
327 my ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_1',
328 $samplebranch2->{branchcode});
329 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects default return policy - return to homebranch" );
331 # item2 returned at branch2 should trigger transfer to holding branch
333 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
334 $dbh->do( $query, {}, $borrower_id1, $item_id2, $samplebranch2->{branchcode} );
335 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_2',
336 $samplebranch2->{branchcode});
337 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects branch return policy - item2->homebranch policy = 'holdingbranch'" );
339 # item3 should not trigger transfer - floating collection
341 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
342 $dbh->do( $query, {}, $borrower_id1, $item_id3, $samplebranch1->{branchcode} );
343 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_3',
344 $samplebranch1->{branchcode});
345 is($messages->{NeedsTransfer},undef,"AddReturn respects branch item return policy - noreturn");