Bug 15675 - Add issue_id column to accountlines and use it for updating fines
[koha.git] / Koha / Virtualshelfshare.pm
1 package Koha::Virtualshelfshare;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Carp;
21 use DateTime;
22 use DateTime::Duration;
23
24 use Koha::Database;
25 use Koha::DateUtils;
26 use Koha::Exceptions;
27
28 use base qw(Koha::Object);
29
30 use constant SHARE_INVITATION_EXPIRY_DAYS => 14; #two weeks to accept
31
32 =head1 NAME
33
34 Koha::Virtualshelfshare - Koha Virtualshelfshare Object class
35
36 =head1 API
37
38 =head2 Class Methods
39
40 =cut
41
42 =head3 type
43
44 =cut
45
46 sub accept {
47     my ( $self, $invitekey, $borrowernumber ) = @_;
48     if ( $self->has_expired ) {
49         Koha::Exceptions::Virtualshelves::ShareHasExpired->throw;
50     }
51     if ( $self->invitekey ne $invitekey ) {
52         Koha::Exceptions::Virtualshelves::InvalidInviteKey->throw;
53     }
54     $self->invitekey(undef);
55     $self->sharedate(dt_from_string);
56     $self->borrowernumber($borrowernumber);
57     $self->store;
58 }
59
60 sub has_expired {
61     my ($self) = @_;
62     my $dt_sharedate     = dt_from_string( $self->sharedate, 'sql' );
63     my $today            = dt_from_string;
64     my $expiration_delay = DateTime::Duration->new( days => SHARE_INVITATION_EXPIRY_DAYS );
65     my $has_expired = DateTime->compare( $today, $dt_sharedate->add_duration($expiration_delay) );
66     # Note: has_expired = 0 if the share expires today
67     return $has_expired == 1 ? 1 : 0
68 }
69
70 sub _type {
71     return 'Virtualshelfshare';
72 }
73
74 1;