Going to MARC 1.4...
[koha.git] / C4 / Biblio.pm
1 package C4::Biblio; 
2
3 # Contains all sub used for biblio management. tables :
4 # biblio, biblioitems, items
5 # bibliosubject, bibliosubtitle
6
7 use strict;
8 require Exporter;
9 use C4::Database;
10 use MARC::Record;
11
12 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
13
14 # set the version for version checking
15 $VERSION = 0.01;
16
17 @ISA = qw(Exporter);
18 @EXPORT = qw(
19              &MARCmodsubfield &MARCaddsubfield &MARCfindsubfield 
20              &MARCaddbiblio &MARCnextsubfieldid &MARCkoha2marc
21              &MARCgetbiblio
22
23              &newBiblio &newBiblioItem &newItem 
24              &updateBiblio &updateBiblioItem &updateItem 
25              &itemcount &newbiblio &newbiblioitem 
26              &modnote &newsubject &newsubtitle
27              &newordernum &modbiblio &checkitems
28              &newitems &modbibitem
29              &modsubtitle &modsubject &modaddauthor &moditem &countitems 
30              &delitem &deletebiblioitem &delbiblio  
31              &getitemtypes &getbiblio
32              &getbiblioitembybiblionumber
33              &getbiblioitem &getitemsbybiblioitem &isbnsearch
34              &skip
35  );
36 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
37
38 # your exported package globals go here,
39 # as well as any optionally exported functions
40
41 @EXPORT_OK   = qw($Var1 %Hashit);
42
43
44 # non-exported package globals go here
45 use vars qw(@more $stuff);
46
47 # initalize package globals, first exported ones
48
49 my $Var1   = '';
50 my %Hashit = ();
51
52
53 # then the others (which are still accessible as $Some::Module::stuff)
54 my $stuff  = '';
55 my @more   = ();
56
57 # all file-scoped lexicals must be created before
58 # the functions below that use them.
59
60 # file-private lexicals go here
61 my $priv_var    = '';
62 my %secret_hash = ();
63
64 # here's a file-private function as a closure,
65 # callable as &$priv_func;  it cannot be prototyped.
66 my $priv_func = sub {
67   # stuff goes here.
68   };
69   
70 # make all your functions, whether exported or not;
71
72 sub MARCaddbiblio {
73 # pass the MARC::Record to this function, and it will create the records in the marc tables
74     my ($record) = @_;
75     my @fields=$record->fields();
76     my $dbh=C4Connect;
77     my $bibid;
78     # adding main table, and retrieving bibid
79     $dbh->do("lock tables marc_biblio WRITE");
80     my $sth=$dbh->prepare("insert into marc_biblio (datecreated) values (now())");
81     $sth->execute;
82     $sth=$dbh->prepare("select max(bibid) from marc_biblio");
83     $sth->execute;
84     ($bibid)=$sth->fetchrow;
85 #    print "BIBID :::".$marcstructure->{bibid}."\n";
86     $sth->finish;
87     $dbh->do("unlock tables");
88     my $fieldcount=0;
89     # now, add subfields...
90     foreach my $field (@fields) {
91         my @subfields=$field->subfields();
92         $fieldcount++;
93         foreach my $subfieldcount (0..$#subfields) {
94             print $field->tag().":".$field->indicator(1).$field->indicator(2).":".$subfields[$subfieldcount][0].":".$subfields[$subfieldcount][1]."\n";
95                     &MARCaddsubfield($bibid,
96                                  $field->tag(),
97                                  $field->indicator(1).$field->indicator(2),
98                                  $fieldcount,
99                                  $subfields[$subfieldcount][0],
100                                  $subfieldcount,
101                                  $subfields[$subfieldcount][1]
102                                  );
103         }
104     }
105 }
106
107 sub MARCgetbiblio {
108 # Returns MARC::Record of the biblio passed in parameter.
109     my ($bibid)=@_;
110     my $dbh=&C4Connect;
111     my $record = MARC::Record->new();
112 #---- TODO : the leader is missing (000 tag ?)
113     my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink 
114                                  from marc_subfield_table 
115                                  where bibid=?
116                          ");
117     my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
118     $sth->execute($bibid);
119     while (my $row=$sth->fetchrow_hashref) {
120         if ($row->{'valuebloblink'}) { #---- search blob if there is one
121             $sth2->execute($row->{'valuebloblink'});
122             my $row2=$sth2->fetchrow_hashref;
123             $sth2->finish;
124             $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
125         }
126         if ($record->field($row->{'tag'})) {
127             my $tag =$record->field($row->{'tag'});
128             if ($tag) {
129                 $tag->add_subfields($row->{'subfieldcode'},$row->{'subfieldvalue'});
130                 $record->delete_field($tag);
131                 $record->add_fields($tag);
132             }
133         } else {
134             $record->add_fields($row->{'tag'}," "," ",$row->{'subfieldcode'} => $row->{'subfieldvalue'});
135         }
136
137     }
138     return $record;
139 }
140
141 sub MARCmodsubfield {
142 # Subroutine changes a subfield value given a subfieldid.
143     my ( $subfieldid, $subfieldvalue )=@_;
144
145     my $dbh=&C4Connect;
146     $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
147     my $sth1=$dbh->prepare("select valuebloblink from marc_subfield_table where subfieldid=?");
148     $sth1->execute($subfieldid);
149     my ($oldvaluebloblink)=$sth1->fetchrow;
150     $sth1->finish;
151     my $sth;
152     # if too long, use a bloblink
153     if (length($subfieldvalue)>255 ) {
154         # if already a bloblink, update it, otherwise, insert a new one.
155         if ($oldvaluebloblink) {
156             $sth=$dbh->prepare("update marc_blob_subfield set subfieldvalue=? where blobidlink=?");
157             $sth->execute($subfieldvalue,$oldvaluebloblink);
158         } else {
159             $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
160             $sth->execute($subfieldvalue);
161             $sth=$dbh->prepare("select max(blobidlink) from marc_blob_subfield");
162             $sth->execute;
163             my ($res)=$sth->fetchrow;
164             $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=null, valuebloblink=$res where subfieldid=?");
165             $sth->execute($subfieldid);
166         }
167     } else {
168         # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
169         $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?");
170         $sth->execute($subfieldvalue, $subfieldid);
171     }
172     $dbh->do("unlock tables");
173     $sth->finish;
174     $dbh->disconnect;
175     return($subfieldid, $subfieldvalue);
176 }
177
178 sub MARCfindsubfield {
179 # returns a subfields number given a bibid/tag/subfield values
180     my ($bibid,$tag,$subfieldcode,$subfieldvalue,$subfieldorder) = @_;
181     my $resultcounter=0;
182     my $subfieldid;
183     my $lastsubfieldid;
184     my $dbh=&C4Connect;
185     my $query="select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
186     if ($subfieldvalue) {
187         $query .= " and subfieldvalue=".$dbh->quote($subfieldvalue);
188     } else {
189         if ($subfieldorder<1) {
190             $subfieldorder=1;
191         }
192         $query .= " and subfieldorder=$subfieldorder";
193     }
194     my $sti=$dbh->prepare($query);
195     $sti->execute($bibid,$tag, $subfieldcode);
196     while (($subfieldid) = $sti->fetchrow) {
197         $resultcounter++;
198         $lastsubfieldid=$subfieldid;
199     }
200     if ($resultcounter>1) {
201         # Error condition.  Values given did not resolve into a unique record.  Don't know what to edit
202         # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
203         return -1;
204     } else {
205         return $lastsubfieldid;
206     }
207 }
208
209 sub MARCaddsubfield {
210 # Add a new subfield to a tag into the DB.
211     my $bibid=shift;
212     my $tagid=shift;
213     my $indicator=shift;
214     my $tagorder=shift;
215     my $subfieldcode=shift;
216     my $subfieldorder=shift;
217     my $subfieldvalue=shift;
218
219     my $dbh=&C4Connect;
220     unless ($subfieldorder) {
221         my $sth=$dbh->prepare("select max(subfieldorder) from marc_subfield_table where tag=$tagid");
222         $sth->execute;
223         if ($sth->rows) {
224             ($subfieldorder) = $sth->fetchrow;
225             $subfieldorder++;
226         } else {
227             $subfieldorder=1;
228         }
229     }
230     if (length($subfieldvalue)>255) {
231         $dbh->do("lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE");
232         my $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
233         $sth->execute($subfieldvalue);
234         $sth=$dbh->prepare("select max(blobidlink)from marc_blob_subfield");
235         $sth->execute;
236         my ($res)=$sth->fetchrow;
237         my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?)");
238         $sth->execute($bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$res);
239         $dbh->do("unlock tables");
240     } else {
241         my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?)");
242         $sth->execute($bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
243     }
244 }
245
246
247 sub MARCkoha2marc {
248 # this function builds MARC::Record from the old koha-DB fields
249     my ($biblionumber,$biblioitemnumber,$itemnumber) = @_;
250     my $dbh=&C4Connect;
251     my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
252     my $record = MARC::Record->new();
253 #--- if bibid, then retrieve old-style koha data
254     if ($biblionumber>0) {
255         my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp 
256                 from biblio where biblionumber=?");             
257         $sth2->execute($biblionumber);
258         my $row=$sth2->fetchrow_hashref;
259         my $code;
260         foreach $code (keys %$row) {
261             if ($row->{$code}) {
262                 &MARCkoha2marcOnefield($sth,$record,"biblio.".$code,$row->{$code});
263             }
264         }
265     }
266 #--- if biblioitem, then retrieve old-style koha data
267     if ($biblioitemnumber>0) {
268         my $sth2=$dbh->prepare(" SELECT biblioitemnumber,biblionumber,volume,number,classification,
269                                                 itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
270                                                 volumedate,volumeddesc,timestamp,illus,pages,notes,size,place 
271                                         FROM biblioitems
272                                         WHERE biblionumber=? and biblioitemnumber=?
273                                         ");             
274         $sth2->execute($biblionumber,$biblioitemnumber);
275         my $row=$sth2->fetchrow_hashref;
276         my $code;
277         foreach $code (keys %$row) {
278             if ($row->{$code}) {
279                 &MARCkoha2marcOnefield($sth,$record,"biblioitem.".$code,$row->{$code});
280             }
281         }
282     }
283 #--- if item, then retrieve old-style koha data
284     if ($itemnumber>0) {
285         my $sth2=$dbh->prepare("SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
286                                                 booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
287                                                 datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,bulk,issues,renewals,
288                                         reserves,restricted,binding,itemnotes,holdingbranch,interim,timestamp 
289                                         FROM items
290                                         WHERE biblionumber=? and itemnumber=?");
291         $sth2->execute($biblionumber,$itemnumber);
292         my $row=$sth2->fetchrow_hashref;
293         my $code;
294         foreach $code (keys %$row) {
295             if ($row->{$code}) {
296                 &MARCkoha2marcOnefield($sth,$record,"items.".$code,$row->{$code});
297             }
298         }
299     }
300     return $record;
301 }
302
303 sub MARCkoha2marcOnefield {
304     my ($sth,$record,$kohafieldname,$value)=@_;
305     my $tagfield;
306     my $tagsubfield;
307     $sth->execute($kohafieldname);
308     if (($tagfield,$tagsubfield)=$sth->fetchrow) {
309         if ($record->field($tagfield)) {
310             my $tag =$record->field($tagfield);
311             if ($tag) {
312                 $tag->add_subfields($tagsubfield,$value);
313                 $record->delete_field($tag);
314                 $record->add_fields($tag);
315             }
316         } else {
317             $record->add_fields($tagfield," "," ",$tagsubfield => $value);
318         }
319     }
320     return $record;
321 }
322
323 sub updateBiblio {
324 # Update the biblio with biblionumber $biblio->{'biblionumber'}
325 # I guess this routine should search through all marc records for a record that
326 # has the same biblionumber stored in it, and modify the MARC record as well as
327 # the biblio table.
328 #
329 # Also, this subroutine should search through the $biblio object and compare it
330 # to the existing record and _LOG ALL CHANGES MADE_ in some way.  I'd like for
331 # this logging feature to be usable to undo changes easily.
332
333     my ($env, $biblio) = @_;
334     my $Record_ID;
335     my $biblionumber=$biblio->{'biblionumber'};
336     my $dbh=&C4Connect;  
337     my $sth=$dbh->prepare("select * from biblio where biblionumber=$biblionumber");
338     $sth->execute;
339     my $origbiblio=$sth->fetchrow_hashref;
340     $sth=$dbh->prepare("select subtitle from bibliosubtitle where biblionumber=$biblionumber");
341     $sth->execute;
342     my ($subtitle)=$sth->fetchrow;
343     $origbiblio->{'subtitle'}=$subtitle;
344     $sth=$dbh->prepare("select author from additionalauthors where biblionumber=$biblionumber");
345     $sth->execute;
346     my $origadditionalauthors;
347     while (my ($author) = $sth->fetchrow) {
348         push (@{$origbiblio->{'additionalauthors'}}, $author);
349         $origadditionalauthors->{$author}=1;
350     }
351     $sth=$dbh->prepare("select subject from bibliosubject where biblionumber=$biblionumber");
352     $sth->execute;
353     my $origsubjects;
354     while (my ($subject) = $sth->fetchrow) {
355         push (@{$origbiblio->{'subjects'}}, $subject);
356         $origsubjects->{$subject}=1;
357     }
358
359     
360 # Obtain a list of MARC Record_ID's that are tied to this biblio
361     $sth=$dbh->prepare("select bibid from marc_subfield_table where tag='090' and subfieldvalue=$biblionumber and subfieldcode='c'");
362     $sth->execute;
363     my @marcrecords;
364     while (my ($bibid) = $sth->fetchrow) {
365         push(@marcrecords, $bibid);
366     }
367
368     my $bibid='';
369     if ($biblio->{'author'} ne $origbiblio->{'author'}) {
370         my $q_author=$dbh->quote($biblio->{'author'});
371         logchange('kohadb', 'change', 'biblio', 'author', $origbiblio->{'author'}, $biblio->{'author'});
372         my $sti=$dbh->prepare("update biblio set author=$q_author where biblionumber=$biblio->{'biblionumber'}");
373         $sti->execute;
374         foreach $bibid (@marcrecords) {
375             logchange('marc', 'change', $bibid, '100', 'a', $origbiblio->{'author'}, $biblio->{'author'});
376             changeSubfield($bibid, '100', 'a', $origbiblio->{'author'}, $biblio->{'author'});
377         }
378     }
379     if ($biblio->{'title'} ne $origbiblio->{'title'}) {
380         my $q_title=$dbh->quote($biblio->{'title'});
381         logchange('kohadb', 'change', 'biblio', 'title', $origbiblio->{'title'}, $biblio->{'title'});
382         my $sti=$dbh->prepare("update biblio set title=$q_title where biblionumber=$biblio->{'biblionumber'}");
383         $sti->execute;
384         foreach $Record_ID (@marcrecords) {
385             logchange('marc', 'change', $Record_ID, '245', 'a', $origbiblio->{'title'}, $biblio->{'title'});
386             changeSubfield($Record_ID, '245', 'a', $origbiblio->{'title'}, $biblio->{'title'});
387         }
388     }
389     if ($biblio->{'subtitle'} ne $origbiblio->{'subtitle'}) {
390         my $q_subtitle=$dbh->quote($biblio->{'subtitle'});
391         logchange('kohadb', 'change', 'biblio', 'subtitle', $origbiblio->{'subtitle'}, $biblio->{'subtitle'});
392         my $sti=$dbh->prepare("update bibliosubtitle set subtitle=$q_subtitle where biblionumber=$biblio->{'biblionumber'}");
393         $sti->execute;
394         foreach $Record_ID (@marcrecords) {
395             logchange('marc', 'change', $Record_ID, '245', 'b', $origbiblio->{'subtitle'}, $biblio->{'subtitle'});
396             changeSubfield($Record_ID, '245', 'b', $origbiblio->{'subtitle'}, $biblio->{'subtitle'});
397         }
398     }
399     if ($biblio->{'unititle'} ne $origbiblio->{'unititle'}) {
400         my $q_unititle=$dbh->quote($biblio->{'unititle'});
401         logchange('kohadb', 'change', 'biblio', 'unititle', $origbiblio->{'unititle'}, $biblio->{'unititle'});
402         my $sti=$dbh->prepare("update biblio set unititle=$q_unititle where biblionumber=$biblio->{'biblionumber'}");
403         $sti->execute;
404     }
405     if ($biblio->{'notes'} ne $origbiblio->{'notes'}) {
406         my $q_notes=$dbh->quote($biblio->{'notes'});
407         logchange('kohadb', 'change', 'biblio', 'notes', $origbiblio->{'notes'}, $biblio->{'notes'});
408         my $sti=$dbh->prepare("update biblio set notes=$q_notes where biblionumber=$biblio->{'biblionumber'}");
409         $sti->execute;
410         foreach $Record_ID (@marcrecords) {
411             logchange('marc', 'change', $Record_ID, '500', 'a', $origbiblio->{'notes'}, $biblio->{'notes'});
412             changeSubfield($Record_ID, '500', 'a', $origbiblio->{'notes'}, $biblio->{'notes'});
413         }
414     }
415     if ($biblio->{'serial'} ne $origbiblio->{'serial'}) {
416         my $q_serial=$dbh->quote($biblio->{'serial'});
417         logchange('kohadb', 'change', 'biblio', 'serial', $origbiblio->{'serial'}, $biblio->{'serial'});
418         my $sti=$dbh->prepare("update biblio set serial=$q_serial where biblionumber=$biblio->{'biblionumber'}");
419         $sti->execute;
420     }
421     if ($biblio->{'seriestitle'} ne $origbiblio->{'seriestitle'}) {
422         my $q_seriestitle=$dbh->quote($biblio->{'seriestitle'});
423         logchange('kohadb', 'change', 'biblio', 'seriestitle', $origbiblio->{'seriestitle'}, $biblio->{'seriestitle'});
424         my $sti=$dbh->prepare("update biblio set seriestitle=$q_seriestitle where biblionumber=$biblio->{'biblionumber'}");
425         $sti->execute;
426         foreach $Record_ID (@marcrecords) {
427             logchange('marc', 'change', $Record_ID, '440', 'a', $origbiblio->{'seriestitle'}, $biblio->{'seriestitle'});
428             changeSubfield($Record_ID, '440', 'a', $origbiblio->{'seriestitle'}, $biblio->{'seriestitle'});
429         }
430     }
431     if ($biblio->{'copyrightdate'} ne $origbiblio->{'copyrightdate'}) {
432         my $q_copyrightdate=$dbh->quote($biblio->{'copyrightdate'});
433         logchange('kohadb', 'change', 'biblio', 'copyrightdate', $origbiblio->{'copyrightdate'}, $biblio->{'copyrightdate'});
434         my $sti=$dbh->prepare("update biblio set copyrightdate=$q_copyrightdate where biblionumber=$biblio->{'biblionumber'}");
435         $sti->execute;
436         foreach $Record_ID (@marcrecords) {
437             logchange('marc', 'change', $Record_ID, '260', 'c', "c$origbiblio->{'notes'}", "c$biblio->{'notes'}");
438             changeSubfield($Record_ID, '260', 'c', "c$origbiblio->{'notes'}", "c$biblio->{'notes'}");
439         }
440     }
441
442 # Check for subject heading changes
443     
444     my $newsubject='';
445     my $subjects;
446     foreach $newsubject (@{$biblio->{'subject'}}) {
447         $subjects->{$newsubject}=1;
448         if ($origsubjects->{$newsubject}) {
449             $subjects->{$newsubject}=2;
450         } else {
451             my $q_newsubject=$dbh->quote($newsubject);
452             my $sth=$dbh->prepare("insert into bibliosubject (subject,biblionumber) values ($q_newsubject, $biblionumber)");
453             $sth->execute;
454             logchange('kohadb', 'add', 'biblio', 'subject', $newsubject);
455             my $subfields;
456             $subfields->{1}->{'Subfield_Mark'}='a';
457             $subfields->{1}->{'Subfield_Value'}=$newsubject;
458             my $tag='650';
459             my $Record_ID;
460             foreach $Record_ID (@marcrecords) {
461                 addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
462                 logchange('marc', 'add', $Record_ID, '650', 'a', $newsubject);
463             }
464         }
465     }
466     my $origsubject;
467     foreach $origsubject (keys %$origsubjects) {
468         if ($subjects->{$origsubject} == 1) {
469             my $q_origsubject=$dbh->quote($origsubject);
470             logchange('kohadb', 'delete', 'biblio', '$biblionumber', 'subject', $origsubject);
471             my $sth=$dbh->prepare("delete from bibliosubject where biblionumber=$biblionumber and subject=$q_origsubject");
472             $sth->execute;
473         }
474     }
475
476
477 sub newBiblioItem {
478     my ($env, $biblioitem) = @_;
479     my $dbh=&C4Connect;  
480     my $biblionumber=$biblioitem->{'biblionumber'};
481     my $biblioitemnumber=$biblioitem->{'biblioitemnumber'};
482     my $volume=$biblioitem->{'volume'};
483     my $q_volume=$dbh->quote($volume);
484     my $number=$biblioitem->{'number'};
485     my $q_number=$dbh->quote($number);
486     my $classification=$biblioitem->{'classification'};
487     my $q_classification=$dbh->quote($classification);
488     my $itemtype=$biblioitem->{'itemtype'};
489     my $q_itemtype=$dbh->quote($itemtype);
490     my $isbn=$biblioitem->{'isbn'};
491     my $q_isbn=$dbh->quote($isbn);
492     my $issn=$biblioitem->{'issn'};
493     my $q_issn=$dbh->quote($issn);
494     my $dewey=$biblioitem->{'dewey'};
495     $dewey=~s/\.*0*$//;
496     ($dewey == 0) && ($dewey='');
497     my $subclass=$biblioitem->{'subclass'};
498     my $q_subclass=$dbh->quote($subclass);
499     my $publicationyear=$biblioitem->{'publicationyear'};
500     my $publishercode=$biblioitem->{'publishercode'};
501     my $q_publishercode=$dbh->quote($publishercode);
502     my $volumedate=$biblioitem->{'volumedate'};
503     my $q_volumedate=$dbh->quote($volumedate);
504     my $illus=$biblioitem->{'illus'};
505     my $q_illus=$dbh->quote($illus);
506     my $pages=$biblioitem->{'pages'};
507     my $q_pages=$dbh->quote($pages);
508     my $notes=$biblioitem->{'notes'};
509     my $q_notes=$dbh->quote($notes);
510     my $size=$biblioitem->{'size'};
511     my $q_size=$dbh->quote($size);
512     my $place=$biblioitem->{'place'};
513     my $q_place=$dbh->quote($place);
514     my $lccn=$biblioitem->{'lccn'};
515     my $q_lccn=$dbh->quote($lccn);
516
517
518 # Unless the $env->{'marconly'} flag is set, update the biblioitems table with
519 # the new data
520
521     unless ($env->{'marconly'}) {
522         #my $sth=$dbh->prepare("lock tables biblioitems write");
523         #$sth->execute;
524         my $sth=$dbh->prepare("select max(biblioitemnumber) from biblioitems");
525         $sth->execute;
526         my ($biblioitemnumber) =$sth->fetchrow;
527         $biblioitemnumber++;
528         $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)");
529         $sth->execute;
530         #my $sth=$dbh->prepare("unlock tables");
531         #$sth->execute;
532     }
533
534
535 # Should we check if there is already a biblioitem/marc with the
536 # same isbn/lccn/issn?
537
538     my $sth=$dbh->prepare("select title,unititle,seriestitle,copyrightdate,notes,author from biblio where biblionumber=$biblionumber");
539     $sth->execute;
540     my ($title, $unititle,$seriestitle,$copyrightdate,$biblionotes,$author) = $sth->fetchrow;
541     $sth=$dbh->prepare("select subtitle from bibliosubtitle where biblionumber=$biblionumber");
542     $sth->execute;
543     my ($subtitle) = $sth->fetchrow;
544     $sth=$dbh->prepare("select author from additionalauthors where biblionumber=$biblionumber");
545     $sth->execute;
546     my @additionalauthors;
547     while (my ($additionalauthor) = $sth->fetchrow) {
548         push (@additionalauthors, $additionalauthor);
549     }
550     $sth=$dbh->prepare("select subject from bibliosubject where biblionumber=$biblionumber");
551     $sth->execute;
552     my @subjects;
553     while (my ($subject) = $sth->fetchrow) {
554         push (@subjects, $subject);
555     }
556
557 # MARC SECTION
558
559     $sth=$dbh->prepare("insert into Resource_Table (Record_ID) values (0)");
560     $sth->execute;
561     my $Resource_ID=$dbh->{'mysql_insertid'};
562     my $Record_ID=$Resource_ID;
563     $sth=$dbh->prepare("update Resource_Table set Record_ID=$Record_ID where Resource_ID=$Resource_ID");
564     $sth->execute;
565
566 # Title
567     {
568         my $subfields;
569         $subfields->{1}->{'Subfield_Mark'}='a';
570         $subfields->{1}->{'Subfield_Value'}=$title;
571         if ($subtitle) {
572             $subfields->{2}->{'Subfield_Mark'}='b';
573             $subfields->{2}->{'Subfield_Value'}=$subtitle;
574         }
575         my $tag='245';
576         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
577     }
578
579 # author
580     {
581         my $subfields;
582         $subfields->{1}->{'Subfield_Mark'}='a';
583         $subfields->{1}->{'Subfield_Value'}=$author;
584         my $tag='100';
585         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
586     }
587 # Series Title
588     if ($seriestitle) {
589         my $subfields;
590         $subfields->{1}->{'Subfield_Mark'}='a';
591         $subfields->{1}->{'Subfield_Value'}=$seriestitle;
592         my $tag='440';
593         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
594     }
595 # Biblio Note
596     if ($biblionotes) {
597         my $subfields;
598         $subfields->{1}->{'Subfield_Mark'}='a';
599         $subfields->{1}->{'Subfield_Value'}=$biblionotes;
600         $subfields->{2}->{'Subfield_Mark'}='3';
601         $subfields->{2}->{'Subfield_Value'}='biblio';
602         my $tag='500';
603         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
604     }
605 # Additional Authors
606     foreach (@additionalauthors) {
607         my $author=$_;
608         (next) unless ($author);
609         my $subfields;
610         $subfields->{1}->{'Subfield_Mark'}='a';
611         $subfields->{1}->{'Subfield_Value'}=$author;
612         $subfields->{2}->{'Subfield_Mark'}='e';
613         $subfields->{2}->{'Subfield_Value'}='author';
614         my $tag='700';
615         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
616     }
617 # Illustrator
618     if ($illus) {
619         (next) unless ($illus);
620         my $subfields;
621         $subfields->{1}->{'Subfield_Mark'}='a';
622         $subfields->{1}->{'Subfield_Value'}=$illus;
623         $subfields->{2}->{'Subfield_Mark'}='e';
624         $subfields->{2}->{'Subfield_Value'}='illustrator';
625         my $tag='700';
626         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
627     }
628 # Subjects
629     foreach (@subjects) {
630         my $subject=$_;
631         (next) unless ($subject);
632         my $subfields;
633         $subfields->{1}->{'Subfield_Mark'}='a';
634         $subfields->{1}->{'Subfield_Value'}=$subject;
635         my $tag='650';
636         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
637     }
638
639
640 # ISBN
641     if ($isbn) {
642         my $subfields;
643         $subfields->{1}->{'Subfield_Mark'}='a';
644         $subfields->{1}->{'Subfield_Value'}=$isbn;
645         my $tag='020';
646         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
647     }
648 # LCCN
649     if ($lccn) {
650         my $subfields;
651         $subfields->{1}->{'Subfield_Mark'}='a';
652         $subfields->{1}->{'Subfield_Value'}=$lccn;
653         my $tag='010';
654         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
655     }
656 # ISSN
657     if ($issn) {
658         my $subfields;
659         $subfields->{1}->{'Subfield_Mark'}='a';
660         $subfields->{1}->{'Subfield_Value'}=$issn;
661         my $tag='022';
662         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
663     }
664 # DEWEY
665     if ($dewey) {
666         my $subfields;
667         $subfields->{1}->{'Subfield_Mark'}='a';
668         $subfields->{1}->{'Subfield_Value'}=$dewey;
669         my $tag='082';
670         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
671     }
672 # DEWEY subclass and itemtype
673     {
674         my $subfields;
675         $subfields->{1}->{'Subfield_Mark'}='a';
676         $subfields->{1}->{'Subfield_Value'}=$itemtype;
677         $subfields->{2}->{'Subfield_Mark'}='b';
678         $subfields->{2}->{'Subfield_Value'}=$subclass;
679         $subfields->{3}->{'Subfield_Mark'}='c';
680         $subfields->{3}->{'Subfield_Value'}=$biblionumber;
681         $subfields->{4}->{'Subfield_Mark'}='d';
682         $subfields->{4}->{'Subfield_Value'}=$biblioitemnumber;
683         my $tag='090';
684         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
685     }
686 # PUBLISHER
687     {
688         my $subfields;
689         $subfields->{1}->{'Subfield_Mark'}='a';
690         $subfields->{1}->{'Subfield_Value'}=$place;
691         $subfields->{2}->{'Subfield_Mark'}='b';
692         $subfields->{2}->{'Subfield_Value'}=$publishercode;
693         $subfields->{3}->{'Subfield_Mark'}='c';
694         $subfields->{3}->{'Subfield_Value'}=$publicationyear;
695         if ($copyrightdate) {
696             $subfields->{4}->{'Subfield_Mark'}='c';
697             $subfields->{4}->{'Subfield_Value'}="c$copyrightdate";
698         }
699         my $tag='260';
700         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
701     }
702 # PHYSICAL
703     if ($pages || $size) {
704         my $subfields;
705         $subfields->{1}->{'Subfield_Mark'}='a';
706         $subfields->{1}->{'Subfield_Value'}=$pages;
707         $subfields->{2}->{'Subfield_Mark'}='c';
708         $subfields->{2}->{'Subfield_Value'}=$size;
709         my $tag='300';
710         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
711     }
712 # Volume/Number
713     if ($volume || $number) {
714         my $subfields;
715         $subfields->{1}->{'Subfield_Mark'}='v';
716         $subfields->{1}->{'Subfield_Value'}=$volume;
717         $subfields->{2}->{'Subfield_Mark'}='n';
718         $subfields->{2}->{'Subfield_Value'}=$number;
719         my $tag='440';
720         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
721     }
722 # Biblioitem Note
723     if ($notes) {
724         my $subfields;
725         $subfields->{1}->{'Subfield_Mark'}='a';
726         $subfields->{1}->{'Subfield_Value'}=$notes;
727         $subfields->{2}->{'Subfield_Mark'}='3';
728         $subfields->{2}->{'Subfield_Value'}='biblioitem';
729         my $tag='500';
730         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
731     }
732     $sth->finish;
733     $dbh->disconnect;
734     return ($env, $Record_ID);
735 }
736
737 sub updateBiblioItem {
738 # Update the biblioitem with biblioitemnumber $biblioitem->{'biblioitemnumber'}
739 #
740 # This routine should also check to see which fields are actually being
741 # modified, and log all changes.
742
743     my ($env, $biblioitem) = @_;
744     my $dbh=&C4Connect;  
745
746     my $biblioitemnumber=$biblioitem->{'biblioitemnumber'};
747     my $sth=$dbh->prepare("select * from biblioitems where biblioitemnumber=$biblioitemnumber");
748 # obi = original biblioitem
749     my $obi=$sth->fetchrow_hashref;
750     $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");
751     $sth->execute;
752     my ($Record_ID) = $sth->fetchrow;
753     if ($biblioitem->{'biblionumber'} ne $obi->{'biblionumber'}) {
754         logchange('kohadb', 'change', 'biblioitems', 'biblionumber', $obi->{'biblionumber'}, $biblioitem->{'biblionumber'});
755         my $sth=$dbh->prepare("update biblioitems set biblionumber=$biblioitem->{'biblionumber'} where biblioitemnumber=$biblioitemnumber");
756         logchange('marc', 'change', $Record_ID, '090', 'c', $obi->{'biblionumber'}, $biblioitem->{'biblionumber'});
757         changeSubfield($Record_ID, '090', 'c', $obi->{'biblionumber'}, $biblioitem->{'biblionumber'});
758     }
759     if ($biblioitem->{'volume'} ne $obi->{'volume'}) {
760         logchange('kohadb', 'change', 'biblioitems', 'volume', $obi->{'volume'}, $biblioitem->{'volume'});
761         my $q_volume=$dbh->quote($biblioitem->{'volume'});
762         my $sth=$dbh->prepare("update biblioitems set volume=$q_volume where biblioitemnumber=$biblioitemnumber");
763         logchange('marc', 'change', $Record_ID, '440', 'v', $obi->{'volume'}, $biblioitem->{'volume'});
764         changeSubfield($Record_ID, '440', 'v', $obi->{'volume'}, $biblioitem->{'volume'});
765     }
766     if ($biblioitem->{'number'} ne $obi->{'number'}) {
767         logchange('kohadb', 'change', 'biblioitems', 'number', $obi->{'number'}, $biblioitem->{'number'});
768         my $q_number=$dbh->quote($biblioitem->{'number'});
769         my $sth=$dbh->prepare("update biblioitems set number=$q_number where biblioitemnumber=$biblioitemnumber");
770         logchange('marc', 'change', $Record_ID, '440', 'v', $obi->{'number'}, $biblioitem->{'number'});
771         changeSubfield($Record_ID, '440', 'v', $obi->{'number'}, $biblioitem->{'number'});
772     }
773     if ($biblioitem->{'itemtype'} ne $obi->{'itemtype'}) {
774         logchange('kohadb', 'change', 'biblioitems', 'itemtype', $obi->{'itemtype'}, $biblioitem->{'itemtype'});
775         my $q_itemtype=$dbh->quote($biblioitem->{'itemtype'});
776         my $sth=$dbh->prepare("update biblioitems set itemtype=$q_itemtype where biblioitemnumber=$biblioitemnumber");
777         logchange('marc', 'change', $Record_ID, '090', 'a', $obi->{'itemtype'}, $biblioitem->{'itemtype'});
778         changeSubfield($Record_ID, '090', 'a', $obi->{'itemtype'}, $biblioitem->{'itemtype'});
779     }
780     if ($biblioitem->{'isbn'} ne $obi->{'isbn'}) {
781         logchange('kohadb', 'change', 'biblioitems', 'isbn', $obi->{'isbn'}, $biblioitem->{'isbn'});
782         my $q_isbn=$dbh->quote($biblioitem->{'isbn'});
783         my $sth=$dbh->prepare("update biblioitems set isbn=$q_isbn where biblioitemnumber=$biblioitemnumber");
784         logchange('marc', 'change', $Record_ID, '020', 'a', $obi->{'isbn'}, $biblioitem->{'isbn'});
785         changeSubfield($Record_ID, '020', 'a', $obi->{'isbn'}, $biblioitem->{'isbn'});
786     }
787     if ($biblioitem->{'issn'} ne $obi->{'issn'}) {
788         logchange('kohadb', 'change', 'biblioitems', 'issn', $obi->{'issn'}, $biblioitem->{'issn'});
789         my $q_issn=$dbh->quote($biblioitem->{'issn'});
790         my $sth=$dbh->prepare("update biblioitems set issn=$q_issn where biblioitemnumber=$biblioitemnumber");
791         logchange('marc', 'change', $Record_ID, '022', 'a', $obi->{'issn'}, $biblioitem->{'issn'});
792         changeSubfield($Record_ID, '022', 'a', $obi->{'issn'}, $biblioitem->{'issn'});
793     }
794     if ($biblioitem->{'dewey'} ne $obi->{'dewey'}) {
795         logchange('kohadb', 'change', 'biblioitems', 'dewey', $obi->{'dewey'}, $biblioitem->{'dewey'});
796         my $sth=$dbh->prepare("update biblioitems set dewey=$biblioitem->{'dewey'} where biblioitemnumber=$biblioitemnumber");
797         logchange('marc', 'change', $Record_ID, '082', 'a', $obi->{'dewey'}, $biblioitem->{'dewey'});
798         changeSubfield($Record_ID, '082', 'a', $obi->{'dewey'}, $biblioitem->{'dewey'});
799     }
800     if ($biblioitem->{'subclass'} ne $obi->{'subclass'}) {
801         logchange('kohadb', 'change', 'biblioitems', 'subclass', $obi->{'subclass'}, $biblioitem->{'subclass'});
802         my $q_subclass=$dbh->quote($biblioitem->{'subclass'});
803         my $sth=$dbh->prepare("update biblioitems set subclass=$q_subclass where biblioitemnumber=$biblioitemnumber");
804         logchange('marc', 'change', $Record_ID, '090', 'b', $obi->{'subclass'}, $biblioitem->{'subclass'});
805         changeSubfield($Record_ID, '090', 'b', $obi->{'subclass'}, $biblioitem->{'subclass'});
806     }
807     if ($biblioitem->{'place'} ne $obi->{'place'}) {
808         logchange('kohadb', 'change', 'biblioitems', 'place', $obi->{'place'}, $biblioitem->{'place'});
809         my $q_place=$dbh->quote($biblioitem->{'place'});
810         my $sth=$dbh->prepare("update biblioitems set place=$q_place where biblioitemnumber=$biblioitemnumber");
811         logchange('marc', 'change', $Record_ID, '260', 'a', $obi->{'place'}, $biblioitem->{'place'});
812         changeSubfield($Record_ID, '260', 'a', $obi->{'place'}, $biblioitem->{'place'});
813     }
814     if ($biblioitem->{'publishercode'} ne $obi->{'publishercode'}) {
815         logchange('kohadb', 'change', 'biblioitems', 'publishercode', $obi->{'publishercode'}, $biblioitem->{'publishercode'});
816         my $q_publishercode=$dbh->quote($biblioitem->{'publishercode'});
817         my $sth=$dbh->prepare("update biblioitems set publishercode=$q_publishercode where biblioitemnumber=$biblioitemnumber");
818         logchange('marc', 'change', $Record_ID, '260', 'b', $obi->{'publishercode'}, $biblioitem->{'publishercode'});
819         changeSubfield($Record_ID, '260', 'b', $obi->{'publishercode'}, $biblioitem->{'publishercode'});
820     }
821     if ($biblioitem->{'publicationyear'} ne $obi->{'publicationyear'}) {
822         logchange('kohadb', 'change', 'biblioitems', 'publicationyear', $obi->{'publicationyear'}, $biblioitem->{'publicationyear'});
823         my $q_publicationyear=$dbh->quote($biblioitem->{'publicationyear'});
824         my $sth=$dbh->prepare("update biblioitems set publicationyear=$q_publicationyear where biblioitemnumber=$biblioitemnumber");
825         logchange('marc', 'change', $Record_ID, '260', 'c', $obi->{'publicationyear'}, $biblioitem->{'publicationyear'});
826         changeSubfield($Record_ID, '260', 'c', $obi->{'publicationyear'}, $biblioitem->{'publicationyear'});
827     }
828     if ($biblioitem->{'illus'} ne $obi->{'illus'}) {
829         logchange('kohadb', 'change', 'biblioitems', 'illus', $obi->{'illus'}, $biblioitem->{'illus'});
830         my $q_illus=$dbh->quote($biblioitem->{'illus'});
831         my $sth=$dbh->prepare("update biblioitems set illus=$q_illus where biblioitemnumber=$biblioitemnumber");
832         logchange('marc', 'change', $Record_ID, '700', 'a', $obi->{'illus'}, $biblioitem->{'illus'});
833         changeSubfield($Record_ID, '700', 'a', $obi->{'illus'}, $biblioitem->{'illus'});
834     }
835     if ($biblioitem->{'pages'} ne $obi->{'pages'}) {
836         logchange('kohadb', 'change', 'biblioitems', 'pages', $obi->{'pages'}, $biblioitem->{'pages'});
837         my $q_pages=$dbh->quote($biblioitem->{'pages'});
838         my $sth=$dbh->prepare("update biblioitems set pages=$q_pages where biblioitemnumber=$biblioitemnumber");
839         logchange('marc', 'change', $Record_ID, '300', 'a', $obi->{'pages'}, $biblioitem->{'pages'});
840         changeSubfield($Record_ID, '300', 'a', $obi->{'pages'}, $biblioitem->{'pages'});
841     }
842     if ($biblioitem->{'size'} ne $obi->{'size'}) {
843         logchange('kohadb', 'change', 'biblioitems', 'size', $obi->{'size'}, $biblioitem->{'size'});
844         my $q_size=$dbh->quote($biblioitem->{'size'});
845         my $sth=$dbh->prepare("update biblioitems set size=$q_size where biblioitemnumber=$biblioitemnumber");
846         logchange('marc', 'change', $Record_ID, '300', 'c', $obi->{'size'}, $biblioitem->{'size'});
847         changeSubfield($Record_ID, '300', 'c', $obi->{'size'}, $biblioitem->{'size'});
848     }
849     if ($biblioitem->{'notes'} ne $obi->{'notes'}) {
850         logchange('kohadb', 'change', 'biblioitems', 'notes', $obi->{'notes'}, $biblioitem->{'notes'});
851         my $q_notes=$dbh->quote($biblioitem->{'notes'});
852         my $sth=$dbh->prepare("update biblioitems set notes=$q_notes where biblioitemnumber=$biblioitemnumber");
853         logchange('marc', 'change', $Record_ID, '500', 'a', $obi->{'notes'}, $biblioitem->{'notes'});
854         changeSubfield($Record_ID, '500', 'a', $obi->{'notes'}, $biblioitem->{'notes'});
855     }
856     if ($biblioitem->{'lccn'} ne $obi->{'lccn'}) {
857         logchange('kohadb', 'change', 'biblioitems', 'lccn', $obi->{'lccn'}, $biblioitem->{'lccn'});
858         my $q_lccn=$dbh->quote($biblioitem->{'lccn'});
859         my $sth=$dbh->prepare("update biblioitems set lccn=$q_lccn where biblioitemnumber=$biblioitemnumber");
860         logchange('marc', 'change', $Record_ID, '010', 'a', $obi->{'lccn'}, $biblioitem->{'lccn'});
861         changeSubfield($Record_ID, '010', 'a', $obi->{'lccn'}, $biblioitem->{'lccn'});
862     }
863     $sth->finish;
864     $dbh->disconnect;
865
866 }
867
868
869 sub newItem {
870     my ($env, $Record_ID, $item) = @_;
871     my $dbh=&C4Connect;  
872     my $barcode=$item->{'barcode'};
873     my $q_barcode=$dbh->quote($barcode);
874     my $biblionumber=$item->{'biblionumber'};
875     my $biblioitemnumber=$item->{'biblioitemnumber'};
876     my $dateaccessioned=$item->{'dateaccessioned'};
877     my $booksellerid=$item->{'booksellerid'};
878     my $q_booksellerid=$dbh->quote($booksellerid);
879     my $homebranch=$item->{'homebranch'};
880     my $q_homebranch=$dbh->quote($homebranch);
881     my $holdingbranch=$item->{'holdingbranch'};
882     my $price=$item->{'price'};
883     my $replacementprice=$item->{'replacementprice'};
884     my $replacementpricedate=$item->{'replacementpricedate'};
885     my $q_replacementpricedate=$dbh->quote($replacementpricedate);
886     my $notforloan=$item->{'notforloan'};
887     my $itemlost=$item->{'itemlost'};
888     my $wthdrawn=$item->{'wthdrawn'};
889     my $restricted=$item->{'restricted'};
890     my $itemnotes=$item->{'itemnotes'};
891     my $q_itemnotes=$dbh->quote($itemnotes);
892     my $itemtype=$item->{'itemtype'};
893     my $subclass=$item->{'subclass'};
894
895 # KOHADB Section
896
897     unless ($env->{'marconly'}) {
898         my $sth=$dbh->prepare("select max(itemnumber) from items");
899         $sth->execute;
900         my ($itemnumber) =$sth->fetchrow;
901         $itemnumber++;
902         $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)");
903         $sth->execute;
904     }
905
906
907 # MARC SECTION
908     my $subfields;
909     $subfields->{1}->{'Subfield_Mark'}='p';
910     $subfields->{1}->{'Subfield_Value'}=$barcode;
911     $subfields->{2}->{'Subfield_Mark'}='d';
912     $subfields->{2}->{'Subfield_Value'}=$dateaccessioned;
913     $subfields->{3}->{'Subfield_Mark'}='e';
914     $subfields->{3}->{'Subfield_Value'}=$booksellerid;
915     $subfields->{4}->{'Subfield_Mark'}='b';
916     $subfields->{4}->{'Subfield_Value'}=$homebranch;
917     $subfields->{5}->{'Subfield_Mark'}='l';
918     $subfields->{5}->{'Subfield_Value'}=$holdingbranch;
919     $subfields->{6}->{'Subfield_Mark'}='c';
920     $subfields->{6}->{'Subfield_Value'}=$price;
921     $subfields->{7}->{'Subfield_Mark'}='c';
922     $subfields->{7}->{'Subfield_Value'}=$replacementprice;
923     $subfields->{8}->{'Subfield_Mark'}='d';
924     $subfields->{8}->{'Subfield_Value'}=$replacementpricedate;
925     if ($notforloan) {
926         $subfields->{9}->{'Subfield_Mark'}='h';
927         $subfields->{9}->{'Subfield_Value'}='Not for loan';
928     }
929     if ($notforloan) {
930         $subfields->{10}->{'Subfield_Mark'}='j';
931         $subfields->{10}->{'Subfield_Value'}='Item lost';
932     }
933     if ($notforloan) {
934         $subfields->{11}->{'Subfield_Mark'}='j';
935         $subfields->{11}->{'Subfield_Value'}='Item withdrawn';
936     }
937     if ($notforloan) {
938         $subfields->{12}->{'Subfield_Mark'}='z';
939         $subfields->{12}->{'Subfield_Value'}=$itemnotes;
940     }
941     my $tag='876';
942     my $Tag_ID;
943     $env->{'linkage'}=1;
944     ($env, $Tag_ID) = addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
945     $env->{'linkage'}=0;
946     $env->{'linkid'}=$Tag_ID;
947     $tag='852';
948     my $subfields2;
949     $subfields2->{1}->{'Subfield_Mark'}='a';
950     $subfields2->{1}->{'Subfield_Value'}='Coast Mountains School District';
951     $subfields2->{1}->{'Subfield_Mark'}='b';
952     $subfields2->{1}->{'Subfield_Value'}=$homebranch;
953     $subfields2->{1}->{'Subfield_Mark'}='c';
954     $subfields2->{1}->{'Subfield_Value'}=$itemtype;
955     $subfields2->{2}->{'Subfield_Mark'}='m';
956     $subfields2->{2}->{'Subfield_Value'}=$subclass;
957     addTag($env, $Record_ID, $tag, ' ', ' ', $subfields2);
958     $env->{'linkid'}='';
959 }
960
961 sub updateItem {
962 # Update the item with itemnumber $item->{'itemnumber'}
963 # This routine should also modify the corresponding MARC record data. (852 and
964 # 876 tags with 876p tag the same as $item->{'barcode'}
965 #
966 # This routine should also check to see which fields are actually being
967 # modified, and log all changes.
968
969     my ($env, $item) = @_;
970     my $dbh=&C4Connect;  
971     my $itemnumber=$item->{'itemnumber'};
972     my $biblionumber=$item->{'biblionumber'};
973     my $biblioitemnumber=$item->{'biblioitemnumber'};
974     my $barcode=$item->{'barcode'};
975     my $dateaccessioned=$item->{'dateaccessioned'};
976     my $booksellerid=$item->{'booksellerid'};
977     my $homebranch=$item->{'homebranch'};
978     my $price=$item->{'price'};
979     my $replacementprice=$item->{'replacementprice'};
980     my $replacementpricedate=$item->{'replacementpricedate'};
981     my $multivolume=$item->{'multivolume'};
982     my $stack=$item->{'stack'};
983     my $notforloan=$item->{'notforloan'};
984     my $itemlost=$item->{'itemlost'};
985     my $wthdrawn=$item->{'wthdrawn'};
986     my $bulk=$item->{'bulk'};
987     my $restricted=$item->{'restricted'};
988     my $binding=$item->{'binding'};
989     my $itemnotes=$item->{'itemnotes'};
990     my $holdingbranch=$item->{'holdingbranch'};
991     my $interim=$item->{'interim'};
992     my $sth=$dbh->prepare("select * from items where itemnumber=$itemnumber");
993     $sth->execute;
994     my $olditem=$sth->fetchrow_hashref;
995     my $q_barcode=$dbh->quote($olditem->{'barcode'});
996     $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");
997     $sth->execute;
998     my ($Subfield876_ID, $Record_ID) = $sth->fetchrow;
999     $sth=$dbh->prepare("select Subfield_Value from 8XX_Subfield_Table where Subfield_Mark=8 and Subfield_ID=$Subfield876_ID");
1000     $sth->execute;
1001     my ($link) = $sth->fetchrow;
1002     $sth=$dbh->prepare("select Subfield_ID from 8XX_Subfield_Table where Subfield_Mark=8 and Subfield_Value=$link and !(Subfield_ID=$Subfield876_ID)");
1003     $sth->execute;
1004     my ($Subfield852_ID) = $sth->fetchrow;
1005     
1006     if ($item->{'barcode'} ne $olditem->{'barcode'}) {
1007         logchange('kohadb', 'change', 'items', 'barcode', $olditem->{'barcode'}, $item->{'barcode'});
1008         my $q_barcode=$dbh->quote($item->{'barcode'});
1009         my $sth=$dbh->prepare("update items set barcode=$q_barcode where itemnumber=$itemnumber");
1010         $sth->execute;
1011         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'p', $olditem->{'barcode'}, $item->{'barcode'}, $Subfield876_ID);
1012         logchange('marc', 'change', $Record_ID, '876', 'p', $Subfield_Key, $olditem->{'barcode'}, $item->{'barcode'});
1013     }
1014     if ($item->{'booksellerid'} ne $olditem->{'booksellerid'}) {
1015         logchange('kohadb', 'change', 'items', 'booksellerid', $olditem->{'booksellerid'}, $item->{'booksellerid'});
1016         my $q_booksellerid=$dbh->quote($item->{'booksellerid'});
1017         my $sth=$dbh->prepare("update items set booksellerid=$q_booksellerid where itemnumber=$itemnumber");
1018         $sth->execute;
1019         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'e', $olditem->{'booksellerid'}, $item->{'booksellerid'}, $Subfield876_ID);
1020         logchange('marc', 'change', $Record_ID, '876', 'e', $Subfield_Key, $olditem->{'booksellerid'}, $item->{'booksellerid'});
1021     }
1022     if ($item->{'dateaccessioned'} ne $olditem->{'dateaccessioned'}) {
1023         logchange('kohadb', 'change', 'items', 'dateaccessioned', $olditem->{'dateaccessioned'}, $item->{'dateaccessioned'});
1024         my $q_dateaccessioned=$dbh->quote($item->{'dateaccessioned'});
1025         my $sth=$dbh->prepare("update items set dateaccessioned=$q_dateaccessioned where itemnumber=$itemnumber");
1026         $sth->execute;
1027         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'd', $olditem->{'dateaccessioned'}, $item->{'dateaccessioned'}, $Subfield876_ID);
1028         logchange('marc', 'change', $Record_ID, '876', 'd', $Subfield_Key, $olditem->{'dateaccessioned'}, $item->{'dateaccessioned'});
1029     }
1030     if ($item->{'homebranch'} ne $olditem->{'homebranch'}) {
1031         logchange('kohadb', 'change', 'items', 'homebranch', $olditem->{'homebranch'}, $item->{'homebranch'});
1032         my $q_homebranch=$dbh->quote($item->{'homebranch'});
1033         my $sth=$dbh->prepare("update items set homebranch=$q_homebranch where itemnumber=$itemnumber");
1034         $sth->execute;
1035         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'b', $olditem->{'homebranch'}, $item->{'homebranch'}, $Subfield876_ID);
1036         logchange('marc', 'change', $Record_ID, '876', 'b', $Subfield_Key, $olditem->{'homebranch'}, $item->{'homebranch'});
1037     }
1038     if ($item->{'holdingbranch'} ne $olditem->{'holdingbranch'}) {
1039         logchange('kohadb', 'change', 'items', 'holdingbranch', $olditem->{'holdingbranch'}, $item->{'holdingbranch'});
1040         my $q_holdingbranch=$dbh->quote($item->{'holdingbranch'});
1041         my $sth=$dbh->prepare("update items set holdingbranch=$q_holdingbranch where itemnumber=$itemnumber");
1042         $sth->execute;
1043         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'l', $olditem->{'holdingbranch'}, $item->{'holdingbranch'}, $Subfield876_ID);
1044         logchange('marc', 'change', $Record_ID, '876', 'l', $Subfield_Key, $olditem->{'holdingbranch'}, $item->{'holdingbranch'});
1045     }
1046     if ($item->{'price'} ne $olditem->{'price'}) {
1047         logchange('kohadb', 'change', 'items', 'price', $olditem->{'price'}, $item->{'price'});
1048         my $q_price=$dbh->quote($item->{'price'});
1049         my $sth=$dbh->prepare("update items set price=$q_price where itemnumber=$itemnumber");
1050         $sth->execute;
1051         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'c', $olditem->{'price'}, $item->{'price'}, $Subfield876_ID);
1052         logchange('marc', 'change', $Record_ID, '876', 'c', $Subfield_Key, $olditem->{'price'}, $item->{'price'});
1053     }
1054     if ($item->{'itemnotes'} ne $olditem->{'itemnotes'}) {
1055         logchange('kohadb', 'change', 'items', 'itemnotes', $olditem->{'itemnotes'}, $item->{'itemnotes'});
1056         my $q_itemnotes=$dbh->quote($item->{'itemnotes'});
1057         my $sth=$dbh->prepare("update items set itemnotes=$q_itemnotes where itemnumber=$itemnumber");
1058         $sth->execute;
1059         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'c', $olditem->{'itemnotes'}, $item->{'itemnotes'}, $Subfield876_ID);
1060         logchange('marc', 'change', $Record_ID, '876', 'c', $Subfield_Key, $olditem->{'itemnotes'}, $item->{'itemnotes'});
1061     }
1062     if ($item->{'notforloan'} ne $olditem->{'notforloan'}) {
1063         logchange('kohadb', 'change', 'items', 'notforloan', $olditem->{'notforloan'}, $item->{'notforloan'});
1064         my $sth=$dbh->prepare("update items set notforloan=$notforloan where itemnumber=$itemnumber");
1065         $sth->execute;
1066         if ($item->{'notforloan'}) {
1067             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Not for loan', $Subfield876_ID);
1068             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Not for loan');
1069         } else {
1070             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Not for loan', $Subfield876_ID);
1071             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Not for loan');
1072         }
1073     }
1074     if ($item->{'itemlost'} ne $olditem->{'itemlost'}) {
1075         logchange('kohadb', 'change', 'items', 'itemlost', $olditem->{'itemlost'}, $item->{'itemlost'});
1076         my $sth=$dbh->prepare("update items set itemlost=$itemlost where itemnumber=$itemnumber");
1077         $sth->execute;
1078         if ($item->{'itemlost'}) {
1079             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Item lost', $Subfield876_ID);
1080             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Item lost');
1081         } else {
1082             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Item lost', $Subfield876_ID);
1083             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Item lost');
1084         }
1085     }
1086     if ($item->{'wthdrawn'} ne $olditem->{'wthdrawn'}) {
1087         logchange('kohadb', 'change', 'items', 'wthdrawn', $olditem->{'wthdrawn'}, $item->{'wthdrawn'});
1088         my $sth=$dbh->prepare("update items set wthdrawn=$wthdrawn where itemnumber=$itemnumber");
1089         $sth->execute;
1090         if ($item->{'wthdrawn'}) {
1091             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Withdrawn', $Subfield876_ID);
1092             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Withdrawn');
1093         } else {
1094             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Withdrawn', $Subfield876_ID);
1095             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Withdrawn');
1096         }
1097     }
1098     if ($item->{'restricted'} ne $olditem->{'restricted'}) {
1099         logchange('kohadb', 'change', 'items', 'restricted', $olditem->{'restricted'}, $item->{'restricted'});
1100         my $sth=$dbh->prepare("update items set restricted=$restricted where itemnumber=$itemnumber");
1101         $sth->execute;
1102         if ($item->{'restricted'}) {
1103             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Restricted', $Subfield876_ID);
1104             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Restricted');
1105         } else {
1106             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Restricted', $Subfield876_ID);
1107             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Restricted');
1108         }
1109     }
1110     $sth->finish;
1111     $dbh->disconnect;
1112 }
1113
1114
1115 sub itemcount{
1116   my ($biblio)=@_;
1117   my $dbh=C4Connect;
1118   my $query="Select count(*) from items where biblionumber=$biblio";
1119 #  print $query;
1120   my $sth=$dbh->prepare($query);
1121   $sth->execute;
1122   my $data=$sth->fetchrow_hashref;
1123   $sth->finish;
1124   $dbh->disconnect;
1125   return($data->{'count(*)'});
1126 }
1127
1128 sub getorder{
1129   my ($bi,$bib)=@_;
1130   my $dbh=C4Connect;
1131   my $query="Select ordernumber from aqorders where biblionumber=$bib and
1132   biblioitemnumber='$bi'";
1133   my $sth=$dbh->prepare($query);
1134   $sth->execute;
1135   my $ordnum=$sth->fetchrow_hashref;
1136   $sth->finish;
1137   my $order=getsingleorder($ordnum->{'ordernumber'});
1138   $dbh->disconnect;
1139 #  print $query;
1140   return ($order,$ordnum->{'ordernumber'});
1141 }
1142
1143 sub getsingleorder {
1144   my ($ordnum)=@_;
1145   my $dbh=C4Connect;
1146   my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown 
1147   where aqorders.ordernumber='$ordnum' 
1148   and biblio.biblionumber=aqorders.biblionumber and
1149   biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
1150   aqorders.ordernumber=aqorderbreakdown.ordernumber";
1151   my $sth=$dbh->prepare($query);
1152   $sth->execute;
1153   my $data=$sth->fetchrow_hashref;
1154   $sth->finish;
1155   $dbh->disconnect;
1156   return($data);
1157 }
1158
1159 sub newbiblio {
1160   my ($biblio) = @_;
1161   my $dbh    = &C4Connect;
1162   my $query  = "Select max(biblionumber) from biblio";
1163   my $sth    = $dbh->prepare($query);
1164   $sth->execute;
1165   my $data   = $sth->fetchrow_arrayref;
1166   my $bibnum = $$data[0] + 1;
1167   my $series = 0;
1168
1169   $biblio->{'title'}       = $dbh->quote($biblio->{'title'});
1170   $biblio->{'author'}      = $dbh->quote($biblio->{'author'});
1171   $biblio->{'copyright'}   = $dbh->quote($biblio->{'copyright'});
1172   $biblio->{'seriestitle'} = $dbh->quote($biblio->{'seriestitle'});
1173   $biblio->{'notes'}       = $dbh->quote($biblio->{'notes'});
1174   $biblio->{'abstract'}    = $dbh->quote($biblio->{'abstract'});
1175   if ($biblio->{'seriestitle'}) { $series = 1 };
1176
1177   $sth->finish;
1178   $query = "insert into biblio set
1179 biblionumber  = $bibnum,
1180 title         = $biblio->{'title'},
1181 author        = $biblio->{'author'},
1182 copyrightdate = $biblio->{'copyright'},
1183 serial        = $series,
1184 seriestitle   = $biblio->{'seriestitle'},
1185 notes         = $biblio->{'notes'},
1186 abstract      = $biblio->{'abstract'}";
1187
1188   $sth = $dbh->prepare($query);
1189   $sth->execute;
1190
1191   $sth->finish;
1192   $dbh->disconnect;
1193   return($bibnum);
1194 }
1195
1196 sub modbiblio {
1197   my ($biblio) = @_;
1198   my $dbh   = C4Connect;
1199   my $query;
1200   my $sth;
1201   
1202   $biblio->{'title'}         = $dbh->quote($biblio->{'title'});
1203   $biblio->{'author'}        = $dbh->quote($biblio->{'author'});
1204   $biblio->{'abstract'}      = $dbh->quote($biblio->{'abstract'});
1205   $biblio->{'copyrightdate'} = $dbh->quote($biblio->{'copyrightdate'});
1206   $biblio->{'seriestitle'}   = $dbh->quote($biblio->{'serirestitle'});
1207   $biblio->{'serial'}        = $dbh->quote($biblio->{'serial'});
1208   $biblio->{'unititle'}      = $dbh->quote($biblio->{'unititle'});
1209   $biblio->{'notes'}         = $dbh->quote($biblio->{'notes'});
1210
1211   $query = "Update biblio set
1212 title         = $biblio->{'title'},
1213 author        = $biblio->{'author'},
1214 abstract      = $biblio->{'abstract'},
1215 copyrightdate = $biblio->{'copyrightdate'},
1216 seriestitle   = $biblio->{'seriestitle'},
1217 serial        = $biblio->{'serial'},
1218 unititle      = $biblio->{'unititle'},
1219 notes         = $biblio->{'notes'}
1220 where biblionumber = $biblio->{'biblionumber'}";
1221   $sth   = $dbh->prepare($query);
1222
1223   $sth->execute;
1224
1225   $sth->finish;
1226   $dbh->disconnect;
1227   return($biblio->{'biblionumber'});
1228 } # sub modbiblio
1229
1230 sub modsubtitle {
1231   my ($bibnum, $subtitle) = @_;
1232   my $dbh   = C4Connect;
1233   my $query = "update bibliosubtitle set
1234 subtitle = '$subtitle'
1235 where biblionumber = $bibnum";
1236   my $sth   = $dbh->prepare($query);
1237
1238   $sth->execute;
1239   $sth->finish;
1240   $dbh->disconnect;
1241 } # sub modsubtitle
1242
1243
1244 sub modaddauthor {
1245     my ($bibnum, $author) = @_;
1246     my $dbh   = C4Connect;
1247     my $query = "Delete from additionalauthors where biblionumber = $bibnum";
1248     my $sth = $dbh->prepare($query);
1249
1250     $sth->execute;
1251     $sth->finish;
1252
1253     if ($author ne '') {
1254         $query = "Insert into additionalauthors set
1255 author       = '$author',
1256 biblionumber = '$bibnum'";
1257         $sth   = $dbh->prepare($query);
1258
1259         $sth->execute;
1260
1261         $sth->finish;
1262     } # if
1263
1264   $dbh->disconnect;
1265 } # sub modaddauthor
1266
1267
1268 sub modsubject {
1269   my ($bibnum, $force, @subject) = @_;
1270   my $dbh   = C4Connect;
1271   my $count = @subject;
1272   my $error;
1273   for (my $i = 0; $i < $count; $i++) {
1274     $subject[$i] =~ s/^ //g;
1275     $subject[$i] =~ s/ $//g;
1276     my $query = "select * from catalogueentry
1277 where entrytype = 's'
1278 and catalogueentry = '$subject[$i]'";
1279     my $sth   = $dbh->prepare($query);
1280     $sth->execute;
1281
1282     if (my $data = $sth->fetchrow_hashref) {
1283     } else {
1284       if ($force eq $subject[$i]) {
1285
1286          # subject not in aut, chosen to force anway
1287          # so insert into cataloguentry so its in auth file
1288          $query = "Insert into catalogueentry
1289 (entrytype,catalogueentry)
1290 values ('s','$subject[$i]')";
1291          my $sth2 = $dbh->prepare($query);
1292
1293          $sth2->execute;
1294          $sth2->finish;
1295
1296       } else {
1297
1298         $error = "$subject[$i]\n does not exist in the subject authority file";
1299         $query = "Select * from catalogueentry
1300 where entrytype = 's'
1301 and (catalogueentry like '$subject[$i] %'
1302 or catalogueentry like '% $subject[$i] %'
1303 or catalogueentry like '% $subject[$i]')";
1304         my $sth2 = $dbh->prepare($query);
1305
1306         $sth2->execute;
1307         while (my $data = $sth2->fetchrow_hashref) {
1308           $error = $error."<br>$data->{'catalogueentry'}";
1309         } # while
1310         $sth2->finish;
1311       } # else
1312     } # else
1313     $sth->finish;
1314   } # else
1315
1316   if ($error eq '') {
1317     my $query = "Delete from bibliosubject where biblionumber = $bibnum";
1318     my $sth   = $dbh->prepare($query);
1319
1320     $sth->execute;
1321     $sth->finish;
1322
1323     for (my $i = 0; $i < $count; $i++) {
1324       $sth = $dbh->prepare("Insert into bibliosubject
1325 values ('$subject[$i]', $bibnum)");
1326
1327       $sth->execute;
1328       $sth->finish;
1329     } # for
1330   } # if
1331
1332   $dbh->disconnect;
1333   return($error);
1334 } # sub modsubject
1335
1336 sub modbibitem {
1337     my ($biblioitem) = @_;
1338     my $dbh   = C4Connect;
1339     my $query;
1340
1341     $biblioitem->{'itemtype'}        = $dbh->quote($biblioitem->{'itemtype'});
1342     $biblioitem->{'url'}             = $dbh->quote($biblioitem->{'url'});
1343     $biblioitem->{'isbn'}            = $dbh->quote($biblioitem->{'isbn'});
1344     $biblioitem->{'publishercode'}   = $dbh->quote($biblioitem->{'publishercode'});
1345     $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1346     $biblioitem->{'classification'}  = $dbh->quote($biblioitem->{'classification'});
1347     $biblioitem->{'dewey'}           = $dbh->quote($biblioitem->{'dewey'});
1348     $biblioitem->{'subclass'}        = $dbh->quote($biblioitem->{'subclass'});
1349     $biblioitem->{'illus'}           = $dbh->quote($biblioitem->{'illus'});
1350     $biblioitem->{'pages'}           = $dbh->quote($biblioitem->{'pages'});
1351     $biblioitem->{'volumeddesc'}     = $dbh->quote($biblioitem->{'volumeddesc'});
1352     $biblioitem->{'notes'}           = $dbh->quote($biblioitem->{'notes'});
1353     $biblioitem->{'size'}            = $dbh->quote($biblioitem->{'size'});
1354     $biblioitem->{'place'}           = $dbh->quote($biblioitem->{'place'});
1355
1356     $query = "Update biblioitems set
1357 itemtype        = $biblioitem->{'itemtype'},
1358 url             = $biblioitem->{'url'},
1359 isbn            = $biblioitem->{'isbn'},
1360 publishercode   = $biblioitem->{'publishercode'},
1361 publicationyear = $biblioitem->{'publicationyear'},
1362 classification  = $biblioitem->{'classification'},
1363 dewey           = $biblioitem->{'dewey'},
1364 subclass        = $biblioitem->{'subclass'},
1365 illus           = $biblioitem->{'illus'},
1366 pages           = $biblioitem->{'pages'},
1367 volumeddesc     = $biblioitem->{'volumeddesc'},
1368 notes           = $biblioitem->{'notes'},
1369 size            = $biblioitem->{'size'},
1370 place           = $biblioitem->{'place'}
1371 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
1372
1373     $dbh->do($query);
1374
1375     $dbh->disconnect;
1376 } # sub modbibitem
1377
1378 sub modnote {
1379   my ($bibitemnum,$note)=@_;
1380   my $dbh=C4Connect;
1381   my $query="update biblioitems set notes='$note' where
1382   biblioitemnumber='$bibitemnum'";
1383   my $sth=$dbh->prepare($query);
1384   $sth->execute;
1385   $sth->finish;
1386   $dbh->disconnect;
1387 }
1388
1389 sub newbiblioitem {
1390   my ($biblioitem) = @_;
1391   my $dbh   = C4Connect;
1392   my $query = "Select max(biblioitemnumber) from biblioitems";
1393   my $sth   = $dbh->prepare($query);
1394   my $data;
1395   my $bibitemnum;
1396
1397   $biblioitem->{'volume'}          = $dbh->quote($biblioitem->{'volume'});
1398   $biblioitem->{'number'}          = $dbh->quote($biblioitem->{'number'});
1399   $biblioitem->{'classification'}  = $dbh->quote($biblioitem->{'classification'});
1400   $biblioitem->{'itemtype'}        = $dbh->quote($biblioitem->{'itemtype'});
1401   $biblioitem->{'url'}             = $dbh->quote($biblioitem->{'url'});
1402   $biblioitem->{'isbn'}            = $dbh->quote($biblioitem->{'isbn'});
1403   $biblioitem->{'issn'}            = $dbh->quote($biblioitem->{'issn'});
1404   $biblioitem->{'dewey'}           = $dbh->quote($biblioitem->{'dewey'});
1405   $biblioitem->{'subclass'}        = $dbh->quote($biblioitem->{'subclass'});
1406   $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1407   $biblioitem->{'publishercode'}   = $dbh->quote($biblioitem->{'publishercode'});
1408   $biblioitem->{'volumedate'}      = $dbh->quote($biblioitem->{'volumedate'});
1409   $biblioitem->{'volumeddesc'}     = $dbh->quote($biblioitem->{'volumeddesc'});  $biblioitem->{'illus'}            = $dbh->quote($biblioitem->{'illus'});
1410   $biblioitem->{'illus'}           = $dbh->quote($biblioitem->{'illus'});
1411   $biblioitem->{'pages'}           = $dbh->quote($biblioitem->{'pages'});
1412   $biblioitem->{'notes'}           = $dbh->quote($biblioitem->{'notes'});
1413   $biblioitem->{'size'}            = $dbh->quote($biblioitem->{'size'});
1414   $biblioitem->{'place'}           = $dbh->quote($biblioitem->{'place'});
1415   $biblioitem->{'lccn'}            = $dbh->quote($biblioitem->{'lccn'});
1416   $biblioitem->{'marc'}            = $dbh->quote($biblioitem->{'marc'});
1417   
1418   $sth->execute;
1419   $data       = $sth->fetchrow_arrayref;
1420   $bibitemnum = $$data[0] + 1;
1421
1422   $sth->finish;
1423
1424   $query = "insert into biblioitems set
1425 biblioitemnumber = $bibitemnum,
1426 biblionumber     = $biblioitem->{'biblionumber'},
1427 volume           = $biblioitem->{'volume'},
1428 number           = $biblioitem->{'number'},
1429 classification   = $biblioitem->{'classification'},
1430 itemtype         = $biblioitem->{'itemtype'},
1431 url              = $biblioitem->{'url'},
1432 isbn             = $biblioitem->{'isbn'},
1433 issn             = $biblioitem->{'issn'},
1434 dewey            = $biblioitem->{'dewey'},
1435 subclass         = $biblioitem->{'subclass'},
1436 publicationyear  = $biblioitem->{'publicationyear'},
1437 publishercode    = $biblioitem->{'publishercode'},
1438 volumedate       = $biblioitem->{'volumedate'},
1439 volumeddesc      = $biblioitem->{'volumeddesc'},
1440 illus            = $biblioitem->{'illus'},
1441 pages            = $biblioitem->{'pages'},
1442 notes            = $biblioitem->{'notes'},
1443 size             = $biblioitem->{'size'},
1444 lccn             = $biblioitem->{'lccn'},
1445 marc             = $biblioitem->{'marc'},
1446 place            = $biblioitem->{'place'}";
1447
1448   $sth = $dbh->prepare($query);
1449   $sth->execute;
1450
1451   $sth->finish;
1452   $dbh->disconnect;
1453   return($bibitemnum);
1454 }
1455
1456 sub newsubject {
1457   my ($bibnum)=@_;
1458   my $dbh=C4Connect;
1459   my $query="insert into bibliosubject (biblionumber) values
1460   ($bibnum)";
1461   my $sth=$dbh->prepare($query);
1462 #  print $query;
1463   $sth->execute;
1464   $sth->finish;
1465   $dbh->disconnect;
1466 }
1467
1468 sub newsubtitle {
1469   my ($bibnum, $subtitle) = @_;
1470   my $dbh   = C4Connect;
1471   $subtitle = $dbh->quote($subtitle);
1472   my $query = "insert into bibliosubtitle set
1473 biblionumber = $bibnum,
1474 subtitle = $subtitle";
1475   my $sth   = $dbh->prepare($query);
1476
1477   $sth->execute;
1478
1479   $sth->finish;
1480   $dbh->disconnect;
1481 }
1482
1483
1484 sub newitems {
1485   my ($item, @barcodes) = @_;
1486   my $dbh   = C4Connect;
1487   my $query = "Select max(itemnumber) from items";
1488   my $sth   = $dbh->prepare($query);
1489   my $data;
1490   my $itemnumber;
1491   my $error;
1492
1493   $sth->execute;
1494   $data       = $sth->fetchrow_hashref;
1495   $itemnumber = $data->{'max(itemnumber)'} + 1;
1496   $sth->finish;
1497   
1498   $item->{'booksellerid'}     = $dbh->quote($item->{'booksellerid'});
1499   $item->{'homebranch'}       = $dbh->quote($item->{'homebranch'});
1500   $item->{'price'}            = $dbh->quote($item->{'price'});
1501   $item->{'replacementprice'} = $dbh->quote($item->{'replacementprice'});
1502   $item->{'itemnotes'}        = $dbh->quote($item->{'itemnotes'});
1503
1504   foreach my $barcode (@barcodes) {
1505     $barcode = uc($barcode);
1506     $barcode = $dbh->quote($barcode);
1507     $query   = "Insert into items set
1508 itemnumber           = $itemnumber,
1509 biblionumber         = $item->{'biblionumber'},
1510 biblioitemnumber     = $item->{'biblioitemnumber'},
1511 barcode              = $barcode,
1512 booksellerid         = $item->{'booksellerid'},
1513 dateaccessioned      = NOW(),
1514 homebranch           = $item->{'homebranch'},
1515 holdingbranch        = $item->{'homebranch'},
1516 price                = $item->{'price'},
1517 replacementprice     = $item->{'replacementprice'},
1518 replacementpricedate = NOW(),
1519 itemnotes            = $item->{'itemnotes'}";
1520
1521     if ($item->{'loan'}) {
1522       $query .= ",
1523 notforloan           = $item->{'loan'}";
1524     } # if
1525
1526     $sth = $dbh->prepare($query);
1527     $sth->execute;
1528
1529     $error .= $sth->errstr;
1530
1531     $sth->finish;
1532     $itemnumber++;
1533   } # for
1534
1535   $dbh->disconnect;
1536   return($error);
1537 }
1538
1539 sub checkitems{
1540   my ($count,@barcodes)=@_;
1541   my $dbh=C4Connect;
1542   my $error;
1543   for (my $i=0;$i<$count;$i++){
1544     $barcodes[$i]=uc $barcodes[$i];
1545     my $query="Select * from items where barcode='$barcodes[$i]'";
1546     my $sth=$dbh->prepare($query);
1547     $sth->execute;
1548     if (my $data=$sth->fetchrow_hashref){
1549       $error.=" Duplicate Barcode: $barcodes[$i]";
1550     }
1551     $sth->finish;
1552   }
1553   $dbh->disconnect;
1554   return($error);
1555 }
1556
1557 sub moditem {
1558   my ($loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1559   my $dbh=C4Connect;
1560   my $query="update items set biblioitemnumber=$bibitemnum,
1561   barcode='$barcode',itemnotes='$notes'
1562   where itemnumber=$itemnum";
1563   if ($barcode eq ''){
1564     $query="update items set biblioitemnumber=$bibitemnum,notforloan=$loan where itemnumber=$itemnum";
1565   }
1566   if ($lost ne ''){
1567     $query="update items set biblioitemnumber=$bibitemnum,
1568       barcode='$barcode',itemnotes='$notes',homebranch='$homebranch',
1569       itemlost='$lost',wthdrawn='$wthdrawn' where itemnumber=$itemnum";
1570   }
1571   if ($replacement ne ''){
1572     $query=~ s/ where/,replacementprice='$replacement' where/;
1573   }
1574
1575   my $sth=$dbh->prepare($query);
1576   $sth->execute;
1577   $sth->finish;
1578   $dbh->disconnect;
1579 }
1580
1581 sub countitems{
1582   my ($bibitemnum)=@_;
1583   my $dbh=C4Connect;
1584   my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
1585   my $sth=$dbh->prepare($query);
1586   $sth->execute;
1587   my $data=$sth->fetchrow_hashref;
1588   $sth->finish;
1589   $dbh->disconnect;
1590   return($data->{'count(*)'});
1591 }
1592
1593 sub delitem{
1594   my ($itemnum)=@_;
1595   my $dbh=C4Connect;
1596   my $query="select * from items where itemnumber=$itemnum";
1597   my $sth=$dbh->prepare($query);
1598   $sth->execute;
1599   my @data=$sth->fetchrow_array;
1600   $sth->finish;
1601   $query="Insert into deleteditems values (";
1602   foreach my $temp (@data){
1603     $query=$query."'$temp',";
1604   }
1605   $query=~ s/\,$/\)/;
1606 #  print $query;
1607   $sth=$dbh->prepare($query);
1608   $sth->execute;
1609   $sth->finish;
1610   $query = "Delete from items where itemnumber=$itemnum";
1611   $sth=$dbh->prepare($query);
1612   $sth->execute;
1613   $sth->finish;
1614   $dbh->disconnect;
1615 }
1616
1617 sub deletebiblioitem {
1618     my ($biblioitemnumber) = @_;
1619     my $dbh   = C4Connect;
1620     my $query = "Select * from biblioitems
1621 where biblioitemnumber = $biblioitemnumber";
1622     my $sth   = $dbh->prepare($query);
1623     my @results;
1624
1625     $sth->execute;
1626   
1627     if (@results = $sth->fetchrow_array) {
1628
1629         $query = "Insert into deletedbiblioitems values (";
1630         foreach my $value (@results) {
1631             $value  = $dbh->quote($value);
1632             $query .= "$value,";
1633         } # foreach
1634
1635         $query =~ s/\,$/\)/;
1636         $dbh->do($query);
1637
1638         $query = "Delete from biblioitems
1639 where biblioitemnumber = $biblioitemnumber";
1640         $dbh->do($query);
1641     } # if
1642
1643     $sth->finish;
1644
1645 # Now delete all the items attached to the biblioitem
1646
1647     $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1648     $sth   = $dbh->prepare($query);
1649
1650     $sth->execute;
1651
1652     while (@results = $sth->fetchrow_array) {
1653
1654         $query = "Insert into deleteditems values (";
1655         foreach my $value (@results) {
1656             $value  = $dbh->quote($value);
1657             $query .= "$value,";
1658         } # foreach
1659
1660         $query =~ s/\,$/\)/;
1661         $dbh->do($query);
1662     } # while
1663
1664     $sth->finish;
1665
1666     $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1667     $dbh->do($query);
1668     
1669     $dbh->disconnect;
1670 } # sub deletebiblioitem
1671
1672
1673 sub delbiblio{
1674   my ($biblio)=@_;
1675   my $dbh=C4Connect;
1676   my $query="select * from biblio where biblionumber=$biblio";
1677   my $sth=$dbh->prepare($query);
1678   $sth->execute;
1679   if (my @data=$sth->fetchrow_array){
1680     $sth->finish;
1681     $query="Insert into deletedbiblio values (";
1682     foreach my $temp (@data){
1683       $temp=~ s/\'/\\\'/g;
1684       $query=$query."'$temp',";
1685     }
1686     $query=~ s/\,$/\)/;
1687 #   print $query;
1688     $sth=$dbh->prepare($query);
1689     $sth->execute;
1690     $sth->finish;
1691     $query = "Delete from biblio where biblionumber=$biblio";
1692     $sth=$dbh->prepare($query);
1693     $sth->execute;
1694     $sth->finish;
1695   }
1696
1697   $sth->finish;
1698   $dbh->disconnect;
1699 }
1700
1701 sub getitemtypes {
1702   my $dbh   = C4Connect;
1703   my $query = "select * from itemtypes";
1704   my $sth   = $dbh->prepare($query);
1705     # || die "Cannot prepare $query" . $dbh->errstr;
1706   my $count = 0;
1707   my @results;
1708   
1709   $sth->execute;
1710     # || die "Cannot execute $query\n" . $sth->errstr;
1711   while (my $data = $sth->fetchrow_hashref) {
1712     @results[$count] = $data;
1713     $count++;
1714   } # while
1715   
1716   $sth->finish;
1717   $dbh->disconnect;
1718   return($count, @results);
1719 } # sub getitemtypes
1720
1721 sub getbiblio {
1722     my ($biblionumber) = @_;
1723     my $dbh   = C4Connect;
1724     my $query = "Select * from biblio where biblionumber = $biblionumber";
1725     my $sth   = $dbh->prepare($query);
1726       # || die "Cannot prepare $query\n" . $dbh->errstr;
1727     my $count = 0;
1728     my @results;
1729     
1730     $sth->execute;
1731       # || die "Cannot execute $query\n" . $sth->errstr;
1732     while (my $data = $sth->fetchrow_hashref) {
1733       $results[$count] = $data;
1734       $count++;
1735     } # while
1736     
1737     $sth->finish;
1738     $dbh->disconnect;
1739     return($count, @results);
1740 } # sub getbiblio
1741
1742 sub getbiblioitem {
1743     my ($biblioitemnum) = @_;
1744     my $dbh   = C4Connect;
1745     my $query = "Select * from biblioitems where
1746 biblioitemnumber = $biblioitemnum";
1747     my $sth   = $dbh->prepare($query);
1748     my $count = 0;
1749     my @results;
1750
1751     $sth->execute;
1752
1753     while (my $data = $sth->fetchrow_hashref) {
1754         $results[$count] = $data;
1755         $count++;
1756     } # while
1757
1758     $sth->finish;
1759     $dbh->disconnect;
1760     return($count, @results);
1761 } # sub getbiblioitem
1762
1763 sub getbiblioitembybiblionumber {
1764     my ($biblionumber) = @_;
1765     my $dbh   = C4Connect;
1766     my $query = "Select * from biblioitems where biblionumber =
1767 $biblionumber";
1768     my $sth   = $dbh->prepare($query);
1769     my $count = 0;
1770     my @results;
1771
1772     $sth->execute;
1773
1774     while (my $data = $sth->fetchrow_hashref) {
1775         $results[$count] = $data;
1776         $count++;
1777     } # while
1778
1779     $sth->finish;
1780     $dbh->disconnect;
1781     return($count, @results);
1782 } # sub
1783
1784 sub getitemsbybiblioitem {
1785     my ($biblioitemnum) = @_;
1786     my $dbh   = C4Connect;
1787     my $query = "Select * from items, biblio where
1788 biblio.biblionumber = items.biblionumber and biblioitemnumber
1789 = $biblioitemnum";
1790     my $sth   = $dbh->prepare($query);
1791       # || die "Cannot prepare $query\n" . $dbh->errstr;
1792     my $count = 0;
1793     my @results;
1794     
1795     $sth->execute;
1796       # || die "Cannot execute $query\n" . $sth->errstr;
1797     while (my $data = $sth->fetchrow_hashref) {
1798       $results[$count] = $data;
1799       $count++;
1800     } # while
1801     
1802     $sth->finish;
1803     $dbh->disconnect;
1804     return($count, @results);
1805 } # sub getitemsbybiblioitem
1806
1807 sub isbnsearch {
1808     my ($isbn) = @_;
1809     my $dbh   = C4Connect;
1810     my $count = 0;
1811     my $query;
1812     my $sth;
1813     my @results;
1814     
1815     $isbn  = $dbh->quote($isbn);
1816     $query = "Select biblio.* from biblio, biblioitems where
1817 biblio.biblionumber = biblioitems.biblionumber
1818 and isbn = $isbn";
1819     $sth   = $dbh->prepare($query);
1820     
1821     $sth->execute;
1822     while (my $data = $sth->fetchrow_hashref) {
1823         $results[$count] = $data;
1824         $count++;
1825     } # while
1826
1827     $sth->finish;
1828     $dbh->disconnect;
1829     return($count, @results);
1830 } # sub isbnsearch
1831 sub skip {
1832 # At the moment this is just a straight copy of the subject code.  Needs heavy
1833 # modification to work for additional authors, obviously.
1834 # Check for additional author changes
1835     
1836     my $newadditionalauthor='';
1837     my $additionalauthors;
1838     foreach $newadditionalauthor (@{$biblio->{'additionalauthor'}}) {
1839         $additionalauthors->{$newadditionalauthor}=1;
1840         if ($origadditionalauthors->{$newadditionalauthor}) {
1841             $additionalauthors->{$newadditionalauthor}=2;
1842         } else {
1843             my $q_newadditionalauthor=$dbh->quote($newadditionalauthor);
1844             my $sth=$dbh->prepare("insert into biblioadditionalauthors (additionalauthor,biblionumber) values ($q_newadditionalauthor, $biblionumber)");
1845             $sth->execute;
1846             logchange('kohadb', 'add', 'biblio', 'additionalauthor', $newadditionalauthor);
1847             my $subfields;
1848             $subfields->{1}->{'Subfield_Mark'}='a';
1849             $subfields->{1}->{'Subfield_Value'}=$newadditionalauthor;
1850             my $tag='650';
1851             my $Record_ID;
1852             foreach $Record_ID (@marcrecords) {
1853                 addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1854                 logchange('marc', 'add', $Record_ID, '650', 'a', $newadditionalauthor);
1855             }
1856         }
1857     }
1858     my $origadditionalauthor;
1859     foreach $origadditionalauthor (keys %$origadditionalauthors) {
1860         if ($additionalauthors->{$origadditionalauthor} == 1) {
1861             my $q_origadditionalauthor=$dbh->quote($origadditionalauthor);
1862             logchange('kohadb', 'delete', 'biblio', '$biblionumber', 'additionalauthor', $origadditionalauthor);
1863             my $sth=$dbh->prepare("delete from biblioadditionalauthors where biblionumber=$biblionumber and additionalauthor=$q_origadditionalauthor");
1864             $sth->execute;
1865         }
1866     }
1867
1868 }
1869     $dbh->disconnect;
1870 }
1871
1872 sub logchange {
1873 # Subroutine to log changes to databases
1874 # Eventually, this subroutine will be used to create a log of all changes made,
1875 # with the possibility of "undo"ing some changes
1876     my $database=shift;
1877     if ($database eq 'kohadb') {
1878         my $type=shift;
1879         my $section=shift;
1880         my $item=shift;
1881         my $original=shift;
1882         my $new=shift;
1883         print STDERR "KOHA: $type $section $item $original $new\n";
1884     } elsif ($database eq 'marc') {
1885         my $type=shift;
1886         my $Record_ID=shift;
1887         my $tag=shift;
1888         my $mark=shift;
1889         my $subfield_ID=shift;
1890         my $original=shift;
1891         my $new=shift;
1892         print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
1893     }
1894 }
1895
1896 END { }       # module clean-up code here (global destructor)