Move checks of additem.pl to the API
authorNahuel Angelinetti <nahuel.angelinetti@biblibre.com>
Thu, 13 Nov 2008 14:30:42 +0000 (15:30 +0100)
committerHenri-Damien LAURENT <henridamien.laurent@biblibre.com>
Fri, 6 Mar 2009 10:09:41 +0000 (11:09 +0100)
This patch create a new function DelItemCheck, that add some checks before deleting an item and return 1 if it worked fine, else an error
code (the same as in additem.pl).

Signed-off-by: Henri-Damien LAURENT <henridamien.laurent@biblibre.com>
C4/Items.pm
cataloguing/additem.pl

index c69d693..81ec0c9 100644 (file)
@@ -49,6 +49,7 @@ BEGIN {
         ModDateLastSeen
         ModItemTransfer
         DelItem
+        DelItemCheck
     
         CheckItemPreSave
     
@@ -586,6 +587,47 @@ sub DelItem {
     logaction("CATALOGUING", "DELETE", $itemnumber, "item") if C4::Context->preference("CataloguingLog");
 }
 
+=head2 DelItemCheck
+
+=over 4
+
+DelItemCheck($dbh, $biblionumber, $itemnumber);
+
+=back
+
+Exported function (core API) for deleting an item record in Koha if there no current issue.
+
+=cut
+
+sub DelItemCheck {
+    my ( $dbh, $biblionumber, $itemnumber ) = @_;
+    my $error;
+
+    # check that there is no issue on this item before deletion.
+    my $sth=$dbh->prepare("select * from issues i where i.itemnumber=?");
+    $sth->execute($itemnumber);
+
+    my $onloan=$sth->fetchrow;
+
+    $sth->finish();
+    if ($onloan){
+        $error = "book_on_loan" 
+    }else{
+        # check it doesnt have a waiting reserve
+        $sth=$dbh->prepare("SELECT * FROM reserves WHERE found = 'W' AND itemnumber = ?");
+        $sth->execute($itemnumber);
+        my $reserve=$sth->fetchrow;
+        $sth->finish();
+        if ($reserve){
+            $error = "book_reserved";
+        }else{
+            DelItem($dbh, $biblionumber, $itemnumber);
+            return 1;
+        }
+    }
+    return $error;
+}
+
 =head2 CheckItemPreSave
 
 =over 4
index 376687c..ee551c9 100755 (executable)
@@ -134,27 +134,12 @@ if ($op eq "additem") {
 #-------------------------------------------------------------------------------
 } elsif ($op eq "delitem") {
 #-------------------------------------------------------------------------------
-    # check that there is no issue on this item before deletion.
-    my $sth=$dbh->prepare("select * from issues i where i.itemnumber=?");
-    $sth->execute($itemnumber);
-    my $onloan=$sth->fetchrow;
-       $sth->finish();
-    push @errors,"book_on_loan" if ($onloan); ##error book_on_loan added to template as well
-    if ($onloan){
-               $nextop="additem";
-    } else {
-               # check it doesnt have a waiting reserve
-               $sth=$dbh->prepare("SELECT * FROM reserves WHERE found = 'W' AND itemnumber = ?");
-               $sth->execute($itemnumber);
-               my $reserve=$sth->fetchrow;
-               if ($reserve){
-                       push @errors,"book_reserved";
-                       $nextop="additem";
-               }
-               else {
-                       &DelItem($dbh,$biblionumber,$itemnumber);
-                       print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
-               }
+    $error = &DelItemCheck($dbh,$biblionumber,$itemnumber);
+    if($error == 1){
+        print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
+    }else{
+        push @errors,$error;
+        $nextop="additem";
     }
 #-------------------------------------------------------------------------------
 } elsif ($op eq "delallitems") {