Added skeletons of update routines.
[koha.git] / C4 / Catalogue.pm
1 package C4::Catalogue; #asummes C4/Acquisitions.pm
2
3 use strict;
4 require Exporter;
5 use C4::Database;
6
7 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
8
9 # set the version for version checking
10 $VERSION = 0.01;
11
12 @ISA = qw(Exporter);
13 @EXPORT = qw(&newBiblio &newBiblioItem &newItem &updateBiblio &updateBiblioItem
14              &updateItem);
15 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
16
17 # your exported package globals go here,
18 # as well as any optionally exported functions
19
20 @EXPORT_OK   = qw($Var1 %Hashit);
21
22
23 # non-exported package globals go here
24 use vars qw(@more $stuff);
25
26 # initalize package globals, first exported ones
27
28 my $Var1   = '';
29 my %Hashit = ();
30
31
32 # then the others (which are still accessible as $Some::Module::stuff)
33 my $stuff  = '';
34 my @more   = ();
35
36 # all file-scoped lexicals must be created before
37 # the functions below that use them.
38
39 # file-private lexicals go here
40 my $priv_var    = '';
41 my %secret_hash = ();
42
43 # here's a file-private function as a closure,
44 # callable as &$priv_func;  it cannot be prototyped.
45 my $priv_func = sub {
46   # stuff goes here.
47   };
48   
49 # make all your functions, whether exported or not;
50
51
52
53 sub newBiblio {
54     my ($env, $biblio) = @_;
55     my $title=$biblio->{'title'};
56     my $author=$biblio->{'author'};
57     my $subtitle=$biblio->{'subtitle'};
58     my $title=$biblio->{'title'};
59     my $copyrightdate=$biblio->{'copyrightdate'};
60     my $serial=$biblio->{'serial'};
61     my $seriestitle=$biblio->{'seriestitle'};
62     my $unititle=$biblio->{'unititle'};
63     my $notes=$biblio->{'notes'};
64 }
65
66 sub updateBiblio {
67 # Update the biblio with biblionumber $biblio->{'biblionumber'}
68 # I guess this routine should search through all marc records for a record that
69 # has the same biblionumber stored in it, and modify the MARC record as well as
70 # the biblio table.
71 #
72 # Also, this subroutine should search through the $biblio object and compare it
73 # to the existing record and _LOG ALL CHANGES MADE_ in some way.  I'd like for
74 # this logging feature to be usable to undo changes easily.
75     my ($env, $biblio) = @_;
76 }
77
78
79 sub addTag {
80     my ($env, $Record_ID, $tag, $Indicator1, $Indicator2, $subfields) = @_;
81     my $dbh=&C4Connect;  
82     ($Indicator1) || ($Indicator1=' ');
83     ($Indicator2) || ($Indicator2=' ');
84     my $firstdigit=substr($tag,0,1);
85     my $Subfield_ID;
86     foreach (sort keys %$subfields) {
87         my $Subfield_Mark=$subfields->{$_}->{'Subfield_Mark'};
88         my $Subfield_Value=$subfields->{$_}->{'Subfield_Value'};
89         my $q_Subfield_Value=$dbh->quote($Subfield_Value);
90         if ($Subfield_ID) {
91             my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_ID, Subfield_Mark, Subfield_Value) values ($Subfield_ID, '$Subfield_Mark', $q_Subfield_Value)");
92             $sth->execute;
93         } else {
94             my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_Mark, Subfield_Value) values ('$Subfield_Mark', $q_Subfield_Value)");
95             $sth->execute;
96             my $Subfield_Key=$dbh->{'mysql_insertid'};
97             $Subfield_ID=$Subfield_Key;
98             $sth=$dbh->prepare("update $firstdigit\XX_Subfield_Table set Subfield_ID=$Subfield_ID where Subfield_Key=$Subfield_Key");
99             $sth->execute;
100         }
101     }
102     if (my $linkid=$env->{'linkid'}) {
103         $env->{'linkage'}=0;
104         my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_ID, Subfield_Mark, Subfield_Value) values ($Subfield_ID, '8', '$linkid')");
105         $sth->execute;
106     }
107     my $sth=$dbh->prepare("insert into $firstdigit\XX_Tag_Table (Indicator1, Indicator2, Tag, Subfield_ID) values ('$Indicator1', '$Indicator2', '$tag', $Subfield_ID)");
108     $sth->execute;
109     my $Tag_Key=$dbh->{'mysql_insertid'};
110     my $Tag_ID=$Tag_Key;
111     $sth=$dbh->prepare("update $firstdigit\XX_Tag_Table set Tag_ID=$Tag_ID where Tag_Key=$Tag_Key");
112     $sth->execute;
113     $sth=$dbh->prepare("insert into Bib_Table (Record_ID, Tag_$firstdigit\XX_ID) values ($Record_ID, $Tag_ID)");
114     $sth->execute;
115     if ($env->{'linkage'}) {
116         my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_ID, Subfield_Mark, Subfield_Value) values ($Subfield_ID, '8', '$Tag_ID')");
117         $sth->execute;
118         
119     }
120     $sth->finish;
121     $dbh->disconnect;
122     return ($env, $Tag_ID);
123 }
124
125 sub newBiblioItem {
126     my ($env, $biblioitem) = @_;
127     my $dbh=&C4Connect;  
128     my $biblionumber=$biblioitem->{'biblionumber'};
129     my $volume=$biblioitem->{'volume'};
130     my $number=$biblioitem->{'number'};
131     my $classification=$biblioitem->{'classification'};
132     my $itemtype=$biblioitem->{'itemtype'};
133     my $isbn=$biblioitem->{'isbn'};
134     my $issn=$biblioitem->{'issn'};
135     my $dewey=$biblioitem->{'dewey'};
136     $dewey=~s/\.*0*$//;
137     ($dewey == 0) && ($dewey='');
138     my $subclass=$biblioitem->{'subclass'};
139     my $publicationyear=$biblioitem->{'publicationyear'};
140     my $publishercode=$biblioitem->{'publishercode'};
141     my $volumedate=$biblioitem->{'volumedate'};
142     my $illus=$biblioitem->{'illus'};
143     my $pages=$biblioitem->{'pages'};
144     my $notes=$biblioitem->{'notes'};
145     my $size=$biblioitem->{'size'};
146     my $place=$biblioitem->{'place'};
147     my $lccn=$biblioitem->{'lccn'};
148
149 # Should we check if there is already a biblioitem/marc with the
150 # same isbn/lccn/issn?
151
152     my $sth=$dbh->prepare("select title,unititle,seriestitle,copyrightdate,notes,author from biblio where biblionumber=$biblionumber");
153     $sth->execute;
154     my ($title, $unititle,$seriestitle,$copyrightdate,$biblionotes,$author) = $sth->fetchrow;
155     $sth=$dbh->prepare("select subtitle from bibliosubtitle where biblionumber=$biblionumber");
156     $sth->execute;
157     my ($subtitle) = $sth->fetchrow;
158     $sth=$dbh->prepare("select author from additionalauthors where biblionumber=$biblionumber");
159     $sth->execute;
160     my @additionalauthors;
161     while (my ($additionalauthor) = $sth->fetchrow) {
162         push (@additionalauthors, $additionalauthor);
163     }
164     $sth=$dbh->prepare("select subject from bibliosubject where biblionumber=$biblionumber");
165     $sth->execute;
166     my @subjects;
167     while (my ($subject) = $sth->fetchrow) {
168         push (@subjects, $subject);
169     }
170
171 # MARC SECTION
172
173     $sth=$dbh->prepare("insert into Resource_Table (Record_ID) values (0)");
174     $sth->execute;
175     my $Resource_ID=$dbh->{'mysql_insertid'};
176     my $Record_ID=$Resource_ID;
177     $sth=$dbh->prepare("update Resource_Table set Record_ID=$Record_ID where Resource_ID=$Resource_ID");
178     $sth->execute;
179
180 # Title
181     {
182         my $subfields;
183         $subfields->{1}->{'Subfield_Mark'}='a';
184         $subfields->{1}->{'Subfield_Value'}=$title;
185         if ($subtitle) {
186             $subfields->{2}->{'Subfield_Mark'}='b';
187             $subfields->{2}->{'Subfield_Value'}=$subtitle;
188         }
189         my $tag='245';
190         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
191     }
192
193 # author
194     {
195         my $subfields;
196         $subfields->{1}->{'Subfield_Mark'}='a';
197         $subfields->{1}->{'Subfield_Value'}=$author;
198         my $tag='100';
199         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
200     }
201 # Series Title
202     if ($seriestitle) {
203         my $subfields;
204         $subfields->{1}->{'Subfield_Mark'}='a';
205         $subfields->{1}->{'Subfield_Value'}=$seriestitle;
206         my $tag='440';
207         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
208     }
209 # Biblio Note
210     if ($biblionotes) {
211         my $subfields;
212         $subfields->{1}->{'Subfield_Mark'}='a';
213         $subfields->{1}->{'Subfield_Value'}=$biblionotes;
214         $subfields->{2}->{'Subfield_Mark'}='3';
215         $subfields->{2}->{'Subfield_Value'}='biblio';
216         my $tag='440';
217         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
218     }
219 # Additional Authors
220     foreach (@additionalauthors) {
221         my $author=$_;
222         (next) unless ($author);
223         my $subfields;
224         $subfields->{1}->{'Subfield_Mark'}='a';
225         $subfields->{1}->{'Subfield_Value'}=$author;
226         $subfields->{2}->{'Subfield_Mark'}='e';
227         $subfields->{2}->{'Subfield_Value'}='author';
228         my $tag='700';
229         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
230     }
231 # Illustrator
232     if ($illus) {
233         (next) unless ($illus);
234         my $subfields;
235         $subfields->{1}->{'Subfield_Mark'}='a';
236         $subfields->{1}->{'Subfield_Value'}=$illus;
237         $subfields->{2}->{'Subfield_Mark'}='e';
238         $subfields->{2}->{'Subfield_Value'}='illustrator';
239         my $tag='700';
240         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
241     }
242 # Subjects
243     foreach (@subjects) {
244         my $subject=$_;
245         (next) unless ($subject);
246         my $subfields;
247         $subfields->{1}->{'Subfield_Mark'}='a';
248         $subfields->{1}->{'Subfield_Value'}=$subject;
249         my $tag='650';
250         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
251     }
252
253
254 # ISBN
255     if ($isbn) {
256         my $subfields;
257         $subfields->{1}->{'Subfield_Mark'}='a';
258         $subfields->{1}->{'Subfield_Value'}=$isbn;
259         my $tag='020';
260         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
261     }
262 # LCCN
263     if ($lccn) {
264         my $subfields;
265         $subfields->{1}->{'Subfield_Mark'}='a';
266         $subfields->{1}->{'Subfield_Value'}=$lccn;
267         my $tag='010';
268         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
269     }
270 # ISSN
271     if ($issn) {
272         my $subfields;
273         $subfields->{1}->{'Subfield_Mark'}='a';
274         $subfields->{1}->{'Subfield_Value'}=$issn;
275         my $tag='022';
276         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
277     }
278 # DEWEY
279     if ($dewey) {
280         my $subfields;
281         $subfields->{1}->{'Subfield_Mark'}='a';
282         $subfields->{1}->{'Subfield_Value'}=$dewey;
283         my $tag='082';
284         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
285     }
286 # DEWEY subclass and itemtype
287     {
288         my $subfields;
289         $subfields->{1}->{'Subfield_Mark'}='a';
290         $subfields->{1}->{'Subfield_Value'}=$itemtype;
291         $subfields->{2}->{'Subfield_Mark'}='b';
292         $subfields->{2}->{'Subfield_Value'}=$subclass;
293         my $tag='090';
294         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
295     }
296 # PUBLISHER
297     {
298         my $subfields;
299         $subfields->{1}->{'Subfield_Mark'}='a';
300         $subfields->{1}->{'Subfield_Value'}=$place;
301         $subfields->{2}->{'Subfield_Mark'}='b';
302         $subfields->{2}->{'Subfield_Value'}=$publishercode;
303         $subfields->{3}->{'Subfield_Mark'}='c';
304         $subfields->{3}->{'Subfield_Value'}=$publicationyear;
305         if ($copyrightdate) {
306             $subfields->{4}->{'Subfield_Mark'}='c';
307             $subfields->{4}->{'Subfield_Value'}="c$copyrightdate";
308         }
309         my $tag='260';
310         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
311     }
312 # PHYSICAL
313     if ($pages || $size) {
314         my $subfields;
315         $subfields->{1}->{'Subfield_Mark'}='a';
316         $subfields->{1}->{'Subfield_Value'}=$pages;
317         $subfields->{2}->{'Subfield_Mark'}='c';
318         $subfields->{2}->{'Subfield_Value'}=$size;
319         my $tag='300';
320         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
321     }
322 # Volume/Number
323     if ($volume || $number) {
324         my $subfields;
325         $subfields->{1}->{'Subfield_Mark'}='v';
326         $subfields->{1}->{'Subfield_Value'}=$volume;
327         $subfields->{2}->{'Subfield_Mark'}='n';
328         $subfields->{2}->{'Subfield_Value'}=$number;
329         my $tag='440';
330         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
331     }
332 # Biblioitem Note
333     if ($notes) {
334         my $subfields;
335         $subfields->{1}->{'Subfield_Mark'}='a';
336         $subfields->{1}->{'Subfield_Value'}=$notes;
337         $subfields->{2}->{'Subfield_Mark'}='3';
338         $subfields->{2}->{'Subfield_Value'}='biblioitem';
339         my $tag='500';
340         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
341     }
342     $sth->finish;
343     $dbh->disconnect;
344     return ($env, $Record_ID);
345 }
346
347 sub updateBiblioItem {
348 # Update the biblioitem with biblioitemnumber $biblioitem->{'biblioitemnumber'}
349 # This routine should also modify the corresponding MARC record data.
350 #
351 # This routine should also check to see which fields are actually being
352 # modified, and log all changes.
353
354     my ($env, $biblioitem) = @_;
355 }
356
357
358 sub newItem {
359     my ($env, $Record_ID, $item) = @_;
360     my $barcode=$item->{'barcode'};
361     my $dateaccessioned=$item->{'dateaccessioned'};
362     my $booksellerid=$item->{'booksellerid'};
363     my $homebranch=$item->{'homebranch'};
364     my $holdingbranch=$item->{'holdingbranch'};
365     my $price=$item->{'price'};
366     my $replacementprice=$item->{'replacementprice'};
367     my $replacementpricedate=$item->{'replacementpricedate'};
368     my $notforloan=$item->{'notforloan'};
369     my $itemlost=$item->{'itemlost'};
370     my $wthdrawn=$item->{'wthdrawn'};
371     my $restricted=$item->{'restricted'};
372     my $itemnotes=$item->{'itemnotes'};
373     my $itemtype=$item->{'itemtype'};
374     my $subclass=$item->{'subclass'};
375     my $subfields;
376     $subfields->{1}->{'Subfield_Mark'}='p';
377     $subfields->{1}->{'Subfield_Value'}=$barcode;
378     $subfields->{2}->{'Subfield_Mark'}='d';
379     $subfields->{2}->{'Subfield_Value'}=$dateaccessioned;
380     $subfields->{3}->{'Subfield_Mark'}='e';
381     $subfields->{3}->{'Subfield_Value'}=$booksellerid;
382     $subfields->{4}->{'Subfield_Mark'}='b';
383     $subfields->{4}->{'Subfield_Value'}=$homebranch;
384     $subfields->{5}->{'Subfield_Mark'}='l';
385     $subfields->{5}->{'Subfield_Value'}=$holdingbranch;
386     $subfields->{6}->{'Subfield_Mark'}='c';
387     $subfields->{6}->{'Subfield_Value'}=$price;
388     $subfields->{7}->{'Subfield_Mark'}='c';
389     $subfields->{7}->{'Subfield_Value'}=$replacementprice;
390     $subfields->{8}->{'Subfield_Mark'}='d';
391     $subfields->{8}->{'Subfield_Value'}=$replacementpricedate;
392     if ($notforloan) {
393         $subfields->{9}->{'Subfield_Mark'}='h';
394         $subfields->{9}->{'Subfield_Value'}='Not for loan';
395     }
396     if ($notforloan) {
397         $subfields->{10}->{'Subfield_Mark'}='j';
398         $subfields->{10}->{'Subfield_Value'}='Item lost';
399     }
400     if ($notforloan) {
401         $subfields->{11}->{'Subfield_Mark'}='j';
402         $subfields->{11}->{'Subfield_Value'}='Item withdrawn';
403     }
404     if ($notforloan) {
405         $subfields->{12}->{'Subfield_Mark'}='z';
406         $subfields->{12}->{'Subfield_Value'}=$itemnotes;
407     }
408     my $tag='876';
409     my $Tag_ID;
410     $env->{'linkage'}=1;
411     ($env, $Tag_ID) = addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
412     $env->{'linkage'}=0;
413     $env->{'linkid'}=$Tag_ID;
414     $tag='852';
415     my $subfields2;
416     $subfields2->{1}->{'Subfield_Mark'}='a';
417     $subfields2->{1}->{'Subfield_Value'}='Coast Mountains School District';
418     $subfields2->{1}->{'Subfield_Mark'}='b';
419     $subfields2->{1}->{'Subfield_Value'}=$homebranch;
420     $subfields2->{1}->{'Subfield_Mark'}='c';
421     $subfields2->{1}->{'Subfield_Value'}=$itemtype;
422     $subfields2->{2}->{'Subfield_Mark'}='m';
423     $subfields2->{2}->{'Subfield_Value'}=$subclass;
424     addTag($env, $Record_ID, $tag, ' ', ' ', $subfields2);
425     $env->{'linkid'}='';
426 }
427
428 sub updateItem {
429 # Update the item with itemnumber $item->{'itemnumber'}
430 # This routine should also modify the corresponding MARC record data. (852 and
431 # 876 tags with 876p tag the same as $item->{'barcode'}
432 #
433 # This routine should also check to see which fields are actually being
434 # modified, and log all changes.
435
436     my ($env, $biblio) = @_;
437 }
438
439 END { }       # module clean-up code here (global destructor)