Bug 12787: Reorganise t/db_dependent files
[srvgit] / t / db_dependent / Circulation / Branch.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use t::lib::Mocks;
5 use C4::Biblio;
6 use C4::Members;
7 use C4::Circulation;
8 use C4::Items;
9 use C4::Context;
10 use Koha::Library;
11
12 use Test::More tests => 14;
13
14 BEGIN {
15     use_ok('C4::Circulation');
16 }
17
18 can_ok( 'C4::Circulation', qw(
19     AddIssue
20     AddReturn
21     GetBranchBorrowerCircRule
22     GetBranchItemRule
23     GetIssuingRule
24     )
25 );
26
27 #Start transaction
28 my $dbh = C4::Context->dbh;
29 $dbh->{RaiseError} = 1;
30 $dbh->{AutoCommit} = 0;
31
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|);
44
45 #Add branch and category
46 my $samplebranch1 = {
47     branchcode     => 'SAB1',
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',
63 };
64 my $samplebranch2 = {
65     branchcode     => 'SAB2',
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',
81 };
82 Koha::Library->new($samplebranch1)->store;
83 Koha::Library->new($samplebranch2)->store;
84
85 my $samplecat = {
86     categorycode          => 'CAT1',
87     description           => 'Description1',
88     enrolmentperiod       => 'Null',
89     enrolmentperioddate   => 'Null',
90     dateofbirthrequired   => 'Null',
91     finetype              => 'Null',
92     bulk                  => 'Null',
93     enrolmentfee          => 'Null',
94     overduenoticerequired => 'Null',
95     issuelimit            => 'Null',
96     reservefee            => 'Null',
97     hidelostitems         => 0,
98     category_type         => 'Null'
99 };
100 my $query =
101 "INSERT INTO categories (categorycode,
102                         description,
103                         enrolmentperiod,
104                         enrolmentperioddate,
105                         dateofbirthrequired ,
106                         finetype,
107                         bulk,
108                         enrolmentfee,
109                         overduenoticerequired,
110                         issuelimit,
111                         reservefee,
112                         hidelostitems,
113                         category_type
114                         )
115 VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
116 $dbh->do(
117     $query, {},
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}
125 );
126
127 #Add itemtypes
128 my $sampleitemtype1 = {
129     itemtype     => 'BOOK',
130     description  => 'BookDescription',
131     rentalcharge => '10.0',
132     notforloan   => 1,
133     imageurl     => 'Null',
134     summary      => 'BookSummary'
135 };
136 my $sampleitemtype2 = {
137     itemtype     => 'DVD',
138     description  => 'DvdDescription',
139     rentalcharge => '5.0',
140     notforloan   => 0,
141     imageurl     => 'Null',
142     summary      => 'DvdSummary'
143 };
144 $query =
145 "INSERT INTO itemtypes (itemtype,
146                     description,
147                     rentalcharge,
148                     notforloan,
149                     imageurl,
150                     summary
151                     )
152  VALUES( ?,?,?,?,?,?)";
153 my $sth = $dbh->prepare($query);
154 $sth->execute(
155     $sampleitemtype1->{itemtype},     $sampleitemtype1->{description},
156     $sampleitemtype1->{rentalcharge}, $sampleitemtype1->{notforloan},
157     $sampleitemtype1->{imageurl},     $sampleitemtype1->{summary}
158 );
159 $sth->execute(
160     $sampleitemtype2->{itemtype},     $sampleitemtype2->{description},
161     $sampleitemtype2->{rentalcharge}, $sampleitemtype2->{notforloan},
162     $sampleitemtype2->{imageurl},     $sampleitemtype2->{summary}
163 );
164
165 #Add biblio and item
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, '' );
170
171 # item 2 has home branch and holding branch samplebranch1
172 my @sampleitem1 = C4::Items::AddItem(
173     {
174         barcode        => 'barcode_1',
175         itemcallnumber => 'callnumber1',
176         homebranch     => $samplebranch1->{branchcode},
177         holdingbranch  => $samplebranch1->{branchcode}
178     },
179     $biblionumber
180 );
181 my $item_id1    = $sampleitem1[2];
182
183 # item 2 has holding branch samplebranch2
184 my @sampleitem2 = C4::Items::AddItem(
185     {
186         barcode        => 'barcode_2',
187         itemcallnumber => 'callnumber2',
188         homebranch     => $samplebranch2->{branchcode},
189         holdingbranch  => $samplebranch1->{branchcode}
190     },
191     $biblionumber
192 );
193 my $item_id2 = $sampleitem2[2];
194
195 # item 3 has item type sampleitemtype2 with noreturn policy
196 my @sampleitem3 = C4::Items::AddItem(
197     {
198         barcode        => 'barcode_3',
199         itemcallnumber => 'callnumber3',
200         homebranch     => $samplebranch2->{branchcode},
201         holdingbranch  => $samplebranch2->{branchcode},
202         itype          => $sampleitemtype2->{itemtype}
203     },
204     $biblionumber
205 );
206 my $item_id3 = $sampleitem3[2];
207
208 #Add borrower
209 my $borrower_id1 = C4::Members::AddMember(
210     firstname    => 'firstname1',
211     surname      => 'surname1 ',
212     categorycode => $samplecat->{categorycode},
213     branchcode   => $samplebranch1->{branchcode},
214 );
215 my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
216
217 is_deeply(
218     GetBranchBorrowerCircRule(),
219     { maxissueqty => undef, maxonsiteissueqty => undef },
220 "Without parameter, GetBranchBorrower returns undef (unilimited) for maxissueqty and maxonsiteissueqty if no rules defined"
221 );
222
223 $query = q|
224     INSERT INTO branch_borrower_circ_rules
225     (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
226     VALUES( ?, ?, ?, ? )
227 |;
228
229 $dbh->do(
230     $query, {},
231     $samplebranch1->{branchcode},
232     $samplecat->{categorycode}, 5, 6
233 );
234
235 $query = q|
236     INSERT INTO default_branch_circ_rules
237     (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
238     VALUES( ?, ?, ?, ?, ? )
239 |;
240 $dbh->do( $query, {}, $samplebranch2->{branchcode},
241     3, 2, 1, 'holdingbranch' );
242 $query = q|
243     INSERT INTO default_circ_rules
244     (singleton, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
245     VALUES( ?, ?, ?, ?, ? )
246 |;
247 $dbh->do( $query, {}, 'singleton', 4, 5, 3, 'homebranch' );
248
249 $query =
250 "INSERT INTO branch_item_rules (branchcode,itemtype,holdallowed,returnbranch) VALUES( ?,?,?,?)";
251 $sth = $dbh->prepare($query);
252 $sth->execute(
253     $samplebranch1->{branchcode},
254     $sampleitemtype1->{itemtype},
255     5, 'homebranch'
256 );
257 $sth->execute(
258     $samplebranch2->{branchcode},
259     $sampleitemtype1->{itemtype},
260     5, 'holdingbranch'
261 );
262 $sth->execute(
263     $samplebranch2->{branchcode},
264     $sampleitemtype2->{itemtype},
265     5, 'noreturn'
266 );
267
268 #Test GetBranchBorrowerCircRule
269 is_deeply(
270     GetBranchBorrowerCircRule(),
271     { maxissueqty => 4, maxonsiteissueqty => 5 },
272 "Without parameter, GetBranchBorrower returns the maxissueqty and maxonsiteissueqty of default_circ_rules"
273 );
274 is_deeply(
275     GetBranchBorrowerCircRule( $samplebranch2->{branchcode} ),
276     { maxissueqty => 3, maxonsiteissueqty => 2 },
277 "Without only the branchcode specified, GetBranchBorrower returns the maxissueqty and maxonsiteissueqty corresponding"
278 );
279 is_deeply(
280     GetBranchBorrowerCircRule(
281         $samplebranch1->{branchcode},
282         $samplecat->{categorycode}
283     ),
284     { maxissueqty => 5, maxonsiteissueqty => 6 },
285     "GetBranchBorrower returns the maxissueqty and maxonsiteissueqty of the branch1 and the category1"
286 );
287 is_deeply(
288     GetBranchBorrowerCircRule( -1, -1 ),
289     { maxissueqty => 4, maxonsiteissueqty => 5 },
290 "GetBranchBorrower with wrong parameters returns the maxissueqty and maxonsiteissueqty of default_circ_rules"
291 );
292
293 #Test GetBranchItemRule
294 is_deeply(
295     GetBranchItemRule(
296         $samplebranch1->{branchcode},
297         $sampleitemtype1->{itemtype}
298     ),
299     { returnbranch => 'homebranch', holdallowed => 5 },
300     "GetBranchitem returns holdallowed and return branch"
301 );
302 is_deeply(
303     GetBranchItemRule(),
304     { returnbranch => 'homebranch', holdallowed => 3 },
305 "Without parameters GetBranchItemRule returns the values in default_circ_rules"
306 );
307 is_deeply(
308     GetBranchItemRule( $samplebranch2->{branchcode} ),
309     { returnbranch => 'holdingbranch', holdallowed => 1 },
310 "With only a branchcode GetBranchItemRule returns values in default_branch_circ_rules"
311 );
312 is_deeply(
313     GetBranchItemRule( -1, -1 ),
314     { returnbranch => 'homebranch', holdallowed => 3 },
315     "With only one parametern GetBranchItemRule returns default values"
316 );
317
318 # Test return policies
319 t::lib::Mocks::mock_preference('AutomaticItemReturn','0');
320
321 # item1 returned at branch2 should trigger transfer to homebranch
322 $query =
323 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
324 $dbh->do( $query, {}, $borrower_id1, $item_id1, $samplebranch1->{branchcode} );
325
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" );
329
330 # item2 returned at branch2 should trigger transfer to holding branch
331 $query =
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'" );
337
338 # item3 should not trigger transfer - floating collection
339 $query =
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");
345
346
347 $dbh->rollback;