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