Bug 21722: Use Koha::Account->add_debit in chargelostitem
[koha.git] / C4 / RotatingCollections.pm
index 5a63cd8..709eaf2 100644 (file)
@@ -9,33 +9,32 @@ package C4::RotatingCollections;
 #
 # 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 2 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 C4::Context;
 use C4::Circulation;
-use C4::Reserves qw(GetReserveStatus);
+use C4::Reserves qw(CheckReserves);
+use Koha::Database;
 
 use DBI;
 
 use Data::Dumper;
 
-use vars qw($VERSION @ISA @EXPORT);
+use vars qw(@ISA @EXPORT);
 
-# set the version for version checking
-$VERSION = 3.07.00.049;
 
 =head1 NAME
 
@@ -84,14 +83,18 @@ BEGIN {
 sub CreateCollection {
     my ( $title, $description ) = @_;
 
-    ## Check for all neccessary parameters
+    my $schema = Koha::Database->new()->schema();
+    my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title });
+
+    ## Check for all necessary parameters
     if ( !$title ) {
-        return ( 0, 1, "No Title Given" );
-    }
-    if ( !$description ) {
-        return ( 0, 2, "No Description Given" );
+        return ( 0, 1, "NO_TITLE" );
+    } elsif ( $duplicate_titles ) {
+        return ( 0, 2, "DUPLICATE_TITLE" );
     }
 
+    $description ||= q{};
+
     my $success = 1;
 
     my $dbh = C4::Context->dbh;
@@ -128,19 +131,24 @@ Updates a collection
 sub UpdateCollection {
     my ( $colId, $title, $description ) = @_;
 
-    ## Check for all neccessary parameters
+    my $schema = Koha::Database->new()->schema();
+    my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title,  -not => { colId => $colId } });
+
+    ## Check for all necessary parameters
     if ( !$colId ) {
-        return ( 0, 1, "No Id Given" );
+        return ( 0, 1, "NO_ID" );
     }
     if ( !$title ) {
-        return ( 0, 2, "No Title Given" );
+        return ( 0, 2, "NO_TITLE" );
     }
-    if ( !$description ) {
-        return ( 0, 3, "No Description Given" );
+    if ( $duplicate_titles ) {
+        return ( 0, 3, "DUPLICATE_TITLE" );
     }
 
     my $dbh = C4::Context->dbh;
 
+    $description ||= q{};
+
     my $sth;
     $sth = $dbh->prepare(
         "UPDATE collections
@@ -161,7 +169,7 @@ sub UpdateCollection {
  Deletes a collection of the given id
 
  Input:
-   $colId : id of the Archtype to be deleted
+   $colId : id of the Archetype to be deleted
 
  Output:
    $success: 1 if all database operations were successful, 0 otherwise
@@ -173,9 +181,9 @@ sub UpdateCollection {
 sub DeleteCollection {
     my ($colId) = @_;
 
-    ## Paramter check
+    ## Parameter check
     if ( !$colId ) {
-        return ( 0, 1, "No Collection Id Given" );
+        return ( 0, 1, "NO_ID" );
     }
 
     my $dbh = C4::Context->dbh;
@@ -237,9 +245,9 @@ sub GetCollections {
 sub GetItemsInCollection {
     my ($colId) = @_;
 
-    ## Paramter check
+    ## Parameter check
     if ( !$colId ) {
-        return ( 0, 0, 1, "No Collection Id Given" );
+        return ( 0, 0, 1, "NO_ID" );
     }
 
     my $dbh = C4::Context->dbh;
@@ -247,6 +255,7 @@ sub GetItemsInCollection {
     my $sth = $dbh->prepare(
         "SELECT
                              biblio.title,
+                             biblio.biblionumber,
                              items.itemcallnumber,
                              items.barcode
                            FROM collections, collections_tracking, items, biblio
@@ -315,19 +324,19 @@ Adds an item to a rotating collection.
 sub AddItemToCollection {
     my ( $colId, $itemnumber ) = @_;
 
-    ## Check for all neccessary parameters
+    ## Check for all necessary parameters
     if ( !$colId ) {
-        return ( 0, 1, "No Collection Given" );
+        return ( 0, 1, "NO_ID" );
     }
     if ( !$itemnumber ) {
-        return ( 0, 2, "No Itemnumber Given" );
+        return ( 0, 2, "NO_ITEM" );
     }
 
     if ( isItemInThisCollection( $itemnumber, $colId ) ) {
-        return ( 0, 2, "Item is already in the collection!" );
+        return ( 0, 2, "IN_COLLECTION" );
     }
     elsif ( isItemInAnyCollection($itemnumber) ) {
-        return ( 0, 3, "Item is already in a different collection!" );
+        return ( 0, 3, "IN_COLLECTION_OTHER" );
     }
 
     my $dbh = C4::Context->dbh;
@@ -365,13 +374,13 @@ Removes an item to a collection
 sub RemoveItemFromCollection {
     my ( $colId, $itemnumber ) = @_;
 
-    ## Check for all neccessary parameters
+    ## Check for all necessary parameters
     if ( !$itemnumber ) {
-        return ( 0, 2, "No Itemnumber Given" );
+        return ( 0, 2, "NO_ITEM" );
     }
 
     if ( !isItemInThisCollection( $itemnumber, $colId ) ) {
-        return ( 0, 2, "Item is not in the collection!" );
+        return ( 0, 2, "NOT_IN_COLLECTION" );
     }
 
     my $dbh = C4::Context->dbh;
@@ -406,12 +415,12 @@ Transfers a collection to another branch
 sub TransferCollection {
     my ( $colId, $colBranchcode ) = @_;
 
-    ## Check for all neccessary parameters
+    ## Check for all necessary parameters
     if ( !$colId ) {
-        return ( 0, 1, "No Id Given" );
+        return ( 0, 1, "NO_ID" );
     }
     if ( !$colBranchcode ) {
-        return ( 0, 2, "No Branchcode Given" );
+        return ( 0, 2, "NO_BRANCHCODE" );
     }
 
     my $dbh = C4::Context->dbh;
@@ -435,9 +444,9 @@ sub TransferCollection {
     $sth->execute($colId) or return ( 0, 4, $sth->errstr );
     my @results;
     while ( my $item = $sth->fetchrow_hashref ) {
-        transferbook( $colBranchcode, $item->{barcode},
-            my $ignore_reserves = 1 )
-          unless ( GetReserveStatus( $item->{itemnumber} ) eq "Waiting" );
+        my ($status) = CheckReserves( $item->{itemnumber} );
+        my @transfers = C4::Circulation::GetTransfers( $item->{itemnumber} );
+        C4::Circulation::transferbook( $colBranchcode, $item->{barcode}, my $ignore_reserves = 1 ) unless ( $status eq 'Waiting' || @transfers );
     }
 
     return 1;