Bug 28774: Don't store blank values for rental discount
[koha-ffzg.git] / Koha / Virtualshelfshare.pm
index 09e383f..5f61e27 100644 (file)
@@ -2,27 +2,26 @@ package Koha::Virtualshelfshare;
 
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 
-use Carp;
 use DateTime;
 use DateTime::Duration;
 
 use Koha::Database;
-use Koha::DateUtils;
+use Koha::DateUtils qw( dt_from_string );
 use Koha::Exceptions;
 
 use base qw(Koha::Object);
@@ -51,10 +50,20 @@ sub accept {
     if ( $self->invitekey ne $invitekey ) {
         Koha::Exceptions::Virtualshelves::InvalidInviteKey->throw;
     }
-    $self->invitekey(undef);
-    $self->sharedate(dt_from_string);
-    $self->borrowernumber($borrowernumber);
-    $self->store;
+
+    # If this borrower already has a share, there is no need to accept twice
+    # We solve this by 'pretending' to reaccept, but delete instead
+    my $search = Koha::Virtualshelfshares->search({ shelfnumber => $self->shelfnumber, borrowernumber => $borrowernumber, invitekey => undef });
+    if( $search->count ) {
+        $self->delete;
+        return $search->next;
+    } else {
+        $self->invitekey(undef);
+        $self->sharedate(dt_from_string);
+        $self->borrowernumber($borrowernumber);
+        $self->store;
+        return $self;
+    }
 }
 
 sub has_expired {