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