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

(-)a/misc/cronjobs/delete_items.pl (-11 / +9 lines)
Lines 23-29 use Pod::Usage qw( pod2usage ); Link Here
23
use Koha::Script -cron;
23
use Koha::Script -cron;
24
use C4::Biblio qw( DelBiblio );
24
use C4::Biblio qw( DelBiblio );
25
use C4::Context;
25
use C4::Context;
26
use Koha::Biblios;
27
use Koha::Items;
26
use Koha::Items;
28
27
29
my $dbh = C4::Context->dbh();
28
my $dbh = C4::Context->dbh();
Lines 44-50 my $OPTIONS = { Link Here
44
          , commit    => ''
43
          , commit    => ''
45
          , help      => ''
44
          , help      => ''
46
          , manual    => ''
45
          , manual    => ''
47
          , version   => ''
48
          , del_bibs  => ''
46
          , del_bibs  => ''
49
      }
47
      }
50
};
48
};
Lines 52-58 my $OPTIONS = { Link Here
52
GetOptions(
50
GetOptions(
53
      'where=s'    => $OPTIONS->{where}
51
      'where=s'    => $OPTIONS->{where}
54
    , 'v|verbose'  => sub { $OPTIONS->{flags}->{verbose}   = 1 }
52
    , 'v|verbose'  => sub { $OPTIONS->{flags}->{verbose}   = 1 }
55
    , 'V|version'  => sub { $OPTIONS->{flags}->{version}   = 1 }
56
    , 'h|help'     => sub { $OPTIONS->{flags}->{help}      = 1 }
53
    , 'h|help'     => sub { $OPTIONS->{flags}->{help}      = 1 }
57
    , 'm|manual'   => sub { $OPTIONS->{flags}->{manual}    = 1 }
54
    , 'm|manual'   => sub { $OPTIONS->{flags}->{manual}    = 1 }
58
    , 'c|commit'   => sub { $OPTIONS->{flags}->{commit}    = 1 } # aka DO-EET!
55
    , 'c|commit'   => sub { $OPTIONS->{flags}->{commit}    = 1 } # aka DO-EET!
Lines 76-81 if( $OPTIONS->{flags}->{del_bibs} ) { Link Here
76
    verbose "Deleting empty bib records!"
73
    verbose "Deleting empty bib records!"
77
}
74
}
78
75
76
print "Test run only! No data will be deleted.\n" unless $OPTIONS->{flags}->{commit};
77
my $deleted_string = $OPTIONS->{flags}->{commit} ? "Deleted" : "Would have deleted";
78
79
# FIXME Use Koha::Items instead
79
# FIXME Use Koha::Items instead
80
$GLOBAL->{sth}->{target_items} = $dbh->prepare( $query->{target_items} . $where_clause  );
80
$GLOBAL->{sth}->{target_items} = $dbh->prepare( $query->{target_items} . $where_clause  );
81
$GLOBAL->{sth}->{target_items}->execute();
81
$GLOBAL->{sth}->{target_items}->execute();
Lines 83-109 $GLOBAL->{sth}->{target_items}->execute(); Link Here
83
DELITEM: while ( my $item = $GLOBAL->{sth}->{target_items}->fetchrow_hashref() ) {
83
DELITEM: while ( my $item = $GLOBAL->{sth}->{target_items}->fetchrow_hashref() ) {
84
84
85
    my $item_object    = Koha::Items->find( $item->{itemnumber} );
85
    my $item_object    = Koha::Items->find( $item->{itemnumber} );
86
    my $bibs_object    = Koha::Biblios->find( $item_object->biblionumber );
86
    my $holdings_count = $item_object->biblio->items->count;
87
    my $holdings_count = $bibs_object->items->count;
88
    my $error;
87
    my $error;
89
88
90
    my $safe_to_delete = $item_object->safe_to_delete;
89
    my $safe_to_delete = $item_object->safe_to_delete;
91
    if ($safe_to_delete) {
90
    if ($safe_to_delete) {
92
        $item_object->safe_delete
91
        $item_object->safe_delete
93
            if $OPTIONS->{flags}->{commit};
92
            if $OPTIONS->{flags}->{commit};
94
        verbose "Deleting item '$item->{itemnumber}' (of $holdings_count)";
93
        verbose "$deleted_string item '$item->{itemnumber}' (of $holdings_count)";
95
94
96
        if ( $OPTIONS->{flags}->{del_bibs} && $holdings_count == 1 ) {    # aka DO-EET for empty bibs!
95
        if ( $OPTIONS->{flags}->{del_bibs} && $holdings_count == 1 ) {    # aka DO-EET for empty bibs!
97
            $error = &DelBiblio( $item->{biblionumber} );
96
            $error = &DelBiblio( $item->{biblionumber} ) if $OPTIONS->{flags}->{commit};
98
            if ($error) {
97
            if ($error) {
99
                verbose "Could not delete bib '$item->{biblionumber}': $error";
98
                verbose "Could not delete bib '$item->{biblionumber}': $error";
100
            } else {
99
            } else {
101
                verbose "Last item deleted - deleting bib '$item->{biblionumber}'";
100
                verbose "No items remaining. $deleted_string bibliographic record '$item->{biblionumber}'";
102
            }
101
            }
103
        }
102
        }
104
    } else {
103
    } else {
105
        verbose sprintf "Item '%s' (Barcode: '%s', Title: '%s') not deleted: %s",
104
        verbose sprintf "Item '%s' (Barcode: '%s', Title: '%s') cannot deleted: %s",
106
            $item->{itemnumber},
105
            $item_object->itemnumber,
107
            $item_object->barcode,
106
            $item_object->barcode,
108
            $item_object->biblio->title,
107
            $item_object->biblio->title,
109
            @{ $safe_to_delete->messages }[0]->message;
108
            @{ $safe_to_delete->messages }[0]->message;
110
- 

Return to bug 35654