Changed search interface to allow searches on Illustrator. This is
[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     $sth=$dbh->prepare("select subtitle from bibliosubtitle where biblionumber=$biblionumber"):
179     $sth->execute;
180     my ($subtitle)=$sth->fetchrow;
181     $origbiblio->{'subtitle'}=$subtitle;
182     $sth=$dbh->prepare("select author from additionalauthors where biblionumber=$biblionumber");
183     $sth->execute;
184     my $origadditionalauthors;
185     while (my ($author) = $sth->fetchrow) {
186         push (@{$origbiblio->{'additionalauthors'}}, $author);
187         $origadditionalauthors->{$subject}=1;
188     }
189     $sth=$dbh->prepare("select subject from bibliosubjects where biblionumber=$biblionumber");
190     $sth->execute;
191     my $origsubjects;
192     while (my ($subject) = $sth->fetchrow) {
193         push (@{$origbiblio->{'subjects'}}, $subject);
194         $origsubjects->{$subject}=1;
195     }
196
197     
198 # Obtain a list of MARC Record_ID's that are tied to this biblio
199     $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'");
200     $sth->execute;
201     my @marcrecords;
202     while (my ($Record_ID) = $sth->fetchrow) {
203         push(@marcrecords, $Record_ID);
204     }
205
206
207
208     my $Record_ID='';
209     if ($biblio->{'author'} ne $origbiblio->{'author'}) {
210         my $q_author=$dbh->quote($biblio->{'author'});
211         logchange('kohadb', 'change', 'biblio', 'author', $origbiblio->{'author'}, $biblio->{'author'});
212         my $sti=$dbh->prepare("update biblio set author=$q_author where biblionumber=$biblio->{'biblionumber'}");
213         $sti->execute;
214         foreach $Record_ID (@marcrecords) {
215             logchange('marc', 'change', $Record_ID, '100', 'a', $origbiblio->{'author'}, $biblio->{'author'});
216             changeSubfield($Record_ID, '100', 'a', $origbiblio->{'author'}, $biblio->{'author'});
217         }
218     }
219     if ($biblio->{'title'} ne $origbiblio->{'title'}) {
220         my $q_title=$dbh->quote($biblio->{'title'});
221         logchange('kohadb', 'change', 'biblio', 'title', $origbiblio->{'title'}, $biblio->{'title'});
222         my $sti=$dbh->prepare("update biblio set title=$q_title where biblionumber=$biblio->{'biblionumber'}");
223         $sti->execute;
224         foreach $Record_ID (@marcrecords) {
225             logchange('marc', 'change', $Record_ID, '245', 'a', $origbiblio->{'title'}, $biblio->{'title'});
226             changeSubfield($Record_ID, '245', 'a', $origbiblio->{'title'}, $biblio->{'title'});
227         }
228     }
229     if ($biblio->{'subtitle'} ne $origbiblio->{'subtitle'}) {
230         my $q_subtitle=$dbh->quote($biblio->{'subtitle'});
231         logchange('kohadb', 'change', 'biblio', 'subtitle', $origbiblio->{'subtitle'}, $biblio->{'subtitle'});
232         my $sti=$dbh->prepare("update bibliosubtitle set subtitle=$q_subtitle where biblionumber=$biblio->{'biblionumber'}");
233         $sti->execute;
234         foreach $Record_ID (@marcrecords) {
235             logchange('marc', 'change', $Record_ID, '245', 'b', $origbiblio->{'subtitle'}, $biblio->{'subtitle'});
236             changeSubfield($Record_ID, '245', 'b', $origbiblio->{'subtitle'}, $biblio->{'subtitle'});
237         }
238     }
239     if ($biblio->{'unititle'} ne $origbiblio->{'unititle'}) {
240         my $q_unititle=$dbh->quote($biblio->{'unititle'});
241         logchange('kohadb', 'change', 'biblio', 'unititle', $origbiblio->{'unititle'}, $biblio->{'unititle'});
242         my $sti=$dbh->prepare("update biblio set unititle=$q_unititle where biblionumber=$biblio->{'biblionumber'}");
243         $sti->execute;
244     }
245     if ($biblio->{'notes'} ne $origbiblio->{'notes'}) {
246         my $q_notes=$dbh->quote($biblio->{'notes'});
247         logchange('kohadb', 'change', 'biblio', 'notes', $origbiblio->{'notes'}, $biblio->{'notes'});
248         my $sti=$dbh->prepare("update biblio set notes=$q_notes where biblionumber=$biblio->{'biblionumber'}");
249         $sti->execute;
250         foreach $Record_ID (@marcrecords) {
251             logchange('marc', 'change', $Record_ID, '500', 'a', $origbiblio->{'notes'}, $biblio->{'notes'});
252             changeSubfield($Record_ID, '500', 'a', $origbiblio->{'notes'}, $biblio->{'notes'});
253         }
254     }
255     if ($biblio->{'serial'} ne $origbiblio->{'serial'}) {
256         my $q_serial=$dbh->quote($biblio->{'serial'});
257         logchange('kohadb', 'change', 'biblio', 'serial', $origbiblio->{'serial'}, $biblio->{'serial'});
258         my $sti=$dbh->prepare("update biblio set serial=$q_serial where biblionumber=$biblio->{'biblionumber'}");
259         $sti->execute;
260     }
261     if ($biblio->{'seriestitle'} ne $origbiblio->{'seriestitle'}) {
262         my $q_seriestitle=$dbh->quote($biblio->{'seriestitle'});
263         logchange('kohadb', 'change', 'biblio', 'seriestitle', $origbiblio->{'seriestitle'}, $biblio->{'seriestitle'});
264         my $sti=$dbh->prepare("update biblio set seriestitle=$q_seriestitle where biblionumber=$biblio->{'biblionumber'}");
265         $sti->execute;
266         foreach $Record_ID (@marcrecords) {
267             logchange('marc', 'change', $Record_ID, '440', 'a', $origbiblio->{'seriestitle'}, $biblio->{'seriestitle'});
268             changeSubfield($Record_ID, '440', 'a', $origbiblio->{'seriestitle'}, $biblio->{'seriestitle'});
269         }
270     }
271     if ($biblio->{'copyrightdate'} ne $origbiblio->{'copyrightdate'}) {
272         my $q_copyrightdate=$dbh->quote($biblio->{'copyrightdate'});
273         logchange('kohadb', 'change', 'biblio', 'copyrightdate', $origbiblio->{'copyrightdate'}, $biblio->{'copyrightdate'});
274         my $sti=$dbh->prepare("update biblio set copyrightdate=$q_copyrightdate where biblionumber=$biblio->{'biblionumber'}");
275         $sti->execute;
276         foreach $Record_ID (@marcrecords) {
277             logchange('marc', 'change', $Record_ID, '260', 'c', "c$origbiblio->{'notes'}", "c$biblio->{'notes'}");
278             changeSubfield($Record_ID, '260', 'c', "c$origbiblio->{'notes'}", "c$biblio->{'notes'}");
279         }
280     }
281
282 # Check for subject heading changes
283     
284     my $newsubject='';
285     foreach $newsubject (@{$biblio->{'subject'}}) {
286         $subjects->{$newsubject}=1;
287         if ($origsubjects->{$newsubject}) {
288             $subjects->{$newsubject}=2;
289         } else {
290             my $q_newsubject=$dbh->quote($newsubject);
291             my $sth=$dbh->prepare("insert into bibliosubjects (subject,biblionumber) values ($q_newsubject, $biblionumber)");
292             $sth->execute;
293             logchange('kohadb', 'add', 'biblio', 'subject', $newsubject);
294             my $subfields;
295             $subfields->{1}->{'Subfield_Mark'}='a';
296             $subfields->{1}->{'Subfield_Value'}=$newsubject;
297             my $tag='650';
298             my $Record_ID;
299             foreach $Record_ID (@marcrecords) {
300                 addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
301                 logchange('marc', 'add', $Record_ID, '650', 'a', $newsubject);
302             }
303         }
304     }
305     foreach $origsubject (keys %$origsubjects) {
306         if ($subjects->{$origsubject} == 1) {
307             my $q_origsubject=$dbh->quote($origsubject);
308             logchange('kohadb', 'delete', 'biblio', '$biblionumber', 'subject', $origsubject);
309             my $sth=$dbh->prepare("delete from bibliosubjects where biblionumber=$biblionumber and subject=$q_origsubject");
310             $sth->execute;
311         }
312     }
313
314 }
315
316 sub logchange {
317 # Subroutine to log changes to databases
318     my $database=shift;
319     if ($database eq 'kohadb') {
320         my $type=shift;
321         my $section=shift;
322         my $item=shift;
323         my $original=shift;
324         my $new=shift;
325         print "KOHA: $section $item $original $new\n";
326     } elsif ($database eq 'marc') {
327         my $type=shift;
328         my $tag=shift;
329         my $mark=shift;
330         my $subfield_ID=shift;
331         my $original=shift;
332         my $new=shift;
333         print "MARC: $tag $mark $subfield_ID $original $new\n";
334     }
335 }
336
337 sub addTag {
338 # Subroutine to add a tag to an existing MARC Record.  If a new linkage id is
339 # desired, set $env->{'linkage'} to 1.  If an existing linkage id should be
340 # set, set $env->{'linkid'} to the link number.
341     my ($env, $Record_ID, $tag, $Indicator1, $Indicator2, $subfields) = @_;
342     my $dbh=&C4Connect;  
343     ($Indicator1) || ($Indicator1=' ');
344     ($Indicator2) || ($Indicator2=' ');
345     my $firstdigit=substr($tag,0,1);
346     my $Subfield_ID;
347     foreach (sort keys %$subfields) {
348         my $Subfield_Mark=$subfields->{$_}->{'Subfield_Mark'};
349         my $Subfield_Value=$subfields->{$_}->{'Subfield_Value'};
350         my $q_Subfield_Value=$dbh->quote($Subfield_Value);
351         if ($Subfield_ID) {
352             my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_ID, Subfield_Mark, Subfield_Value) values ($Subfield_ID, '$Subfield_Mark', $q_Subfield_Value)");
353             $sth->execute;
354         } else {
355             my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_Mark, Subfield_Value) values ('$Subfield_Mark', $q_Subfield_Value)");
356             $sth->execute;
357             my $Subfield_Key=$dbh->{'mysql_insertid'};
358             $Subfield_ID=$Subfield_Key;
359             $sth=$dbh->prepare("update $firstdigit\XX_Subfield_Table set Subfield_ID=$Subfield_ID where Subfield_Key=$Subfield_Key");
360             $sth->execute;
361         }
362     }
363     if (my $linkid=$env->{'linkid'}) {
364         $env->{'linkage'}=0;
365         my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_ID, Subfield_Mark, Subfield_Value) values ($Subfield_ID, '8', '$linkid')");
366         $sth->execute;
367     }
368     my $sth=$dbh->prepare("insert into $firstdigit\XX_Tag_Table (Indicator1, Indicator2, Tag, Subfield_ID) values ('$Indicator1', '$Indicator2', '$tag', $Subfield_ID)");
369     $sth->execute;
370     my $Tag_Key=$dbh->{'mysql_insertid'};
371     my $Tag_ID=$Tag_Key;
372     $sth=$dbh->prepare("update $firstdigit\XX_Tag_Table set Tag_ID=$Tag_ID where Tag_Key=$Tag_Key");
373     $sth->execute;
374     $sth=$dbh->prepare("insert into Bib_Table (Record_ID, Tag_$firstdigit\XX_ID) values ($Record_ID, $Tag_ID)");
375     $sth->execute;
376     if ($env->{'linkage'}) {
377         my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_ID, Subfield_Mark, Subfield_Value) values ($Subfield_ID, '8', '$Tag_ID')");
378         $sth->execute;
379         
380     }
381     $sth->finish;
382     $dbh->disconnect;
383     return ($env, $Tag_ID);
384 }
385
386 sub newBiblioItem {
387     my ($env, $biblioitem) = @_;
388     my $dbh=&C4Connect;  
389     my $biblionumber=$biblioitem->{'biblionumber'};
390     my $biblioitemnumber=$biblioitem->{'biblioitemnumber'};
391     my $volume=$biblioitem->{'volume'};
392     my $q_volume=$dbh->quote($volume);
393     my $number=$biblioitem->{'number'};
394     my $q_number=$dbh->quote($number);
395     my $classification=$biblioitem->{'classification'};
396     my $q_classification=$dbh->quote($classification);
397     my $itemtype=$biblioitem->{'itemtype'};
398     my $q_itemtype=$dbh->quote($itemtype);
399     my $isbn=$biblioitem->{'isbn'};
400     my $q_isbn=$dbh->quote($isbn);
401     my $issn=$biblioitem->{'issn'};
402     my $q_issn=$dbh->quote($issn);
403     my $dewey=$biblioitem->{'dewey'};
404     $dewey=~s/\.*0*$//;
405     ($dewey == 0) && ($dewey='');
406     my $subclass=$biblioitem->{'subclass'};
407     my $q_subclass=$dbh->quote($subclass);
408     my $publicationyear=$biblioitem->{'publicationyear'};
409     my $publishercode=$biblioitem->{'publishercode'};
410     my $q_publishercode=$dbh->quote($publishercode);
411     my $volumedate=$biblioitem->{'volumedate'};
412     my $q_volumedate=$dbh->quote($volumedate);
413     my $illus=$biblioitem->{'illus'};
414     my $q_illus=$dbh->quote($illus);
415     my $pages=$biblioitem->{'pages'};
416     my $q_pages=$dbh->quote($pages);
417     my $notes=$biblioitem->{'notes'};
418     my $q_notes=$dbh->quote($notes);
419     my $size=$biblioitem->{'size'};
420     my $q_size=$dbh->quote($size);
421     my $place=$biblioitem->{'place'};
422     my $q_place=$dbh->quote($place);
423     my $lccn=$biblioitem->{'lccn'};
424     my $q_lccn=$dbh->quote($lccn);
425
426
427 # Unless the $env->{'marconly'} flag is set, update the biblioitems table with
428 # the new data
429
430     unless ($env->{'marconly'}) {
431         #my $sth=$dbh->prepare("lock tables biblioitems write");
432         #$sth->execute;
433         my $sth=$dbh->prepare("select max(biblioitemnumber) from biblioitems");
434         $sth->execute;
435         my ($biblioitemnumber) =$sth->fetchrow;
436         $biblioitemnumber++;
437         $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)");
438         $sth->execute;
439         #my $sth=$dbh->prepare("unlock tables");
440         #$sth->execute;
441     }
442
443
444 # Should we check if there is already a biblioitem/marc with the
445 # same isbn/lccn/issn?
446
447     my $sth=$dbh->prepare("select title,unititle,seriestitle,copyrightdate,notes,author from biblio where biblionumber=$biblionumber");
448     $sth->execute;
449     my ($title, $unititle,$seriestitle,$copyrightdate,$biblionotes,$author) = $sth->fetchrow;
450     $sth=$dbh->prepare("select subtitle from bibliosubtitle where biblionumber=$biblionumber");
451     $sth->execute;
452     my ($subtitle) = $sth->fetchrow;
453     $sth=$dbh->prepare("select author from additionalauthors where biblionumber=$biblionumber");
454     $sth->execute;
455     my @additionalauthors;
456     while (my ($additionalauthor) = $sth->fetchrow) {
457         push (@additionalauthors, $additionalauthor);
458     }
459     $sth=$dbh->prepare("select subject from bibliosubject where biblionumber=$biblionumber");
460     $sth->execute;
461     my @subjects;
462     while (my ($subject) = $sth->fetchrow) {
463         push (@subjects, $subject);
464     }
465
466 # MARC SECTION
467
468     $sth=$dbh->prepare("insert into Resource_Table (Record_ID) values (0)");
469     $sth->execute;
470     my $Resource_ID=$dbh->{'mysql_insertid'};
471     my $Record_ID=$Resource_ID;
472     $sth=$dbh->prepare("update Resource_Table set Record_ID=$Record_ID where Resource_ID=$Resource_ID");
473     $sth->execute;
474
475 # Title
476     {
477         my $subfields;
478         $subfields->{1}->{'Subfield_Mark'}='a';
479         $subfields->{1}->{'Subfield_Value'}=$title;
480         if ($subtitle) {
481             $subfields->{2}->{'Subfield_Mark'}='b';
482             $subfields->{2}->{'Subfield_Value'}=$subtitle;
483         }
484         my $tag='245';
485         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
486     }
487
488 # author
489     {
490         my $subfields;
491         $subfields->{1}->{'Subfield_Mark'}='a';
492         $subfields->{1}->{'Subfield_Value'}=$author;
493         my $tag='100';
494         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
495     }
496 # Series Title
497     if ($seriestitle) {
498         my $subfields;
499         $subfields->{1}->{'Subfield_Mark'}='a';
500         $subfields->{1}->{'Subfield_Value'}=$seriestitle;
501         my $tag='440';
502         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
503     }
504 # Biblio Note
505     if ($biblionotes) {
506         my $subfields;
507         $subfields->{1}->{'Subfield_Mark'}='a';
508         $subfields->{1}->{'Subfield_Value'}=$biblionotes;
509         $subfields->{2}->{'Subfield_Mark'}='3';
510         $subfields->{2}->{'Subfield_Value'}='biblio';
511         my $tag='500';
512         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
513     }
514 # Additional Authors
515     foreach (@additionalauthors) {
516         my $author=$_;
517         (next) unless ($author);
518         my $subfields;
519         $subfields->{1}->{'Subfield_Mark'}='a';
520         $subfields->{1}->{'Subfield_Value'}=$author;
521         $subfields->{2}->{'Subfield_Mark'}='e';
522         $subfields->{2}->{'Subfield_Value'}='author';
523         my $tag='700';
524         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
525     }
526 # Illustrator
527     if ($illus) {
528         (next) unless ($illus);
529         my $subfields;
530         $subfields->{1}->{'Subfield_Mark'}='a';
531         $subfields->{1}->{'Subfield_Value'}=$illus;
532         $subfields->{2}->{'Subfield_Mark'}='e';
533         $subfields->{2}->{'Subfield_Value'}='illustrator';
534         my $tag='700';
535         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
536     }
537 # Subjects
538     foreach (@subjects) {
539         my $subject=$_;
540         (next) unless ($subject);
541         my $subfields;
542         $subfields->{1}->{'Subfield_Mark'}='a';
543         $subfields->{1}->{'Subfield_Value'}=$subject;
544         my $tag='650';
545         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
546     }
547
548
549 # ISBN
550     if ($isbn) {
551         my $subfields;
552         $subfields->{1}->{'Subfield_Mark'}='a';
553         $subfields->{1}->{'Subfield_Value'}=$isbn;
554         my $tag='020';
555         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
556     }
557 # LCCN
558     if ($lccn) {
559         my $subfields;
560         $subfields->{1}->{'Subfield_Mark'}='a';
561         $subfields->{1}->{'Subfield_Value'}=$lccn;
562         my $tag='010';
563         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
564     }
565 # ISSN
566     if ($issn) {
567         my $subfields;
568         $subfields->{1}->{'Subfield_Mark'}='a';
569         $subfields->{1}->{'Subfield_Value'}=$issn;
570         my $tag='022';
571         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
572     }
573 # DEWEY
574     if ($dewey) {
575         my $subfields;
576         $subfields->{1}->{'Subfield_Mark'}='a';
577         $subfields->{1}->{'Subfield_Value'}=$dewey;
578         my $tag='082';
579         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
580     }
581 # DEWEY subclass and itemtype
582     {
583         my $subfields;
584         $subfields->{1}->{'Subfield_Mark'}='a';
585         $subfields->{1}->{'Subfield_Value'}=$itemtype;
586         $subfields->{2}->{'Subfield_Mark'}='b';
587         $subfields->{2}->{'Subfield_Value'}=$subclass;
588         $subfields->{3}->{'Subfield_Mark'}='c';
589         $subfields->{3}->{'Subfield_Value'}=$biblionumber;
590         $subfields->{4}->{'Subfield_Mark'}='d';
591         $subfields->{4}->{'Subfield_Value'}=$biblioitemnumber;
592         my $tag='090';
593         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
594     }
595 # PUBLISHER
596     {
597         my $subfields;
598         $subfields->{1}->{'Subfield_Mark'}='a';
599         $subfields->{1}->{'Subfield_Value'}=$place;
600         $subfields->{2}->{'Subfield_Mark'}='b';
601         $subfields->{2}->{'Subfield_Value'}=$publishercode;
602         $subfields->{3}->{'Subfield_Mark'}='c';
603         $subfields->{3}->{'Subfield_Value'}=$publicationyear;
604         if ($copyrightdate) {
605             $subfields->{4}->{'Subfield_Mark'}='c';
606             $subfields->{4}->{'Subfield_Value'}="c$copyrightdate";
607         }
608         my $tag='260';
609         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
610     }
611 # PHYSICAL
612     if ($pages || $size) {
613         my $subfields;
614         $subfields->{1}->{'Subfield_Mark'}='a';
615         $subfields->{1}->{'Subfield_Value'}=$pages;
616         $subfields->{2}->{'Subfield_Mark'}='c';
617         $subfields->{2}->{'Subfield_Value'}=$size;
618         my $tag='300';
619         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
620     }
621 # Volume/Number
622     if ($volume || $number) {
623         my $subfields;
624         $subfields->{1}->{'Subfield_Mark'}='v';
625         $subfields->{1}->{'Subfield_Value'}=$volume;
626         $subfields->{2}->{'Subfield_Mark'}='n';
627         $subfields->{2}->{'Subfield_Value'}=$number;
628         my $tag='440';
629         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
630     }
631 # Biblioitem Note
632     if ($notes) {
633         my $subfields;
634         $subfields->{1}->{'Subfield_Mark'}='a';
635         $subfields->{1}->{'Subfield_Value'}=$notes;
636         $subfields->{2}->{'Subfield_Mark'}='3';
637         $subfields->{2}->{'Subfield_Value'}='biblioitem';
638         my $tag='500';
639         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
640     }
641     $sth->finish;
642     $dbh->disconnect;
643     return ($env, $Record_ID);
644 }
645
646 sub updateBiblioItem {
647 # Update the biblioitem with biblioitemnumber $biblioitem->{'biblioitemnumber'}
648 #
649 # This routine should also check to see which fields are actually being
650 # modified, and log all changes.
651
652     my ($env, $biblioitem) = @_;
653     my $dbh=&C4Connect;  
654
655     my $biblioitemnumber=$biblioitem->{'biblioitemnumber'};
656     my $sth=$dbh->prepare("select * from biblioitems where biblioitemnumber=$biblioitemnumber");
657 # obi = original biblioitem
658     my $obi=$sth->fetchrow_hashref;
659     $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");
660     $sth->execute;
661     my ($Record_ID) = $sth->fetchrow;
662     if ($biblioitem->{'biblionumber'} ne $obi->{'biblionumber'}) {
663         logchange('kohadb', 'change', 'biblioitems', 'biblionumber', $obi->{'biblionumber'}, $biblioitem->{'biblionumber'});
664         my $sth=$dbh->prepare("update biblioitems set biblionumber=$biblioitem->{'biblionumber'} where biblioitemnumber=$biblioitemnumber");
665         logchange('marc', 'change', $Record_ID, '090', 'c', $obi->{'biblionumber'}, $biblioitem->{'biblionumber'});
666         changeSubfield($Record_ID, '090', 'c', $obi->{'biblionumber'}, $biblioitem->{'biblionumber'});
667     }
668     if ($biblioitem->{'volume'} ne $obi->{'volume'}) {
669         logchange('kohadb', 'change', 'biblioitems', 'volume', $obi->{'volume'}, $biblioitem->{'volume'});
670         my $q_volume=$dbh->quote($biblioitem->{'volume'});
671         my $sth=$dbh->prepare("update biblioitems set volume=$q_volume where biblioitemnumber=$biblioitemnumber");
672         logchange('marc', 'change', $Record_ID, '440', 'v', $obi->{'volume'}, $biblioitem->{'volume'});
673         changeSubfield($Record_ID, '440', 'v', $obi->{'volume'}, $biblioitem->{'volume'});
674     }
675     if ($biblioitem->{'number'} ne $obi->{'number'}) {
676         logchange('kohadb', 'change', 'biblioitems', 'number', $obi->{'number'}, $biblioitem->{'number'});
677         my $q_number=$dbh->quote($biblioitem->{'number'});
678         my $sth=$dbh->prepare("update biblioitems set number=$q_number where biblioitemnumber=$biblioitemnumber");
679         logchange('marc', 'change', $Record_ID, '440', 'v', $obi->{'number'}, $biblioitem->{'number'});
680         changeSubfield($Record_ID, '440', 'v', $obi->{'number'}, $biblioitem->{'number'});
681     }
682     if ($biblioitem->{'itemtype'} ne $obi->{'itemtype'}) {
683         logchange('kohadb', 'change', 'biblioitems', 'itemtype', $obi->{'itemtype'}, $biblioitem->{'itemtype'});
684         my $q_itemtype=$dbh->quote($biblioitem->{'itemtype'});
685         my $sth=$dbh->prepare("update biblioitems set itemtype=$q_itemtype where biblioitemnumber=$biblioitemnumber");
686         logchange('marc', 'change', $Record_ID, '090', 'a', $obi->{'itemtype'}, $biblioitem->{'itemtype'});
687         changeSubfield($Record_ID, '090', 'a', $obi->{'itemtype'}, $biblioitem->{'itemtype'});
688     }
689     if ($biblioitem->{'isbn'} ne $obi->{'isbn'}) {
690         logchange('kohadb', 'change', 'biblioitems', 'isbn', $obi->{'isbn'}, $biblioitem->{'isbn'});
691         my $q_isbn=$dbh->quote($biblioitem->{'isbn'});
692         my $sth=$dbh->prepare("update biblioitems set isbn=$q_isbn where biblioitemnumber=$biblioitemnumber");
693         logchange('marc', 'change', $Record_ID, '020', 'a', $obi->{'isbn'}, $biblioitem->{'isbn'});
694         changeSubfield($Record_ID, '020', 'a', $obi->{'isbn'}, $biblioitem->{'isbn'});
695     }
696     if ($biblioitem->{'issn'} ne $obi->{'issn'}) {
697         logchange('kohadb', 'change', 'biblioitems', 'issn', $obi->{'issn'}, $biblioitem->{'issn'});
698         my $q_issn=$dbh->quote($biblioitem->{'issn'});
699         my $sth=$dbh->prepare("update biblioitems set issn=$q_issn where biblioitemnumber=$biblioitemnumber");
700         logchange('marc', 'change', $Record_ID, '022', 'a', $obi->{'issn'}, $biblioitem->{'issn'});
701         changeSubfield($Record_ID, '022', 'a', $obi->{'issn'}, $biblioitem->{'issn'});
702     }
703     if ($biblioitem->{'dewey'} ne $obi->{'dewey'}) {
704         logchange('kohadb', 'change', 'biblioitems', 'dewey', $obi->{'dewey'}, $biblioitem->{'dewey'});
705         my $sth=$dbh->prepare("update biblioitems set dewey=$biblioitem->{'dewey'} where biblioitemnumber=$biblioitemnumber");
706         logchange('marc', 'change', $Record_ID, '082', 'a', $obi->{'dewey'}, $biblioitem->{'dewey'});
707         changeSubfield($Record_ID, '082', 'a', $obi->{'dewey'}, $biblioitem->{'dewey'});
708     }
709     if ($biblioitem->{'subclass'} ne $obi->{'subclass'}) {
710         logchange('kohadb', 'change', 'biblioitems', 'subclass', $obi->{'subclass'}, $biblioitem->{'subclass'});
711         my $q_subclass=$dbh->quote($biblioitem->{'subclass'});
712         my $sth=$dbh->prepare("update biblioitems set subclass=$q_subclass where biblioitemnumber=$biblioitemnumber");
713         logchange('marc', 'change', $Record_ID, '090', 'b', $obi->{'subclass'}, $biblioitem->{'subclass'});
714         changeSubfield($Record_ID, '090', 'b', $obi->{'subclass'}, $biblioitem->{'subclass'});
715     }
716     if ($biblioitem->{'place'} ne $obi->{'place'}) {
717         logchange('kohadb', 'change', 'biblioitems', 'place', $obi->{'place'}, $biblioitem->{'place'});
718         my $q_place=$dbh->quote($biblioitem->{'place'});
719         my $sth=$dbh->prepare("update biblioitems set place=$q_place where biblioitemnumber=$biblioitemnumber");
720         logchange('marc', 'change', $Record_ID, '260', 'a', $obi->{'place'}, $biblioitem->{'place'});
721         changeSubfield($Record_ID, '260', 'a', $obi->{'place'}, $biblioitem->{'place'});
722     }
723     if ($biblioitem->{'publishercode'} ne $obi->{'publishercode'}) {
724         logchange('kohadb', 'change', 'biblioitems', 'publishercode', $obi->{'publishercode'}, $biblioitem->{'publishercode'});
725         my $q_publishercode=$dbh->quote($biblioitem->{'publishercode'});
726         my $sth=$dbh->prepare("update biblioitems set publishercode=$q_publishercode where biblioitemnumber=$biblioitemnumber");
727         logchange('marc', 'change', $Record_ID, '260', 'b', $obi->{'publishercode'}, $biblioitem->{'publishercode'});
728         changeSubfield($Record_ID, '260', 'b', $obi->{'publishercode'}, $biblioitem->{'publishercode'});
729     }
730     if ($biblioitem->{'publicationyear'} ne $obi->{'publicationyear'}) {
731         logchange('kohadb', 'change', 'biblioitems', 'publicationyear', $obi->{'publicationyear'}, $biblioitem->{'publicationyear'});
732         my $q_publicationyear=$dbh->quote($biblioitem->{'publicationyear'});
733         my $sth=$dbh->prepare("update biblioitems set publicationyear=$q_publicationyear where biblioitemnumber=$biblioitemnumber");
734         logchange('marc', 'change', $Record_ID, '260', 'c', $obi->{'publicationyear'}, $biblioitem->{'publicationyear'});
735         changeSubfield($Record_ID, '260', 'c', $obi->{'publicationyear'}, $biblioitem->{'publicationyear'});
736     }
737     if ($biblioitem->{'illus'} ne $obi->{'illus'}) {
738         logchange('kohadb', 'change', 'biblioitems', 'illus', $obi->{'illus'}, $biblioitem->{'illus'});
739         my $q_illus=$dbh->quote($biblioitem->{'illus'});
740         my $sth=$dbh->prepare("update biblioitems set illus=$q_illus where biblioitemnumber=$biblioitemnumber");
741         logchange('marc', 'change', $Record_ID, '700', 'a', $obi->{'illus'}, $biblioitem->{'illus'});
742         changeSubfield($Record_ID, '700', 'a', $obi->{'illus'}, $biblioitem->{'illus'});
743     }
744     if ($biblioitem->{'pages'} ne $obi->{'pages'}) {
745         logchange('kohadb', 'change', 'biblioitems', 'pages', $obi->{'pages'}, $biblioitem->{'pages'});
746         my $q_pages=$dbh->quote($biblioitem->{'pages'});
747         my $sth=$dbh->prepare("update biblioitems set pages=$q_pages where biblioitemnumber=$biblioitemnumber");
748         logchange('marc', 'change', $Record_ID, '300', 'a', $obi->{'pages'}, $biblioitem->{'pages'});
749         changeSubfield($Record_ID, '300', 'a', $obi->{'pages'}, $biblioitem->{'pages'});
750     }
751     if ($biblioitem->{'size'} ne $obi->{'size'}) {
752         logchange('kohadb', 'change', 'biblioitems', 'size', $obi->{'size'}, $biblioitem->{'size'});
753         my $q_size=$dbh->quote($biblioitem->{'size'});
754         my $sth=$dbh->prepare("update biblioitems set size=$q_size where biblioitemnumber=$biblioitemnumber");
755         logchange('marc', 'change', $Record_ID, '300', 'c', $obi->{'size'}, $biblioitem->{'size'});
756         changeSubfield($Record_ID, '300', 'c', $obi->{'size'}, $biblioitem->{'size'});
757     }
758     if ($biblioitem->{'notes'} ne $obi->{'notes'}) {
759         logchange('kohadb', 'change', 'biblioitems', 'notes', $obi->{'notes'}, $biblioitem->{'notes'});
760         my $q_notes=$dbh->quote($biblioitem->{'notes'});
761         my $sth=$dbh->prepare("update biblioitems set notes=$q_notes where biblioitemnumber=$biblioitemnumber");
762         logchange('marc', 'change', $Record_ID, '500', 'a', $obi->{'notes'}, $biblioitem->{'notes'});
763         changeSubfield($Record_ID, '500', 'a', $obi->{'notes'}, $biblioitem->{'notes'});
764     }
765     if ($biblioitem->{'lccn'} ne $obi->{'lccn'}) {
766         logchange('kohadb', 'change', 'biblioitems', 'lccn', $obi->{'lccn'}, $biblioitem->{'lccn'});
767         my $q_lccn=$dbh->quote($biblioitem->{'lccn'});
768         my $sth=$dbh->prepare("update biblioitems set lccn=$q_lccn where biblioitemnumber=$biblioitemnumber");
769         logchange('marc', 'change', $Record_ID, '010', 'a', $obi->{'lccn'}, $biblioitem->{'lccn'});
770         changeSubfield($Record_ID, '010', 'a', $obi->{'lccn'}, $biblioitem->{'lccn'});
771     }
772
773 }
774
775
776 sub newItem {
777     my ($env, $Record_ID, $item) = @_;
778     my $dbh=&C4Connect;  
779     my $barcode=$item->{'barcode'};
780     my $q_barcode=$dbh->quote($barcode);
781     my $biblionumber=$item->{'biblionumber'};
782     my $biblioitemnumber=$item->{'biblioitemnumber'};
783     my $dateaccessioned=$item->{'dateaccessioned'};
784     my $booksellerid=$item->{'booksellerid'};
785     my $q_booksellerid=$dbh->quote($booksellerid);
786     my $homebranch=$item->{'homebranch'};
787     my $q_homebranch=$dbh->quote($homebranch);
788     my $holdingbranch=$item->{'holdingbranch'};
789     my $price=$item->{'price'};
790     my $replacementprice=$item->{'replacementprice'};
791     my $replacementpricedate=$item->{'replacementpricedate'};
792     my $q_replacementpricedate=$dbh->quote($replacementpricedate);
793     my $notforloan=$item->{'notforloan'};
794     my $itemlost=$item->{'itemlost'};
795     my $wthdrawn=$item->{'wthdrawn'};
796     my $restricted=$item->{'restricted'};
797     my $itemnotes=$item->{'itemnotes'};
798     my $q_itemnotes=$dbh->quote($itemnotes);
799     my $itemtype=$item->{'itemtype'};
800     my $subclass=$item->{'subclass'};
801
802 # KOHADB Section
803
804     unless ($env->{'marconly'}) {
805         my $sth=$dbh->prepare("select max(itemnumber) from items");
806         $sth->execute;
807         my ($itemnumber) =$sth->fetchrow;
808         $itemnumber++;
809         $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)");
810         $sth->execute;
811     }
812
813
814 # MARC SECTION
815     my $subfields;
816     $subfields->{1}->{'Subfield_Mark'}='p';
817     $subfields->{1}->{'Subfield_Value'}=$barcode;
818     $subfields->{2}->{'Subfield_Mark'}='d';
819     $subfields->{2}->{'Subfield_Value'}=$dateaccessioned;
820     $subfields->{3}->{'Subfield_Mark'}='e';
821     $subfields->{3}->{'Subfield_Value'}=$booksellerid;
822     $subfields->{4}->{'Subfield_Mark'}='b';
823     $subfields->{4}->{'Subfield_Value'}=$homebranch;
824     $subfields->{5}->{'Subfield_Mark'}='l';
825     $subfields->{5}->{'Subfield_Value'}=$holdingbranch;
826     $subfields->{6}->{'Subfield_Mark'}='c';
827     $subfields->{6}->{'Subfield_Value'}=$price;
828     $subfields->{7}->{'Subfield_Mark'}='c';
829     $subfields->{7}->{'Subfield_Value'}=$replacementprice;
830     $subfields->{8}->{'Subfield_Mark'}='d';
831     $subfields->{8}->{'Subfield_Value'}=$replacementpricedate;
832     if ($notforloan) {
833         $subfields->{9}->{'Subfield_Mark'}='h';
834         $subfields->{9}->{'Subfield_Value'}='Not for loan';
835     }
836     if ($notforloan) {
837         $subfields->{10}->{'Subfield_Mark'}='j';
838         $subfields->{10}->{'Subfield_Value'}='Item lost';
839     }
840     if ($notforloan) {
841         $subfields->{11}->{'Subfield_Mark'}='j';
842         $subfields->{11}->{'Subfield_Value'}='Item withdrawn';
843     }
844     if ($notforloan) {
845         $subfields->{12}->{'Subfield_Mark'}='z';
846         $subfields->{12}->{'Subfield_Value'}=$itemnotes;
847     }
848     my $tag='876';
849     my $Tag_ID;
850     $env->{'linkage'}=1;
851     ($env, $Tag_ID) = addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
852     $env->{'linkage'}=0;
853     $env->{'linkid'}=$Tag_ID;
854     $tag='852';
855     my $subfields2;
856     $subfields2->{1}->{'Subfield_Mark'}='a';
857     $subfields2->{1}->{'Subfield_Value'}='Coast Mountains School District';
858     $subfields2->{1}->{'Subfield_Mark'}='b';
859     $subfields2->{1}->{'Subfield_Value'}=$homebranch;
860     $subfields2->{1}->{'Subfield_Mark'}='c';
861     $subfields2->{1}->{'Subfield_Value'}=$itemtype;
862     $subfields2->{2}->{'Subfield_Mark'}='m';
863     $subfields2->{2}->{'Subfield_Value'}=$subclass;
864     addTag($env, $Record_ID, $tag, ' ', ' ', $subfields2);
865     $env->{'linkid'}='';
866 }
867
868 sub updateItem {
869 # Update the item with itemnumber $item->{'itemnumber'}
870 # This routine should also modify the corresponding MARC record data. (852 and
871 # 876 tags with 876p tag the same as $item->{'barcode'}
872 #
873 # This routine should also check to see which fields are actually being
874 # modified, and log all changes.
875
876     my ($env, $item) = @_;
877     my $dbh=&C4Connect;  
878     my $itemnumber=$item->{'itemnumber'};
879     my $biblionumber=$item->{'biblionumber'};
880     my $biblioitemnumber=$item->{'biblioitemnumber'};
881     my $barcode=$item->{'barcode'};
882     my $dateaccessioned=$item->{'dateaccessioned'};
883     my $booksellerid=$item->{'booksellerid'};
884     my $homebranch=$item->{'homebranch'};
885     my $price=$item->{'price'};
886     my $replacementprice=$item->{'replacementprice'};
887     my $replacementpricedate=$item->{'replacementpricedate'};
888     my $multivolume=$item->{'multivolume'};
889     my $stack=$item->{'stack'};
890     my $notforloan=$item->{'notforloan'};
891     my $itemlost=$item->{'itemlost'};
892     my $wthdrawn=$item->{'wthdrawn'};
893     my $bulk=$item->{'bulk'};
894     my $restricted=$item->{'restricted'};
895     my $binding=$item->{'binding'};
896     my $itemnotes=$item->{'itemnotes'};
897     my $holdingbranch=$item->{'holdingbranch'};
898     my $interim=$item->{'interim'};
899     my $sth=$dbh->prepare("select * from items where itemnumber=$itemnumber");
900     $sth->execute;
901     my $olditem=$sth->fetchrow_hashref;
902     my $q_barcode=$dbh->quote($olditem->{'barcode'});
903     $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");
904     $sth->execute;
905     my ($Subfield876_ID, $Record_ID) = $sth->fetchrow;
906     $sth=$dbh->prepare("select Subfield_Value from 8XX_Subfield_Table where Subfield_Mark=8 and Subfield_ID=$Subfield876_ID");
907     $sth->execute;
908     my ($link) = $sth->fetchrow;
909     $sth=$dbh->prepare("select Subfield_ID from 8XX_Subfield_Table where Subfield_Mark=8 and Subfield_Value=$link and !(Subfield_ID=$Subfield876_ID)");
910     $sth->execute;
911     my ($Subfield852_ID) = $sth->fetchrow;
912     
913     if ($item->{'barcode'} ne $olditem->{'barcode'}) {
914         logchange('kohadb', 'change', 'items', 'barcode', $olditem->{'barcode'}, $item->{'barcode'});
915         my $q_barcode=$dbh->quote($item->{'barcode'});
916         my $sth=$dbh->prepare("update items set barcode=$q_barcode where itemnumber=$itemnumber");
917         $sth->execute;
918         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'p', $olditem->{'barcode'}, $item->{'barcode'}, $Subfield876_ID);
919         logchange('marc', 'change', $Record_ID, '876', 'p', $Subfield_Key, $olditem->{'barcode'}, $item->{'barcode'});
920     }
921     if ($item->{'booksellerid'} ne $olditem->{'booksellerid'}) {
922         logchange('kohadb', 'change', 'items', 'booksellerid', $olditem->{'booksellerid'}, $item->{'booksellerid'});
923         my $q_booksellerid=$dbh->quote($item->{'booksellerid'});
924         my $sth=$dbh->prepare("update items set booksellerid=$q_booksellerid where itemnumber=$itemnumber");
925         $sth->execute;
926         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'e', $olditem->{'booksellerid'}, $item->{'booksellerid'}, $Subfield876_ID);
927         logchange('marc', 'change', $Record_ID, '876', 'e', $Subfield_Key, $olditem->{'booksellerid'}, $item->{'booksellerid'});
928     }
929     if ($item->{'dateaccessioned'} ne $olditem->{'dateaccessioned'}) {
930         logchange('kohadb', 'change', 'items', 'dateaccessioned', $olditem->{'dateaccessioned'}, $item->{'dateaccessioned'});
931         my $q_dateaccessioned=$dbh->quote($item->{'dateaccessioned'});
932         my $sth=$dbh->prepare("update items set dateaccessioned=$q_dateaccessioned where itemnumber=$itemnumber");
933         $sth->execute;
934         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'd', $olditem->{'dateaccessioned'}, $item->{'dateaccessioned'}, $Subfield876_ID);
935         logchange('marc', 'change', $Record_ID, '876', 'd', $Subfield_Key, $olditem->{'dateaccessioned'}, $item->{'dateaccessioned'});
936     }
937     if ($item->{'homebranch'} ne $olditem->{'homebranch'}) {
938         logchange('kohadb', 'change', 'items', 'homebranch', $olditem->{'homebranch'}, $item->{'homebranch'});
939         my $q_homebranch=$dbh->quote($item->{'homebranch'});
940         my $sth=$dbh->prepare("update items set homebranch=$q_homebranch where itemnumber=$itemnumber");
941         $sth->execute;
942         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'b', $olditem->{'homebranch'}, $item->{'homebranch'}, $Subfield876_ID);
943         logchange('marc', 'change', $Record_ID, '876', 'b', $Subfield_Key, $olditem->{'homebranch'}, $item->{'homebranch'});
944     }
945     if ($item->{'holdingbranch'} ne $olditem->{'holdingbranch'}) {
946         logchange('kohadb', 'change', 'items', 'holdingbranch', $olditem->{'holdingbranch'}, $item->{'holdingbranch'});
947         my $q_holdingbranch=$dbh->quote($item->{'holdingbranch'});
948         my $sth=$dbh->prepare("update items set holdingbranch=$q_holdingbranch where itemnumber=$itemnumber");
949         $sth->execute;
950         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'l', $olditem->{'holdingbranch'}, $item->{'holdingbranch'}, $Subfield876_ID);
951         logchange('marc', 'change', $Record_ID, '876', 'l', $Subfield_Key, $olditem->{'holdingbranch'}, $item->{'holdingbranch'});
952     }
953     if ($item->{'price'} ne $olditem->{'price'}) {
954         logchange('kohadb', 'change', 'items', 'price', $olditem->{'price'}, $item->{'price'});
955         my $q_price=$dbh->quote($item->{'price'});
956         my $sth=$dbh->prepare("update items set price=$q_price where itemnumber=$itemnumber");
957         $sth->execute;
958         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'c', $olditem->{'price'}, $item->{'price'}, $Subfield876_ID);
959         logchange('marc', 'change', $Record_ID, '876', 'c', $Subfield_Key, $olditem->{'price'}, $item->{'price'});
960     }
961     if ($item->{'itemnotes'} ne $olditem->{'itemnotes'}) {
962         logchange('kohadb', 'change', 'items', 'itemnotes', $olditem->{'itemnotes'}, $item->{'itemnotes'});
963         my $q_itemnotes=$dbh->quote($item->{'itemnotes'});
964         my $sth=$dbh->prepare("update items set itemnotes=$q_itemnotes where itemnumber=$itemnumber");
965         $sth->execute;
966         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'c', $olditem->{'itemnotes'}, $item->{'itemnotes'}, $Subfield876_ID);
967         logchange('marc', 'change', $Record_ID, '876', 'c', $Subfield_Key, $olditem->{'itemnotes'}, $item->{'itemnotes'});
968     }
969     if ($item->{'notforloan'} ne $olditem->{'notforloan'}) {
970         logchange('kohadb', 'change', 'items', 'notforloan', $olditem->{'notforloan'}, $item->{'notforloan'});
971         my $sth=$dbh->prepare("update items set notforloan=$notforloan where itemnumber=$itemnumber");
972         $sth->execute;
973         if ($item->{'notforloan'}) {
974             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Not for loan', $Subfield876_ID);
975             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Not for loan');
976         } else {
977             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Not for loan', $Subfield876_ID);
978             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Not for loan');
979         }
980     }
981     if ($item->{'itemlost'} ne $olditem->{'itemlost'}) {
982         logchange('kohadb', 'change', 'items', 'itemlost', $olditem->{'itemlost'}, $item->{'itemlost'});
983         my $sth=$dbh->prepare("update items set itemlost=$itemlost where itemnumber=$itemnumber");
984         $sth->execute;
985         if ($item->{'itemlost'}) {
986             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Item lost', $Subfield876_ID);
987             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Item lost');
988         } else {
989             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Item lost', $Subfield876_ID);
990             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Item lost');
991         }
992     }
993     if ($item->{'wthdrawn'} ne $olditem->{'wthdrawn'}) {
994         logchange('kohadb', 'change', 'items', 'wthdrawn', $olditem->{'wthdrawn'}, $item->{'wthdrawn'});
995         my $sth=$dbh->prepare("update items set wthdrawn=$wthdrawn where itemnumber=$itemnumber");
996         $sth->execute;
997         if ($item->{'wthdrawn'}) {
998             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Withdrawn', $Subfield876_ID);
999             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Withdrawn');
1000         } else {
1001             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Withdrawn', $Subfield876_ID);
1002             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Withdrawn');
1003         }
1004     }
1005     if ($item->{'restricted'} ne $olditem->{'restricted'}) {
1006         logchange('kohadb', 'change', 'items', 'restricted', $olditem->{'restricted'}, $item->{'restricted'});
1007         my $sth=$dbh->prepare("update items set restricted=$restricted where itemnumber=$itemnumber");
1008         $sth->execute;
1009         if ($item->{'restricted'}) {
1010             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Restricted', $Subfield876_ID);
1011             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Restricted');
1012         } else {
1013             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Restricted', $Subfield876_ID);
1014             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Restricted');
1015         }
1016     }
1017 }
1018
1019 END { }       # module clean-up code here (global destructor)