Bug 21202: Replace C4::Items::GetItemsByBiblioitemnumber calls
[koha.git] / C4 / Items.pm
index a3ecd87..6a8cb07 100644 (file)
@@ -69,13 +69,11 @@ BEGIN {
         CheckItemPreSave
     
         GetItemsForInventory
-        GetItemsByBiblioitemnumber
         GetItemsInfo
        GetItemsLocationInfo
        GetHostItemsInfo
         GetItemnumbersForBiblio
        get_hostitemnumbers_of
-        GetItemnumberFromBarcode
         GetBarcodeFromItemnumber
         GetHiddenItemnumbers
         ItemSafeToDelete
@@ -497,7 +495,6 @@ sub ModItemFromMarc {
     my $biblionumber = shift;
     my $itemnumber = shift;
 
-    my $dbh           = C4::Context->dbh;
     my $frameworkcode = C4::Biblio::GetFrameworkCode($biblionumber);
     my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
 
@@ -511,13 +508,21 @@ sub ModItemFromMarc {
     }
     my $unlinked_item_subfields = _get_unlinked_item_subfields( $localitemmarc, $frameworkcode );
 
-    ModItem($item, $biblionumber, $itemnumber, $dbh, $frameworkcode, $unlinked_item_subfields); 
+    ModItem( $item, $biblionumber, $itemnumber, { unlinked_item_subfields => $unlinked_item_subfields } );
     return $item;
 }
 
 =head2 ModItem
 
-  ModItem({ column => $newvalue }, $biblionumber, $itemnumber);
+ModItem(
+    { column => $newvalue },
+    $biblionumber,
+    $itemnumber,
+    {
+        [ unlinked_item_subfields => $unlinked_item_subfields, ]
+        [ log_action => 1, ]
+    }
+);
 
 Change one or more columns in an item record and update
 the MARC representation of the item.
@@ -525,9 +530,11 @@ the MARC representation of the item.
 The first argument is a hashref mapping from item column
 names to the new values.  The second and third arguments
 are the biblionumber and itemnumber, respectively.
+The fourth, optional parameter (additional_params) may contain the keys
+unlinked_item_subfields and log_action.
 
-The fourth, optional parameter, C<$unlinked_item_subfields>, contains
-an arrayref containing subfields present in the original MARC
+C<$unlinked_item_subfields> contains an arrayref containing
+subfields present in the original MARC
 representation of the item (e.g., from the item editor) that are
 not mapped to C<items> columns directly but should instead
 be stored in C<items.more_subfields_xml> and included in 
@@ -538,24 +545,22 @@ the derived value of a column such as C<items.cn_sort>,
 this routine will perform the necessary calculation
 and set the value.
 
+If log_action is set to false, the action will not be logged.
+If log_action is true or undefined, the action will be logged.
+
 =cut
 
 sub ModItem {
-    my $item = shift;
-    my $biblionumber = shift;
-    my $itemnumber = shift;
+    my ( $item, $biblionumber, $itemnumber, $additional_params ) = @_;
+    my $log_action = $additional_params->{log_action} // 1;
+    my $unlinked_item_subfields = $additional_params->{unlinked_item_subfields};
 
     # if $biblionumber is undefined, get it from the current item
     unless (defined $biblionumber) {
         $biblionumber = _get_single_item_column('biblionumber', $itemnumber);
     }
 
-    my $dbh           = @_ ? shift : C4::Context->dbh;
-    my $frameworkcode = @_ ? shift : C4::Biblio::GetFrameworkCode( $biblionumber );
-    
-    my $unlinked_item_subfields;  
-    if (@_) {
-        $unlinked_item_subfields = shift;
+    if ($unlinked_item_subfields) {
         $item->{'more_subfields_xml'} = _get_unlinked_subfields_xml($unlinked_item_subfields);
     };
 
@@ -602,7 +607,8 @@ sub ModItem {
     # item status is possible
     ModZebra( $biblionumber, "specialUpdate", "biblioserver" );
 
-    logaction("CATALOGUING", "MODIFY", $itemnumber, "item ".Dumper($item)) if C4::Context->preference("CataloguingLog");
+    logaction( "CATALOGUING", "MODIFY", $itemnumber, "item " . Dumper($item) )
+      if $log_action && C4::Context->preference("CataloguingLog");
 }
 
 =head2 ModItemTransfer
@@ -637,18 +643,24 @@ sub ModItemTransfer {
 
 =head2 ModDateLastSeen
 
-  ModDateLastSeen($itemnum);
+ModDateLastSeen( $itemnumber, $leave_item_lost );
 
 Mark item as seen. Is called when an item is issued, returned or manually marked during inventory/stocktaking.
-C<$itemnum> is the item number
+C<$itemnumber> is the item number
+C<$leave_item_lost> determines if a lost item will be found or remain lost
 
 =cut
 
 sub ModDateLastSeen {
-    my ($itemnumber) = @_;
-    
+    my ( $itemnumber, $leave_item_lost ) = @_;
+
     my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
-    ModItem({ itemlost => 0, datelastseen => $today }, undef, $itemnumber);
+
+    my $params;
+    $params->{datelastseen} = $today;
+    $params->{itemlost} = 0 unless $leave_item_lost;
+
+    ModItem( $params, undef, $itemnumber, { log_action => 0 } );
 }
 
 =head2 DelItem
@@ -732,10 +744,10 @@ sub CheckItemPreSave {
 
     # check for duplicate barcode
     if (exists $item_ref->{'barcode'} and defined $item_ref->{'barcode'}) {
-        my $existing_itemnumber = GetItemnumberFromBarcode($item_ref->{'barcode'});
-        if ($existing_itemnumber) {
+        my $existing_item= Koha::Items->find({barcode => $item_ref->{'barcode'}});
+        if ($existing_item) {
             if (!exists $item_ref->{'itemnumber'}                       # new item
-                or $item_ref->{'itemnumber'} != $existing_itemnumber) { # existing item
+                or $item_ref->{'itemnumber'} != $existing_item->itemnumber) { # existing item
                 $errors{'duplicate_barcode'} = $item_ref->{'barcode'};
             }
         }
@@ -767,11 +779,6 @@ The following functions provide various ways of
 getting an item record, a set of item records, or
 lists of authorized values for certain item fields.
 
-Some of the functions in this group are candidates
-for refactoring -- for example, some of the code
-in C<GetItemsByBiblioitemnumber> and C<GetItemsInfo>
-has copy-and-paste work.
-
 =cut
 
 =head2 GetItemsForInventory
@@ -924,58 +931,6 @@ sub GetItemsForInventory {
     return (\@results, $iTotalRecords);
 }
 
-=head2 GetItemsByBiblioitemnumber
-
-  GetItemsByBiblioitemnumber($biblioitemnumber);
-
-Returns an arrayref of hashrefs suitable for use in a TMPL_LOOP
-Called by C<C4::XISBN>
-
-=cut
-
-sub GetItemsByBiblioitemnumber {
-    my ( $bibitem ) = @_;
-    my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare("SELECT * FROM items WHERE items.biblioitemnumber = ?") || die $dbh->errstr;
-    # Get all items attached to a biblioitem
-    my $i = 0;
-    my @results; 
-    $sth->execute($bibitem) || die $sth->errstr;
-    while ( my $data = $sth->fetchrow_hashref ) {  
-        # Foreach item, get circulation information
-        my $sth2 = $dbh->prepare( "SELECT * FROM issues,borrowers
-                                   WHERE itemnumber = ?
-                                   AND issues.borrowernumber = borrowers.borrowernumber"
-        );
-        $sth2->execute( $data->{'itemnumber'} );
-        if ( my $data2 = $sth2->fetchrow_hashref ) {
-            # if item is out, set the due date and who it is out too
-            $data->{'date_due'}   = $data2->{'date_due'};
-            $data->{'cardnumber'} = $data2->{'cardnumber'};
-            $data->{'borrowernumber'}   = $data2->{'borrowernumber'};
-        }
-        else {
-            # set date_due to blank, so in the template we check itemlost, and withdrawn
-            $data->{'date_due'} = '';                                                                                                         
-        }    # else         
-        # Find the last 3 people who borrowed this item.                  
-        my $query2 = "SELECT * FROM old_issues, borrowers WHERE itemnumber = ?
-                      AND old_issues.borrowernumber = borrowers.borrowernumber
-                      ORDER BY returndate desc,timestamp desc LIMIT 3";
-        $sth2 = $dbh->prepare($query2) || die $dbh->errstr;
-        $sth2->execute( $data->{'itemnumber'} ) || die $sth2->errstr;
-        my $i2 = 0;
-        while ( my $data2 = $sth2->fetchrow_hashref ) {
-            $data->{"timestamp$i2"} = $data2->{'timestamp'};
-            $data->{"card$i2"}      = $data2->{'cardnumber'};
-            $data->{"borrower$i2"}  = $data2->{'borrowernumber'};
-            $i2++;
-        }
-        push(@results,$data);
-    } 
-    return (\@results); 
-}
-
 =head2 GetItemsInfo
 
   @results = GetItemsInfo($biblionumber);
@@ -1198,44 +1153,40 @@ sub GetItemsLocationInfo {
 
 =head2 GetHostItemsInfo
 
-       $hostiteminfo = GetHostItemsInfo($hostfield);
-       Returns the iteminfo for items linked to records via a host field
+    $hostiteminfo = GetHostItemsInfo($hostfield);
+    Returns the iteminfo for items linked to records via a host field
 
 =cut
 
 sub GetHostItemsInfo {
-       my ($record) = @_;
-       my @returnitemsInfo;
-
-       if (C4::Context->preference('marcflavour') eq 'MARC21' ||
-        C4::Context->preference('marcflavour') eq 'NORMARC'){
-           foreach my $hostfield ( $record->field('773') ) {
-               my $hostbiblionumber = $hostfield->subfield("0");
-               my $linkeditemnumber = $hostfield->subfield("9");
-               my @hostitemInfos = GetItemsInfo($hostbiblionumber);
-               foreach my $hostitemInfo (@hostitemInfos){
-                       if ($hostitemInfo->{itemnumber} eq $linkeditemnumber){
-                               push (@returnitemsInfo,$hostitemInfo);
-                               last;
-                       }
-               }
-           }
-       } elsif ( C4::Context->preference('marcflavour') eq 'UNIMARC'){
-           foreach my $hostfield ( $record->field('461') ) {
-               my $hostbiblionumber = $hostfield->subfield("0");
-               my $linkeditemnumber = $hostfield->subfield("9");
-               my @hostitemInfos = GetItemsInfo($hostbiblionumber);
-               foreach my $hostitemInfo (@hostitemInfos){
-                       if ($hostitemInfo->{itemnumber} eq $linkeditemnumber){
-                               push (@returnitemsInfo,$hostitemInfo);
-                               last;
-                       }
-               }
-           }
-       }
-       return @returnitemsInfo;
-}
+    my ($record) = @_;
+    my @returnitemsInfo;
+
+    if( !C4::Context->preference('EasyAnalyticalRecords') ) {
+        return @returnitemsInfo;
+    }
 
+    my @fields;
+    if( C4::Context->preference('marcflavour') eq 'MARC21' ||
+      C4::Context->preference('marcflavour') eq 'NORMARC') {
+        @fields = $record->field('773');
+    } elsif( C4::Context->preference('marcflavour') eq 'UNIMARC') {
+        @fields = $record->field('461');
+    }
+
+    foreach my $hostfield ( @fields ) {
+        my $hostbiblionumber = $hostfield->subfield("0");
+        my $linkeditemnumber = $hostfield->subfield("9");
+        my @hostitemInfos = GetItemsInfo($hostbiblionumber);
+        foreach my $hostitemInfo (@hostitemInfos) {
+            if( $hostitemInfo->{itemnumber} eq $linkeditemnumber ) {
+                push @returnitemsInfo, $hostitemInfo;
+                last;
+            }
+        }
+    }
+    return @returnitemsInfo;
+}
 
 =head2 GetLastAcquisitions
 
@@ -1357,23 +1308,6 @@ sub get_hostitemnumbers_of {
 }
 
 
-=head2 GetItemnumberFromBarcode
-
-  $result = GetItemnumberFromBarcode($barcode);
-
-=cut
-
-sub GetItemnumberFromBarcode {
-    my ($barcode) = @_;
-    my $dbh = C4::Context->dbh;
-
-    my $rq =
-      $dbh->prepare("SELECT itemnumber FROM items WHERE items.barcode=?");
-    $rq->execute($barcode);
-    my ($result) = $rq->fetchrow;
-    return ($result);
-}
-
 =head2 GetBarcodeFromItemnumber
 
   $result = GetBarcodeFromItemnumber($itemnumber);