View | Details | Raw Unified | Return to bug 23382
Collapse All | Expand All

(-)a/Koha/Charges/Fees.pm (-7 / +23 lines)
Lines 19-25 package Koha::Charges::Fees; Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Carp qw( confess );
22
use Carp qw( carp confess );
23
23
24
use Koha::Calendar;
24
use Koha::Calendar;
25
use Koha::IssuingRules;
25
use Koha::IssuingRules;
Lines 82-88 sub new { Link Here
82
82
83
    my $fee = $self->accumulate_rentalcharge();
83
    my $fee = $self->accumulate_rentalcharge();
84
84
85
    This method calculates the daily rental fee for a given itemtype for a given
85
    This method calculates the daily/hourly rental fee for a given itemtype for a given
86
    period of time passed in as a pair of DateTime objects.
86
    period of time passed in as a pair of DateTime objects.
87
87
88
=cut
88
=cut
Lines 138-144 my $patron = $fees->patron( $patron ); Link Here
138
sub patron {
138
sub patron {
139
    my ( $self, $patron ) = @_;
139
    my ( $self, $patron ) = @_;
140
140
141
    $self->{patron} = $patron if $patron && $patron->isa('Koha::Patron');
141
    Carp::carp("Setting 'patron' to something other than a Koha::Patron is not supported!")
142
      if ($patron && !$patron->isa('Koha::Patron'));
143
144
    $self->{patron} = $patron if $patron;
142
145
143
    return $self->{patron};
146
    return $self->{patron};
144
}
147
}
Lines 152-158 my $library = $fees->library( $library ); Link Here
152
sub library {
155
sub library {
153
    my ( $self, $library ) = @_;
156
    my ( $self, $library ) = @_;
154
157
155
    $self->{library} = $library if $library && $library->isa('Koha::Library');
158
    Carp::carp("Setting 'library' to something other than a Koha::Library is not supported!")
159
      if ($library && !$library->isa('Koha::Library'));
160
161
    $self->{library} = $library if $library;
156
162
157
    return $self->{library};
163
    return $self->{library};
158
}
164
}
Lines 166-172 my $item = $fees->item( $item ); Link Here
166
sub item {
172
sub item {
167
    my ( $self, $item ) = @_;
173
    my ( $self, $item ) = @_;
168
174
169
    $self->{item} = $item if $item && $item->isa('Koha::Item');
175
    Carp::carp("Setting 'item' to something other than a Koha::Item is not supported!")
176
      if ($item && !$item->isa('Koha::Item'));
177
178
    $self->{item} = $item if $item;
170
179
171
    return $self->{item};
180
    return $self->{item};
172
}
181
}
Lines 180-186 my $to_date = $fees->to_date( $to_date ); Link Here
180
sub to_date {
189
sub to_date {
181
    my ( $self, $to_date ) = @_;
190
    my ( $self, $to_date ) = @_;
182
191
183
    $self->{to_date} = $to_date if $to_date && $to_date->isa('DateTime');
192
    Carp::carp("Setting 'to_date' to something other than a DateTime is not supported!")
193
      if ($to_date && !$to_date->isa('DateTime'));
194
195
    $self->{to_date} = $to_date if $to_date;
184
196
185
    return $self->{to_date};
197
    return $self->{to_date};
186
}
198
}
Lines 194-207 my $from_date = $fees->from_date( $from_date ); Link Here
194
sub from_date {
206
sub from_date {
195
    my ( $self, $from_date ) = @_;
207
    my ( $self, $from_date ) = @_;
196
208
209
    Carp::carp("Setting 'from_date' to something other than a DateTime is not supported!")
210
      if ($from_date && !$from_date->isa('DateTime'));
211
197
    $self->{from_date} = $from_date if $from_date && $from_date->isa('DateTime');
212
    $self->{from_date} = $from_date if $from_date && $from_date->isa('DateTime');
198
213
199
    return $self->{from_date};
214
    return $self->{from_date};
200
}
215
}
201
216
202
=head1 AUTHOR
217
=head1 AUTHORS
203
218
204
Kyle M Hall <kyle.m.hall@gmail.com>
219
Kyle M Hall <kyle.m.hall@gmail.com>
220
Martin Renvoize <martin.renvoize@ptfs-europe.com>
205
221
206
=cut
222
=cut
207
223
(-)a/t/db_dependent/Koha/Charges/Fees.t (-20 / +241 lines)
Lines 19-31 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 2;
22
use Test::More tests => 8;
23
use Test::Exception;
24
use Test::Warn;
23
25
24
use t::lib::Mocks;
26
use t::lib::Mocks;
25
use t::lib::TestBuilder;
27
use t::lib::TestBuilder;
26
28
27
use Data::Dumper;
29
use Time::Fake;
28
29
use C4::Calendar;
30
use C4::Calendar;
30
use Koha::DateUtils qw(dt_from_string);
31
use Koha::DateUtils qw(dt_from_string);
31
32
Lines 59-67 my $itemtype = $builder->build_object( Link Here
59
        class => 'Koha::ItemTypes',
60
        class => 'Koha::ItemTypes',
60
        value => {
61
        value => {
61
            rentalcharge_daily => '0.00',
62
            rentalcharge_daily => '0.00',
62
            rentalcharge        => '0.00',
63
            rentalcharge       => '0.00',
63
            processfee          => '0.00',
64
            processfee         => '0.00',
64
            defaultreplacecost  => '0.00',
65
            defaultreplacecost => '0.00',
65
        },
66
        },
66
    }
67
    }
