+=head2 MoveItemFromBiblio
+
+=over 4
+
+MoveItemFromBiblio($itenumber, $frombiblio, $tobiblio);
+
+=back
+
+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("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) {
+
+ # Getting framework
+ my $frameworkcode = GetFrameworkCode($frombiblio);
+
+ # 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 undef;
+ }
+}
+
+=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;
+}
+