Bug 12759: Use a list (shelf) for batch record modification and deletion
[koha.git] / tools / batch_delete_records.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2013 BibLibre
6 #
7 # Koha is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 3
10 # of the License, or (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General
18 # Public License along with Koha; if not, see
19 # <http://www.gnu.org/licenses>
20
21 use Modern::Perl;
22
23 use CGI;
24 use List::MoreUtils qw( uniq );
25
26 use C4::Auth;
27 use C4::Output;
28 use C4::AuthoritiesMarc;
29 use C4::Biblio;
30 use Koha::Virtualshelves;
31
32 use Koha::Authorities;
33 use Koha::Biblios;
34 use Koha::Items;
35
36 my $input = new CGI;
37 my $op = $input->param('op') // q|form|;
38 my $recordtype = $input->param('recordtype') // 'biblio';
39
40 my ($template, $loggedinuser, $cookie) = get_template_and_user({
41         template_name => 'tools/batch_delete_records.tt',
42         query => $input,
43         type => "intranet",
44         authnotrequired => 0,
45         flagsrequired => { tools => 'records_batchdel' },
46 });
47
48 my @lists = Koha::Virtualshelves->search({});
49 $template->param( lists => \@lists );
50
51 my @records;
52 my @messages;
53 if ( $op eq 'form' ) {
54     # Display the form
55     $template->param( op => 'form' );
56 } elsif ( $op eq 'list' ) {
57     # List all records to process
58     my @record_ids;
59     if ( my $bib_list = $input->param('bib_list') ) {
60         # Come from the basket
61         @record_ids = split /\//, $bib_list;
62         $recordtype = 'biblio';
63     } elsif ( my $uploadfile = $input->param('uploadfile') ) {
64         # A file of id is given
65         binmode $uploadfile, ':encoding(UTF-8)';
66         while ( my $content = <$uploadfile> ) {
67             next unless $content;
68             $content =~ s/[\r\n]*$//;
69             push @record_ids, $content if $content;
70         }
71     } elsif ( my $shelf_number = $input->param('shelf_number') ) {
72         my $shelf = Koha::Virtualshelves->find($shelf_number);
73         my $contents = $shelf->get_contents;
74         while ( my $content = $contents->next ) {
75             my $biblionumber = $content->biblionumber;
76             push @record_ids, $biblionumber;
77         }
78     } else {
79         # The user enters manually the list of id
80         push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
81     }
82
83     for my $record_id ( uniq @record_ids ) {
84         if ( $recordtype eq 'biblio' ) {
85             # Retrieve biblio information
86             my $biblio = Koha::Biblios->find( $record_id );
87             unless ( $biblio ) {
88                 push @messages, {
89                     type => 'warning',
90                     code => 'biblio_not_exists',
91                     biblionumber => $record_id,
92                 };
93                 next;
94             }
95             my $holds_count = $biblio->holds->count;
96             $biblio = $biblio->unblessed;
97             my $record = &GetMarcBiblio({ biblionumber => $record_id });
98             $biblio->{subtitle} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $record_id ) );
99             $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
100             $biblio->{holds_count} = $holds_count;
101             $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
102             push @records, $biblio;
103         } else {
104             # Retrieve authority information
105             my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
106             unless ( $authority ) {
107                 push @messages, {
108                     type => 'warning',
109                     code => 'authority_not_exists',
110                     authid => $record_id,
111                 };
112                 next;
113             }
114
115             $authority = {
116                 authid => $record_id,
117                 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
118                 count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
119             };
120             push @records, $authority;
121         }
122     }
123     $template->param(
124         records => \@records,
125         op => 'list',
126     );
127 } elsif ( $op eq 'delete' ) {
128     # We want to delete selected records!
129     my @record_ids = $input->multi_param('record_id');
130     my $schema = Koha::Database->new->schema;
131
132     my $error;
133     my $report = {
134         total_records => 0,
135         total_success => 0,
136     };
137     RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
138         $report->{total_records}++;
139         next unless $record_id;
140         $schema->storage->txn_begin;
141
142         if ( $recordtype eq 'biblio' ) {
143             # Biblios
144             my $biblionumber = $record_id;
145             # First, checking if issues exist.
146             # If yes, nothing to do
147             my $biblio = Koha::Biblios->find( $biblionumber );
148
149             # TODO Replace with $biblio->get_issues->count
150             if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) {
151                 push @messages, {
152                     type => 'warning',
153                     code => 'item_issued',
154                     biblionumber => $biblionumber,
155                 };
156                 $schema->storage->txn_rollback;
157                 next;
158             }
159
160             # Cancel reserves
161             my $holds = $biblio->holds;
162             while ( my $hold = $holds->next ) {
163                 eval{
164                     $hold->cancel;
165                 };
166                 if ( $@ ) {
167                     push @messages, {
168                         type => 'error',
169                         code => 'reserve_not_cancelled',
170                         biblionumber => $biblionumber,
171                         reserve_id => $hold->reserve_id,
172                         error => $@,
173                     };
174                     $schema->storage->txn_rollback;
175                     next RECORD_IDS;
176                 }
177             }
178
179             # Delete items
180             my @itemnumbers = Koha::Items->search({ biblionumber => $biblionumber })->get_column('itemnumber');
181             ITEMNUMBER: for my $itemnumber ( @itemnumbers ) {
182                 my $error = eval { C4::Items::DelItemCheck( $biblionumber, $itemnumber ) };
183                 if ( $error != 1 or $@ ) {
184                     push @messages, {
185                         type => 'error',
186                         code => 'item_not_deleted',
187                         biblionumber => $biblionumber,
188                         itemnumber => $itemnumber,
189                         error => ($@ ? $@ : $error),
190                     };
191                     $schema->storage->txn_rollback;
192                     next RECORD_IDS;
193                 }
194             }
195
196             # Finally, delete the biblio
197             my $error = eval {
198                 C4::Biblio::DelBiblio( $biblionumber );
199             };
200             if ( $error or $@ ) {
201                 push @messages, {
202                     type => 'error',
203                     code => 'biblio_not_deleted',
204                     biblionumber => $biblionumber,
205                     error => ($@ ? $@ : $error),
206                 };
207                 $schema->storage->txn_rollback;
208                 next;
209             }
210
211             push @messages, {
212                 type => 'success',
213                 code => 'biblio_deleted',
214                 biblionumber => $biblionumber,
215             };
216             $report->{total_success}++;
217             $schema->storage->txn_commit;
218         } else {
219             # Authorities
220             my $authid = $record_id;
221             eval { C4::AuthoritiesMarc::DelAuthority({ authid => $authid }) };
222             if ( $@ ) {
223                 push @messages, {
224                     type => 'error',
225                     code => 'authority_not_deleted',
226                     authid => $authid,
227                     error => ($@ ? $@ : 0),
228                 };
229                 $schema->storage->txn_rollback;
230                 next;
231             } else {
232                 push @messages, {
233                     type => 'success',
234                     code => 'authority_deleted',
235                     authid => $authid,
236                 };
237                 $report->{total_success}++;
238                 $schema->storage->txn_commit;
239             }
240         }
241     }
242     $template->param(
243         op => 'report',
244         report => $report,
245     );
246 }
247
248 $template->param(
249     messages => \@messages,
250     recordtype => $recordtype,
251 );
252
253 output_html_with_http_headers $input, $cookie, $template->output;