Bug 18936: Convert issuingrules fields to circulation_rules
[koha-equinox.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::CirculationRules;
9 use Koha::Library;
10
11 use Test::More tests => 8;
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 circulation_rules|);
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 get_effective_rules
115 my $sampleissuingrule1 = {
116     branchcode   => $samplebranch1->{branchcode},
117     categorycode => $samplecat->{categorycode},
118     itemtype     => 'BOOK',
119     rules        => {
120         reservecharge                    => 0,
121         restrictedtype                   => 0,
122         accountsent                      => 0,
123         finedays                         => 0,
124         lengthunit                       => 'days',
125         renewalperiod                    => 5,
126         norenewalbefore                  => 6,
127         auto_renew                       => 0,
128         issuelength                      => 5,
129         chargeperiod                     => 0,
130         chargeperiod_charge_at           => 0,
131         rentaldiscount                   => 2,
132         reservesallowed                  => 0,
133         hardduedate                      => '2013-01-01',
134         fine                             => 0,
135         hardduedatecompare               => 5,
136         overduefinescap                  => 0,
137         renewalsallowed                  => 0,
138         firstremind                      => 0,
139         maxsuspensiondays                => 0,
140         onshelfholds                     => 0,
141         opacitemholds                    => 'N',
142         cap_fine_to_replacement_price    => 0,
143         holds_per_record                 => 1,
144         article_requests                 => 'yes',
145         no_auto_renewal_after            => undef,
146         no_auto_renewal_after_hard_limit => undef,
147         suspension_chargeperiod          => 1,
148         holds_per_day                    => undef,
149     }
150 };
151 my $sampleissuingrule2 = {
152     branchcode   => $samplebranch2->{branchcode},
153     categorycode => $samplecat->{categorycode},
154     itemtype     => 'BOOK',
155     rules        => {
156         renewalsallowed               => 0,
157         renewalperiod                 => 2,
158         norenewalbefore               => 7,
159         auto_renew                    => 0,
160         reservesallowed               => 0,
161         issuelength                   => 2,
162         lengthunit                    => 'days',
163         hardduedate                   => 2,
164         hardduedatecompare            => undef,
165         fine                          => undef,
166         finedays                      => undef,
167         firstremind                   => undef,
168         chargeperiod                  => undef,
169         chargeperiod_charge_at        => 0,
170         rentaldiscount                => 2.00,
171         overduefinescap               => undef,
172         accountsent                   => undef,
173         reservecharge                 => undef,
174         restrictedtype                => undef,
175         maxsuspensiondays             => 0,
176         onshelfholds                  => 1,
177         opacitemholds                 => 'Y',
178         cap_fine_to_replacement_price => 0,
179         holds_per_record              => 1,
180         article_requests              => 'yes',
181     }
182 };
183 my $sampleissuingrule3 = {
184     branchcode   => $samplebranch1->{branchcode},
185     categorycode => $samplecat->{categorycode},
186     itemtype     => 'DVD',
187     rules        => {
188         renewalsallowed               => 0,
189         renewalperiod                 => 3,
190         norenewalbefore               => 8,
191         auto_renew                    => 0,
192         reservesallowed               => 0,
193         issuelength                   => 3,
194         lengthunit                    => 'days',
195         hardduedate                   => 3,
196         hardduedatecompare            => undef,
197         fine                          => undef,
198         finedays                      => undef,
199         firstremind                   => undef,
200         chargeperiod                  => undef,
201         chargeperiod_charge_at        => 0,
202         rentaldiscount                => 3.00,
203         overduefinescap               => undef,
204         accountsent                   => undef,
205         reservecharge                 => undef,
206         restrictedtype                => undef,
207         maxsuspensiondays             => 0,
208         onshelfholds                  => 1,
209         opacitemholds                 => 'F',
210         cap_fine_to_replacement_price => 0,
211         holds_per_record              => 1,
212         article_requests              => 'yes',
213     }
214 };
215
216 Koha::CirculationRules->set_rules( $sampleissuingrule1 );
217 Koha::CirculationRules->set_rules( $sampleissuingrule2 );
218 Koha::CirculationRules->set_rules( $sampleissuingrule3 );
219
220 #Test GetLoanLength
221 is_deeply(
222     C4::Circulation::GetLoanLength(
223         $samplecat->{categorycode},
224         'BOOK', $samplebranch1->{branchcode}
225     ),
226     { issuelength => 5, lengthunit => 'days', renewalperiod => 5 },
227     "GetLoanLength"
228 );
229
230 is_deeply(
231     C4::Circulation::GetLoanLength(),
232     $default,
233     "Without parameters, GetLoanLength returns hardcoded values"
234 );
235 is_deeply(
236     C4::Circulation::GetLoanLength( -1, -1 ),
237     $default,
238     "With wrong parameters, GetLoanLength returns hardcoded values"
239 );
240 is_deeply(
241     C4::Circulation::GetLoanLength( $samplecat->{categorycode} ),
242     $default,
243     "With only one parameter, GetLoanLength returns hardcoded values"
244 );    #NOTE : is that really what is expected?
245 is_deeply(
246     C4::Circulation::GetLoanLength( $samplecat->{categorycode}, 'BOOK' ),
247     $default,
248     "With only two parameters, GetLoanLength returns hardcoded values"
249 );    #NOTE : is that really what is expected?
250 is_deeply(
251     C4::Circulation::GetLoanLength( $samplecat->{categorycode}, 'BOOK', $samplebranch1->{branchcode} ),
252     {
253         issuelength   => 5,
254         renewalperiod => 5,
255         lengthunit    => 'days',
256     },
257     "With the correct number of parameters, GetLoanLength returns the expected values"
258 );
259
260 #Test GetHardDueDate
261 my @hardduedate = C4::Circulation::GetHardDueDate( $samplecat->{categorycode},
262     'BOOK', $samplebranch1->{branchcode} );
263 is_deeply(
264     \@hardduedate,
265     [
266         dt_from_string( $sampleissuingrule1->{rules}->{hardduedate}, 'iso' ),
267         $sampleissuingrule1->{rules}->{hardduedatecompare}
268     ],
269     "GetHardDueDate returns the duedate and the duedatecompare"
270 );