View | Details | Raw Unified | Return to bug 35654
Collapse All | Expand All

(-)a/misc/cronjobs/delete_items.pl (-18 / +13 lines)
Lines 46-52 pod2usage( -verbose => 2 ) Link Here
46
pod2usage( -verbose => 1 )                                                            if $help;
46
pod2usage( -verbose => 1 )                                                            if $help;
47
pod2usage( -verbose => 1, -message => 'You must supply at least one --where option' ) if scalar @where == 0;
47
pod2usage( -verbose => 1, -message => 'You must supply at least one --where option' ) if scalar @where == 0;
48
48
49
my $where_clause = ' where ' . join( " and ", @where );
49
my $where_clause = join( " AND ", @where );
50
50
51
$verbose && say "Where statement: $where_clause";
51
$verbose && say "Where statement: $where_clause";
52
if ($delete_biblios) {
52
if ($delete_biblios) {
Lines 56-92 if ($delete_biblios) { Link Here
56
print "Test run only! No data will be deleted.\n" unless $commit;
56
print "Test run only! No data will be deleted.\n" unless $commit;
57
my $deleted_string = $commit ? "Deleted" : "Would have deleted";
57
my $deleted_string = $commit ? "Deleted" : "Would have deleted";
58
58
59
# FIXME Use Koha::Items instead
59
my $items = Koha::Items->search( \$where_clause );
60
my $query = "SELECT itemnumber, biblionumber from items ";
61
my $sth   = $dbh->prepare( $query . $where_clause );
62
$sth->execute();
63
60
64
DELITEM: while ( my $item = $sth->fetchrow_hashref() ) {
61
DELITEM: while ( my $item = $items->next ){ #$sth->fetchrow_hashref() ) {
65
62
66
    my $item_object    = Koha::Items->find( $item->{itemnumber} );
63
    my $holdings_count = $item->biblio->items->count;
67
    my $holdings_count = $item_object->biblio->items->count;
68
    my $error;
64
    my $error;
69
65
70
    my $safe_to_delete = $item_object->safe_to_delete;
66
    my $safe_to_delete = $item->safe_to_delete;
71
    if ($safe_to_delete) {
67
    if ($safe_to_delete) {
72
        $item_object->safe_delete
68
        $item->safe_delete
73
            if $commit;
69
            if $commit;
74
        $verbose && say "$deleted_string item '$item->{itemnumber}' (of $holdings_count)";
70
        $verbose && say "$deleted_string item " . $item->itemnumber . " ($holdings_count items remain on record)";
75
71
76
        if ( $delete_biblios && $holdings_count == 1 ) {    # aka DO-EET for empty bibs!
72
        if ( $delete_biblios && $holdings_count == 1 ) {    # aka DO-EET for empty bibs!
77
            $error = &DelBiblio( $item->{biblionumber} ) if $commit;
73
            $error = &DelBiblio( $item->biblionumber ) if $commit;
78
            if ($error) {
74
            if ($error) {
79
                $verbose && say "Could not delete bib '$item->{biblionumber}': $error";
75
                $verbose && say "Could not delete bib " . $item->biblionumber . ": $error";
80
            } else {
76
            } else {
81
                $verbose && say "No items remaining. $deleted_string bibliographic record '$item->{biblionumber}'";
77
                $verbose && say "No items remaining. $deleted_string bibliographic record " . $item->biblionumber;
82
            }
78
            }
83
        }
79
        }
84
    } else {
80
    } else {
85
        say sprintf(
81
        say sprintf(
86
            "Item '%s' (Barcode: '%s', Title: '%s') cannot deleted: %s",
82
            "Item '%s' (Barcode: '%s', Title: '%s') cannot deleted: %s",
87
            $item_object->itemnumber,
83
            $item->itemnumber,
88
            $item_object->barcode,
84
            $item->barcode,
89
            $item_object->biblio->title,
85
            $item->biblio->title,
90
            @{ $safe_to_delete->messages }[0]->message
86
            @{ $safe_to_delete->messages }[0]->message
91
        ) if $verbose;
87
        ) if $verbose;
92
    }
88
    }
93
- 

Return to bug 35654