1st draft for z3950 client import.
[koha.git] / C4 / Breeding.pm
1 package C4::Breeding;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use C4::Biblio;
22 use MARC::File::USMARC;
23 require Exporter;
24
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
26
27 # set the version for version checking
28 $VERSION = 0.01;
29
30 =head1 NAME
31
32 C4::Breeding : script to add a biblio in marc_breeding table.
33
34 =head1 SYNOPSIS
35
36   use C4::Scan;
37   &ImportBreeding($marcrecords,$);
38
39 =head1 DESCRIPTION
40
41 This module doesn't do anything.
42
43 =cut
44
45 @ISA = qw(Exporter);
46 @EXPORT = qw(&ImportBreeding);
47
48 sub  ImportBreeding {
49         my ($marcrecords,$overwrite_biblio,$filename) = @_;
50         my @marcarray = split /\x1D/, $marcrecords;
51         my $dbh = C4::Context->dbh;
52         my $searchisbn = $dbh->prepare("select biblioitemnumber from biblioitems where isbn=?");
53         my $searchissn = $dbh->prepare("select biblioitemnumber from biblioitems where issn=?");
54         my $searchbreeding = $dbh->prepare("select id from marc_breeding where isbn=?");
55         my $insertsql = $dbh->prepare("insert into marc_breeding (file,isbn,title,author,marc) values(?,?,?,?,?)");
56         my $replacesql = $dbh->prepare("update marc_breeding set file=?,isbn=?,title=?,author=?,marc=? where id=?");
57         # fields used for import results
58         my $imported=0;
59         my $alreadyindb = 0;
60         my $alreadyinfarm = 0;
61         my $notmarcrecord = 0;
62         for (my $i=0;$i<=$#marcarray;$i++) {
63                 my $marcrecord = MARC::File::USMARC::decode($marcarray[$i]."\x1D");
64                 if (ref($marcrecord) eq undef) {
65                         $notmarcrecord++;
66                 } else {
67                         my $oldbiblio = MARCmarc2koha($dbh,$marcrecord);
68                         $oldbiblio->{title} = char_decode($oldbiblio->{title});
69                         $oldbiblio->{author} = char_decode($oldbiblio->{author});
70                         # if isbn found and biblio does not exist, add it. If isbn found and biblio exists, overwrite or ignore depending on user choice
71                         # drop every "special" char : spaces, - ...
72                         $oldbiblio->{isbn} =~ s/ |-|\.//g,
73                         # search if biblio exists
74                         my $biblioitemnumber;
75                         if ($oldbiblio->{isbn}) {
76                                 $searchisbn->execute($oldbiblio->{isbn});
77                                 ($biblioitemnumber) = $searchisbn->fetchrow;
78                         } else {
79                                 $searchissn->execute($oldbiblio->{issn});
80                                 ($biblioitemnumber) = $searchissn->fetchrow;
81                         }
82                         if ($biblioitemnumber) {
83                                 $alreadyindb++;
84                         } else {
85                                 # search in breeding farm
86                                 my $breedingid;
87                                 if ($oldbiblio->{isbn}) {
88                                         $searchbreeding->execute($oldbiblio->{isbn});
89                                         ($breedingid) = $searchbreeding->fetchrow;
90                                 } else {
91                                         $searchbreeding->execute($oldbiblio->{issn});
92                                         ($breedingid) = $searchbreeding->fetchrow;
93                                 }
94                                 if (!$breedingid || $overwrite_biblio) {
95                                         my $recoded;
96                                         $recoded = $marcrecord->as_usmarc();
97                                                 if ($breedingid) {
98                                                         $replacesql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,10),$oldbiblio->{title},$oldbiblio->{author},$recoded,$breedingid);
99                                                 } else {
100                                                         $insertsql ->execute($filename,substr($oldbiblio->{isbn}.$oldbiblio->{issn},0,10),$oldbiblio->{title},$oldbiblio->{author},$recoded);
101                                                 }
102                                         $imported++;
103                                 } else {
104                                         $alreadyinfarm++;
105                                 }
106                         }
107                 }
108         }
109         return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported);
110 }
111
112 END { }       # module clean-up code here (global destructor)