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