Adding Some new functions
authorHenri-Damien LAURENT <henridamien.laurent@biblibre.com>
Mon, 24 Aug 2009 20:10:28 +0000 (22:10 +0200)
committerHenri-Damien LAURENT <henridamien.laurent@biblibre.com>
Wed, 30 Sep 2009 09:22:22 +0000 (11:22 +0200)
Signed-off-by: Galen Charlton <gmcharlt@gmail.com>
C4/Items.pm

index 3f550ef..4d53d91 100644 (file)
@@ -64,6 +64,9 @@ BEGIN {
         get_itemnumbers_of
         GetItemnumberFromBarcode
 
+               DelItemCheck
+               MoveItemFromBiblio 
+               GetLatestAcquisitions
         CartToShelf
     );
 }
@@ -800,7 +803,6 @@ sub GetItemStatus {
             while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
                 $itemstatus{$authorisedvalue} = $lib;
             }
-            $authvalsth->finish;
             return \%itemstatus;
             exit 1;
         }
@@ -809,7 +811,6 @@ sub GetItemStatus {
             #No authvalue list
             # build default
         }
-        $sth->finish;
     }
 
     #No authvalue list
@@ -900,7 +901,6 @@ sub GetItemLocation {
             while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
                 $itemlocation{$authorisedvalue} = $lib;
             }
-            $authvalsth->finish;
             return \%itemlocation;
             exit 1;
         }
@@ -909,7 +909,6 @@ sub GetItemLocation {
             #No authvalue list
             # build default
         }
-        $sth->finish;
     }
 
     #No authvalue list
