Bug 2505 adding use warnings, also added missing copyright statement
[koha_gimpoz] / C4 / Items.pm
index e8e0cef..7a6182d 100644 (file)
@@ -30,6 +30,7 @@ use C4::Log;
 use C4::Branch;
 require C4::Reserves;
 use C4::Charset;
+use C4::Acquisition;
 
 use vars qw($VERSION @ISA @EXPORT);
 
@@ -208,8 +209,12 @@ sub AddItemFromMarc {
 
     # parse item hash from MARC
     my $frameworkcode = GetFrameworkCode( $biblionumber );
-    my $item = &TransformMarcToKoha( $dbh, $source_item_marc, $frameworkcode );
-    my $unlinked_item_subfields = _get_unlinked_item_subfields($source_item_marc, $frameworkcode);
+       my ($itemtag,$itemsubfield)=GetMarcFromKohaField("items.itemnumber",$frameworkcode);
+       
+       my $localitemmarc=MARC::Record->new;
+       $localitemmarc->append_fields($source_item_marc->field($itemtag));
+    my $item = &TransformMarcToKoha( $dbh, $localitemmarc, $frameworkcode ,'items');
+    my $unlinked_item_subfields = _get_unlinked_item_subfields($localitemmarc, $frameworkcode);
     return AddItem($item, $biblionumber, $dbh, $frameworkcode, $unlinked_item_subfields);
 }
 
@@ -447,11 +452,15 @@ sub ModItemFromMarc {
 
     my $dbh = C4::Context->dbh;
     my $frameworkcode = GetFrameworkCode( $biblionumber );
-    my $item = &TransformMarcToKoha( $dbh, $item_marc, $frameworkcode );
+       my ($itemtag,$itemsubfield)=GetMarcFromKohaField("items.itemnumber",$frameworkcode);
+       
+       my $localitemmarc=MARC::Record->new;
+       $localitemmarc->append_fields($item_marc->field($itemtag));
+    my $item = &TransformMarcToKoha( $dbh, $localitemmarc, $frameworkcode, 'items');
     foreach my $item_field (keys %default_values_for_mod_from_marc) {
         $item->{$item_field} = $default_values_for_mod_from_marc{$item_field} unless exists $item->{$item_field};
     }
-    my $unlinked_item_subfields = _get_unlinked_item_subfields($item_marc, $frameworkcode);
+    my $unlinked_item_subfields = _get_unlinked_item_subfields($localitemmarc, $frameworkcode);
    
     return ModItem($item, $biblionumber, $itemnumber, $dbh, $frameworkcode, $unlinked_item_subfields); 
 }
@@ -1074,7 +1083,7 @@ END_SQL
         $query .= 'WHERE ';
         $query .= join ' AND ', @where_strings;
     }
-    $query .= ' ORDER BY itemcallnumber, title';
+    $query .= ' ORDER BY items.cn_sort, itemcallnumber, title';
     my $sth = $dbh->prepare($query);
     $sth->execute( @bind_params );
 
@@ -1262,11 +1271,12 @@ sub GetItemsInfo {
            items.notforloan as itemnotforloan,
            itemtypes.description
      FROM items
+     LEFT JOIN branches ON items.homebranch = branches.branchcode
      LEFT JOIN biblio      ON      biblio.biblionumber     = items.biblionumber
      LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
      LEFT JOIN itemtypes   ON   itemtypes.itemtype         = "
      . (C4::Context->preference('item-level_itypes') ? 'items.itype' : 'biblioitems.itemtype');
-    $query .= " WHERE items.biblionumber = ? ORDER BY items.dateaccessioned desc" ;
+    $query .= " WHERE items.biblionumber = ? ORDER BY branches.branchname,items.dateaccessioned desc" ;
     my $sth = $dbh->prepare($query);
     $sth->execute($biblionumber);
     my $i = 0;
@@ -2021,22 +2031,69 @@ MoveItemFromBiblio($itenumber, $frombiblio, $tobiblio);
 
 Moves an item from a biblio to another
 
+Returns undef if the move failed or the biblionumber of the destination record otherwise
 =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);
+    my $sth = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = ?");
+    $sth->execute( $tobiblio );
+    my ( $tobiblioitem ) = $sth->fetchrow();
+    $sth = $dbh->prepare("UPDATE items SET biblioitemnumber = ?, biblionumber = ? WHERE itemnumber = ? AND biblionumber = ?");
+    my $return = $sth->execute($tobiblioitem, $tobiblio, $itemnumber, $frombiblio);
     if ($return == 1) {
-       my $record = GetMarcBiblio($frombiblio);
+
+       # Getting framework
        my $frameworkcode = GetFrameworkCode($frombiblio);
-       ModBiblioMarc($record, $frombiblio, $frameworkcode);
 
-       $record = GetMarcBiblio($tobiblio);
-       $frameworkcode = GetFrameworkCode($frombiblio);
-       ModBiblioMarc($record, $tobiblio, $frameworkcode);
+       # Getting marc field for itemnumber
+       my ($itemtag, $itemsubfield) = GetMarcFromKohaField('items.itemnumber', $frameworkcode);
+
+       # Getting the record we want to move the item from
+       my $record = GetMarcBiblio($frombiblio);
+
+       # The item we want to move
+       my $item;
+
+       # For each item
+       foreach my $fielditem ($record->field($itemtag)){
+               # If it is the item we want to move
+               if ($fielditem->subfield($itemsubfield) == $itemnumber) {
+                   # We save it
+                   $item = $fielditem;
+                   # Then delete it from the record
+                   $record->delete_field($fielditem) 
+               }
+       }
+
+       # If we found an item (should always true, except in case of database-marcxml inconsistency)
+       if ($item) {
+
+           # Checking if the item we want to move is in an order 
+           my $order = GetOrderFromItemnumber($itemnumber);
+           if ($order) {
+               # Replacing the biblionumber within the order if necessary
+               $order->{'biblionumber'} = $tobiblio;
+               ModOrder($order);
+           }
+
+           # Saving the modification
+           ModBiblioMarc($record, $frombiblio, $frameworkcode);
+
+           # Getting the record we want to move the item to
+           $record = GetMarcBiblio($tobiblio);
+
+           # Inserting the previously saved item
+           $record->insert_fields_ordered($item);      
+
+           # Saving the modification
+           ModBiblioMarc($record, $tobiblio, $frameworkcode);
+
+       } else {
+           return undef;
+       }
     } else {
-       return -1;
+       return undef;
     }
 }