Bug 20076: (RM follow-up) Fix system preference boolean logic
[koha.git] / misc / migration_tools / create_analytical_rel.pl
1 #!/usr/bin/perl
2
3 use strict;
4 #use warnings; FIXME - Bug 2505
5
6 use Koha::Script;
7 use C4::Context;
8 use C4::Biblio qw( GetMarcBiblio ModBiblio );
9 use Koha::Items;
10 use Getopt::Long qw( GetOptions );
11
12 $| = 1;
13
14 # command-line parameters
15 my $want_help = 0;
16 my $do_update = 0;
17 my $wherestrings;
18
19 my $result = GetOptions(
20     'run-update'    => \$do_update,
21     'where=s@'         => \$wherestrings,
22     'h|help'        => \$want_help,
23 );
24
25 if (not $result or $want_help or not $do_update) {
26     print_usage();
27     exit 0;
28 }
29
30 my $num_bibs_processed     = 0;
31 my $num_bibs_modified      = 0;
32 my $num_noitem_forbarcode = 0;
33 my $num_nobarcode_inhostfield =0;
34 my $num_hostfields_unabletomodify =0;
35 my $num_bad_bibs           = 0;
36 my $dbh = C4::Context->dbh;
37 $dbh->{AutoCommit} = 0;
38
39 process_bibs();
40 $dbh->commit();
41
42 exit 0;
43
44 sub process_bibs {
45     my $sql = "SELECT biblionumber FROM biblio JOIN biblioitems USING (biblionumber)";
46     $sql.="WHERE ". join(" AND ",@$wherestrings) if ($wherestrings);
47     $sql.="ORDER BY biblionumber ASC";
48     my $sth = $dbh->prepare($sql);
49     eval{$sth->execute();};
50     if ($@){ die "error $@";};
51     while (my ($biblionumber) = $sth->fetchrow_array()) {
52         $num_bibs_processed++;
53         process_bib($biblionumber);
54
55         if (($num_bibs_processed % 100) == 0) {
56             print_progress_and_commit($num_bibs_processed);
57         }
58     }
59
60     $dbh->commit;
61
62     print <<_SUMMARY_;
63
64 Create Analytical records relationships report
65 -----------------------------------------------
66 Number of bibs checked:                   $num_bibs_processed
67 Number of bibs modified:                  $num_bibs_modified
68 Number of hostfields with no barcodes:          $num_nobarcode_inhostfield
69 Number of barcodes not found:                   $num_noitem_forbarcode
70 Number of hostfields unable to modify:          $num_hostfields_unabletomodify
71 Number of bibs with errors:               $num_bad_bibs
72 _SUMMARY_
73 }
74
75 sub process_bib {
76     my $biblionumber = shift;
77
78     my $bib = GetMarcBiblio({ biblionumber => $biblionumber });
79     unless (defined $bib) {
80         print "\nCould not retrieve bib $biblionumber from the database - record is corrupt.\n";
81         $num_bad_bibs++;
82         return;
83     }
84         #loop through each host field and populate subfield 0 and 9
85     my $analyticfield = '773';
86         foreach my $hostfield ( $bib->field($analyticfield) ) {
87                 if(my $barcode = $hostfield->subfield('o')){
88             my $item = Koha::Items->find({ barcode => $barcode });
89             if ($item) {
90                 my $modif;
91                 if ( $hostfield->subfield('0') ne $biblionumber ) {
92                     $hostfield->update( '0', $biblionumber );
93                     $modif = 1;
94                 }
95                 if ( $hostfield->subfield('9') ne $item->itemnumber ) {
96                     $hostfield->update( '9', $item->itemnumber );
97                     $modif = 1;
98                 }
99                 if ($modif) {
100                     $num_bibs_modified++;
101                     my $modresult = ModBiblio( $bib, $biblionumber, '' );
102                     warn "Modifying biblio $biblionumber";
103                     if ( !$modresult ) {
104                         warn "Unable to modify biblio $biblionumber with update host field";
105                         $num_hostfields_unabletomodify++;
106                     }
107                 }
108             }
109             else {
110                 warn "No item record found for barcode $barcode";
111                 $num_noitem_forbarcode++;
112             }
113                 } else{
114                         warn "No barcode in host field for biblionumber $biblionumber";
115                         $num_nobarcode_inhostfield++;
116                 }
117         }
118 }
119
120 sub print_progress_and_commit {
121     my $recs = shift;
122     $dbh->commit();
123     print "... processed $recs records\n";
124 }
125
126 sub print_usage {
127     print <<_USAGE_;
128 $0: establish relationship to host items
129
130 Based on barcode in host field populates subfield 0 with host biblionumber and subfield 9 with host itemnumber.
131
132 Subfield 0 and 9 are used in Koha screns to display relationships between analytical records and host bibs and items.
133
134 NOT usable with UNIMARC data. You can use it only if you have tag 461 with also an items id (like barcode or item numbers). In UNIMARC this situation is very rare. If you have data coded in this way, send a mail to koha-dev mailing list and ask for the feature.
135
136 Parameters:
137     --run-update            run the synchronization
138     --where condition       selects the biblios on a criterium (Repeatable)
139     --help or -h            show this message.
140 _USAGE_
141 }