@@ -997,7 +996,7 @@ sub GetLostItems {
 
 =over 4
 
-$itemlist = GetItemsForInventory($minlocation, $maxlocation, $location, $itemtype $datelastseen, $branch, $offset, $size);
+$itemlist = GetItemsForInventory($minlocation, $maxlocation, $location, $itemtype $datelastseen, $branch, $offset, $size, $statushash);
 
 =back
 
@@ -1010,11 +1009,12 @@ seen. It is ordered by callnumber then title.
 The required minlocation & maxlocation parameters are used to specify a range of item callnumbers
 the datelastseen can be used to specify that you want to see items not seen since a past date only.
 offset & size can be used to retrieve only a part of the whole listing (defaut behaviour)
+$statushash requires a hashref that has the authorized values fieldname (intems.notforloan, etc...) as keys, and an arrayref of statuscodes we are searching for as values.
 
 =cut
 
 sub GetItemsForInventory {
-    my ( $minlocation, $maxlocation,$location, $itemtype, $ignoreissued, $datelastseen, $branch, $offset, $size ) = @_;
+    my ( $minlocation, $maxlocation,$location, $itemtype, $ignoreissued, $datelastseen, $branch, $offset, $size, $statushash ) = @_;
     my $dbh = C4::Context->dbh;
     my ( @bind_params, @where_strings );
 
@@ -1024,6 +1024,14 @@ FROM items
   LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber
   LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber
 END_SQL
+    if ($statushash){
+        for my $authvfield (keys %$statushash){
+            if ( scalar @{$statushash->{$authvfield}} > 0 ){
+                my $joinedvals = join ',', @{$statushash->{$authvfield}};
+                push @where_strings, "$authvfield in (" . $joinedvals . ")";
+            }
+        }
+    }
 
     if ($minlocation) {
         push @where_strings, 'itemcallnumber >= ?';
@@ -1102,7 +1110,6 @@ sub GetItemsCount {
     my $sth = $dbh->prepare($query);
     $sth->execute($biblionumber);
     my $count = $sth->fetchrow;  
-    $sth->finish;
     return ($count);
 }
 
@@ -1165,7 +1172,6 @@ sub GetItemsByBiblioitemnumber {
             # set date_due to blank, so in the template we check itemlost, and wthdrawn 
             $data->{'date_due'} = '';                                                                                                         
         }    # else         
-        $sth2->finish;
         # Find the last 3 people who borrowed this item.                  
         my $query2 = "SELECT * FROM old_issues, borrowers WHERE itemnumber = ?
                       AND old_issues.borrowernumber = borrowers.borrowernumber
@@ -1179,10 +1185,8 @@ sub GetItemsByBiblioitemnumber {
             $data->{"borrower$i2"}  = $data2->{'borrowernumber'};
             $i2++;
         }
-        $sth2->finish;
         push(@results,$data);
     } 
-    $sth->finish;
     return (\@results); 
 }
 
@@ -1304,8 +1308,6 @@ sub GetItemsInfo {
 # value.
             $count_reserves = $restype;
         }
-        $isth->finish;
-        $ssth->finish;
         #get branch information.....
         my $bsth = $dbh->prepare(
             "SELECT * FROM branches WHERE branchcode = ?
@@ -1388,6 +1390,60 @@ sub GetItemsInfo {
        }
 }
 
+=head2 GetLastAcquisitions
+
+=over 4
+
+my $lastacq = GetLastAcquisitions({'branches' => ('branch1','branch2'), 'itemtypes' => ('BK','BD')}, 10);
+
+=back
+
+=cut
+
+sub  GetLastAcquisitions {
+       my ($data,$max) = @_;
+
+       my $itemtype = C4::Context->preference('item-level_itypes') ? 'itype' : 'itemtype';
+       
+       my $number_of_branches = @{$data->{branches}};
+       my $number_of_itemtypes   = @{$data->{itemtypes}};
+       
+       
+       my @where = ('WHERE 1 '); 
+       $number_of_branches and push @where
+          , 'AND holdingbranch IN (' 
+          , join(',', ('?') x $number_of_branches )
+          , ')'
+        ;
+       
+       $number_of_itemtypes and push @where
+          , "AND $itemtype IN (" 
+          , join(',', ('?') x $number_of_itemtypes )
+          , ')'
+        ;
+
+       my $query = "SELECT biblio.biblionumber as biblionumber, title, dateaccessioned
+                                FROM items RIGHT JOIN biblio ON (items.biblionumber=biblio.biblionumber) 
+                                   RIGHT JOIN biblioitems ON (items.biblioitemnumber=biblioitems.biblioitemnumber)
+                                   @where
+                                   GROUP BY biblio.biblionumber 
+                                   ORDER BY dateaccessioned DESC LIMIT $max";
+
+       my $dbh = C4::Context->dbh;
+       my $sth = $dbh->prepare($query);
+    
+    $sth->execute((@{$data->{branches}}, @{$data->{itemtypes}}));
+       
+       my @results;
+       while( my $row = $sth->fetchrow_hashref){
+               push @results, {date => $row->{dateaccessioned} 
+                                               , biblionumber => $row->{biblionumber}
+                                               , title => $row->{title}};
+       }
+       
+       return @results;
+}
+
 =head2 get_itemnumbers_of
 
 =over 4
@@ -1946,10 +2002,77 @@ sub _koha_new_item {
     if ( defined $sth->errstr ) {
         $error.="ERROR in _koha_new_item $query".$sth->errstr;
     }
-    $sth->finish();
     return ( $itemnumber, $error );
 }
 
+=head2 MoveItemFromBiblio
+
+=over 4
+
+MoveItemFromBiblio($itenumber, $frombiblio, $tobiblio);
+
+=back
+
+Moves an item from a biblio to another
+
+=cut
+sub MoveItemFromBiblio {
+    my ($itemnumber, $frombiblio, $tobiblio) = @_;
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare("UPDATE items SET biblioitemnumber = ?, biblionumber = ? WHERE itemnumber = ? AND biblionumber = ?");
+    my $return = $sth->execute($tobiblio, $tobiblio, $itemnumber, $frombiblio);
+    if ($return == 1) {
+       my $record = GetMarcBiblio($frombiblio);
+       my $frameworkcode = GetFrameworkCode($frombiblio);
+       ModBiblioMarc($record, $frombiblio, $frameworkcode);
+
+       $record = GetMarcBiblio($tobiblio);
+       $frameworkcode = GetFrameworkCode($frombiblio);
+       ModBiblioMarc($record, $tobiblio, $frameworkcode);
+    } else {
+       return -1;
+    }
+}
+
+=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;
+
+    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;
+        if ($reserve){
+            $error = "book_reserved";
+        }else{
+            DelItem($dbh, $biblionumber, $itemnumber);
+            return 1;
+        }
+    }
+    return $error;
+}
+
 =head2 _koha_modify_item
 
 =over 4
@@ -1983,7 +2106,6 @@ sub _koha_modify_item {
         $error.="ERROR in _koha_modify_item $query".$dbh->errstr;
         warn $error;
     }
-    $sth->finish();
     return ($item->{'itemnumber'},$error);
 }
 
@@ -2006,7 +2128,6 @@ sub _koha_delete_item {
     my $sth = $dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
     $sth->execute($itemnum);
     my $data = $sth->fetchrow_hashref();
-    $sth->finish();
     my $query = "INSERT INTO deleteditems SET ";
     my @bind  = ();
     foreach my $key ( keys %$data ) {
@@ -2016,12 +2137,10 @@ sub _koha_delete_item {
     $query =~ s/\,$//;
     $sth = $dbh->prepare($query);
     $sth->execute(@bind);
-    $sth->finish();
 
     # delete from items table
     $sth = $dbh->prepare("DELETE FROM items WHERE itemnumber=?");
     $sth->execute($itemnum);
-    $sth->finish();
     return undef;
 }