Bug 20473: Whitespace
[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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use DateTime;
21 use DateTime::Duration;
22
23 use Koha::Database;
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::Exceptions;
26 use Koha::Patron;
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 accept
43
44 =cut
45
46 sub accept {
47     my ( $self, $invitekey, $borrowernumber ) = @_;
48     if ( $self->has_expired ) {
49         Koha::Exceptions::Virtualshelf::ShareHasExpired->throw;
50     }
51     if ( $self->invitekey ne $invitekey ) {
52         Koha::Exceptions::Virtualshelf::InvalidInviteKey->throw;
53     }
54
55     # If this borrower already has a share, there is no need to accept twice
56     # We solve this by 'pretending' to reaccept, but delete instead
57     my $search = Koha::Virtualshelfshares->search({ shelfnumber => $self->shelfnumber, borrowernumber => $borrowernumber, invitekey => undef });
58     if( $search->count ) {
59         $self->delete;
60         return $search->next;
61     } else {
62         $self->invitekey(undef);
63         $self->sharedate(dt_from_string);
64         $self->borrowernumber($borrowernumber);
65         $self->store;
66         return $self;
67     }
68 }
69
70 =head3 has_expired
71
72 =cut
73
74 sub has_expired {
75     my ($self) = @_;
76     my $dt_sharedate     = dt_from_string( $self->sharedate, 'sql' );
77     my $today            = dt_from_string;
78     my $expiration_delay = DateTime::Duration->new( days => SHARE_INVITATION_EXPIRY_DAYS );
79     my $has_expired = DateTime->compare( $today, $dt_sharedate->add_duration($expiration_delay) );
80     # Note: has_expired = 0 if the share expires today
81     return $has_expired == 1 ? 1 : 0
82 }
83
84 =head3 sharee
85
86     Returns related Koha::Patron object for the sharee (patron who got this share).
87
88 =cut
89
90 sub sharee {
91     my $self = shift;
92     return Koha::Patron->_new_from_dbic( $self->{_result}->borrowernumber );
93 }
94
95 =head3 _type
96
97 =cut
98
99 sub _type {
100     return 'Virtualshelfshare';
101 }
102
103 1;