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