Finished updateBiblioItem subroutine
[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 &changeSubfield);
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 # This subroutine makes no modifications to the MARC tables.  MARC records are
55 # only created when new biblioitems are added.
56     my ($env, $biblio) = @_;
57     my $title=$biblio->{'title'};
58     my $q_title=$dbh->quote($title);
59     my $subtitle=$biblio->{'subtitle'};
60     my $q_subtitle=$dbh->quote($subtitle);
61     ($q_subtitle) || ($q_subtitle="''");
62     my $author=$biblio->{'author'};
63     my $q_author=$dbh->quote($author);
64     my $unititle=$biblio->{'unititle'};
65     my $q_unititle=$dbh->quote($unititle);
66     my $copyrightdate=$biblio->{'copyrightdate'};
67     my $serial=$biblio->{'serial'};
68     my $seriestitle=$biblio->{'seriestitle'};
69     my $q_seriestitle=$dbh->quote($seriestitle);
70     my $notes=$biblio->{'notes'};
71     my $q_notes=$dbh->quote($notes);
72     my $subject=$biblio->{'subject'};
73     my $additionalauthors=$biblio->{'additionalauthors'};
74     my $sth=$dbh->prepare("select max(biblionumber) from biblio");
75     $sth->execute;
76     my ($biblionumber) = $sth->fetchrow;
77     $biblionumber++;
78     $sth=$dbh->prepare("insert into biblio (biblionumber,title,author,unititle,copyrightdate,serial,seriestitle,notes) values ($biblionumber,$q_title,$q_author,$q_unititle,$copyrightdate,$serial,$q_seriestitle,$q_notes)");
79     $sth->execute;
80     $sth=$dbh->prepare("insert into bibliosubtitle (biblionumber,subtitle) values ($biblionumber,$q_subtitle)");
81     $sth->execute;
82     foreach (@$subject) {
83         my $q_subject=$dbh->quote($_);
84         my $sth=$dbh->prepare("insert into bibliosubject (biblionumber,subject) values ($biblionumber,$q_subject)");
85         $sth->execute;
86     }
87     foreach (@$additionalauthors) {
88         my $q_additionalauthor=$dbh->quote($_);
89         my $sth=$dbh->prepare("insert into additionalauthors (biblionumber,author) values ($biblionumber,$q_additionalauthor)");
90         $sth->execute;
91     }
92 }
93
94
95 sub changeSubfield {
96 # Subroutine changes a subfield value given a Record_ID, Tag, and Subfield_Mark.
97 # Routine should be made more robust.  It currently checks to make sure that
98 # the existing Subfield_Value is the same as the one passed in.  What if no
99 # subfield matches this Subfield_OldValue?  Create a new Subfield?  Maybe check
100 # to make sure that the mark is repeatable first and that no other subfield
101 # with that mark already exists?  Ability to return errors and status?
102
103 # Also, currently, if more than one subfield matches the Record_ID, Tag,
104 # Subfield_Mark, and Subfield_OldValue, only the first one will be modified.
105 #
106 # Might be nice to be able to pass a Subfield_ID directly to this routine to
107 # remove ambiguity, if possible.
108
109     my $Record_ID=shift;
110     my $tag=shift;
111     my $firstdigit=substr($tag, 0, 1);
112     my $Subfield_Mark=shift;
113     my $Subfield_OldValue=shift;
114     my $Subfield_Value=shift;
115     my $dbh=&C4Connect;  
116     my $sth=$dbh->prepare("select S.Subfield_ID, S.Subfield_Value from Bib_Table B, $firstdigit\XX_Tag_Table T, $firstdigit\XX_Subfield_Table S where B.Record_ID=$Record_ID and B.Tag_$firstdigit\XX_ID=T.Tag_ID and T.Subfield_ID=S.Subfield_ID and S.Subfield_Mark='$Subfield_Mark'");
117     $sth->execute;
118     while (my ($ID, $Value) = $sth->fetchrow) {
119         if ($Value eq $Subfield_OldValue) {
120             my $q_Subfield_Value=$dbh->quote($Subfield_Value);
121             my $sti=$dbh->prepare("update $firstdigit\XX_Subfield_Table set Subfield_Value=$q_Subfield_Value where Subfield_ID=$ID");
122             $sti->execute;
123             last;
124         }
125     }
126 }
127
128 sub updateBiblio {
129 # Update the biblio with biblionumber $biblio->{'biblionumber'}
130 # I guess this routine should search through all marc records for a record that
131 # has the same biblionumber stored in it, and modify the MARC record as well as
132 # the biblio table.
133 #
134 # Also, this subroutine should search through the $biblio object and compare it
135 # to the existing record and _LOG ALL CHANGES MADE_ in some way.  I'd like for
136 # this logging feature to be usable to undo changes easily.
137 #
138 # Need to add support for bibliosubject, additionalauthors, bibliosubtitle tables
139
140     my ($env, $biblio) = @_;
141     my $biblionumber=$biblio->{'biblionumber'};
142     my $dbh=&C4Connect;  
143     my $sth=$dbh->prepare("select * from biblio where biblionumber=$biblionumber");
144     $sth->execute;
145     my $origbiblio=$sth->fetchrow_hashref;
146
147     
148 # Obtain a list of MARC Record_ID's that are tied to this biblio
149     $sth=$dbh->prepare("select B.Record_ID from Bib_Table B, 0XX_Tag_Table T, 0XX_Subfield_Table S where B.Tag_0XX_ID=T.Tag_ID and T.Subfield_ID=S.Subfield_ID and T.Tag='090' and S.Subfield_Value=$biblionumber and S.Subfield_Mark='c'");
150     $sth->execute;
151     my @marcrecords;
152     while (my ($Record_ID) = $sth->fetchrow) {
153         push(@marcrecords, $Record_ID);
154     }
155
156
157
158     if ($biblio->{'author'} ne $origbiblio->{'author'}) {
159         my $q_author=$dbh->quote($biblio->{'author'});
160         logchange('kohadb', 'biblio', 'author', $origbiblio->{'author'}, $biblio->{'author'});
161         my $sti=$dbh->prepare("update biblio set author=$q_author where biblionumber=$biblio->{'biblionumber'}");
162         $sti->execute;
163         logchange('marc', '100', 'a', $origbiblio->{'author'}, $biblio->{'author'});
164         foreach (@marcrecords) {
165             changeSubfield($_, '100', 'a', $origbiblio->{'author'}, $biblio->{'author'});
166         }
167     }
168     if ($biblio->{'title'} ne $origbiblio->{'title'}) {
169         my $q_title=$dbh->quote($biblio->{'title'});
170         logchange('kohadb', 'biblio', 'title', $origbiblio->{'title'}, $biblio->{'title'});
171         my $sti=$dbh->prepare("update biblio set title=$q_title where biblionumber=$biblio->{'biblionumber'}");
172         $sti->execute;
173         logchange('marc', '245', 'a', $origbiblio->{'title'}, $biblio->{'title'});
174         foreach (@marcrecords) {
175             changeSubfield($_, '245', 'a', $origbiblio->{'title'}, $biblio->{'title'});
176         }
177     }
178     if ($biblio->{'unititle'} ne $origbiblio->{'unititle'}) {
179         my $q_unititle=$dbh->quote($biblio->{'unititle'});
180         logchange('kohadb', 'biblio', 'unititle', $origbiblio->{'unititle'}, $biblio->{'unititle'});
181         my $sti=$dbh->prepare("update biblio set unititle=$q_unititle where biblionumber=$biblio->{'biblionumber'}");
182         $sti->execute;
183     }
184     if ($biblio->{'notes'} ne $origbiblio->{'notes'}) {
185         my $q_notes=$dbh->quote($biblio->{'notes'});
186         logchange('kohadb', 'biblio', 'notes', $origbiblio->{'notes'}, $biblio->{'notes'});
187         my $sti=$dbh->prepare("update biblio set notes=$q_notes where biblionumber=$biblio->{'biblionumber'}");
188         $sti->execute;
189         logchange('marc', '500', 'a', $origbiblio->{'notes'}, $biblio->{'notes'});
190         foreach (@marcrecords) {
191             changeSubfield($_, '500', 'a', $origbiblio->{'notes'}, $biblio->{'notes'});
192         }
193     }
194     if ($biblio->{'serial'} ne $origbiblio->{'serial'}) {
195         my $q_serial=$dbh->quote($biblio->{'serial'});
196         logchange('kohadb', 'biblio', 'serial', $origbiblio->{'serial'}, $biblio->{'serial'});
197         my $sti=$dbh->prepare("update biblio set serial=$q_serial where biblionumber=$biblio->{'biblionumber'}");
198         $sti->execute;
199     }
200     if ($biblio->{'seriestitle'} ne $origbiblio->{'seriestitle'}) {
201         my $q_seriestitle=$dbh->quote($biblio->{'seriestitle'});
202         logchange('kohadb', 'biblio', 'seriestitle', $origbiblio->{'seriestitle'}, $biblio->{'seriestitle'});
203         my $sti=$dbh->prepare("update biblio set seriestitle=$q_seriestitle where biblionumber=$biblio->{'biblionumber'}");
204         $sti->execute;
205         logchange('marc', '440', 'a', $origbiblio->{'seriestitle'}, $biblio->{'seriestitle'});
206         foreach (@marcrecords) {
207             changeSubfield($_, '440', 'a', $origbiblio->{'seriestitle'}, $biblio->{'seriestitle'});
208         }
209     }
210     if ($biblio->{'copyrightdate'} ne $origbiblio->{'copyrightdate'}) {
211         my $q_copyrightdate=$dbh->quote($biblio->{'copyrightdate'});
212         logchange('kohadb', 'biblio', 'copyrightdate', $origbiblio->{'copyrightdate'}, $biblio->{'copyrightdate'});
213         my $sti=$dbh->prepare("update biblio set copyrightdate=$q_copyrightdate where biblionumber=$biblio->{'biblionumber'}");
214         $sti->execute;
215         logchange('marc', '260', 'c', "c$origbiblio->{'notes'}", "c$biblio->{'notes'}");
216         foreach (@marcrecords) {
217             changeSubfield($_, '260', 'c', "c$origbiblio->{'notes'}", "c$biblio->{'notes'}");
218         }
219     }
220 }
221
222 sub logchange {
223 # Subroutine to log changes to databases
224     my $database=shift;
225     my $section=shift;
226     my $item=shift;
227     my $original=shift;
228     my $new=shift;
229 }
230
231 sub addTag {
232 # Subroutine to add a tag to an existing MARC Record.  If a new linkage id is
233 # desired, set $env->{'linkage'} to 1.  If an existing linkage id should be
234 # set, set $env->{'linkid'} to the link number.
235     my ($env, $Record_ID, $tag, $Indicator1, $Indicator2, $subfields) = @_;
236     my $dbh=&C4Connect;  
237     ($Indicator1) || ($Indicator1=' ');
238     ($Indicator2) || ($Indicator2=' ');
239     my $firstdigit=substr($tag,0,1);
240     my $Subfield_ID;
241     foreach (sort keys %$subfields) {
242         my $Subfield_Mark=$subfields->{$_}->{'Subfield_Mark'};
243         my $Subfield_Value=$subfields->{$_}->{'Subfield_Value'};
244         my $q_Subfield_Value=$dbh->quote($Subfield_Value);
245         if ($Subfield_ID) {
246             my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_ID, Subfield_Mark, Subfield_Value) values ($Subfield_ID, '$Subfield_Mark', $q_Subfield_Value)");
247             $sth->execute;
248         } else {
249             my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_Mark, Subfield_Value) values ('$Subfield_Mark', $q_Subfield_Value)");
250             $sth->execute;
251             my $Subfield_Key=$dbh->{'mysql_insertid'};
252             $Subfield_ID=$Subfield_Key;
253             $sth=$dbh->prepare("update $firstdigit\XX_Subfield_Table set Subfield_ID=$Subfield_ID where Subfield_Key=$Subfield_Key");
254             $sth->execute;
255         }
256     }
257     if (my $linkid=$env->{'linkid'}) {
258         $env->{'linkage'}=0;
259         my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_ID, Subfield_Mark, Subfield_Value) values ($Subfield_ID, '8', '$linkid')");
260         $sth->execute;
261     }
262     my $sth=$dbh->prepare("insert into $firstdigit\XX_Tag_Table (Indicator1, Indicator2, Tag, Subfield_ID) values ('$Indicator1', '$Indicator2', '$tag', $Subfield_ID)");
263     $sth->execute;
264     my $Tag_Key=$dbh->{'mysql_insertid'};
265     my $Tag_ID=$Tag_Key;
266     $sth=$dbh->prepare("update $firstdigit\XX_Tag_Table set Tag_ID=$Tag_ID where Tag_Key=$Tag_Key");
267     $sth->execute;
268     $sth=$dbh->prepare("insert into Bib_Table (Record_ID, Tag_$firstdigit\XX_ID) values ($Record_ID, $Tag_ID)");
269     $sth->execute;
270     if ($env->{'linkage'}) {
271         my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_ID, Subfield_Mark, Subfield_Value) values ($Subfield_ID, '8', '$Tag_ID')");
272         $sth->execute;
273         
274     }
275     $sth->finish;
276     $dbh->disconnect;
277     return ($env, $Tag_ID);
278 }
279
280 sub newBiblioItem {
281     my ($env, $biblioitem) = @_;
282     my $dbh=&C4Connect;  
283     my $biblionumber=$biblioitem->{'biblionumber'};
284     my $biblioitemnumber=$biblioitem->{'biblioitemnumber'};
285     my $volume=$biblioitem->{'volume'};
286     my $q_volume=$dbh->quote($volume);
287     my $number=$biblioitem->{'number'};
288     my $q_number=$dbh->quote($number);
289     my $classification=$biblioitem->{'classification'};
290     my $q_classification=$dbh->quote($classification);
291     my $itemtype=$biblioitem->{'itemtype'};
292     my $q_itemtype=$dbh->quote($itemtype);
293     my $isbn=$biblioitem->{'isbn'};
294     my $q_isbn=$dbh->quote($isbn);
295     my $issn=$biblioitem->{'issn'};
296     my $q_issn=$dbh->quote($issn);
297     my $dewey=$biblioitem->{'dewey'};
298     $dewey=~s/\.*0*$//;
299     ($dewey == 0) && ($dewey='');
300     my $subclass=$biblioitem->{'subclass'};
301     my $q_subclass=$dbh->quote($subclass);
302     my $publicationyear=$biblioitem->{'publicationyear'};
303     my $publishercode=$biblioitem->{'publishercode'};
304     my $q_publishercode=$dbh->quote($publishercode);
305     my $volumedate=$biblioitem->{'volumedate'};
306     my $q_volumedate=$dbh->quote($volumedate);
307     my $illus=$biblioitem->{'illus'};
308     my $q_illus=$dbh->quote($illus);
309     my $pages=$biblioitem->{'pages'};
310     my $q_pages=$dbh->quote($pages);
311     my $notes=$biblioitem->{'notes'};
312     my $q_notes=$dbh->quote($notes);
313     my $size=$biblioitem->{'size'};
314     my $q_size=$dbh->quote($size);
315     my $place=$biblioitem->{'place'};
316     my $q_place=$dbh->quote($place);
317     my $lccn=$biblioitem->{'lccn'};
318     my $q_lccn=$dbh->quote($lccn);
319
320
321 # Unless the $env->{'marconly'} flag is set, update the biblioitems table with
322 # the new data
323
324     unless ($env->{'marconly'}) {
325         #my $sth=$dbh->prepare("lock tables biblioitems write");
326         #$sth->execute;
327         my $sth=$dbh->prepare("select max(biblioitemnumber) from biblioitems");
328         $sth->execute;
329         my ($biblioitemnumber) =$sth->fetchrow;
330         $biblioitemnumber++;
331         $sth=$dbh->prepare("insert into biblioitems (biblionumber,biblioitemnumber,volume,number,classification,itemtype,isbn,issn,dewey,subclass,publicationyear,publishercode,volumedate,illus,pages,notes,size,place,lccn) values ($biblionumber, $biblioitemnumber, $q_volume, $q_number, $q_classification, $q_itemtype, $q_isbn, $q_issn, $dewey, $q_subclass, $q_publicationyear, $q_publishercode, $q_volumedate, $q_illus, $q_pages,$q_notes, $q_size, $q_place, $q_lccn)");
332         $sth->execute;
333         #my $sth=$dbh->prepare("unlock tables");
334         #$sth->execute;
335     }
336
337
338 # Should we check if there is already a biblioitem/marc with the
339 # same isbn/lccn/issn?
340
341     $sth=$dbh->prepare("select title,unititle,seriestitle,copyrightdate,notes,author from biblio where biblionumber=$biblionumber");
342     $sth->execute;
343     my ($title, $unititle,$seriestitle,$copyrightdate,$biblionotes,$author) = $sth->fetchrow;
344     $sth=$dbh->prepare("select subtitle from bibliosubtitle where biblionumber=$biblionumber");
345     $sth->execute;
346     my ($subtitle) = $sth->fetchrow;
347     $sth=$dbh->prepare("select author from additionalauthors where biblionumber=$biblionumber");
348     $sth->execute;
349     my @additionalauthors;
350     while (my ($additionalauthor) = $sth->fetchrow) {
351         push (@additionalauthors, $additionalauthor);
352     }
353     $sth=$dbh->prepare("select subject from bibliosubject where biblionumber=$biblionumber");
354     $sth->execute;
355     my @subjects;
356     while (my ($subject) = $sth->fetchrow) {
357         push (@subjects, $subject);
358     }
359
360 # MARC SECTION
361
362     $sth=$dbh->prepare("insert into Resource_Table (Record_ID) values (0)");
363     $sth->execute;
364     my $Resource_ID=$dbh->{'mysql_insertid'};
365     my $Record_ID=$Resource_ID;
366     $sth=$dbh->prepare("update Resource_Table set Record_ID=$Record_ID where Resource_ID=$Resource_ID");
367     $sth->execute;
368
369 # Title
370     {
371         my $subfields;
372         $subfields->{1}->{'Subfield_Mark'}='a';
373         $subfields->{1}->{'Subfield_Value'}=$title;
374         if ($subtitle) {
375             $subfields->{2}->{'Subfield_Mark'}='b';
376             $subfields->{2}->{'Subfield_Value'}=$subtitle;
377         }
378         my $tag='245';
379         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
380     }
381
382 # author
383     {
384         my $subfields;
385         $subfields->{1}->{'Subfield_Mark'}='a';
386         $subfields->{1}->{'Subfield_Value'}=$author;
387         my $tag='100';
388         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
389     }
390 # Series Title
391     if ($seriestitle) {
392         my $subfields;
393         $subfields->{1}->{'Subfield_Mark'}='a';
394         $subfields->{1}->{'Subfield_Value'}=$seriestitle;
395         my $tag='440';
396         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
397     }
398 # Biblio Note
399     if ($biblionotes) {
400         my $subfields;
401         $subfields->{1}->{'Subfield_Mark'}='a';
402         $subfields->{1}->{'Subfield_Value'}=$biblionotes;
403         $subfields->{2}->{'Subfield_Mark'}='3';
404         $subfields->{2}->{'Subfield_Value'}='biblio';
405         my $tag='440';
406         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
407     }
408 # Additional Authors
409     foreach (@additionalauthors) {
410         my $author=$_;
411         (next) unless ($author);
412         my $subfields;
413         $subfields->{1}->{'Subfield_Mark'}='a';
414         $subfields->{1}->{'Subfield_Value'}=$author;
415         $subfields->{2}->{'Subfield_Mark'}='e';
416         $subfields->{2}->{'Subfield_Value'}='author';
417         my $tag='700';
418         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
419     }
420 # Illustrator
421     if ($illus) {
422         (next) unless ($illus);
423         my $subfields;
424         $subfields->{1}->{'Subfield_Mark'}='a';
425         $subfields->{1}->{'Subfield_Value'}=$illus;
426         $subfields->{2}->{'Subfield_Mark'}='e';
427         $subfields->{2}->{'Subfield_Value'}='illustrator';
428         my $tag='700';
429         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
430     }
431 # Subjects
432     foreach (@subjects) {
433         my $subject=$_;
434         (next) unless ($subject);
435         my $subfields;
436         $subfields->{1}->{'Subfield_Mark'}='a';
437         $subfields->{1}->{'Subfield_Value'}=$subject;
438         my $tag='650';
439         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
440     }
441
442
443 # ISBN
444     if ($isbn) {
445         my $subfields;
446         $subfields->{1}->{'Subfield_Mark'}='a';
447         $subfields->{1}->{'Subfield_Value'}=$isbn;
448         my $tag='020';
449         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
450     }
451 # LCCN
452     if ($lccn) {
453         my $subfields;
454         $subfields->{1}->{'Subfield_Mark'}='a';
455         $subfields->{1}->{'Subfield_Value'}=$lccn;
456         my $tag='010';
457         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
458     }
459 # ISSN
460     if ($issn) {
461         my $subfields;
462         $subfields->{1}->{'Subfield_Mark'}='a';
463         $subfields->{1}->{'Subfield_Value'}=$issn;
464         my $tag='022';
465         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
466     }
467 # DEWEY
468     if ($dewey) {
469         my $subfields;
470         $subfields->{1}->{'Subfield_Mark'}='a';
471         $subfields->{1}->{'Subfield_Value'}=$dewey;
472         my $tag='082';
473         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
474     }
475 # DEWEY subclass and itemtype
476     {
477         my $subfields;
478         $subfields->{1}->{'Subfield_Mark'}='a';
479         $subfields->{1}->{'Subfield_Value'}=$itemtype;
480         $subfields->{2}->{'Subfield_Mark'}='b';
481         $subfields->{2}->{'Subfield_Value'}=$subclass;
482         $subfields->{3}->{'Subfield_Mark'}='c';
483         $subfields->{3}->{'Subfield_Value'}=$biblionumber;
484         $subfields->{4}->{'Subfield_Mark'}='d';
485         $subfields->{4}->{'Subfield_Value'}=$biblioitemnumber;
486         my $tag='090';
487         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
488     }
489 # PUBLISHER
490     {
491         my $subfields;
492         $subfields->{1}->{'Subfield_Mark'}='a';
493         $subfields->{1}->{'Subfield_Value'}=$place;
494         $subfields->{2}->{'Subfield_Mark'}='b';
495         $subfields->{2}->{'Subfield_Value'}=$publishercode;
496         $subfields->{3}->{'Subfield_Mark'}='c';
497         $subfields->{3}->{'Subfield_Value'}=$publicationyear;
498         if ($copyrightdate) {
499             $subfields->{4}->{'Subfield_Mark'}='c';
500             $subfields->{4}->{'Subfield_Value'}="c$copyrightdate";
501         }
502         my $tag='260';
503         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
504     }
505 # PHYSICAL
506     if ($pages || $size) {
507         my $subfields;
508         $subfields->{1}->{'Subfield_Mark'}='a';
509         $subfields->{1}->{'Subfield_Value'}=$pages;
510         $subfields->{2}->{'Subfield_Mark'}='c';
511         $subfields->{2}->{'Subfield_Value'}=$size;
512         my $tag='300';
513         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
514     }
515 # Volume/Number
516     if ($volume || $number) {
517         my $subfields;
518         $subfields->{1}->{'Subfield_Mark'}='v';
519         $subfields->{1}->{'Subfield_Value'}=$volume;
520         $subfields->{2}->{'Subfield_Mark'}='n';
521         $subfields->{2}->{'Subfield_Value'}=$number;
522         my $tag='440';
523         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
524     }
525 # Biblioitem Note
526     if ($notes) {
527         my $subfields;
528         $subfields->{1}->{'Subfield_Mark'}='a';
529         $subfields->{1}->{'Subfield_Value'}=$notes;
530         $subfields->{2}->{'Subfield_Mark'}='3';
531         $subfields->{2}->{'Subfield_Value'}='biblioitem';
532         my $tag='500';
533         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
534     }
535     $sth->finish;
536     $dbh->disconnect;
537     return ($env, $Record_ID);
538 }
539
540 sub updateBiblioItem {
541 # Update the biblioitem with biblioitemnumber $biblioitem->{'biblioitemnumber'}
542 # This routine should also modify the corresponding MARC record data.
543 #
544 # This routine should also check to see which fields are actually being
545 # modified, and log all changes.
546
547     my ($env, $biblioitem) = @_;
548     my $dbh=&C4Connect;  
549
550     my $biblioitemnumber=$biblioitem->{'biblioitemnumber'};
551     my $sth=$dbh->prepare("select * from biblioitems where biblioitemnumber=$biblioitemnumber");
552 # obi = original biblioitem
553     my $obi=$sth->fetchrow_hashref;
554     $sth=$dbh->prepare("select B.Record_ID from Bib_Table B, 0XX_Tag_Table T, 0XX_Subfield_Table S where B.Tag_0XX_ID=T.Tag_ID and T.Subfield_ID=S.Subfield_ID and T.Tag='090' and S.Subfield_Mark='c' and S.Subfield_Value=$biblioitemnumber");
555     $sth->execute;
556     my ($Record_ID) = $sth->fetchrow;
557     if ($biblioitem->{'biblionumber'} ne $obi->{'biblionumber'}) {
558         logchange('kohadb', 'biblioitems', 'biblionumber', $obi->{'biblionumber'}, $biblioitem->{'biblionumber'});
559         my $sth=$dbh->prepare("update biblioitems set biblionumber=$biblioitem->{'biblionumber'} where biblioitemnumber=$biblioitemnumber");
560         logchange('marc', '090', 'c', $obi->{'biblionumber'}, $biblioitem->{'biblionumber'});
561         changeSubfield($Record_ID, '090', 'c', $obi->{'biblionumber'}, $biblioitem->{'biblionumber'});
562     }
563     if ($biblioitem->{'volume'} ne $obi->{'volume'}) {
564         logchange('kohadb', 'biblioitems', 'volume', $obi->{'volume'}, $biblioitem->{'volume'});
565         my $q_volume=$dbh->quote($biblioitem->{'volume'});
566         my $sth=$dbh->prepare("update biblioitems set volume=$q_volume where biblioitemnumber=$biblioitemnumber");
567         logchange('marc', '440', 'v', $obi->{'volume'}, $biblioitem->{'volume'});
568         changeSubfield($Record_ID, '440', 'v', $obi->{'volume'}, $biblioitem->{'volume'});
569     }
570     if ($biblioitem->{'number'} ne $obi->{'number'}) {
571         logchange('kohadb', 'biblioitems', 'number', $obi->{'number'}, $biblioitem->{'number'});
572         my $q_number=$dbh->quote($biblioitem->{'number'});
573         my $sth=$dbh->prepare("update biblioitems set number=$q_number where biblioitemnumber=$biblioitemnumber");
574         logchange('marc', '440', 'v', $obi->{'number'}, $biblioitem->{'number'});
575         changeSubfield($Record_ID, '440', 'v', $obi->{'number'}, $biblioitem->{'number'});
576     }
577     if ($biblioitem->{'itemtype'} ne $obi->{'itemtype'}) {
578         logchange('kohadb', 'biblioitems', 'itemtype', $obi->{'itemtype'}, $biblioitem->{'itemtype'});
579         my $q_itemtype=$dbh->quote($biblioitem->{'itemtype'});
580         my $sth=$dbh->prepare("update biblioitems set itemtype=$q_itemtype where biblioitemnumber=$biblioitemnumber");
581         logchange('marc', '090', 'a', $obi->{'itemtype'}, $biblioitem->{'itemtype'});
582         changeSubfield($Record_ID, '090', 'a', $obi->{'itemtype'}, $biblioitem->{'itemtype'});
583     }
584     if ($biblioitem->{'isbn'} ne $obi->{'isbn'}) {
585         logchange('kohadb', 'biblioitems', 'isbn', $obi->{'isbn'}, $biblioitem->{'isbn'});
586         my $q_isbn=$dbh->quote($biblioitem->{'isbn'});
587         my $sth=$dbh->prepare("update biblioitems set isbn=$q_isbn where biblioitemnumber=$biblioitemnumber");
588         logchange('marc', '020', 'a', $obi->{'isbn'}, $biblioitem->{'isbn'});
589         changeSubfield($Record_ID, '020', 'a', $obi->{'isbn'}, $biblioitem->{'isbn'});
590     }
591     if ($biblioitem->{'issn'} ne $obi->{'issn'}) {
592         logchange('kohadb', 'biblioitems', 'issn', $obi->{'issn'}, $biblioitem->{'issn'});
593         my $q_issn=$dbh->quote($biblioitem->{'issn'});
594         my $sth=$dbh->prepare("update biblioitems set issn=$q_issn where biblioitemnumber=$biblioitemnumber");
595         logchange('marc', '022', 'a', $obi->{'issn'}, $biblioitem->{'issn'});
596         changeSubfield($Record_ID, '022', 'a', $obi->{'issn'}, $biblioitem->{'issn'});
597     }
598     if ($biblioitem->{'dewey'} ne $obi->{'dewey'}) {
599         logchange('kohadb', 'biblioitems', 'dewey', $obi->{'dewey'}, $biblioitem->{'dewey'});
600         my $sth=$dbh->prepare("update biblioitems set dewey=$dewey where biblioitemnumber=$biblioitemnumber");
601         logchange('marc', '082', 'a', $obi->{'dewey'}, $biblioitem->{'dewey'});
602         changeSubfield($Record_ID, '082', 'a', $obi->{'dewey'}, $biblioitem->{'dewey'});
603     }
604     if ($biblioitem->{'subclass'} ne $obi->{'subclass'}) {
605         logchange('kohadb', 'biblioitems', 'subclass', $obi->{'subclass'}, $biblioitem->{'subclass'});
606         my $q_subclass=$dbh->quote($biblioitem->{'subclass'});
607         my $sth=$dbh->prepare("update biblioitems set subclass=$q_subclass where biblioitemnumber=$biblioitemnumber");
608         logchange('marc', '090', 'b', $obi->{'subclass'}, $biblioitem->{'subclass'});
609         changeSubfield($Record_ID, '090', 'b', $obi->{'subclass'}, $biblioitem->{'subclass'});
610     }
611     if ($biblioitem->{'place'} ne $obi->{'place'}) {
612         logchange('kohadb', 'biblioitems', 'place', $obi->{'place'}, $biblioitem->{'place'});
613         my $q_place=$dbh->quote($biblioitem->{'place'});
614         my $sth=$dbh->prepare("update biblioitems set place=$q_place where biblioitemnumber=$biblioitemnumber");
615         logchange('marc', '260', 'a', $obi->{'place'}, $biblioitem->{'place'});
616         changeSubfield($Record_ID, '260', 'a', $obi->{'place'}, $biblioitem->{'place'});
617     }
618     if ($biblioitem->{'publishercode'} ne $obi->{'publishercode'}) {
619         logchange('kohadb', 'biblioitems', 'publishercode', $obi->{'publishercode'}, $biblioitem->{'publishercode'});
620         my $q_publishercode=$dbh->quote($biblioitem->{'publishercode'});
621         my $sth=$dbh->prepare("update biblioitems set publishercode=$q_publishercode where biblioitemnumber=$biblioitemnumber");
622         logchange('marc', '260', 'b', $obi->{'publishercode'}, $biblioitem->{'publishercode'});
623         changeSubfield($Record_ID, '260', 'b', $obi->{'publishercode'}, $biblioitem->{'publishercode'});
624     }
625     if ($biblioitem->{'publicationyear'} ne $obi->{'publicationyear'}) {
626         logchange('kohadb', 'biblioitems', 'publicationyear', $obi->{'publicationyear'}, $biblioitem->{'publicationyear'});
627         my $q_publicationyear=$dbh->quote($biblioitem->{'publicationyear'});
628         my $sth=$dbh->prepare("update biblioitems set publicationyear=$q_publicationyear where biblioitemnumber=$biblioitemnumber");
629         logchange('marc', '260', 'c', $obi->{'publicationyear'}, $biblioitem->{'publicationyear'});
630         changeSubfield($Record_ID, '260', 'c', $obi->{'publicationyear'}, $biblioitem->{'publicationyear'});
631     }
632     if ($biblioitem->{'illus'} ne $obi->{'illus'}) {
633         logchange('kohadb', 'biblioitems', 'illus', $obi->{'illus'}, $biblioitem->{'illus'});
634         my $q_illus=$dbh->quote($biblioitem->{'illus'});
635         my $sth=$dbh->prepare("update biblioitems set illus=$q_illus where biblioitemnumber=$biblioitemnumber");
636         logchange('marc', '700', 'a', $obi->{'illus'}, $biblioitem->{'illus'});
637         changeSubfield($Record_ID, '700', 'a', $obi->{'illus'}, $biblioitem->{'illus'});
638     }
639     if ($biblioitem->{'pages'} ne $obi->{'pages'}) {
640         logchange('kohadb', 'biblioitems', 'pages', $obi->{'pages'}, $biblioitem->{'pages'});
641         my $q_pages=$dbh->quote($biblioitem->{'pages'});
642         my $sth=$dbh->prepare("update biblioitems set pages=$q_pages where biblioitemnumber=$biblioitemnumber");
643         logchange('marc', '300', 'a', $obi->{'pages'}, $biblioitem->{'pages'});
644         changeSubfield($Record_ID, '300', 'a', $obi->{'pages'}, $biblioitem->{'pages'});
645     }
646     if ($biblioitem->{'size'} ne $obi->{'size'}) {
647         logchange('kohadb', 'biblioitems', 'size', $obi->{'size'}, $biblioitem->{'size'});
648         my $q_size=$dbh->quote($biblioitem->{'size'});
649         my $sth=$dbh->prepare("update biblioitems set size=$q_size where biblioitemnumber=$biblioitemnumber");
650         logchange('marc', '300', 'c', $obi->{'size'}, $biblioitem->{'size'});
651         changeSubfield($Record_ID, '300', 'c', $obi->{'size'}, $biblioitem->{'size'});
652     }
653     if ($biblioitem->{'notes'} ne $obi->{'notes'}) {
654         logchange('kohadb', 'biblioitems', 'notes', $obi->{'notes'}, $biblioitem->{'notes'});
655         my $q_notes=$dbh->quote($biblioitem->{'notes'});
656         my $sth=$dbh->prepare("update biblioitems set notes=$q_notes where biblioitemnumber=$biblioitemnumber");
657         logchange('marc', '500', 'a', $obi->{'notes'}, $biblioitem->{'notes'});
658         changeSubfield($Record_ID, '500', 'a', $obi->{'notes'}, $biblioitem->{'notes'});
659     }
660     if ($biblioitem->{'lccn'} ne $obi->{'lccn'}) {
661         logchange('kohadb', 'biblioitems', 'lccn', $obi->{'lccn'}, $biblioitem->{'lccn'});
662         my $q_lccn=$dbh->quote($biblioitem->{'lccn'});
663         my $sth=$dbh->prepare("update biblioitems set lccn=$q_lccn where biblioitemnumber=$biblioitemnumber");
664         logchange('marc', '010', 'c', $obi->{'lccn'}, $biblioitem->{'lccn'});
665         changeSubfield($Record_ID, '010', 'c', $obi->{'lccn'}, $biblioitem->{'lccn'});
666     }
667
668 }
669
670
671 sub newItem {
672     my ($env, $Record_ID, $item) = @_;
673     my $dbh=&C4Connect;  
674     my $barcode=$item->{'barcode'};
675     my $dateaccessioned=$item->{'dateaccessioned'};
676     my $booksellerid=$item->{'booksellerid'};
677     my $homebranch=$item->{'homebranch'};
678     my $holdingbranch=$item->{'holdingbranch'};
679     my $price=$item->{'price'};
680     my $replacementprice=$item->{'replacementprice'};
681     my $replacementpricedate=$item->{'replacementpricedate'};
682     my $notforloan=$item->{'notforloan'};
683     my $itemlost=$item->{'itemlost'};
684     my $wthdrawn=$item->{'wthdrawn'};
685     my $restricted=$item->{'restricted'};
686     my $itemnotes=$item->{'itemnotes'};
687     my $itemtype=$item->{'itemtype'};
688     my $subclass=$item->{'subclass'};
689
690 # KOHADB Section
691
692     unless ($env->{'marconly'}) {
693         my $sth=$dbh->prepare("select max(itemnumber) from items");
694         $sth->execute;
695         my ($itemnumber) =$sth->fetchrow;
696         $itemnumber++;
697         $sth=$dbh->prepare("insert into items (itemnumber,biblionumber,biblioitemnumber,barcode,dateaccessioned,booksellerid,homebranch,price,replacementprice,replacementpricedate,notforloan,itemlost,wthdrawn,restricted,itemnotes) values ($itemnumber,$biblionumber,$biblioitemnumber,$q_barcode,$dateaccessioned,$q_booksellerid,$q_homebranch,$price,$q_replacementpricedate,$notforloan,$itemlost,$wthdrawn,$restricted,$q_itemnotes)");
698         $sth->execute;
699     }
700
701
702 # MARC SECTION
703     my $subfields;
704     $subfields->{1}->{'Subfield_Mark'}='p';
705     $subfields->{1}->{'Subfield_Value'}=$barcode;
706     $subfields->{2}->{'Subfield_Mark'}='d';
707     $subfields->{2}->{'Subfield_Value'}=$dateaccessioned;
708     $subfields->{3}->{'Subfield_Mark'}='e';
709     $subfields->{3}->{'Subfield_Value'}=$booksellerid;
710     $subfields->{4}->{'Subfield_Mark'}='b';
711     $subfields->{4}->{'Subfield_Value'}=$homebranch;
712     $subfields->{5}->{'Subfield_Mark'}='l';
713     $subfields->{5}->{'Subfield_Value'}=$holdingbranch;
714     $subfields->{6}->{'Subfield_Mark'}='c';
715     $subfields->{6}->{'Subfield_Value'}=$price;
716     $subfields->{7}->{'Subfield_Mark'}='c';
717     $subfields->{7}->{'Subfield_Value'}=$replacementprice;
718     $subfields->{8}->{'Subfield_Mark'}='d';
719     $subfields->{8}->{'Subfield_Value'}=$replacementpricedate;
720     if ($notforloan) {
721         $subfields->{9}->{'Subfield_Mark'}='h';
722         $subfields->{9}->{'Subfield_Value'}='Not for loan';
723     }
724     if ($notforloan) {
725         $subfields->{10}->{'Subfield_Mark'}='j';
726         $subfields->{10}->{'Subfield_Value'}='Item lost';
727     }
728     if ($notforloan) {
729         $subfields->{11}->{'Subfield_Mark'}='j';
730         $subfields->{11}->{'Subfield_Value'}='Item withdrawn';
731     }
732     if ($notforloan) {
733         $subfields->{12}->{'Subfield_Mark'}='z';
734         $subfields->{12}->{'Subfield_Value'}=$itemnotes;
735     }
736     my $tag='876';
737     my $Tag_ID;
738     $env->{'linkage'}=1;
739     ($env, $Tag_ID) = addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
740     $env->{'linkage'}=0;
741     $env->{'linkid'}=$Tag_ID;
742     $tag='852';
743     my $subfields2;
744     $subfields2->{1}->{'Subfield_Mark'}='a';
745     $subfields2->{1}->{'Subfield_Value'}='Coast Mountains School District';
746     $subfields2->{1}->{'Subfield_Mark'}='b';
747     $subfields2->{1}->{'Subfield_Value'}=$homebranch;
748     $subfields2->{1}->{'Subfield_Mark'}='c';
749     $subfields2->{1}->{'Subfield_Value'}=$itemtype;
750     $subfields2->{2}->{'Subfield_Mark'}='m';
751     $subfields2->{2}->{'Subfield_Value'}=$subclass;
752     addTag($env, $Record_ID, $tag, ' ', ' ', $subfields2);
753     $env->{'linkid'}='';
754 }
755
756 sub updateItem {
757 # Update the item with itemnumber $item->{'itemnumber'}
758 # This routine should also modify the corresponding MARC record data. (852 and
759 # 876 tags with 876p tag the same as $item->{'barcode'}
760 #
761 # This routine should also check to see which fields are actually being
762 # modified, and log all changes.
763
764     my ($env, $item) = @_;
765 }
766
767 END { }       # module clean-up code here (global destructor)