Merge remote-tracking branch 'origin/new/bug_8062'
[koha.git] / misc / maintenance / UNIMARC_sync_date_created_with_marc_biblio.pl
1 #!/usr/bin/perl
2 #
3 # This script should be used only with UNIMARC flavour
4 # It is designed to report some missing information from biblio
5 # table into  marc data
6 #
7 use strict;
8 use warnings;
9
10 BEGIN {
11     use FindBin;
12     eval { require "$FindBin::Bin/../kohalib.pl" };
13 }
14
15 use C4::Biblio;
16
17 sub updateMarc {
18     my $id = shift;
19     my $dbh = C4::Context->dbh;
20     my $field;
21     my $biblio = GetMarcBiblio($id);
22
23     return unless $biblio;
24
25     if(!$biblio->field('099'))
26     {
27         $field = new MARC::Field('099','','',
28                     'c' => '',
29                     'd'=>'');
30         $biblio->add_fields($field);
31     }
32
33     $field = $biblio->field('099');
34
35     my $sth = $dbh->prepare("SELECT DATE_FORMAT(datecreated,'%Y-%m-%d') as datecreated,
36                                     DATE_FORMAT(timestamp,'%Y-%m-%d') as timestamp,
37                                     frameworkcode
38                                     FROM biblio
39                                     WHERE biblionumber = ?");
40     $sth->execute($id);
41     (my $bibliorow = $sth->fetchrow_hashref);
42     my $frameworkcode = $bibliorow->{'frameworkcode'};
43
44     $field->update( 'c' => $bibliorow->{'datecreated'},
45                     'd' => $bibliorow->{'timestamp'}
46                     );
47
48      if(&ModBiblio($biblio, $id, $frameworkcode))
49      {
50         print "\r$id";
51      }
52
53 }
54
55 sub process {
56
57     my $dbh = C4::Context->dbh;
58
59     my $sth = $dbh->prepare("SELECT biblionumber FROM biblio");
60     $sth->execute();
61
62     while(my $biblios = $sth->fetchrow_hashref)
63     {
64         updateMarc($biblios->{'biblionumber'});
65         print ".";
66     }
67
68 }
69
70 if (lc(C4::Context->preference('marcflavour')) eq "unimarc"){
71 process();
72
73 else {
74         print "this script is UNIMARC only and should be used only on unimarc databases\n";
75 }