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