67
);
68
);
Lines 86-107 my $patron = $builder->build_object( Link Here
86
    }
87
    }
87
);
88
);
88
89
89
my $dt_from = dt_from_string();
90
my $dt = dt_from_string();
90
my $dt_to = dt_from_string()->add( days => 6 );
91
Time::Fake->offset( $dt->epoch );
91
92
92
my $fees = Koha::Charges::Fees->new(
93
my $dt_from = dt_from_string()->subtract( days => 2 );
93
    {
94
my $dt_to = dt_from_string()->add( days => 4 );
94
        patron    => $patron,
95
95
        library   => $library,
96
subtest 'new' => sub {
96
        item      => $item,
97
    plan tests => 9;
97
        to_date   => $dt_to,
98
98
        from_date => $dt_from,
99
    # Mandatory parameters missing
100
    throws_ok {
101
        Koha::Charges::Fees->new(
102
            {
103
                library => $library,
104
                item    => $item,
105
                to_date => $dt_to,
106
            }
107
          )
99
    }
108
    }
100
);
109
    'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for patron';
110
    throws_ok {
111
        Koha::Charges::Fees->new(
112
            {
113
                patron  => $patron,
114
                item    => $item,
115
                to_date => $dt_to,
116
            }
117
          )
118
    }
119
    'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for library';
120
    throws_ok {
121
        Koha::Charges::Fees->new(
122
            {
123
                patron  => $patron,
124
                library => $library,
125
                to_date => $dt_to,
126
            }
127
          )
128
    }
129
    'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for item';
130
    throws_ok {
131
        Koha::Charges::Fees->new(
132
            {
133
                patron  => $patron,
134
                library => $library,
135
                item    => $item,
136
            }
137
          )
138
    }
139
    'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for to_date';
140
141
    # Mandatory parameter bad
142
    dies_ok {
143
        Koha::Charges::Fees->new(
144
            {
145
                patron  => '12345',
146
                library => $library,
147
                item    => $item,
148
                to_date => $dt_to,
149
            }
150
          )
151
    }
152
    'dies for bad patron';
153
    dies_ok {
154
        Koha::Charges::Fees->new(
155
            {
156
                patron  => $patron,
157
                library => '12345',
158
                item    => $item,
159
                to_date => $dt_to,
160
            }
161
          )
162
    }
163
    'dies for bad library';
164
    dies_ok {
165
        Koha::Charges::Fees->new(
166
            {
167
                patron  => $patron,
168
                library => $library,
169
                item    => '12345',
170
                to_date => $dt_to,
171
            }
172
          )
173
    }
174
    'dies for bad item';
175
    dies_ok {
176
        Koha::Charges::Fees->new(
177
            {
178
                patron  => $patron,
179
                library => $library,
180
                item    => $item,
181
                to_date => 12345
182
            }
183
          )
184
    }
185
    'dies for bad to_date';
186
187
    # Defaults
188
    my $fees = Koha::Charges::Fees->new(
189
        {
190
            patron  => $patron,
191
            library => $library,
192
            item    => $item,
193
            to_date => $dt_to,
194
        }
195
    );
196
    is( $fees->from_date, dt_from_string(),
197
        'from_date default set correctly to today' );
198
};
199
200
subtest 'patron accessor' => sub {
201
    plan tests => 2;
202
203
    my $fees = Koha::Charges::Fees->new(
204
        {
205
            patron  => $patron,
206
            library => $library,
207
            item    => $item,
208
            to_date => $dt_to,
209
        }
210
    );
211
212
    ok(
213
        $fees->patron->isa('Koha::Patron'),
214
        'patron accessor returns a Koha::Patron'
215
    );
216
    warning_is { $fees->patron('12345') }
217
    { carped =>
218
"Setting 'patron' to something other than a Koha::Patron is not supported!"
219
    }, "Warning thrown when attempting to set patron to string";
220
221
};
222
223
subtest 'library accessor' => sub {
224
    plan tests => 2;
225
226
    my $fees = Koha::Charges::Fees->new(
227
        {
228
            patron  => $patron,
229
            library => $library,
230
            item    => $item,
231
            to_date => $dt_to,
232
        }
233
    );
234
235
    ok(
236
        $fees->library->isa('Koha::Library'),
237
        'library accessor returns a Koha::Library'
238
    );
239
    warning_is { $fees->library('12345') }
240
    { carped =>
241
"Setting 'library' to something other than a Koha::Library is not supported!"
242
    }, "Warning thrown when attempting to set library to string";
243
};
244
245
subtest 'item accessor' => sub {
246
    plan tests => 2;
247
248
    my $fees = Koha::Charges::Fees->new(
249
        {
250
            patron  => $patron,
251
            library => $library,
252
            item    => $item,
253
            to_date => $dt_to,
254
        }
255
    );
256
257
    ok( $fees->item->isa('Koha::Item'), 'item accessor returns a Koha::Item' );
258
    warning_is { $fees->item('12345') }
259
    { carped =>
260
"Setting 'item' to something other than a Koha::Item is not supported!"
261
    }, "Warning thrown when attempting to set item to string";
262
};
263
264
subtest 'to_date accessor' => sub {
265
    plan tests => 2;
266
267
    my $fees = Koha::Charges::Fees->new(
268
        {
269
            patron  => $patron,
270
            library => $library,
271
            item    => $item,
272
            to_date => $dt_to,
273
        }
274
    );
275
276
    ok( $fees->to_date->isa('DateTime'),
277
        'to_date accessor returns a DateTime' );
278
    warning_is { $fees->to_date(12345) }
279
    { carped =>
280
"Setting 'to_date' to something other than a DateTime is not supported!"
281
    }, "Warning thrown when attempting to set to_date to integer";
282
};
283
284
subtest 'from_date accessor' => sub {
285
    plan tests => 2;
286
287
    my $fees = Koha::Charges::Fees->new(
288
        {
289
            patron  => $patron,
290
            library => $library,
291
            item    => $item,
292
            to_date => $dt_to,
293
        }
294
    );
295
296
    ok(
297
        $fees->from_date->isa('DateTime'),
298
        'from_date accessor returns a DateTime'
299
    );
300
    warning_is { $fees->from_date(12345) }
301
    { carped =>
302
"Setting 'from_date' to something other than a DateTime is not supported!"
303
    }, "Warning thrown when attempting to set from_date to integer";
304
};
101
305
102
subtest 'accumulate_rentalcharge tests' => sub {
306
subtest 'accumulate_rentalcharge tests' => sub {
103
    plan tests => 4;
307
    plan tests => 4;
104
308
309
    my $fees = Koha::Charges::Fees->new(
310
        {
311
            patron    => $patron,
312
            library   => $library,
313
            item      => $item,
314
            to_date   => $dt_to,
315
            from_date => $dt_from,
316
        }
317
    );
318
105
    $itemtype->rentalcharge_daily(1.00);
319
    $itemtype->rentalcharge_daily(1.00);
106
    $itemtype->store();
320
    $itemtype->store();
107
    is( $itemtype->rentalcharge_daily,
321
    is( $itemtype->rentalcharge_daily,
Lines 109-119 subtest 'accumulate_rentalcharge tests' => sub { Link Here
109
323
110
    t::lib::Mocks::mock_preference( 'finesCalendar', 'ignoreCalendar' );
324
    t::lib::Mocks::mock_preference( 'finesCalendar', 'ignoreCalendar' );
111
    my $charge = $fees->accumulate_rentalcharge();
325
    my $charge = $fees->accumulate_rentalcharge();
112
    is( $charge, 6.00, 'Daily rental charge calculated correctly with finesCalendar = ignoreCalendar' );
326
    is( $charge, 6.00,
327
'Daily rental charge calculated correctly with finesCalendar = ignoreCalendar'
328
    );
113
329
114
    t::lib::Mocks::mock_preference( 'finesCalendar', 'noFinesWhenClosed' );
330
    t::lib::Mocks::mock_preference( 'finesCalendar', 'noFinesWhenClosed' );
115
    $charge = $fees->accumulate_rentalcharge();
331
    $charge = $fees->accumulate_rentalcharge();
116
    is( $charge, 6.00, 'Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed' );
332
    is( $charge, 6.00,
333
'Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed'
334
    );
117
335
118
    my $calendar = C4::Calendar->new( branchcode => $library->id );
336
    my $calendar = C4::Calendar->new( branchcode => $library->id );
119
    $calendar->insert_week_day_holiday(
337
    $calendar->insert_week_day_holiday(
Lines 122-126 subtest 'accumulate_rentalcharge tests' => sub { Link Here
122
        description => 'Test holiday'
340
        description => 'Test holiday'
123
    );
341
    );
124
    $charge = $fees->accumulate_rentalcharge();
342
    $charge = $fees->accumulate_rentalcharge();
125
    is( $charge, 5.00, 'Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed Wednesdays' );
343
    is( $charge, 5.00,
344
'Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed Wednesdays'
345
    );
126
};
346
};
127
- 
347
348
Time::Fake->reset;

Return to bug 23382