Update MARC to add 099$c and 099$d fields, with :
[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     if(!$biblio->field('099'))
24     {
25         $field = new MARC::Field('099','','',
26                     'c' => '',
27                     'd'=>'');
28         $biblio->add_fields($field);
29     }
30
31     $field = $biblio->field('099');
32
33     my $sth = $dbh->prepare("SELECT DATE_FORMAT(datecreated,'%Y-%m-%d') as datecreated,
34                                     DATE_FORMAT(timestamp,'%Y-%m-%d') as timestamp,
35                                     frameworkcode
36                                     FROM biblio
37                                     WHERE biblionumber = ?");
38     $sth->execute($id);
39     (my $bibliorow = $sth->fetchrow_hashref);
40     my $frameworkcode = $bibliorow->{'frameworkcode'};
41
42     $field->update( 'c' => $bibliorow->{'datecreated'},
43                     'd' => $bibliorow->{'timestamp'}
44                     );
45
46      if(&ModBiblio($biblio, $id, $frameworkcode))
47      {
48         print "\r$id";
49      }
50
51 }
52
53 sub process {
54
55     my $dbh = C4::Context->dbh;
56
57     my $sth = $dbh->prepare("SELECT biblionumber FROM biblio");
58     $sth->execute();
59
60     while(my $biblios = $sth->fetchrow_hashref)
61     {
62         updateMarc($biblios->{'biblionumber'});
63         print ".";
64     }
65
66 }
67
68 if (lc(C4::Context->preference('marcflavour')) eq "unimarc"){
69 process();
70
71 else {
72         print "this script is UNIMARC only and should be used only on unimarc databases";
73 }