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