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