Bug 14045: Make GetBranchBorrowerCircRule return maxonsiteissueqty
[koha-ffzg.git] / t / db_dependent / Circulation_Issuingrule.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use C4::Context;
5 use C4::Branch;
6 use DateTime;
7 use Koha::DateUtils;
8
9 use Test::More tests => 9;
10
11 BEGIN {
12     use_ok('C4::Circulation');
13 }
14 can_ok(
15     'C4::Circulation',
16     qw(
17       GetHardDueDate
18       GetIssuingRule
19       GetLoanLength
20       )
21 );
22
23 #Start transaction
24 my $dbh = C4::Context->dbh;
25 $dbh->{RaiseError} = 1;
26 $dbh->{AutoCommit} = 0;
27
28 $dbh->do(q|DELETE FROM issues|);
29 $dbh->do(q|DELETE FROM items|);
30 $dbh->do(q|DELETE FROM borrowers|);
31 $dbh->do(q|DELETE FROM branches|);
32 $dbh->do(q|DELETE FROM categories|);
33 $dbh->do(q|DELETE FROM issuingrules|);
34
35 #Add sample datas
36
37 #Add branch and category
38 my $samplebranch1 = {
39     add            => 1,
40     branchcode     => 'SAB1',
41     branchname     => 'Sample Branch',
42     branchaddress1 => 'sample adr1',
43     branchaddress2 => 'sample adr2',
44     branchaddress3 => 'sample adr3',
45     branchzip      => 'sample zip',
46     branchcity     => 'sample city',
47     branchstate    => 'sample state',
48     branchcountry  => 'sample country',
49     branchphone    => 'sample phone',
50     branchfax      => 'sample fax',
51     branchemail    => 'sample email',
52     branchurl      => 'sample url',
53     branchip       => 'sample ip',
54     branchprinter  => undef,
55     opac_info      => 'sample opac',
56 };
57 my $samplebranch2 = {
58     add            => 1,
59     branchcode     => 'SAB2',
60     branchname     => 'Sample Branch2',
61     branchaddress1 => 'sample adr1_2',
62     branchaddress2 => 'sample adr2_2',
63     branchaddress3 => 'sample adr3_2',
64     branchzip      => 'sample zip2',
65     branchcity     => 'sample city2',
66     branchstate    => 'sample state2',
67     branchcountry  => 'sample country2',
68     branchphone    => 'sample phone2',
69     branchfax      => 'sample fax2',
70     branchemail    => 'sample email2',
71     branchurl      => 'sample url2',
72     branchip       => 'sample ip2',
73     branchprinter  => undef,
74     opac_info      => 'sample opac2',
75 };
76 ModBranch($samplebranch1);
77 ModBranch($samplebranch2);
78
79 my $samplecat = {
80     categorycode          => 'CAT1',
81     description           => 'Description1',
82     enrolmentperiod       => 'Null',
83     enrolmentperioddate   => 'Null',
84     dateofbirthrequired   => 'Null',
85     finetype              => 'Null',
86     bulk                  => 'Null',
87     enrolmentfee          => 'Null',
88     overduenoticerequired => 'Null',
89     issuelimit            => 'Null',
90     reservefee            => 'Null',
91     hidelostitems         => 0,
92     category_type         => 'Null'
93 };
94 my $query =
95 "INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,dateofbirthrequired ,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit ,reservefee ,hidelostitems ,category_type) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
96 $dbh->do(
97     $query, {},
98     $samplecat->{categorycode},          $samplecat->{description},
99     $samplecat->{enrolmentperiod},       $samplecat->{enrolmentperioddate},
100     $samplecat->{dateofbirthrequired},   $samplecat->{finetype},
101     $samplecat->{bulk},                  $samplecat->{enrolmentfee},
102     $samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
103     $samplecat->{reservefee},            $samplecat->{hidelostitems},
104     $samplecat->{category_type}
105 );
106
107 #Begin Tests
108
109 #Test GetIssuingRule
110 my $sampleissuingrule1 = {
111     reservecharge      => '0.000000',
112     chargename         => 'Null',
113     restrictedtype     => 0,
114     accountsent        => 0,
115     maxissueqty        => 5,
116     maxonsiteissueqty  => 4,
117     finedays           => 0,
118     lengthunit         => 'Null',
119     renewalperiod      => 5,
120     norenewalbefore    => 6,
121     auto_renew         => 0,
122     issuelength        => 5,
123     chargeperiod       => 0,
124     rentaldiscount     => '2.000000',
125     reservesallowed    => 0,
126     hardduedate        => '2013-01-01',
127     branchcode         => $samplebranch1->{branchcode},
128     fine               => '0.000000',
129     hardduedatecompare => 5,
130     overduefinescap    => '0.000000',
131     renewalsallowed    => 0,
132     firstremind        => 0,
133     itemtype           => 'BOOK',
134     categorycode       => $samplecat->{categorycode},
135     maxsuspensiondays  => 0,
136     onshelfholds       => 0,
137     opacitemholds      => 'N',
138 };
139 my $sampleissuingrule2 = {
140     branchcode         => $samplebranch2->{branchcode},
141     categorycode       => $samplecat->{categorycode},
142     itemtype           => 'BOOK',
143     maxissueqty        => 2,
144     maxonsiteissueqty  => 1,
145     renewalsallowed    => 'Null',
146     renewalperiod      => 2,
147     norenewalbefore    => 7,
148     auto_renew         => 0,
149     reservesallowed    => 'Null',
150     issuelength        => 2,
151     lengthunit         => 'Null',
152     hardduedate        => 2,
153     hardduedatecompare => 'Null',
154     fine               => 'Null',
155     finedays           => 'Null',
156     firstremind        => 'Null',
157     chargeperiod       => 'Null',
158     rentaldiscount     => 2.00,
159     overduefinescap    => 'Null',
160     accountsent        => 'Null',
161     reservecharge      => 'Null',
162     chargename         => 'Null',
163     restrictedtype     => 'Null',
164     maxsuspensiondays  => 0,
165     onshelfholds       => 1,
166     opacitemholds      => 'Y',
167 };
168 my $sampleissuingrule3 = {
169     branchcode         => $samplebranch1->{branchcode},
170     categorycode       => $samplecat->{categorycode},
171     itemtype           => 'DVD',
172     maxissueqty        => 3,
173     maxonsiteissueqty  => 2,
174     renewalsallowed    => 'Null',
175     renewalperiod      => 3,
176     norenewalbefore    => 8,
177     auto_renew         => 0,
178     reservesallowed    => 'Null',
179     issuelength        => 3,
180     lengthunit         => 'Null',
181     hardduedate        => 3,
182     hardduedatecompare => 'Null',
183     fine               => 'Null',
184     finedays           => 'Null',
185     firstremind        => 'Null',
186     chargeperiod       => 'Null',
187     rentaldiscount     => 3.00,
188     overduefinescap    => 'Null',
189     accountsent        => 'Null',
190     reservecharge      => 'Null',
191     chargename         => 'Null',
192     restrictedtype     => 'Null',
193     maxsuspensiondays  => 0,
194     onshelfholds       => 1,
195     opacitemholds      => 'F',
196 };
197 $query = 'INSERT INTO issuingrules (
198                 branchcode,
199                 categorycode,
200                 itemtype,
201                 maxissueqty,
202                 maxonsiteissueqty,
203                 renewalsallowed,
204                 renewalperiod,
205                 norenewalbefore,
206                 auto_renew,
207                 reservesallowed,
208                 issuelength,
209                 lengthunit,
210                 hardduedate,
211                 hardduedatecompare,
212                 fine,
213                 finedays,
214                 firstremind,
215                 chargeperiod,
216                 rentaldiscount,
217                 overduefinescap,
218                 accountsent,
219                 reservecharge,
220                 chargename,
221                 restrictedtype,
222                 maxsuspensiondays
223                 ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
224 my $sth = $dbh->prepare($query);
225 $sth->execute(
226     $sampleissuingrule1->{branchcode},
227     $sampleissuingrule1->{categorycode},
228     $sampleissuingrule1->{itemtype},
229     $sampleissuingrule1->{maxissueqty},
230     $sampleissuingrule1->{maxonsiteissueqty},
231     $sampleissuingrule1->{renewalsallowed},
232     $sampleissuingrule1->{renewalperiod},
233     $sampleissuingrule1->{norenewalbefore},
234     $sampleissuingrule1->{auto_renew},
235     $sampleissuingrule1->{reservesallowed},
236     $sampleissuingrule1->{issuelength},
237     $sampleissuingrule1->{lengthunit},
238     $sampleissuingrule1->{hardduedate},
239     $sampleissuingrule1->{hardduedatecompare},
240     $sampleissuingrule1->{fine},
241     $sampleissuingrule1->{finedays},
242     $sampleissuingrule1->{firstremind},
243     $sampleissuingrule1->{chargeperiod},
244     $sampleissuingrule1->{rentaldiscount},
245     $sampleissuingrule1->{overduefinescap},
246     $sampleissuingrule1->{accountsent},
247     $sampleissuingrule1->{reservecharge},
248     $sampleissuingrule1->{chargename},
249     $sampleissuingrule1->{restrictedtype},
250     $sampleissuingrule1->{maxsuspensiondays},
251 );
252 $sth->execute(
253     $sampleissuingrule2->{branchcode},
254     $sampleissuingrule2->{categorycode},
255     $sampleissuingrule2->{itemtype},
256     $sampleissuingrule2->{maxissueqty},
257     $sampleissuingrule2->{maxonsiteissueqty},
258     $sampleissuingrule2->{renewalsallowed},
259     $sampleissuingrule2->{renewalperiod},
260     $sampleissuingrule2->{norenewalbefore},
261     $sampleissuingrule2->{auto_renew},
262     $sampleissuingrule2->{reservesallowed},
263     $sampleissuingrule2->{issuelength},
264     $sampleissuingrule2->{lengthunit},
265     $sampleissuingrule2->{hardduedate},
266     $sampleissuingrule2->{hardduedatecompare},
267     $sampleissuingrule2->{fine},
268     $sampleissuingrule2->{finedays},
269     $sampleissuingrule2->{firstremind},
270     $sampleissuingrule2->{chargeperiod},
271     $sampleissuingrule2->{rentaldiscount},
272     $sampleissuingrule2->{overduefinescap},
273     $sampleissuingrule2->{accountsent},
274     $sampleissuingrule2->{reservecharge},
275     $sampleissuingrule2->{chargename},
276     $sampleissuingrule2->{restrictedtype},
277     $sampleissuingrule2->{maxsuspensiondays},
278 );
279 $sth->execute(
280     $sampleissuingrule3->{branchcode},
281     $sampleissuingrule3->{categorycode},
282     $sampleissuingrule3->{itemtype},
283     $sampleissuingrule3->{maxissueqty},
284     $sampleissuingrule3->{maxonsiteissueqty},
285     $sampleissuingrule3->{renewalsallowed},
286     $sampleissuingrule3->{renewalperiod},
287     $sampleissuingrule3->{norenewalbefore},
288     $sampleissuingrule3->{auto_renew},
289     $sampleissuingrule3->{reservesallowed},
290     $sampleissuingrule3->{issuelength},
291     $sampleissuingrule3->{lengthunit},
292     $sampleissuingrule3->{hardduedate},
293     $sampleissuingrule3->{hardduedatecompare},
294     $sampleissuingrule3->{fine},
295     $sampleissuingrule3->{finedays},
296     $sampleissuingrule3->{firstremind},
297     $sampleissuingrule3->{chargeperiod},
298     $sampleissuingrule3->{rentaldiscount},
299     $sampleissuingrule3->{overduefinescap},
300     $sampleissuingrule3->{accountsent},
301     $sampleissuingrule3->{reservecharge},
302     $sampleissuingrule3->{chargename},
303     $sampleissuingrule3->{restrictedtype},
304     $sampleissuingrule3->{maxsuspensiondays},
305 );
306
307 is_deeply(
308     GetIssuingRule(
309         $samplecat->{categorycode},
310         'Book', $samplebranch1->{branchcode}
311     ),
312     $sampleissuingrule1,
313     "GetIssuingCharge returns issuingrule1's informations"
314 );
315
316 #Test GetLoanLength
317 is_deeply(
318     C4::Circulation::GetLoanLength(
319         $samplecat->{categorycode},
320         'BOOK', $samplebranch1->{branchcode}
321     ),
322     { issuelength => 5, lengthunit => 'Null', renewalperiod => 5 },
323     "GetLoanLength"
324 );
325 is_deeply(
326     C4::Circulation::GetLoanLength(),
327     {
328         issuelength   => 21,
329         renewalperiod => 21,
330         lengthunit    => 'days',
331     },
332     "Without parameters, GetLoanLength returns hardcoded values"
333 );
334 is_deeply(
335     C4::Circulation::GetLoanLength( -1, -1 ),
336     {
337         issuelength   => 21,
338         renewalperiod => 21,
339         lengthunit    => 'days',
340     },
341     "With wrong parameters, GetLoanLength returns hardcoded values"
342 );
343 is_deeply(
344     C4::Circulation::GetLoanLength( $samplecat->{categorycode} ),
345     {
346         issuelength   => 21,
347         renewalperiod => 21,
348         lengthunit    => 'days',
349     },
350     "With only one parameter, GetLoanLength returns hardcoded values"
351 );    #NOTE : is that really what is expected?
352 is_deeply(
353     C4::Circulation::GetLoanLength( $samplecat->{categorycode}, 'BOOK' ),
354     {
355         issuelength   => 21,
356         renewalperiod => 21,
357         lengthunit    => 'days',
358     },
359     "With only one parameter, GetLoanLength returns hardcoded values"
360 );    #NOTE : is that really what is expected?
361
362 #Test GetHardDueDate
363 my @hardduedate = C4::Circulation::GetHardDueDate( $samplecat->{categorycode},
364     'BOOK', $samplebranch1->{branchcode} );
365 is_deeply(
366     \@hardduedate,
367     [
368         dt_from_string( $sampleissuingrule1->{hardduedate}, 'iso' ),
369         $sampleissuingrule1->{hardduedatecompare}
370     ],
371     "GetHardDueDate returns the duedate and the duedatecompare"
372 );
373
374 #End transaction
375 $dbh->rollback;