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