Bug 8603: Patron card creator - 'Barcode Type' doesn't stick in layouts
[koha.git] / misc / cronjobs / delete_records_via_leader.pl
1 #!/usr/bin/perl
2
3 #-----------------------------------
4 # Copyright 2013 ByWater Solutions
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 3 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #-----------------------------------
21
22 use Modern::Perl;
23
24 binmode( STDOUT, ":encoding(UTF-8)" );
25
26 BEGIN {
27
28     # find Koha's Perl modules
29     # test carefully before changing this
30     use FindBin;
31     eval { require "$FindBin::Bin/../kohalib.pl" };
32 }
33
34 use Getopt::Long;
35
36 use C4::Biblio;
37 use C4::Items;
38 use Koha::Database;
39 use Koha::Biblios;
40 use Koha::Biblio::Metadatas;
41
42 my $delete_items;
43 my $confirm;
44 my $test;
45 my $verbose;
46 my $help;
47
48 GetOptions(
49     'i|di|delete-items' => \$delete_items,
50     'c|confirm'         => \$confirm,
51     't|test'            => \$test,
52     'v|verbose'         => \$verbose,
53     'h|help'            => \$help,
54 );
55
56 if ( $help || !$confirm ) {
57     say qq{
58 delete_records_via_leader.pl - Attempt to delete any MARC records where the leader character 5 equals 'd'
59 usage: delete_records_via_leader.pl --confirm --verbose [--test]
60 This script has the following parameters :
61     -h --help: Prints this message
62     -c --confirm: Script will do nothing without this parameter
63     -v --verbose: Be verbose
64     -t --test: Test mode, does not delete records.
65                Test mode cannot determine if a record/item will be deleted successfully,
66                it will only tell you what records and items the script will attempt to delete.
67     -i --delete-items: Try deleting items before deleting record.
68                        Records with items cannot be deleted.
69 };
70     exit();
71 }
72
73 my @metadatas =    # Should be replaced by a call to C4::Search on zebra index
74                    # Record-status when bug 15537 will be pushed
75   Koha::Biblio::Metadatas->search( { format => 'marcxml', marcflavour => C4::Context->preference('marcflavour'), metadata => { LIKE => '%<leader>_____d%' } } );
76
77 my $total_records_count   = @metadatas;
78 my $deleted_records_count = 0;
79 my $total_items_count     = 0;
80 my $deleted_items_count   = 0;
81
82 foreach my $m (@metadatas) {
83     my $biblionumber = $m->get_column('biblionumber');
84
85     say "RECORD: $biblionumber" if $verbose;
86
87     if ($delete_items) {
88         my $deleted_count = 0;
89         my $biblio = Koha::Biblios->find( $biblionumber );
90         my @items = $biblio ? $biblio->items : ();
91         foreach my $item ( @items ) {
92             my $itemnumber = $item->itemnumber();
93
94             my $error = $test ? "Test mode enabled" : DelItemCheck( $biblionumber, $itemnumber );
95             $error = undef if $error eq '1';
96
97             if ($error) {
98                 say "ERROR DELETING ITEM $itemnumber: $error";
99             }
100             else {
101                 say "DELETED ITEM $itemnumber" if $verbose;
102                 $deleted_items_count++;
103             }
104
105             $total_items_count++;
106         }
107
108     }
109
110     my $error = $test ? q{Test mode enabled} : DelBiblio($biblionumber);
111     if ( $error ) {
112         say "ERROR DELETING BIBLIO $biblionumber: $error";
113     } else {
114         say "DELETED BIBLIO $biblionumber" if $verbose;
115         $deleted_records_count++;
116     }
117
118     say q{};
119 }
120
121 if ( $verbose ) {
122     say "DELETED $deleted_records_count OF $total_records_count RECORDS";
123     say "DELETED $deleted_items_count OF $total_items_count ITEMS" if $delete_items;
124 }