Bug 11349: Change .tmpl -> .tt in scripts using templates
[koha.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     finedays           => 0,
117     lengthunit         => 'Null',
118     renewalperiod      => 5,
119     norenewalbefore    => 6,
120     issuelength        => 5,
121     chargeperiod       => 0,
122     rentaldiscount     => '2.000000',
123     reservesallowed    => 0,
124     hardduedate        => '2013-01-01',
125     branchcode         => $samplebranch1->{branchcode},
126     fine               => '0.000000',
127     hardduedatecompare => 5,
128     overduefinescap    => '0.000000',
129     renewalsallowed    => 0,
130     firstremind        => 0,
131     itemtype           => 'BOOK',
132     categorycode       => $samplecat->{categorycode},
133     maxsuspensiondays  => 0,
134 };
135 my $sampleissuingrule2 = {
136     branchcode         => $samplebranch2->{branchcode},
137     categorycode       => $samplecat->{categorycode},
138     itemtype           => 'BOOK',
139     maxissueqty        => 2,
140     renewalsallowed    => 'Null',
141     renewalperiod      => 2,
142     norenewalbefore    => 7,
143     reservesallowed    => 'Null',
144     issuelength        => 2,
145     lengthunit         => 'Null',
146     hardduedate        => 2,
147     hardduedatecompare => 'Null',
148     fine               => 'Null',
149     finedays           => 'Null',
150     firstremind        => 'Null',
151     chargeperiod       => 'Null',
152     rentaldiscount     => 2.00,
153     overduefinescap    => 'Null',
154     accountsent        => 'Null',
155     reservecharge      => 'Null',
156     chargename         => 'Null',
157     restrictedtype     => 'Null',
158     maxsuspensiondays  => 0,
159 };
160 my $sampleissuingrule3 = {
161     branchcode         => $samplebranch1->{branchcode},
162     categorycode       => $samplecat->{categorycode},
163     itemtype           => 'DVD',
164     maxissueqty        => 3,
165     renewalsallowed    => 'Null',
166     renewalperiod      => 3,
167     norenewalbefore    => 8,
168     reservesallowed    => 'Null',
169     issuelength        => 3,
170     lengthunit         => 'Null',
171     hardduedate        => 3,
172     hardduedatecompare => 'Null',
173     fine               => 'Null',
174     finedays           => 'Null',
175     firstremind        => 'Null',
176     chargeperiod       => 'Null',
177     rentaldiscount     => 3.00,
178     overduefinescap    => 'Null',
179     accountsent        => 'Null',
180     reservecharge      => 'Null',
181     chargename         => 'Null',
182     restrictedtype     => 'Null',
183     maxsuspensiondays  => 0,
184 };
185 $query = 'INSERT INTO issuingrules (
186                 branchcode,
187                 categorycode,
188                 itemtype,
189                 maxissueqty,
190                 renewalsallowed,
191                 renewalperiod,
192                 norenewalbefore,
193                 reservesallowed,
194                 issuelength,
195                 lengthunit,
196                 hardduedate,
197                 hardduedatecompare,
198                 fine,
199                 finedays,
200                 firstremind,
201                 chargeperiod,
202                 rentaldiscount,
203                 overduefinescap,
204                 accountsent,
205                 reservecharge,
206                 chargename,
207                 restrictedtype,
208                 maxsuspensiondays
209                 ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
210 my $sth = $dbh->prepare($query);
211 $sth->execute(
212     $sampleissuingrule1->{branchcode},
213     $sampleissuingrule1->{categorycode},
214     $sampleissuingrule1->{itemtype},
215     $sampleissuingrule1->{maxissueqty},
216     $sampleissuingrule1->{renewalsallowed},
217     $sampleissuingrule1->{renewalperiod},
218     $sampleissuingrule1->{norenewalbefore},
219     $sampleissuingrule1->{reservesallowed},
220     $sampleissuingrule1->{issuelength},
221     $sampleissuingrule1->{lengthunit},
222     $sampleissuingrule1->{hardduedate},
223     $sampleissuingrule1->{hardduedatecompare},
224     $sampleissuingrule1->{fine},
225     $sampleissuingrule1->{finedays},
226     $sampleissuingrule1->{firstremind},
227     $sampleissuingrule1->{chargeperiod},
228     $sampleissuingrule1->{rentaldiscount},
229     $sampleissuingrule1->{overduefinescap},
230     $sampleissuingrule1->{accountsent},
231     $sampleissuingrule1->{reservecharge},
232     $sampleissuingrule1->{chargename},
233     $sampleissuingrule1->{restrictedtype},
234     $sampleissuingrule1->{maxsuspensiondays},
235 );
236 $sth->execute(
237     $sampleissuingrule2->{branchcode},
238     $sampleissuingrule2->{categorycode},
239     $sampleissuingrule2->{itemtype},
240     $sampleissuingrule2->{maxissueqty},
241     $sampleissuingrule2->{renewalsallowed},
242     $sampleissuingrule2->{renewalperiod},
243     $sampleissuingrule2->{norenewalbefore},
244     $sampleissuingrule2->{reservesallowed},
245     $sampleissuingrule2->{issuelength},
246     $sampleissuingrule2->{lengthunit},
247     $sampleissuingrule2->{hardduedate},
248     $sampleissuingrule2->{hardduedatecompare},
249     $sampleissuingrule2->{fine},
250     $sampleissuingrule2->{finedays},
251     $sampleissuingrule2->{firstremind},
252     $sampleissuingrule2->{chargeperiod},
253     $sampleissuingrule2->{rentaldiscount},
254     $sampleissuingrule2->{overduefinescap},
255     $sampleissuingrule2->{accountsent},
256     $sampleissuingrule2->{reservecharge},
257     $sampleissuingrule2->{chargename},
258     $sampleissuingrule2->{restrictedtype},
259     $sampleissuingrule2->{maxsuspensiondays},
260 );
261 $sth->execute(
262     $sampleissuingrule3->{branchcode},
263     $sampleissuingrule3->{categorycode},
264     $sampleissuingrule3->{itemtype},
265     $sampleissuingrule3->{maxissueqty},
266     $sampleissuingrule3->{renewalsallowed},
267     $sampleissuingrule3->{renewalperiod},
268     $sampleissuingrule3->{norenewalbefore},
269     $sampleissuingrule3->{reservesallowed},
270     $sampleissuingrule3->{issuelength},
271     $sampleissuingrule3->{lengthunit},
272     $sampleissuingrule3->{hardduedate},
273     $sampleissuingrule3->{hardduedatecompare},
274     $sampleissuingrule3->{fine},
275     $sampleissuingrule3->{finedays},
276     $sampleissuingrule3->{firstremind},
277     $sampleissuingrule3->{chargeperiod},
278     $sampleissuingrule3->{rentaldiscount},
279     $sampleissuingrule3->{overduefinescap},
280     $sampleissuingrule3->{accountsent},
281     $sampleissuingrule3->{reservecharge},
282     $sampleissuingrule3->{chargename},
283     $sampleissuingrule3->{restrictedtype},
284     $sampleissuingrule3->{maxsuspensiondays},
285 );
286
287 is_deeply(
288     GetIssuingRule(
289         $samplecat->{categorycode},
290         'Book', $samplebranch1->{branchcode}
291     ),
292     $sampleissuingrule1,
293     "GetIssuingCharge returns issuingrule1's informations"
294 );
295
296 #Test GetLoanLength
297 is_deeply(
298     C4::Circulation::GetLoanLength(
299         $samplecat->{categorycode},
300         'BOOK', $samplebranch1->{branchcode}
301     ),
302     { issuelength => 5, lengthunit => 'Null', renewalperiod => 5 },
303     "GetLoanLength"
304 );
305 is_deeply(
306     C4::Circulation::GetLoanLength(),
307     {
308         issuelength   => 21,
309         renewalperiod => 21,
310         lengthunit    => 'days',
311     },
312     "Without parameters, GetLoanLength returns hardcoded values"
313 );
314 is_deeply(
315     C4::Circulation::GetLoanLength( -1, -1 ),
316     {
317         issuelength   => 21,
318         renewalperiod => 21,
319         lengthunit    => 'days',
320     },
321     "With wrong parameters, GetLoanLength returns hardcoded values"
322 );
323 is_deeply(
324     C4::Circulation::GetLoanLength( $samplecat->{categorycode} ),
325     {
326         issuelength   => 21,
327         renewalperiod => 21,
328         lengthunit    => 'days',
329     },
330     "With only one parameter, GetLoanLength returns hardcoded values"
331 );    #NOTE : is that really what is expected?
332 is_deeply(
333     C4::Circulation::GetLoanLength( $samplecat->{categorycode}, 'BOOK' ),
334     {
335         issuelength   => 21,
336         renewalperiod => 21,
337         lengthunit    => 'days',
338     },
339     "With only one parameter, GetLoanLength returns hardcoded values"
340 );    #NOTE : is that really what is expected?
341
342 #Test GetHardDueDate
343 my @hardduedate = C4::Circulation::GetHardDueDate( $samplecat->{categorycode},
344     'BOOK', $samplebranch1->{branchcode} );
345 is_deeply(
346     \@hardduedate,
347     [
348         dt_from_string( $sampleissuingrule1->{hardduedate}, 'iso' ),
349         $sampleissuingrule1->{hardduedatecompare}
350     ],
351     "GetHardDueDate returns the duedate and the duedatecompare"
352 );
353
354 #End transaction
355 $dbh->rollback;