Bug 20434: Update UNIMARC framework - auth (TM)
[koha.git] / Koha / Charges / Fees.pm
blob41299cee146a37d029461655b38d247992e504f2
1 package Koha::Charges::Fees;
3 # Copyright 2018 ByWater Solutions
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use Carp qw( confess );
24 use Koha::Calendar;
25 use Koha::IssuingRules;
26 use Koha::DateUtils qw( dt_from_string );
27 use Koha::Exceptions;
29 =head1 NAME
31 Koha::Charges::Fees - Module calculating fees in Koha
33 =head2 Class methods
35 =head3 new
37 Koha::Charges::Fees->new(
39 patron => $patron,
40 library => $library,
41 item => $item,
42 to_date => $to_dt,
43 [ from_date => $from_dt, ]
47 =cut
49 sub new {
50 my ( $class, $params ) = @_;
52 Koha::Exceptions::MissingParameter->throw("Missing mandatory parameter: patron")
53 unless $params->{patron};
54 Koha::Exceptions::MissingParameter->throw("Missing mandatory parameter: library")
55 unless $params->{library};
56 Koha::Exceptions::MissingParameter->throw("Missing mandatory parameter: item")
57 unless $params->{item};
58 Koha::Exceptions::MissingParameter->throw("Missing mandatory parameter: to_date")
59 unless $params->{to_date};
61 Carp::confess("Key 'patron' is not a Koha::Patron object!")
62 unless $params->{patron}->isa('Koha::Patron');
63 Carp::confess("Key 'library' is not a Koha::Library object!")
64 unless $params->{library}->isa('Koha::Library');
65 Carp::confess("Key 'item' is not a Koha::Item object!")
66 unless $params->{item}->isa('Koha::Item');
67 Carp::confess("Key 'to_date' is not a DateTime object!")
68 unless $params->{to_date}->isa('DateTime');
70 if ( $params->{from_date} ) {
71 Carp::croak("Key 'from_date' is not a DateTime object!")
72 unless $params->{from_date}->isa('DateTime');
74 else {
75 $params->{from_date} = dt_from_string();
78 return bless( $params, $class );
81 =head3 accumulate_rentalcharge
83 my $fee = $self->accumulate_rentalcharge();
85 This method calculates the daily rental fee for a given itemtype for a given
86 period of time passed in as a pair of DateTime objects.
88 =cut
90 sub accumulate_rentalcharge {
91 my ( $self ) = @_;
93 my $itemtype = Koha::ItemTypes->find( $self->item->effective_itemtype );
94 my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule(
96 categorycode => $self->patron->categorycode,
97 itemtype => $itemtype->id,
98 branchcode => $self->library->id
101 my $units = $issuing_rule->lengthunit;
102 my $rentalcharge_increment = ( $units eq 'days' ) ? $itemtype->rentalcharge_daily : $itemtype->rentalcharge_hourly;
104 return 0 unless $rentalcharge_increment && $rentalcharge_increment > 0;
106 my $duration;
107 my $calendar = Koha::Calendar->new( branchcode => $self->library->id );
109 if ( $units eq 'hours' ) {
110 if ( C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed' ) {
111 $duration =
112 $calendar->hours_between( $self->from_date, $self->to_date );
114 else {
115 $duration = $self->to_date->delta_ms($self->from_date);
118 else {
119 if ( C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed' ) {
120 $duration =
121 $calendar->days_between( $self->from_date, $self->to_date );
123 else {
124 $duration = $self->to_date->delta_days( $self->from_date );
128 my $charge = $rentalcharge_increment * $duration->in_units($units);
129 return $charge;
132 =head3 patron
134 my $patron = $fees->patron( $patron );
136 =cut
138 sub patron {
139 my ( $self, $patron ) = @_;
141 $self->{patron} = $patron if $patron && $patron->isa('Koha::Patron');
143 return $self->{patron};
146 =head3 library
148 my $library = $fees->library( $library );
150 =cut
152 sub library {
153 my ( $self, $library ) = @_;
155 $self->{library} = $library if $library && $library->isa('Koha::Library');
157 return $self->{library};
160 =head3 item
162 my $item = $fees->item( $item );
164 =cut
166 sub item {
167 my ( $self, $item ) = @_;
169 $self->{item} = $item if $item && $item->isa('Koha::Item');
171 return $self->{item};
174 =head3 to_date
176 my $to_date = $fees->to_date( $to_date );
178 =cut
180 sub to_date {
181 my ( $self, $to_date ) = @_;
183 $self->{to_date} = $to_date if $to_date && $to_date->isa('DateTime');
185 return $self->{to_date};
188 =head3 from_date
190 my $from_date = $fees->from_date( $from_date );
192 =cut
194 sub from_date {
195 my ( $self, $from_date ) = @_;
197 $self->{from_date} = $from_date if $from_date && $from_date->isa('DateTime');
199 return $self->{from_date};
202 =head1 AUTHOR
204 Kyle M Hall <kyle.m.hall@gmail.com>
206 =cut