Bug 28835: Fix ability to pass list contents to batch record modification
[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 qw( get_template_and_user );
27 use C4::Output qw( output_html_with_http_headers );
28 use C4::Auth qw( get_template_and_user );
29 use C4::Biblio qw( GetMarcBiblio );
30 use C4::AuthoritiesMarc;
31 use Koha::Virtualshelves;
32
33 use Koha::Authorities;
34 use Koha::Biblios;
35 use Koha::Items;
36
37 my $input = CGI->new;
38 my $op = $input->param('op') // q|form|;
39 my $recordtype = $input->param('recordtype') // 'biblio';
40
41 my ($template, $loggedinuser, $cookie) = get_template_and_user({
42         template_name => 'tools/batch_delete_records.tt',
43         query => $input,
44         type => "intranet",
45         flagsrequired => { tools => 'records_batchdel' },
46 });
47
48 my @records;
49 my @messages;
50 if ( $op eq 'form' ) {
51     # Display the form
52     $template->param(
53         op => 'form',
54         lists => scalar Koha::Virtualshelves->search(
55             [
56                 { category => 1, owner => $loggedinuser },
57                 { category => 2 }
58             ]
59         )
60     );
61 } elsif ( $op eq 'list' ) {
62     # List all records to process
63     my @record_ids;
64     if ( my $bib_list = $input->param('bib_list') ) {
65         # Come from the basket
66         @record_ids = split /\//, $bib_list;
67         $recordtype = 'biblio';
68     } elsif ( my $uploadfile = $input->param('uploadfile') ) {
69         # A file of id is given
70         binmode $uploadfile, ':encoding(UTF-8)';
71         while ( my $content = <$uploadfile> ) {
72             next unless $content;
73             $content =~ s/[\r\n]*$//;
74             push @record_ids, $content if $content;
75         }
76     } elsif ( my $shelf_number = $input->param('shelf_number') ) {
77         my $shelf = Koha::Virtualshelves->find($shelf_number);
78         my $contents = $shelf->get_contents;
79         while ( my $content = $contents->next ) {
80             my $biblionumber = $content->biblionumber;
81             push @record_ids, $biblionumber;
82         }
83     } else {
84         # The user enters manually the list of id
85         push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
86     }
87
88     for my $record_id ( uniq @record_ids ) {
89         if ( $recordtype eq 'biblio' ) {
90             # Retrieve biblio information
91             my $biblio = Koha::Biblios->find( $record_id );
92             unless ( $biblio ) {
93                 push @messages, {
94                     type => 'warning',
95                     code => 'biblio_not_exists',
96                     biblionumber => $record_id,
97                 };
98                 next;
99             }
100             my $holds_count = $biblio->holds->count;
101             $biblio = $biblio->unblessed;
102             my $record = &GetMarcBiblio({ biblionumber => $record_id });
103             $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
104             $biblio->{holds_count} = $holds_count;
105             $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
106             push @records, $biblio;
107         } else {
108             # Retrieve authority information
109             my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
110             unless ( $authority ) {
111                 push @messages, {
112                     type => 'warning',
113                     code => 'authority_not_exists',
114                     authid => $record_id,
115                 };
116                 next;
117             }
118
119             $authority = {
120                 authid => $record_id,
121                 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
122                 count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
123             };
124             push @records, $authority;
125         }
126     }
127     $template->param(
128         records => \@records,
129         op => 'list',
130     );
131 } elsif ( $op eq 'delete' ) {
132     # We want to delete selected records!
133     my @record_ids = $input->multi_param('record_id');
134     my $schema = Koha::Database->new->schema;
135
136     my $error;
137     my $report = {
138         total_records => 0,
139         total_success => 0,
140     };
141     RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
142         $report->{total_records}++;
143         next unless $record_id;
144         $schema->storage->txn_begin;
145
146         if ( $recordtype eq 'biblio' ) {
147             # Biblios
148             my $biblionumber = $record_id;
149             # First, checking if issues exist.
150             # If yes, nothing to do
151             my $biblio = Koha::Biblios->find( $biblionumber );
152
153             # TODO Replace with $biblio->get_issues->count
154             if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) {
155                 push @messages, {
156                     type => 'warning',
157                     code => 'item_issued',
158                     biblionumber => $biblionumber,
159                 };
160                 $schema->storage->txn_rollback;
161                 next;
162             }
163
164             # Cancel reserves
165             my $holds = $biblio->holds;
166             while ( my $hold = $holds->next ) {
167                 eval{
168                     $hold->cancel;
169                 };
170                 if ( $@ ) {
171                     push @messages, {
172                         type => 'error',
173                         code => 'reserve_not_cancelled',
174                         biblionumber => $biblionumber,
175                         reserve_id => $hold->reserve_id,
176                         error => $@,
177                     };
178                     $schema->storage->txn_rollback;
179                     next RECORD_IDS;
180                 }
181             }
182
183             # Delete items
184             my $items = Koha::Items->search({ biblionumber => $biblionumber });
185             while ( my $item = $items->next ) {
186                 my $deleted_item = eval { $item->safe_delete };
187                 if ( !ref($deleted_item) or $@ ) {
188                     push @messages, {
189                         type => 'error',
190                         code => 'item_not_deleted',
191                         biblionumber => $biblionumber,
192                         itemnumber => $item->itemnumber,
193                         error => ($@ ? $@ : $error),
194                     };
195                     $schema->storage->txn_rollback;
196                     next RECORD_IDS;
197                 }
198             }
199
200             # Finally, delete the biblio
201             my $error = eval {
202                 C4::Biblio::DelBiblio( $biblionumber );
203             };
204             if ( $error or $@ ) {
205                 push @messages, {
206                     type => 'error',
207                     code => 'biblio_not_deleted',
208                     biblionumber => $biblionumber,
209                     error => ($@ ? $@ : $error),
210                 };
211                 $schema->storage->txn_rollback;
212                 next;
213             }
214
215             push @messages, {
216                 type => 'success',
217                 code => 'biblio_deleted',
218                 biblionumber => $biblionumber,
219             };
220             $report->{total_success}++;
221             $schema->storage->txn_commit;
222         } else {
223             # Authorities
224             my $authid = $record_id;
225             eval { C4::AuthoritiesMarc::DelAuthority({ authid => $authid }) };
226             if ( $@ ) {
227                 push @messages, {
228                     type => 'error',
229                     code => 'authority_not_deleted',
230                     authid => $authid,
231                     error => ($@ ? $@ : 0),
232                 };
233                 $schema->storage->txn_rollback;
234                 next;
235             } else {
236                 push @messages, {
237                     type => 'success',
238                     code => 'authority_deleted',
239                     authid => $authid,
240                 };
241                 $report->{total_success}++;
242                 $schema->storage->txn_commit;
243             }
244         }
245     }
246     $template->param(
247         op => 'report',
248         report => $report,
249     );
250 }
251
252 $template->param(
253     messages => \@messages,
254     recordtype => $recordtype,
255 );
256
257 output_html_with_http_headers $input, $cookie, $template->output;