Bug 22008: Fixes to make unit tests pass
[koha.git] / tools / batch_delete_records.pl
blob34c6b5a8ac8a2bcd917d0514116905128a73b245
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright 2013 BibLibre
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.
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.
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>
21 use Modern::Perl;
23 use CGI;
24 use List::MoreUtils qw( uniq );
26 use C4::Auth;
27 use C4::Output;
28 use C4::AuthoritiesMarc;
29 use C4::Biblio;
30 use Koha::Virtualshelves;
32 use Koha::Authorities;
33 use Koha::Biblios;
34 use Koha::Items;
36 my $input = new CGI;
37 my $op = $input->param('op') // q|form|;
38 my $recordtype = $input->param('recordtype') // 'biblio';
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 });
48 $template->param( lists => scalar Koha::Virtualshelves->search([{ category => 1, owner => $loggedinuser }, { category => 2 }]) );
50 my @records;
51 my @messages;
52 if ( $op eq 'form' ) {
53 # Display the form
54 $template->param( op => 'form' );
55 } elsif ( $op eq 'list' ) {
56 # List all records to process
57 my @record_ids;
58 if ( my $bib_list = $input->param('bib_list') ) {
59 # Come from the basket
60 @record_ids = split /\//, $bib_list;
61 $recordtype = 'biblio';
62 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
63 # A file of id is given
64 binmode $uploadfile, ':encoding(UTF-8)';
65 while ( my $content = <$uploadfile> ) {
66 next unless $content;
67 $content =~ s/[\r\n]*$//;
68 push @record_ids, $content if $content;
70 } elsif ( my $shelf_number = $input->param('shelf_number') ) {
71 my $shelf = Koha::Virtualshelves->find($shelf_number);
72 my $contents = $shelf->get_contents;
73 while ( my $content = $contents->next ) {
74 my $biblionumber = $content->biblionumber;
75 push @record_ids, $biblionumber;
77 } else {
78 # The user enters manually the list of id
79 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
82 for my $record_id ( uniq @record_ids ) {
83 if ( $recordtype eq 'biblio' ) {
84 # Retrieve biblio information
85 my $biblio = Koha::Biblios->find( $record_id );
86 unless ( $biblio ) {
87 push @messages, {
88 type => 'warning',
89 code => 'biblio_not_exists',
90 biblionumber => $record_id,
92 next;
94 my $holds_count = $biblio->holds->count;
95 $biblio = $biblio->unblessed;
96 my $record = &GetMarcBiblio({ biblionumber => $record_id });
97 $biblio->{subtitle} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $record_id ) );
98 $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
99 $biblio->{holds_count} = $holds_count;
100 $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
101 push @records, $biblio;
102 } else {
103 # Retrieve authority information
104 my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
105 unless ( $authority ) {
106 push @messages, {
107 type => 'warning',
108 code => 'authority_not_exists',
109 authid => $record_id,
111 next;
114 $authority = {
115 authid => $record_id,
116 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
117 count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
119 push @records, $authority;
122 $template->param(
123 records => \@records,
124 op => 'list',
126 } elsif ( $op eq 'delete' ) {
127 # We want to delete selected records!
128 my @record_ids = $input->multi_param('record_id');
129 my $schema = Koha::Database->new->schema;
131 my $error;
132 my $report = {
133 total_records => 0,
134 total_success => 0,
136 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
137 $report->{total_records}++;
138 next unless $record_id;
139 $schema->storage->txn_begin;
141 if ( $recordtype eq 'biblio' ) {
142 # Biblios
143 my $biblionumber = $record_id;
144 # First, checking if issues exist.
145 # If yes, nothing to do
146 my $biblio = Koha::Biblios->find( $biblionumber );
148 # TODO Replace with $biblio->get_issues->count
149 if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) {
150 push @messages, {
151 type => 'warning',
152 code => 'item_issued',
153 biblionumber => $biblionumber,
155 $schema->storage->txn_rollback;
156 next;
159 # Cancel reserves
160 my $holds = $biblio->holds;
161 while ( my $hold = $holds->next ) {
162 eval{
163 $hold->cancel;
165 if ( $@ ) {
166 push @messages, {
167 type => 'error',
168 code => 'reserve_not_cancelled',
169 biblionumber => $biblionumber,
170 reserve_id => $hold->reserve_id,
171 error => $@,
173 $schema->storage->txn_rollback;
174 next RECORD_IDS;
178 # Delete items
179 my @itemnumbers = Koha::Items->search({ biblionumber => $biblionumber })->get_column('itemnumber');
180 ITEMNUMBER: for my $itemnumber ( @itemnumbers ) {
181 my $error = eval { C4::Items::DelItemCheck( $biblionumber, $itemnumber ) };
182 if ( $error != 1 or $@ ) {
183 push @messages, {
184 type => 'error',
185 code => 'item_not_deleted',
186 biblionumber => $biblionumber,
187 itemnumber => $itemnumber,
188 error => ($@ ? $@ : $error),
190 $schema->storage->txn_rollback;
191 next RECORD_IDS;
195 # Finally, delete the biblio
196 my $error = eval {
197 C4::Biblio::DelBiblio( $biblionumber );
199 if ( $error or $@ ) {
200 push @messages, {
201 type => 'error',
202 code => 'biblio_not_deleted',
203 biblionumber => $biblionumber,
204 error => ($@ ? $@ : $error),
206 $schema->storage->txn_rollback;
207 next;
210 push @messages, {
211 type => 'success',
212 code => 'biblio_deleted',
213 biblionumber => $biblionumber,
215 $report->{total_success}++;
216 $schema->storage->txn_commit;
217 } else {
218 # Authorities
219 my $authid = $record_id;
220 eval { C4::AuthoritiesMarc::DelAuthority({ authid => $authid }) };
221 if ( $@ ) {
222 push @messages, {
223 type => 'error',
224 code => 'authority_not_deleted',
225 authid => $authid,
226 error => ($@ ? $@ : 0),
228 $schema->storage->txn_rollback;
229 next;
230 } else {
231 push @messages, {
232 type => 'success',
233 code => 'authority_deleted',
234 authid => $authid,
236 $report->{total_success}++;
237 $schema->storage->txn_commit;
241 $template->param(
242 op => 'report',
243 report => $report,
247 $template->param(
248 messages => \@messages,
249 recordtype => $recordtype,
252 output_html_with_http_headers $input, $cookie, $template->output;