Bug 14504: (QA followup)
[koha.git] / misc / cronjobs / delete_items.pl
1 #! /usr/bin/perl
2
3 use Getopt::Long;
4 use C4::Context;
5 use C4::Items;
6 use C4::Circulation;
7 use Modern::Perl;
8 use Pod::Usage;
9
10 my $dbh = C4::Context->dbh();
11
12 my $query = {
13     target_items => q|SELECT itemnumber, biblionumber from items|
14 };
15
16 my $GLOBAL = {
17       query => $query
18     , sth   => {}
19 };
20
21 my $OPTIONS = {
22       criteria => []
23     , flags    => {
24             verbose   => ''
25           , commit    => ''
26           , help      => ''
27           , manual    => ''
28           , version   => ''
29       }
30 };
31
32 GetOptions(
33       'criteria=s' => $OPTIONS->{criteria}
34     , 'v|verbose'  => sub { $OPTIONS->{flags}->{verbose}   = 1 }
35     , 'V|version'  => sub { $OPTIONS->{flags}->{version}   = 1 }
36     , 'h|help'     => sub { $OPTIONS->{flags}->{help}      = 1 }
37     , 'm|manual'   => sub { $OPTIONS->{flags}->{manual}    = 1 }
38     , 'c|commit'   => sub { $OPTIONS->{flags}->{commit}    = 1 } # aka DO-EET!
39 );
40
41 my @criteria = @{ $OPTIONS->{criteria} };
42
43 pod2usage( -verbose => 2 ) if  $OPTIONS->{flags}->{manual};
44 pod2usage( -verbose => 1 ) if  $OPTIONS->{flags}->{help};
45 pod2usage( -verbose => 1 -msg => 'You must supply at least one --criteria option' ) if  scalar @criteria == 0;
46
47 sub verbose {
48     say @_ if $OPTIONS->{flags}->{verbose};
49 }
50
51 my $where_clause = ' where ' . join ( " and ", @criteria );
52
53 verbose "Where statement: $where_clause";
54
55 $GLOBAL->{sth}->{target_items} = $dbh->prepare( $query->{target_items} . $where_clause  );
56 $GLOBAL->{sth}->{target_items}->execute();
57
58 DELITEM: while ( my $item = $GLOBAL->{sth}->{target_items}->fetchrow_hashref() ) {
59     my $issue = GetOpenIssue( $item->{itemnumber} );
60     my $holds = GetItemHolds( $item->{biblionumber}, $item->{itemnumber} );
61
62     if( defined $issue ) {
63         verbose "Cannot delete '$item->{itemnumber}' -- item is checked out.";
64         next DELITEM;
65     }
66
67     if( $holds ) {
68         verbose "Cannot delete '$item->{itemnumber}' -- item has open holds.";
69         next DELITEM;
70     }
71
72     verbose "Deleting '$item->{itemnumber}' ";
73     C4::Items::DelItem( { itemnumber => $item->{itemnumber} } ) if $OPTIONS->{flags}->{commit} ;
74 }
75
76 =head1 NAME
77
78 delete_items.pl - A batch item deletion tool, which generates a query against the items database and deletes the items matching the criteria specified in the command line arguments.
79
80 =head1 SYNOPSIS
81
82 delete_items.pl [--help|--manual]
83
84 delete_items.pl [--verbose] --criteria "I<SQL CONDITIONAL EXPRESSION>" ... [--commit]
85
86 =cut
87
88 =head1 OPTIONS
89
90 =over 8
91
92 =item B<--help>
93
94 Show the brief help information.
95
96 =item B<--manual>
97
98 Read the manual, with examples.
99
100 =item B<--verbose>
101
102 Send the "WHERE" clause generated by the collected C<--criteria>
103 arguments, as well as items affected to Standard Out.
104
105 =item B<--criteria>
106
107 The C<--criteria> option may called multiple times. The following argument
108 must be a syntactically valid SQL statement which is part of the C<WHERE>
109 clause querying the items table. These are joined by C<AND>.
110
111 =item B<--commit>
112
113 No items will be deleted unless the C<--commit> flag is present.
114
115 =back
116
117 =cut
118
119
120 =head1 EXAMPLES
121
122   The following is an example of this script:
123
124  delete_items.pl --criteria "items.withdrawn ! 0"  --criteria "items.withdrawn_on < $(date --date="13 month ago" --rfc-3339=date)" --commit
125
126  delete_items.pl --criteria "itemlost >= '1'" --criteria "itemlost <='4'" --criteria "itemlost_on < '2014-04-28'" --commit
127
128 =cut
129
130
131 =head1 DESCRIPTION
132
133  This is a lightweight batch deletion tool for items, suitable for running in a cron job.
134
135 =cut
136
137
138 =head1 AUTHOR
139
140  Barton Chittenden <barton@bywatersolutions.com>
141
142 =cut