-=head2 ItemSafeToDelete
-
- ItemSafeToDelete( $biblionumber, $itemnumber);
-
-Exported function (core API) for checking whether an item record is safe to delete.
-
-returns 1 if the item is safe to delete,
-
-"book_on_loan" if the item is checked out,
-
-"not_same_branch" if the item is blocked by independent branches,
-
-"book_reserved" if the there are holds aganst the item, or
-
-"linked_analytics" if the item has linked analytic records.
-
-=cut
-
-sub ItemSafeToDelete {
- my ( $biblionumber, $itemnumber ) = @_;
- my $status;
- my $dbh = C4::Context->dbh;
-
- my $error;
-
- my $countanalytics = GetAnalyticsCount($itemnumber);
-
- my $item = Koha::Items->find($itemnumber) or return;
-
- if ($item->checkout) {
- $status = "book_on_loan";
- }
- elsif ( defined C4::Context->userenv
- and !C4::Context->IsSuperLibrarian()
- and C4::Context->preference("IndependentBranches")
- and ( C4::Context->userenv->{branch} ne $item->homebranch ) )
- {
- $status = "not_same_branch";
- }
- else {
- # check it doesn't have a waiting reserve
- my $sth = $dbh->prepare(
- q{
- SELECT COUNT(*) FROM reserves
- WHERE (found = 'W' OR found = 'T')
- AND itemnumber = ?
- }
- );
- $sth->execute($itemnumber);
- my ($reserve) = $sth->fetchrow;
- if ($reserve) {
- $status = "book_reserved";
- }
- elsif ( $countanalytics > 0 ) {
- $status = "linked_analytics";
- }
- else {
- $status = 1;
- }
- }
- return $status;
-}
-
-=head2 DelItemCheck
-
- DelItemCheck( $biblionumber, $itemnumber);
-
-Exported function (core API) for deleting an item record in Koha if there no current issue.
-
-DelItemCheck wraps ItemSafeToDelete around DelItem.
-
-=cut
-
-sub DelItemCheck {
- my ( $biblionumber, $itemnumber ) = @_;
- my $status = ItemSafeToDelete( $biblionumber, $itemnumber );
-
- if ( $status == 1 ) {
- DelItem(
- {
- biblionumber => $biblionumber,
- itemnumber => $itemnumber
- }
- );
- }
- return $status;
-}
-
-=head2 _koha_modify_item
-
- my ($itemnumber,$error) =_koha_modify_item( $item );
-
-Perform the actual update of the C<items> row. Note that this
-routine accepts a hashref specifying the columns to update.
-
-=cut
-
-sub _koha_modify_item {
- my ( $item ) = @_;
- my $dbh=C4::Context->dbh;
- my $error;
-
- my $query = "UPDATE items SET ";
- my @bind;
- _mod_item_dates( $item );
- for my $key ( keys %$item ) {
- next if ( $key eq 'itemnumber' );
- $query.="$key=?,";
- push @bind, $item->{$key};
- }
- $query =~ s/,$//;
- $query .= " WHERE itemnumber=?";
- push @bind, $item->{'itemnumber'};
- my $sth = $dbh->prepare($query);
- $sth->execute(@bind);
- if ( $sth->err ) {
- $error.="ERROR in _koha_modify_item $query: ".$sth->errstr;
- warn $error;
- }
- return ($item->{'itemnumber'},$error);
-}
-
-sub _mod_item_dates { # date formatting for date fields in item hash
- my ( $item ) = @_;
- return if !$item || ref($item) ne 'HASH';
-
- my @keys = grep
- { $_ =~ /^onloan$|^date|date$|datetime$/ }
- keys %$item;
- # Incl. dateaccessioned,replacementpricedate,datelastborrowed,datelastseen
- # NOTE: We do not (yet) have items fields ending with datetime
- # Fields with _on$ have been handled already
-
- foreach my $key ( @keys ) {
- next if !defined $item->{$key}; # skip undefs
- my $dt = eval { dt_from_string( $item->{$key} ) };
- # eval: dt_from_string will die on us if we pass illegal dates
-
- my $newstr;
- if( defined $dt && ref($dt) eq 'DateTime' ) {
- if( $key =~ /datetime/ ) {
- $newstr = DateTime::Format::MySQL->format_datetime($dt);
- } else {
- $newstr = DateTime::Format::MySQL->format_date($dt);
- }
- }
- $item->{$key} = $newstr; # might be undef to clear garbage
- }
-}
-
-=head2 _koha_delete_item
-
- _koha_delete_item( $itemnum );
-
-Internal function to delete an item record from the koha tables
-
-=cut
-
-sub _koha_delete_item {
- my ( $itemnum ) = @_;
-
- my $dbh = C4::Context->dbh;
- # save the deleted item to deleteditems table
- my $sth = $dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
- $sth->execute($itemnum);
- my $data = $sth->fetchrow_hashref();
-
- # There is no item to delete
- return 0 unless $data;
-
- my $query = "INSERT INTO deleteditems SET ";
- my @bind = ();
- foreach my $key ( keys %$data ) {
- next if ( $key eq 'timestamp' ); # timestamp will be set by db
- $query .= "$key = ?,";
- push( @bind, $data->{$key} );
- }
- $query =~ s/\,$//;
- $sth = $dbh->prepare($query);
- $sth->execute(@bind);
-
- # delete from items table
- $sth = $dbh->prepare("DELETE FROM items WHERE itemnumber=?");
- my $deleted = $sth->execute($itemnum);
- return ( $deleted == 1 ) ? 1 : 0;
-}
-