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