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