* Acquisition synchronised with rel-1-2
[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 # move from 1.2 to 1.4 version : 
8 # 1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
9 # In the 1.4 version, we want to do 2 differents things :
10 #  - keep populating the old-DB, that has a LOT less datas than MARC
11 #  - populate the MARC-DB
12 # To populate the DBs we have 2 differents sources :
13 #  - the standard acquisition system (through book sellers), that does'nt use MARC data
14 #  - the MARC acquisition system, that uses MARC data.
15 #
16 # thus, we have 2 differents cases :
17 #   - with the standard acquisition system, we have non MARC data and want to populate old-DB and MARC-DB, knowing it's an incomplete MARC-record
18 #   - with the MARC acquisition system, we have MARC datas, and want to loose nothing in MARC-DB. So, we can't store datas in old-DB, then copy in MARC-DB.
19 #       we MUST have an API for true MARC data, that populate MARC-DB then old-DB
20 #
21 # That's why we need 4 APIs :
22 # all subs beginning by MARC manage only MARC tables. They manage MARC-DB with MARC::Record parameters
23 # all subs beginning by OLD manage only OLD-DB tables. They manage old-DB with old-DB parameters
24 # all subs beginning by ALL manage both OLD-DB and MARC tables. They use MARC::Record as parameters. it's the API that MUST be used in MARC acquisition system
25 # all subs beginning by seomething else are the old-style API. They use old-DB as parameter, then call internally the OLD and MARC subs.
26 #
27 # Thus, we assume a nice translation to future versions : if we want in a 1.6 release completly forget old-DB, we can do it easily.
28 # in 1.4 version, the translations will be nicer, as we have NOTHING to do in code. Everything has to be done in Biblio.pm ;-)
29
30
31 use strict;
32 require Exporter;
33 use C4::Database;
34 use MARC::Record;
35 use warnings;
36
37 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
38
39 # set the version for version checking
40 $VERSION = 0.01;
41
42 @ISA = qw(Exporter);
43 @EXPORT = qw(
44              &MARCaddbiblio &MARCmodsubfield &MARCaddsubfield 
45              &MARCmodbiblio
46              &MARCfindsubfield 
47              &MARCkoha2marc
48              &MARCgetbiblio
49              &MARCaddword &MARCdelword
50
51              &newBiblio &newBiblioItem &newItem 
52              &updateBiblio &updateBiblioItem &updateItem 
53              &itemcount &newbiblio &newbiblioitem 
54              &modnote &newsubject &newsubtitle
55              &newordernum &modbiblio &checkitems
56              &newitems &modbibitem
57              &modsubtitle &modsubject &modaddauthor &moditem &countitems 
58              &delitem &deletebiblioitem &delbiblio  
59              &getitemtypes &getbiblio
60              &getbiblioitembybiblionumber
61              &getbiblioitem &getitemsbybiblioitem &isbnsearch
62              &skip
63  );
64 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
65
66 # your exported package globals go here,
67 # as well as any optionally exported functions
68
69 @EXPORT_OK   = qw($Var1 %Hashit);
70
71
72 # non-exported package globals go here
73 use vars qw(@more $stuff);
74
75 # initalize package globals, first exported ones
76
77 my $Var1   = '';
78 my %Hashit = ();
79
80
81 # then the others (which are still accessible as $Some::Module::stuff)
82 my $stuff  = '';
83 my @more   = ();
84
85 # all file-scoped lexicals must be created before
86 # the functions below that use them.
87
88 # file-private lexicals go here
89 my $priv_var    = '';
90 my %secret_hash = ();
91
92 # here's a file-private function as a closure,
93 # callable as &$priv_func;  it cannot be prototyped.
94 my $priv_func = sub {
95   # stuff goes here.
96   };
97   
98 # make all your functions, whether exported or not;
99
100 #
101 #
102 # MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
103 #
104 #
105 # all the following subs takes a MARC::Record as parameter and manage
106 # the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the 
107 # ALLxxx subs (xxx deals with old-DB parameters, the ALLxxx deals with MARC-DB parameter)
108 =head1 SYNOPSIS
109
110   use Biblio.pm;
111   $dbh=&C4Connect;
112   $biblio= MARC::Record->new();
113   fill $biblio
114   $bibid = &MARCaddbiblio($dbh,$biblio);
115
116 =head1 DESCRIPTION
117
118   Creates a biblio (in the MARC tables only).
119
120 =head1 AUTHOR
121
122 Paul POULAIN paul.poulain@free.fr
123
124 =cut
125
126 sub MARCaddbiblio {
127 # pass the MARC::Record to this function, and it will create the records in the marc tables
128     my ($dbh,$record) = @_;
129     my @fields=$record->fields();
130     my $bibid;
131     # adding main table, and retrieving bibid
132     $dbh->do("lock tables marc_biblio WRITE");
133     my $sth=$dbh->prepare("insert into marc_biblio (datecreated) values (now())");
134     $sth->execute;
135     $sth=$dbh->prepare("select max(bibid) from marc_biblio");
136     $sth->execute;
137     ($bibid)=$sth->fetchrow;
138     $sth->finish;
139     $dbh->do("unlock tables");
140     my $fieldcount=0;
141     # now, add subfields...
142     foreach my $field (@fields) {
143         my @subfields=$field->subfields();
144         $fieldcount++;
145         foreach my $subfieldcount (0..$#subfields) {
146             print $field->tag().":".$field->indicator(1).$field->indicator(2).":".$subfields[$subfieldcount][0].":".$subfields[$subfieldcount][1]."\n";
147                     &MARCaddsubfield($dbh,$bibid,
148                                  $field->tag(),
149                                  $field->indicator(1).$field->indicator(2),
150                                  $fieldcount,
151                                  $subfields[$subfieldcount][0],
152                                  $subfieldcount,
153                                  $subfields[$subfieldcount][1]
154                                  );
155         }
156     }
157     return $bibid;
158 }
159
160 =head1 SYNOPSIS
161
162   use Biblio.pm;
163   &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
164
165 =head1 DESCRIPTION
166
167   Adds a subfield in a biblio (in the MARC tables only).
168
169 =head1 AUTHOR
170
171 Paul POULAIN paul.poulain@free.fr
172
173 =cut
174
175 sub MARCaddsubfield {
176 # Add a new subfield to a tag into the DB.
177     my $dbh=shift;
178     my $bibid=shift;
179     my $tagid=shift;
180     my $indicator=shift;
181     my $tagorder=shift;
182     my $subfieldcode=shift;
183     my $subfieldorder=shift;
184     my $subfieldvalue=shift;
185
186     unless ($subfieldorder) {
187         my $sth=$dbh->prepare("select max(subfieldorder) from marc_subfield_table where tag=$tagid");
188         $sth->execute;
189         if ($sth->rows) {
190             ($subfieldorder) = $sth->fetchrow;
191             $subfieldorder++;
192         } else {
193             $subfieldorder=1;
194         }
195     }
196     if (length($subfieldvalue)>255) {
197         $dbh->do("lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE");
198         my $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
199         $sth->execute($subfieldvalue);
200         $sth=$dbh->prepare("select max(blobidlink)from marc_blob_subfield");
201         $sth->execute;
202         my ($res)=$sth->fetchrow;
203         my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?)");
204         $sth->execute($bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$res);
205         $dbh->do("unlock tables");
206     } else {
207         my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?)");
208         $sth->execute($bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
209     }
210     &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
211 }
212
213 =head1 SYNOPSIS
214
215   use Biblio.pm;
216   $MARCRecord = &MARCgetbiblio($dbh,$bibid);
217
218 =head1 DESCRIPTION
219
220   Returns a MARC::Record for the biblio $bibid.
221
222 =head1 AUTHOR
223
224 Paul POULAIN paul.poulain@free.fr
225
226 =cut
227
228 sub MARCgetbiblio {
229 # Returns MARC::Record of the biblio passed in parameter.
230     my ($dbh,$bibid)=@_;
231     my $record = MARC::Record->new();
232 #---- TODO : the leader is missing
233     my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink 
234                                  from marc_subfield_table 
235                                  where bibid=?
236                          ");
237     my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
238     $sth->execute($bibid);
239     while (my $row=$sth->fetchrow_hashref) {
240         if ($row->{'valuebloblink'}) { #---- search blob if there is one
241             $sth2->execute($row->{'valuebloblink'});
242             my $row2=$sth2->fetchrow_hashref;
243             $sth2->finish;
244             $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
245         }
246         if ($record->field($row->{'tag'})) {
247             my $field =$record->field($row->{'tag'});
248             if ($field) {
249                 my $x = $field->add_subfields($row->{'subfieldcode'},$row->{'subfieldvalue'});
250                 $record->delete_field($field);
251                 $record->add_fields($field);
252             }
253         } else {
254             my $temp = MARC::Field->new($row->{'tag'}," "," ", $row->{'subfieldcode'} => $row->{'subfieldvalue'});
255             $record->add_fields($temp);
256         }
257
258     }
259 #    print "----------------------\n".$record->as_formatted()."\n-----------------";
260     return $record;
261 }
262
263 =head1 SYNOPSIS
264
265   use Biblio.pm;
266   $MARCRecord = &MARCmodbiblio($dbh,$bibid);
267
268 =head1 DESCRIPTION
269
270   MARCmodbiblio changes a biblio for a biblio,MARC::Record passed as parameter
271   if $delete == 1, every field/subfield not found is deleted in the biblio
272   otherwise, only data passed to MARCmodbiblio is managed.
273   thus, you can change only a small part of a biblio (like an item...)
274
275 =head1 AUTHOR
276
277 Paul POULAIN paul.poulain@free.fr
278
279 =cut
280
281 sub MARCmodbiblio {
282     my ($dbh,$bibid,$delete,$record)=@_;
283     my $oldrecord=&MARCgetbiblio($dbh,$bibid);
284 # if nothing to change, don't waste time...
285     if ($oldrecord eq $record) {
286 #       print "nothing to do \n";
287         return;
288     }
289 # otherwise, skip through each subfield...
290     my @fields = $record->fields();
291     my $tagorder=0;
292     foreach my $field (@fields) {
293 #print "tag : ".$field->tag()."\n";
294         my $oldfield = $oldrecord->field($field->tag());
295         my @subfields=$field->subfields();
296         my $subfieldorder=0;
297         $tagorder++;
298         foreach my $subfield (@subfields) {
299             $subfieldorder++;
300             if ($oldfield eq 0) {
301 # just adding datas...
302                 &MARCaddsubfield($dbh,$bibid,$field->tag(),$field->indicator(1).$field->indicator(2),
303                                  1,@$subfield[0],$subfieldorder,@$subfield[1]);
304             } else {
305 # modify he subfield if it's a different string
306                 if ($oldfield->subfield(@$subfield[0]) ne @$subfield[1] ) {
307                     my $subfieldid=&MARCfindsubfieldid($dbh,$bibid,$field->tag(),$tagorder,@$subfield[0],$subfieldorder);
308                     &MARCmodsubfield($dbh,$subfieldid,@$subfield[1]);
309                 } else {
310 #                   print "nothing to change\n";
311                 }
312             }
313         }
314     }
315 }
316
317 =head1 SYNOPSIS
318
319   use Biblio.pm;
320   ($subfieldid,$subfieldvalue) = &MARCmodsubfield($dbh,$subfieldid,$subfieldvalue);
321
322 =head1 DESCRIPTION
323
324   MARCmodsubfield changes the value of a given subfield
325
326 =head1 AUTHOR
327
328 Paul POULAIN paul.poulain@free.fr
329
330 =cut
331
332 sub MARCmodsubfield {
333 # Subroutine changes a subfield value given a subfieldid.
334     my ($dbh, $subfieldid, $subfieldvalue )=@_;
335
336     $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
337     my $sth1=$dbh->prepare("select valuebloblink from marc_subfield_table where subfieldid=?");
338     $sth1->execute($subfieldid);
339     my ($oldvaluebloblink)=$sth1->fetchrow;
340     $sth1->finish;
341     my $sth;
342     # if too long, use a bloblink
343     if (length($subfieldvalue)>255 ) {
344         # if already a bloblink, update it, otherwise, insert a new one.
345         if ($oldvaluebloblink) {
346             $sth=$dbh->prepare("update marc_blob_subfield set subfieldvalue=? where blobidlink=?");
347             $sth->execute($subfieldvalue,$oldvaluebloblink);
348         } else {
349             $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
350             $sth->execute($subfieldvalue);
351             $sth=$dbh->prepare("select max(blobidlink) from marc_blob_subfield");
352             $sth->execute;
353             my ($res)=$sth->fetchrow;
354             $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=null, valuebloblink=$res where subfieldid=?");
355             $sth->execute($subfieldid);
356         }
357     } else {
358         # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
359         $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?");
360         $sth->execute($subfieldvalue, $subfieldid);
361     }
362     $dbh->do("unlock tables");
363     $sth->finish;
364     $sth=$dbh->prepare("select bibid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from marc_subfield_table where subfieldid=?");
365     $sth->execute($subfieldid);
366     my ($bibid,$tagid,$tagorder,$subfieldcode,$subfieldid,$subfieldorder) = $sth->fetchrow;
367     &MARCdelword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
368     &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
369     return($subfieldid, $subfieldvalue);
370 }
371
372 =head1 SYNOPSIS
373
374   use Biblio.pm;
375   $subfieldid = &MARCfindsubfield($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue);
376
377 =head1 DESCRIPTION
378
379   MARCfindsubfield returns a subfield number given a bibid/tag/subfield values.
380   Returns -1 if more than 1 answer
381
382 =head1 AUTHOR
383
384 Paul POULAIN paul.poulain@free.fr
385
386 =cut
387
388 sub MARCfindsubfield {
389     my ($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
390     my $resultcounter=0;
391     my $subfieldid;
392     my $lastsubfieldid;
393     my $query="select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
394     if ($subfieldvalue) {
395         $query .= " and subfieldvalue=".$dbh->quote($subfieldvalue);
396     } else {
397         if ($subfieldorder<1) {
398             $subfieldorder=1;
399         }
400         $query .= " and subfieldorder=$subfieldorder";
401     }
402     my $sti=$dbh->prepare($query);
403     $sti->execute($bibid,$tag, $subfieldcode);
404     while (($subfieldid) = $sti->fetchrow) {
405         $resultcounter++;
406         $lastsubfieldid=$subfieldid;
407     }
408     if ($resultcounter>1) {
409         # Error condition.  Values given did not resolve into a unique record.  Don't know what to edit
410         # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
411         return -1;
412     } else {
413         return $lastsubfieldid;
414     }
415 }
416
417 =head1 SYNOPSIS
418
419   use Biblio.pm;
420   $subfieldid = &MARCfindsubfieldid($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
421
422 =head1 DESCRIPTION
423
424   MARCfindsubfieldid find a subfieldid for a bibid/tag/tagorder/subfield/subfieldorder
425
426 =head1 AUTHOR
427
428 Paul POULAIN paul.poulain@free.fr
429
430 =cut
431
432 sub MARCfindsubfieldid {
433     my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
434     my $sth=$dbh->prepare("select subfieldid from marc_subfield_table
435                         where bibid=? and tag=? and tagorder=? 
436                                 and subfieldcode=? and subfieldorder=?");
437     $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
438     my ($res) = $sth->fetchrow;
439     return $res;
440 }
441
442 =head1 SYNOPSIS
443
444   use Biblio.pm;
445   &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
446
447 =head1 DESCRIPTION
448
449   MARCdelsubfield delete a subfield for a bibid/tag/tagorder/subfield/subfieldorder
450
451 =head1 AUTHOR
452
453 Paul POULAIN paul.poulain@free.fr
454
455 =cut
456
457 sub MARCdelsubfield {
458 # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
459     my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
460 #    my $dbh=&C4Connect;
461     my $dbh->do("delete from marc_subfield_table where bibid='$bibid' and
462                         tag='$tag' and tagorder='$tagorder' 
463                         and subfieldcode='$subfield'and subfieldorder='$subfieldorder
464                         ");
465 }
466
467 =head1 SYNOPSIS
468
469   use Biblio.pm;
470   &MARCdelbiblio($dbh,$bibid);
471
472 =head1 DESCRIPTION
473
474   MARCdelbiblio delete biblio $bibid
475
476 =head1 AUTHOR
477
478 Paul POULAIN paul.poulain@free.fr
479
480 =cut
481
482 sub MARCdelbiblio {
483 # delete a biblio for a $bibid
484     my ($dbh,$bibid) = @_;
485 #    my $dbh=&C4Connect;
486     $dbh->do("delete from marc_subfield_table where bibid='$bibid'");
487     $dbh->do("delete from marc_biblio where bibid='$bibid'");
488 }
489
490 =head1 SYNOPSIS
491
492   use Biblio.pm;
493   $MARCRecord = &MARCkoha2marc($dbh,$biblionumber,biblioitemnumber,itemnumber);
494
495 =head1 DESCRIPTION
496
497   MARCkoha2marc is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB biblio/biblioitem/item
498
499 =head1 AUTHOR
500
501 Paul POULAIN paul.poulain@free.fr
502
503 =cut
504
505 sub MARCkoha2marc {
506 # this function builds MARC::Record from the old koha-DB fields
507     my ($dbh,$biblionumber,$biblioitemnumber,$itemnumber) = @_;
508 #    my $dbh=&C4Connect;
509     my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
510     my $record = MARC::Record->new();
511 #--- if bibid, then retrieve old-style koha data
512     if ($biblionumber>0) {
513         my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp 
514                 from biblio where biblionumber=?");             
515         $sth2->execute($biblionumber);
516         my $row=$sth2->fetchrow_hashref;
517         my $code;
518         foreach $code (keys %$row) {
519             if ($row->{$code}) {
520                 &MARCkoha2marcOnefield($sth,$record,"biblio.".$code,$row->{$code});
521             }
522         }
523     }
524 #--- if biblioitem, then retrieve old-style koha data
525     if ($biblioitemnumber>0) {
526         my $sth2=$dbh->prepare(" SELECT biblioitemnumber,biblionumber,volume,number,classification,
527                                                 itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
528                                                 volumedate,volumeddesc,timestamp,illus,pages,notes,size,place 
529                                         FROM biblioitems
530                                         WHERE biblionumber=? and biblioitemnumber=?
531                                         ");             
532         $sth2->execute($biblionumber,$biblioitemnumber);
533         my $row=$sth2->fetchrow_hashref;
534         my $code;
535         foreach $code (keys %$row) {
536             if ($row->{$code}) {
537                 &MARCkoha2marcOnefield($sth,$record,"biblioitem.".$code,$row->{$code});
538             }
539         }
540     }
541 #--- if item, then retrieve old-style koha data
542     if ($itemnumber>0) {
543         my $sth2=$dbh->prepare("SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
544                                                 booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
545                                                 datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,bulk,issues,renewals,
546                                         reserves,restricted,binding,itemnotes,holdingbranch,interim,timestamp 
547                                         FROM items
548                                         WHERE biblionumber=? and itemnumber=?");
549         $sth2->execute($biblionumber,$itemnumber);
550         my $row=$sth2->fetchrow_hashref;
551         my $code;
552         foreach $code (keys %$row) {
553             if ($row->{$code}) {
554                 &MARCkoha2marcOnefield($sth,$record,"items.".$code,$row->{$code});
555             }
556         }
557     }
558     return $record;
559 # TODO : retrieve notes, additionalauthors
560 }
561
562 =head1 DESCRIPTION
563
564   MARCkoha2marcOnefield is used by MARCkoha2marc and is not exported
565
566 =head1 AUTHOR
567
568 Paul POULAIN paul.poulain@free.fr
569
570 =cut
571 sub MARCkoha2marcOnefield {
572     my ($sth,$record,$kohafieldname,$value)=@_;
573     my $tagfield;
574     my $tagsubfield;
575     $sth->execute($kohafieldname);
576     if (($tagfield,$tagsubfield)=$sth->fetchrow) {
577         if ($record->field($tagfield)) {
578             my $tag =$record->field($tagfield);
579             if ($tag) {
580                 $tag->add_subfields($tagsubfield,$value);
581                 $record->delete_field($tag);
582                 $record->add_fields($tag);
583             }
584         } else {
585             $record->add_fields($tagfield," "," ",$tagsubfield => $value);
586         }
587     }
588     return $record;
589 }
590
591 =head1 DESCRIPTION
592
593   MARCaddword is used to manage MARC_word table and is not exported
594
595 =head1 AUTHOR
596
597 Paul POULAIN paul.poulain@free.fr
598
599 =cut
600
601 sub MARCaddword {
602 # split a subfield string and adds it into the word table.
603 # removes stopwords
604     my ($dbh,$bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
605     $sentence =~ s/(\.|\?|\:|\!|\'|,|\-)/ /g;
606 # TODO : remove stopwords
607     my @words = split / /,$sentence;
608     my $sth=$dbh->prepare("insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word)
609                         values (?,?,?,?,?,?,soundex(?))");
610     foreach my $word (@words) {
611 # we record only words longer than 2 car
612         if (length($word)>1) {
613             $sth->execute($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,$word);
614 #       print "($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,$word)\n";
615         }
616     }
617 }
618
619 =head1 DESCRIPTION
620
621   MARCdelword is used to manage MARC_word table and is not exported
622
623 =head1 AUTHOR
624
625 Paul POULAIN paul.poulain@free.fr
626
627 =cut
628
629 sub MARCdelword {
630 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
631     my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
632     my $sth=$dbh->prepare("delete from marc_word where bibid=? and tag=? and tagorder=? and subfieldid=? and subfieldorder=?");
633     $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
634 }
635
636 #
637 #
638 # OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
639 #
640 #
641 # all the following subs are the exact copy of 1.0/1.2 version of the sub
642 # without the OLD. The OLDxxx is called by the original xxx sub.
643 # the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
644 # WARNING : there is 1 difference between initialxxx and OLDxxx :
645 # the db header $dbh is always passed as parameter
646 # to avoid over-DB connexion
647
648 sub OLDnewbiblio {
649   my ($dbh,$biblio) = @_;
650 #  my $dbh    = &C4Connect;
651   my $query  = "Select max(biblionumber) from biblio";
652   my $sth    = $dbh->prepare($query);
653   $sth->execute;
654   my $data   = $sth->fetchrow_arrayref;
655   my $bibnum = $$data[0] + 1;
656   my $series = 0;
657
658   $biblio->{'title'}       = $dbh->quote($biblio->{'title'});
659   $biblio->{'author'}      = $dbh->quote($biblio->{'author'});
660   $biblio->{'copyright'}   = $dbh->quote($biblio->{'copyright'});
661   $biblio->{'seriestitle'} = $dbh->quote($biblio->{'seriestitle'});
662   $biblio->{'notes'}       = $dbh->quote($biblio->{'notes'});
663   $biblio->{'abstract'}    = $dbh->quote($biblio->{'abstract'});
664   if ($biblio->{'seriestitle'}) { $series = 1 };
665
666   $sth->finish;
667   $query = "insert into biblio set
668 biblionumber  = $bibnum,
669 title         = $biblio->{'title'},
670 author        = $biblio->{'author'},
671 copyrightdate = $biblio->{'copyright'},
672 serial        = $series,
673 seriestitle   = $biblio->{'seriestitle'},
674 notes         = $biblio->{'notes'},
675 abstract      = $biblio->{'abstract'}";
676
677   $sth = $dbh->prepare($query);
678   $sth->execute;
679
680   $sth->finish;
681 #  $dbh->disconnect;
682   return($bibnum);
683 }
684
685 sub OLDmodbiblio {
686     my ($dbh,$biblio) = @_;
687 #  my $dbh   = C4Connect;
688     my $query;
689     my $sth;
690     
691     $biblio->{'title'}         = $dbh->quote($biblio->{'title'});
692     $biblio->{'author'}        = $dbh->quote($biblio->{'author'});
693     $biblio->{'abstract'}      = $dbh->quote($biblio->{'abstract'});
694     $biblio->{'copyrightdate'} = $dbh->quote($biblio->{'copyrightdate'});
695     $biblio->{'seriestitle'}   = $dbh->quote($biblio->{'serirestitle'});
696     $biblio->{'serial'}        = $dbh->quote($biblio->{'serial'});
697     $biblio->{'unititle'}      = $dbh->quote($biblio->{'unititle'});
698     $biblio->{'notes'}         = $dbh->quote($biblio->{'notes'});
699     
700     $query = "Update biblio set
701 title         = $biblio->{'title'},
702 author        = $biblio->{'author'},
703 abstract      = $biblio->{'abstract'},
704 copyrightdate = $biblio->{'copyrightdate'},
705 seriestitle   = $biblio->{'seriestitle'},
706 serial        = $biblio->{'serial'},
707 unititle      = $biblio->{'unititle'},
708 notes         = $biblio->{'notes'}
709 where biblionumber = $biblio->{'biblionumber'}";
710     $sth   = $dbh->prepare($query);
711     
712     $sth->execute;
713     
714     $sth->finish;
715     $dbh->disconnect;
716     return($biblio->{'biblionumber'});
717 } # sub modbiblio
718
719 sub OLDmodsubtitle {
720   my ($dbh,$bibnum, $subtitle) = @_;
721 #  my $dbh   = C4Connect;
722   my $query = "update bibliosubtitle set
723 subtitle = '$subtitle'
724 where biblionumber = $bibnum";
725   my $sth   = $dbh->prepare($query);
726
727   $sth->execute;
728   $sth->finish;
729 #  $dbh->disconnect;
730 } # sub modsubtitle
731
732
733 sub OLDmodaddauthor {
734     my ($dbh,$bibnum, $author) = @_;
735 #    my $dbh   = C4Connect;
736     my $query = "Delete from additionalauthors where biblionumber = $bibnum";
737     my $sth = $dbh->prepare($query);
738
739     $sth->execute;
740     $sth->finish;
741
742     if ($author ne '') {
743         $query = "Insert into additionalauthors set
744                         author       = '$author',
745                         biblionumber = '$bibnum'";
746         $sth   = $dbh->prepare($query);
747
748         $sth->execute;
749
750         $sth->finish;
751     } # if
752
753   $dbh->disconnect;
754 } # sub modaddauthor
755
756
757 sub OLDmodsubject {
758     my ($dbh,$bibnum, $force, @subject) = @_;
759 #  my $dbh   = C4Connect;
760     my $count = @subject;
761     my $error;
762     for (my $i = 0; $i < $count; $i++) {
763         $subject[$i] =~ s/^ //g;
764         $subject[$i] =~ s/ $//g;
765         my $query = "select * from catalogueentry
766                         where entrytype = 's'
767                                 and catalogueentry = '$subject[$i]'";
768         my $sth   = $dbh->prepare($query);
769         $sth->execute;
770         
771         if (my $data = $sth->fetchrow_hashref) {
772         } else {
773             if ($force eq $subject[$i]) {
774                 # subject not in aut, chosen to force anway
775                 # so insert into cataloguentry so its in auth file
776                 $query = "Insert into catalogueentry
777                                 (entrytype,catalogueentry)
778                             values ('s','$subject[$i]')";
779          my $sth2 = $dbh->prepare($query);
780
781          $sth2->execute;
782          $sth2->finish;
783       } else {
784         $error = "$subject[$i]\n does not exist in the subject authority file";
785         $query = "Select * from catalogueentry
786                             where entrytype = 's'
787                             and (catalogueentry like '$subject[$i] %'
788                                  or catalogueentry like '% $subject[$i] %'
789                                  or catalogueentry like '% $subject[$i]')";
790         my $sth2 = $dbh->prepare($query);
791
792         $sth2->execute;
793         while (my $data = $sth2->fetchrow_hashref) {
794           $error = $error."<br>$data->{'catalogueentry'}";
795         } # while
796         $sth2->finish;
797       } # else
798     } # else
799     $sth->finish;
800   } # else
801   if ($error eq '') {
802     my $query = "Delete from bibliosubject where biblionumber = $bibnum";
803     my $sth   = $dbh->prepare($query);
804     $sth->execute;
805     $sth->finish;
806     for (my $i = 0; $i < $count; $i++) {
807       $sth = $dbh->prepare("Insert into bibliosubject
808                             values ('$subject[$i]', $bibnum)");
809
810       $sth->execute;
811       $sth->finish;
812     } # for
813   } # if
814
815 #  $dbh->disconnect;
816   return($error);
817 } # sub modsubject
818
819 sub OLDmodbibitem {
820     my ($dbh,$biblioitem) = @_;
821 #    my $dbh   = C4Connect;
822     my $query;
823
824     $biblioitem->{'itemtype'}        = $dbh->quote($biblioitem->{'itemtype'});
825     $biblioitem->{'url'}             = $dbh->quote($biblioitem->{'url'});
826     $biblioitem->{'isbn'}            = $dbh->quote($biblioitem->{'isbn'});
827     $biblioitem->{'publishercode'}   = $dbh->quote($biblioitem->{'publishercode'});
828     $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
829     $biblioitem->{'classification'}  = $dbh->quote($biblioitem->{'classification'});
830     $biblioitem->{'dewey'}           = $dbh->quote($biblioitem->{'dewey'});
831     $biblioitem->{'subclass'}        = $dbh->quote($biblioitem->{'subclass'});
832     $biblioitem->{'illus'}           = $dbh->quote($biblioitem->{'illus'});
833     $biblioitem->{'pages'}           = $dbh->quote($biblioitem->{'pages'});
834     $biblioitem->{'volumeddesc'}     = $dbh->quote($biblioitem->{'volumeddesc'});
835     $biblioitem->{'notes'}           = $dbh->quote($biblioitem->{'notes'});
836     $biblioitem->{'size'}            = $dbh->quote($biblioitem->{'size'});
837     $biblioitem->{'place'}           = $dbh->quote($biblioitem->{'place'});
838
839     $query = "Update biblioitems set
840 itemtype        = $biblioitem->{'itemtype'},
841 url             = $biblioitem->{'url'},
842 isbn            = $biblioitem->{'isbn'},
843 publishercode   = $biblioitem->{'publishercode'},
844 publicationyear = $biblioitem->{'publicationyear'},
845 classification  = $biblioitem->{'classification'},
846 dewey           = $biblioitem->{'dewey'},
847 subclass        = $biblioitem->{'subclass'},
848 illus           = $biblioitem->{'illus'},
849 pages           = $biblioitem->{'pages'},
850 volumeddesc     = $biblioitem->{'volumeddesc'},
851 notes           = $biblioitem->{'notes'},
852 size            = $biblioitem->{'size'},
853 place           = $biblioitem->{'place'}
854 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
855
856     $dbh->do($query);
857
858 #    $dbh->disconnect;
859 } # sub modbibitem
860
861 sub OLDmodnote {
862   my ($dbh,$bibitemnum,$note)=@_;
863 #  my $dbh=C4Connect;
864   my $query="update biblioitems set notes='$note' where
865   biblioitemnumber='$bibitemnum'";
866   my $sth=$dbh->prepare($query);
867   $sth->execute;
868   $sth->finish;
869 #  $dbh->disconnect;
870 }
871
872 sub OLDnewbiblioitem {
873     my ($dbh,$biblioitem) = @_;
874 #  my $dbh   = C4Connect;
875     my $query = "Select max(biblioitemnumber) from biblioitems";
876     my $sth   = $dbh->prepare($query);
877     my $data;
878     my $bibitemnum;
879     
880     $biblioitem->{'volume'}          = $dbh->quote($biblioitem->{'volume'});
881     $biblioitem->{'number'}        = $dbh->quote($biblioitem->{'number'});
882     $biblioitem->{'classification'}  = $dbh->quote($biblioitem->{'classification'});
883     $biblioitem->{'itemtype'}        = $dbh->quote($biblioitem->{'itemtype'});
884     $biblioitem->{'url'}             = $dbh->quote($biblioitem->{'url'});
885     $biblioitem->{'isbn'}            = $dbh->quote($biblioitem->{'isbn'});
886     $biblioitem->{'issn'}            = $dbh->quote($biblioitem->{'issn'});
887     $biblioitem->{'dewey'}           = $dbh->quote($biblioitem->{'dewey'});
888     $biblioitem->{'subclass'}        = $dbh->quote($biblioitem->{'subclass'});
889     $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
890     $biblioitem->{'publishercode'}   = $dbh->quote($biblioitem->{'publishercode'});
891     $biblioitem->{'volumedate'}      = $dbh->quote($biblioitem->{'volumedate'});
892     $biblioitem->{'volumeddesc'}     = $dbh->quote($biblioitem->{'volumeddesc'});  $biblioitem->{'illus'}            = $dbh->quote($biblioitem->{'illus'});
893     $biblioitem->{'illus'}         = $dbh->quote($biblioitem->{'illus'});
894     $biblioitem->{'pages'}           = $dbh->quote($biblioitem->{'pages'});
895     $biblioitem->{'notes'}           = $dbh->quote($biblioitem->{'notes'});
896     $biblioitem->{'size'}            = $dbh->quote($biblioitem->{'size'});
897     $biblioitem->{'place'}           = $dbh->quote($biblioitem->{'place'});
898     $biblioitem->{'lccn'}            = $dbh->quote($biblioitem->{'lccn'});
899     $biblioitem->{'marc'}            = $dbh->quote($biblioitem->{'marc'});
900   
901     $sth->execute;
902     $data       = $sth->fetchrow_arrayref;
903     $bibitemnum = $$data[0] + 1;
904
905     $sth->finish;
906
907     $query = "insert into biblioitems set
908                         biblioitemnumber = $bibitemnum,
909                         biblionumber     = $biblioitem->{'biblionumber'},
910                         volume           = $biblioitem->{'volume'},
911                         number           = $biblioitem->{'number'},
912                         classification   = $biblioitem->{'classification'},
913                         itemtype         = $biblioitem->{'itemtype'},
914                         url              = $biblioitem->{'url'},
915                         isbn             = $biblioitem->{'isbn'},
916                         issn             = $biblioitem->{'issn'},
917                         dewey            = $biblioitem->{'dewey'},
918                         subclass         = $biblioitem->{'subclass'},
919                         publicationyear  = $biblioitem->{'publicationyear'},
920                         publishercode    = $biblioitem->{'publishercode'},
921                         volumedate       = $biblioitem->{'volumedate'},
922                         volumeddesc      = $biblioitem->{'volumeddesc'},
923                         illus            = $biblioitem->{'illus'},
924                         pages            = $biblioitem->{'pages'},
925                         notes            = $biblioitem->{'notes'},
926                         size             = $biblioitem->{'size'},
927                         lccn             = $biblioitem->{'lccn'},
928                         marc             = $biblioitem->{'marc'},
929                         place            = $biblioitem->{'place'}";
930
931     $sth = $dbh->prepare($query);
932     $sth->execute;
933
934     $sth->finish;
935 #    $dbh->disconnect;
936     return($bibitemnum);
937 }
938
939 sub OLDnewsubject {
940   my ($dbh,$bibnum)=@_;
941 #  my $dbh=C4Connect;
942   my $query="insert into bibliosubject (biblionumber) values
943   ($bibnum)";
944   my $sth=$dbh->prepare($query);
945 #  print $query;
946   $sth->execute;
947   $sth->finish;
948 #  $dbh->disconnect;
949 }
950
951 sub OLDnewsubtitle {
952     my ($dbh,$bibnum, $subtitle) = @_;
953 #  my $dbh   = C4Connect;
954     $subtitle = $dbh->quote($subtitle);
955     my $query = "insert into bibliosubtitle set
956                             biblionumber = $bibnum,
957                             subtitle = $subtitle";
958     my $sth   = $dbh->prepare($query);
959
960     $sth->execute;
961
962     $sth->finish;
963 #  $dbh->disconnect;
964 }
965
966
967 sub OLDnewitems {
968   my ($dbh,$item, @barcodes) = @_;
969 #  my $dbh   = C4Connect;
970   my $query = "Select max(itemnumber) from items";
971   my $sth   = $dbh->prepare($query);
972   my $data;
973   my $itemnumber;
974   my $error;
975
976   $sth->execute;
977   $data       = $sth->fetchrow_hashref;
978   $itemnumber = $data->{'max(itemnumber)'} + 1;
979   $sth->finish;
980   
981   $item->{'booksellerid'}     = $dbh->quote($item->{'booksellerid'});
982   $item->{'homebranch'}       = $dbh->quote($item->{'homebranch'});
983   $item->{'price'}            = $dbh->quote($item->{'price'});
984   $item->{'replacementprice'} = $dbh->quote($item->{'replacementprice'});
985   $item->{'itemnotes'}        = $dbh->quote($item->{'itemnotes'});
986
987   foreach my $barcode (@barcodes) {
988     $barcode = uc($barcode);
989     $barcode = $dbh->quote($barcode);
990     $query   = "Insert into items set
991                             itemnumber           = $itemnumber,
992                             biblionumber         = $item->{'biblionumber'},
993                             biblioitemnumber     = $item->{'biblioitemnumber'},
994                             barcode              = $barcode,
995                             booksellerid         = $item->{'booksellerid'},
996                             dateaccessioned      = NOW(),
997                             homebranch           = $item->{'homebranch'},
998                             holdingbranch        = $item->{'homebranch'},
999                             price                = $item->{'price'},
1000                             replacementprice     = $item->{'replacementprice'},
1001                             replacementpricedate = NOW(),
1002                             itemnotes            = $item->{'itemnotes'}";
1003     if ($item->{'loan'}) {
1004       $query .= ",notforloan           = $item->{'loan'}";
1005     } # if
1006
1007     $sth = $dbh->prepare($query);
1008     $sth->execute;
1009
1010     $error .= $sth->errstr;
1011
1012     $sth->finish;
1013     $itemnumber++;
1014   } # for
1015
1016 #  $dbh->disconnect;
1017   return($error);
1018 }
1019
1020 sub OLDmoditem {
1021   my ($dbh,$loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1022 #  my $dbh=C4Connect;
1023   my $query="update items set biblioitemnumber=$bibitemnum,
1024                               barcode='$barcode',itemnotes='$notes'
1025                           where itemnumber=$itemnum";
1026   if ($barcode eq ''){
1027     $query="update items set biblioitemnumber=$bibitemnum,notforloan=$loan where itemnumber=$itemnum";
1028   }
1029   if ($lost ne ''){
1030     $query="update items set biblioitemnumber=$bibitemnum,
1031                              barcode='$barcode',
1032                              itemnotes='$notes',
1033                              homebranch='$homebranch',
1034                              itemlost='$lost',
1035                              wthdrawn='$wthdrawn' 
1036                           where itemnumber=$itemnum";
1037   }
1038   if ($replacement ne ''){
1039     $query=~ s/ where/,replacementprice='$replacement' where/;
1040   }
1041
1042   my $sth=$dbh->prepare($query);
1043   $sth->execute;
1044   $sth->finish;
1045 #  $dbh->disconnect;
1046 }
1047
1048 sub OLDdelitem{
1049   my ($dbh,$itemnum)=@_;
1050 #  my $dbh=C4Connect;
1051   my $query="select * from items where itemnumber=$itemnum";
1052   my $sth=$dbh->prepare($query);
1053   $sth->execute;
1054   my @data=$sth->fetchrow_array;
1055   $sth->finish;
1056   $query="Insert into deleteditems values (";
1057   foreach my $temp (@data){
1058     $query=$query."'$temp',";
1059   }
1060   $query=~ s/\,$/\)/;
1061 #  print $query;
1062   $sth=$dbh->prepare($query);
1063   $sth->execute;
1064   $sth->finish;
1065   $query = "Delete from items where itemnumber=$itemnum";
1066   $sth=$dbh->prepare($query);
1067   $sth->execute;
1068   $sth->finish;
1069 #  $dbh->disconnect;
1070 }
1071
1072 sub OLDdeletebiblioitem {
1073     my ($dbh,$biblioitemnumber) = @_;
1074 #    my $dbh   = C4Connect;
1075     my $query = "Select * from biblioitems
1076 where biblioitemnumber = $biblioitemnumber";
1077     my $sth   = $dbh->prepare($query);
1078     my @results;
1079
1080     $sth->execute;
1081   
1082     if (@results = $sth->fetchrow_array) {
1083         $query = "Insert into deletedbiblioitems values (";
1084         foreach my $value (@results) {
1085             $value  = $dbh->quote($value);
1086             $query .= "$value,";
1087         } # foreach
1088
1089         $query =~ s/\,$/\)/;
1090         $dbh->do($query);
1091
1092         $query = "Delete from biblioitems
1093                         where biblioitemnumber = $biblioitemnumber";
1094         $dbh->do($query);
1095     } # if
1096     $sth->finish;
1097 # Now delete all the items attached to the biblioitem
1098     $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1099     $sth   = $dbh->prepare($query);
1100     $sth->execute;
1101     while (@results = $sth->fetchrow_array) {
1102         $query = "Insert into deleteditems values (";
1103         foreach my $value (@results) {
1104             $value  = $dbh->quote($value);
1105             $query .= "$value,";
1106         } # foreach
1107         $query =~ s/\,$/\)/;
1108         $dbh->do($query);
1109     } # while
1110     $sth->finish;
1111     $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1112     $dbh->do($query);
1113 #    $dbh->disconnect;
1114 } # sub deletebiblioitem
1115
1116 sub OLDdelbiblio{
1117   my ($dbh,$biblio)=@_;
1118 #  my $dbh=C4Connect;
1119   my $query="select * from biblio where biblionumber=$biblio";
1120   my $sth=$dbh->prepare($query);
1121   $sth->execute;
1122   if (my @data=$sth->fetchrow_array){
1123     $sth->finish;
1124     $query="Insert into deletedbiblio values (";
1125     foreach my $temp (@data){
1126       $temp=~ s/\'/\\\'/g;
1127       $query=$query."'$temp',";
1128     }
1129     $query=~ s/\,$/\)/;
1130 #   print $query;
1131     $sth=$dbh->prepare($query);
1132     $sth->execute;
1133     $sth->finish;
1134     $query = "Delete from biblio where biblionumber=$biblio";
1135     $sth=$dbh->prepare($query);
1136     $sth->execute;
1137     $sth->finish;
1138   }
1139   $sth->finish;
1140 #  $dbh->disconnect;
1141 }
1142
1143 #
1144 #
1145 # old functions
1146 #
1147 #
1148
1149 sub itemcount{
1150   my ($biblio)=@_;
1151   my $dbh=C4Connect;
1152   my $query="Select count(*) from items where biblionumber=$biblio";
1153 #  print $query;
1154   my $sth=$dbh->prepare($query);
1155   $sth->execute;
1156   my $data=$sth->fetchrow_hashref;
1157   $sth->finish;
1158   $dbh->disconnect;
1159   return($data->{'count(*)'});
1160 }
1161
1162 sub getorder{
1163   my ($bi,$bib)=@_;
1164   my $dbh=C4Connect;
1165   my $query="Select ordernumber 
1166         from aqorders 
1167         where biblionumber=? and biblioitemnumber=?";
1168   my $sth=$dbh->prepare($query);
1169   $sth->execute($bib,$bi);
1170   my $ordnum=$sth->fetchrow_hashref;
1171   $sth->finish;
1172   my $order=getsingleorder($ordnum->{'ordernumber'});
1173   $dbh->disconnect;
1174 #  print $query;
1175   return ($order,$ordnum->{'ordernumber'});
1176 }
1177
1178 sub getsingleorder {
1179   my ($ordnum)=@_;
1180   my $dbh=C4Connect;
1181   my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown 
1182   where aqorders.ordernumber=? 
1183   and biblio.biblionumber=aqorders.biblionumber and
1184   biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
1185   aqorders.ordernumber=aqorderbreakdown.ordernumber";
1186   my $sth=$dbh->prepare($query);
1187   $sth->execute($ordnum);
1188   my $data=$sth->fetchrow_hashref;
1189   $sth->finish;
1190   $dbh->disconnect;
1191   return($data);
1192 }
1193
1194 sub newbiblio {
1195   my ($biblio) = @_;
1196   my $dbh    = &C4Connect;
1197   my $bibnum=OLDnewbiblio($dbh,$biblio);
1198 # TODO : MARC add
1199   $dbh->disconnect;
1200   return($bibnum);
1201 }
1202
1203 sub modbiblio {
1204   my ($biblio) = @_;
1205   my $dbh  = C4Connect;
1206   my $biblionumber=OLDmodbiblio($dbh,$biblio);
1207   $dbh->disconnect;
1208   return($biblionumber);
1209 } # sub modbiblio
1210
1211 sub modsubtitle {
1212   my ($bibnum, $subtitle) = @_;
1213   my $dbh   = C4Connect;
1214   &OLDmodsubtitle($dbh,$bibnum,$subtitle);
1215   $dbh->disconnect;
1216 } # sub modsubtitle
1217
1218
1219 sub modaddauthor {
1220     my ($bibnum, $author) = @_;
1221     my $dbh   = C4Connect;
1222     &OLDmodaddauthor($dbh,$bibnum,$author);
1223     $dbh->disconnect;
1224 } # sub modaddauthor
1225
1226
1227 sub modsubject {
1228   my ($bibnum, $force, @subject) = @_;
1229   my $dbh   = C4Connect;
1230   my $error= &OLDmodsubject($dbh,$bibnum,$force, @subject);
1231   return($error);
1232 } # sub modsubject
1233
1234 sub modbibitem {
1235     my ($biblioitem) = @_;
1236     my $dbh   = C4Connect;
1237     &OLDmodbibitem($dbh,$biblioitem);
1238     $dbh->disconnect;
1239 } # sub modbibitem
1240
1241 sub modnote {
1242   my ($bibitemnum,$note)=@_;
1243   my $dbh=C4Connect;
1244   &OLDmodnote($dbh,$bibitemnum,$note);
1245   $dbh->disconnect;
1246 }
1247
1248 sub newbiblioitem {
1249   my ($biblioitem) = @_;
1250   my $dbh   = C4Connect;
1251   my $bibitemnum = &OLDnewbiblioitem($dbh,$biblioitem);
1252   return($bibitemnum);
1253 }
1254
1255 sub newsubject {
1256   my ($bibnum)=@_;
1257   my $dbh=C4Connect;
1258   &OLDnewsubject($dbh,$bibnum);
1259   $dbh->disconnect;
1260 }
1261
1262 sub newsubtitle {
1263     my ($bibnum, $subtitle) = @_;
1264     my $dbh   = C4Connect;
1265     &OLDnewsubtitle($dbh,$bibnum,$subtitle);
1266   $dbh->disconnect;
1267 }
1268
1269
1270 sub newitems {
1271   my ($item, @barcodes) = @_;
1272   my $dbh   = C4Connect;
1273   my $error=&OLDnewitems($dbh,$item,@barcodes);
1274   $dbh->disconnect;
1275   return($error);
1276 }
1277
1278 sub moditem {
1279   my ($loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1280   my $dbh=C4Connect;
1281   &OLDmoditem($dbh,$loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement);
1282   $dbh->disconnect;
1283 }
1284
1285 sub checkitems{
1286   my ($count,@barcodes)=@_;
1287   my $dbh=C4Connect;
1288   my $error;
1289   for (my $i=0;$i<$count;$i++){
1290     $barcodes[$i]=uc $barcodes[$i];
1291     my $query="Select * from items where barcode='$barcodes[$i]'";
1292     my $sth=$dbh->prepare($query);
1293     $sth->execute;
1294     if (my $data=$sth->fetchrow_hashref){
1295       $error.=" Duplicate Barcode: $barcodes[$i]";
1296     }
1297     $sth->finish;
1298   }
1299   $dbh->disconnect;
1300   return($error);
1301 }
1302
1303 sub countitems{
1304   my ($bibitemnum)=@_;
1305   my $dbh=C4Connect;
1306   my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
1307   my $sth=$dbh->prepare($query);
1308   $sth->execute;
1309   my $data=$sth->fetchrow_hashref;
1310   $sth->finish;
1311   $dbh->disconnect;
1312   return($data->{'count(*)'});
1313 }
1314
1315 sub delitem{
1316   my ($itemnum)=@_;
1317   my $dbh=C4Connect;
1318   &OLDdelitem($dbh,$itemnum);
1319   $dbh->disconnect;
1320 }
1321
1322 sub deletebiblioitem {
1323     my ($biblioitemnumber) = @_;
1324     my $dbh   = C4Connect;
1325     &OLDdeletebiblioitem($dbh,$biblioitemnumber);
1326     $dbh->disconnect;
1327 } # sub deletebiblioitem
1328
1329
1330 sub delbiblio {
1331   my ($biblio)=@_;
1332   my $dbh=C4Connect;
1333   &OLDdelbiblio($dbh,$biblio);
1334   $dbh->disconnect;
1335 }
1336
1337 sub getitemtypes {
1338   my $dbh   = C4Connect;
1339   my $query = "select * from itemtypes";
1340   my $sth   = $dbh->prepare($query);
1341     # || die "Cannot prepare $query" . $dbh->errstr;
1342   my $count = 0;
1343   my @results;
1344   
1345   $sth->execute;
1346     # || die "Cannot execute $query\n" . $sth->errstr;
1347   while (my $data = $sth->fetchrow_hashref) {
1348     @results[$count] = $data;
1349     $count++;
1350   } # while
1351   
1352   $sth->finish;
1353   $dbh->disconnect;
1354   return($count, @results);
1355 } # sub getitemtypes
1356
1357 sub getbiblio {
1358     my ($biblionumber) = @_;
1359     my $dbh   = C4Connect;
1360     my $query = "Select * from biblio where biblionumber = $biblionumber";
1361     my $sth   = $dbh->prepare($query);
1362       # || die "Cannot prepare $query\n" . $dbh->errstr;
1363     my $count = 0;
1364     my @results;
1365     
1366     $sth->execute;
1367       # || die "Cannot execute $query\n" . $sth->errstr;
1368     while (my $data = $sth->fetchrow_hashref) {
1369       $results[$count] = $data;
1370       $count++;
1371     } # while
1372     
1373     $sth->finish;
1374     $dbh->disconnect;
1375     return($count, @results);
1376 } # sub getbiblio
1377
1378 sub getbiblioitem {
1379     my ($biblioitemnum) = @_;
1380     my $dbh   = C4Connect;
1381     my $query = "Select * from biblioitems where
1382 biblioitemnumber = $biblioitemnum";
1383     my $sth   = $dbh->prepare($query);
1384     my $count = 0;
1385     my @results;
1386
1387     $sth->execute;
1388
1389     while (my $data = $sth->fetchrow_hashref) {
1390         $results[$count] = $data;
1391         $count++;
1392     } # while
1393
1394     $sth->finish;
1395     $dbh->disconnect;
1396     return($count, @results);
1397 } # sub getbiblioitem
1398
1399 sub getbiblioitembybiblionumber {
1400     my ($biblionumber) = @_;
1401     my $dbh   = C4Connect;
1402     my $query = "Select * from biblioitems where biblionumber =
1403 $biblionumber";
1404     my $sth   = $dbh->prepare($query);
1405     my $count = 0;
1406     my @results;
1407
1408     $sth->execute;
1409
1410     while (my $data = $sth->fetchrow_hashref) {
1411         $results[$count] = $data;
1412         $count++;
1413     } # while
1414
1415     $sth->finish;
1416     $dbh->disconnect;
1417     return($count, @results);
1418 } # sub
1419
1420 sub getitemsbybiblioitem {
1421     my ($biblioitemnum) = @_;
1422     my $dbh   = C4Connect;
1423     my $query = "Select * from items, biblio where
1424 biblio.biblionumber = items.biblionumber and biblioitemnumber
1425 = $biblioitemnum";
1426     my $sth   = $dbh->prepare($query);
1427       # || die "Cannot prepare $query\n" . $dbh->errstr;
1428     my $count = 0;
1429     my @results;
1430     
1431     $sth->execute;
1432       # || die "Cannot execute $query\n" . $sth->errstr;
1433     while (my $data = $sth->fetchrow_hashref) {
1434       $results[$count] = $data;
1435       $count++;
1436     } # while
1437     
1438     $sth->finish;
1439     $dbh->disconnect;
1440     return($count, @results);
1441 } # sub getitemsbybiblioitem
1442
1443 sub isbnsearch {
1444     my ($isbn) = @_;
1445     my $dbh   = C4Connect;
1446     my $count = 0;
1447     my $query;
1448     my $sth;
1449     my @results;
1450     
1451     $isbn  = $dbh->quote($isbn);
1452     $query = "Select biblio.* from biblio, biblioitems where
1453 biblio.biblionumber = biblioitems.biblionumber
1454 and isbn = $isbn";
1455     $sth   = $dbh->prepare($query);
1456     
1457     $sth->execute;
1458     while (my $data = $sth->fetchrow_hashref) {
1459         $results[$count] = $data;
1460         $count++;
1461     } # while
1462
1463     $sth->finish;
1464     $dbh->disconnect;
1465     return($count, @results);
1466 } # sub isbnsearch
1467
1468 #sub skip {
1469 # At the moment this is just a straight copy of the subject code.  Needs heavy
1470 # modification to work for additional authors, obviously.
1471 # Check for additional author changes
1472     
1473 #    my $newadditionalauthor='';
1474 #    my $additionalauthors;
1475 #    foreach $newadditionalauthor (@{$biblio->{'additionalauthor'}}) {
1476 #       $additionalauthors->{$newadditionalauthor}=1;
1477 #       if ($origadditionalauthors->{$newadditionalauthor}) {
1478 #           $additionalauthors->{$newadditionalauthor}=2;
1479 #       } else {
1480 #           my $q_newadditionalauthor=$dbh->quote($newadditionalauthor);
1481 #           my $sth=$dbh->prepare("insert into biblioadditionalauthors (additionalauthor,biblionumber) values ($q_newadditionalauthor, $biblionumber)");
1482 #           $sth->execute;
1483 #           logchange('kohadb', 'add', 'biblio', 'additionalauthor', $newadditionalauthor);
1484 #           my $subfields;
1485 #           $subfields->{1}->{'Subfield_Mark'}='a';
1486 #           $subfields->{1}->{'Subfield_Value'}=$newadditionalauthor;
1487 #           my $tag='650';
1488 #           my $Record_ID;
1489 #           foreach $Record_ID (@marcrecords) {
1490 #               addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1491 #               logchange('marc', 'add', $Record_ID, '650', 'a', $newadditionalauthor);
1492 #           }
1493 #       }
1494 #    }
1495 #    my $origadditionalauthor;
1496 #    foreach $origadditionalauthor (keys %$origadditionalauthors) {
1497 #       if ($additionalauthors->{$origadditionalauthor} == 1) {
1498 #           my $q_origadditionalauthor=$dbh->quote($origadditionalauthor);
1499 #           logchange('kohadb', 'delete', 'biblio', '$biblionumber', 'additionalauthor', $origadditionalauthor);
1500 #           my $sth=$dbh->prepare("delete from biblioadditionalauthors where biblionumber=$biblionumber and additionalauthor=$q_origadditionalauthor");
1501 #           $sth->execute;
1502 #       }
1503 #    }
1504 #
1505 #}
1506 #    $dbh->disconnect;
1507 #}
1508
1509 sub logchange {
1510 # Subroutine to log changes to databases
1511 # Eventually, this subroutine will be used to create a log of all changes made,
1512 # with the possibility of "undo"ing some changes
1513     my $database=shift;
1514     if ($database eq 'kohadb') {
1515         my $type=shift;
1516         my $section=shift;
1517         my $item=shift;
1518         my $original=shift;
1519         my $new=shift;
1520         print STDERR "KOHA: $type $section $item $original $new\n";
1521     } elsif ($database eq 'marc') {
1522         my $type=shift;
1523         my $Record_ID=shift;
1524         my $tag=shift;
1525         my $mark=shift;
1526         my $subfield_ID=shift;
1527         my $original=shift;
1528         my $new=shift;
1529         print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
1530     }
1531 }
1532
1533 #
1534 #
1535 # UNUSEFUL SUBs. Could be deleted, kept only until beta test
1536 # maybe useful for some MARC tricks steve used.
1537 #
1538
1539 sub OLD_MAYBE_DELETED_newBiblioItem {
1540     my ($env, $biblioitem) = @_;
1541     my $dbh=&C4Connect;  
1542     my $biblionumber=$biblioitem->{'biblionumber'};
1543     my $biblioitemnumber=$biblioitem->{'biblioitemnumber'};
1544     my $volume=$biblioitem->{'volume'};
1545     my $q_volume=$dbh->quote($volume);
1546     my $number=$biblioitem->{'number'};
1547     my $q_number=$dbh->quote($number);
1548     my $classification=$biblioitem->{'classification'};
1549     my $q_classification=$dbh->quote($classification);
1550     my $itemtype=$biblioitem->{'itemtype'};
1551     my $q_itemtype=$dbh->quote($itemtype);
1552     my $isbn=$biblioitem->{'isbn'};
1553     my $q_isbn=$dbh->quote($isbn);
1554     my $issn=$biblioitem->{'issn'};
1555     my $q_issn=$dbh->quote($issn);
1556     my $dewey=$biblioitem->{'dewey'};
1557     $dewey=~s/\.*0*$//;
1558     ($dewey == 0) && ($dewey='');
1559     my $subclass=$biblioitem->{'subclass'};
1560     my $q_subclass=$dbh->quote($subclass);
1561     my $publicationyear=$biblioitem->{'publicationyear'};
1562     my $publishercode=$biblioitem->{'publishercode'};
1563     my $q_publishercode=$dbh->quote($publishercode);
1564     my $volumedate=$biblioitem->{'volumedate'};
1565     my $q_volumedate=$dbh->quote($volumedate);
1566     my $illus=$biblioitem->{'illus'};
1567     my $q_illus=$dbh->quote($illus);
1568     my $pages=$biblioitem->{'pages'};
1569     my $q_pages=$dbh->quote($pages);
1570     my $notes=$biblioitem->{'notes'};
1571     my $q_notes=$dbh->quote($notes);
1572     my $size=$biblioitem->{'size'};
1573     my $q_size=$dbh->quote($size);
1574     my $place=$biblioitem->{'place'};
1575     my $q_place=$dbh->quote($place);
1576     my $lccn=$biblioitem->{'lccn'};
1577     my $q_lccn=$dbh->quote($lccn);
1578
1579
1580 # Unless the $env->{'marconly'} flag is set, update the biblioitems table with
1581 # the new data
1582
1583     unless ($env->{'marconly'}) {
1584         #my $sth=$dbh->prepare("lock tables biblioitems write");
1585         #$sth->execute;
1586         my $sth=$dbh->prepare("select max(biblioitemnumber) from biblioitems");
1587         $sth->execute;
1588         my ($biblioitemnumber) =$sth->fetchrow;
1589         $biblioitemnumber++;
1590         $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)");
1591         $sth->execute;
1592         #my $sth=$dbh->prepare("unlock tables");
1593         #$sth->execute;
1594     }
1595
1596
1597 # Should we check if there is already a biblioitem/amrc with the
1598 # same isbn/lccn/issn?
1599
1600     my $sth=$dbh->prepare("select title,unititle,seriestitle,copyrightdate,notes,author from biblio where biblionumber=$biblionumber");
1601     $sth->execute;
1602     my ($title, $unititle,$seriestitle,$copyrightdate,$biblionotes,$author) = $sth->fetchrow;
1603     $sth=$dbh->prepare("select subtitle from bibliosubtitle where biblionumber=$biblionumber");
1604     $sth->execute;
1605     my ($subtitle) = $sth->fetchrow;
1606     $sth=$dbh->prepare("select author from additionalauthors where biblionumber=$biblionumber");
1607     $sth->execute;
1608     my @additionalauthors;
1609     while (my ($additionalauthor) = $sth->fetchrow) {
1610         push (@additionalauthors, $additionalauthor);
1611     }
1612     $sth=$dbh->prepare("select subject from bibliosubject where biblionumber=$biblionumber");
1613     $sth->execute;
1614     my @subjects;
1615     while (my ($subject) = $sth->fetchrow) {
1616         push (@subjects, $subject);
1617     }
1618
1619 # MARC SECTION
1620
1621     $sth=$dbh->prepare("insert into Resource_Table (Record_ID) values (0)");
1622     $sth->execute;
1623     my $Resource_ID=$dbh->{'mysql_insertid'};
1624     my $Record_ID=$Resource_ID;
1625     $sth=$dbh->prepare("update Resource_Table set Record_ID=$Record_ID where Resource_ID=$Resource_ID");
1626     $sth->execute;
1627
1628 # Title
1629     {
1630         my $subfields;
1631         $subfields->{1}->{'Subfield_Mark'}='a';
1632         $subfields->{1}->{'Subfield_Value'}=$title;
1633         if ($subtitle) {
1634             $subfields->{2}->{'Subfield_Mark'}='b';
1635             $subfields->{2}->{'Subfield_Value'}=$subtitle;
1636         }
1637         my $tag='245';
1638         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1639     }
1640
1641 # author
1642     {
1643         my $subfields;
1644         $subfields->{1}->{'Subfield_Mark'}='a';
1645         $subfields->{1}->{'Subfield_Value'}=$author;
1646         my $tag='100';
1647         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1648     }
1649 # Series Title
1650     if ($seriestitle) {
1651         my $subfields;
1652         $subfields->{1}->{'Subfield_Mark'}='a';
1653         $subfields->{1}->{'Subfield_Value'}=$seriestitle;
1654         my $tag='440';
1655         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1656     }
1657 # Biblio Note
1658     if ($biblionotes) {
1659         my $subfields;
1660         $subfields->{1}->{'Subfield_Mark'}='a';
1661         $subfields->{1}->{'Subfield_Value'}=$biblionotes;
1662         $subfields->{2}->{'Subfield_Mark'}='3';
1663         $subfields->{2}->{'Subfield_Value'}='biblio';
1664         my $tag='500';
1665         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1666     }
1667 # Additional Authors
1668     foreach (@additionalauthors) {
1669         my $author=$_;
1670         (next) unless ($author);
1671         my $subfields;
1672         $subfields->{1}->{'Subfield_Mark'}='a';
1673         $subfields->{1}->{'Subfield_Value'}=$author;
1674         $subfields->{2}->{'Subfield_Mark'}='e';
1675         $subfields->{2}->{'Subfield_Value'}='author';
1676         my $tag='700';
1677         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1678     }
1679 # Illustrator
1680     if ($illus) {
1681         (next) unless ($illus);
1682         my $subfields;
1683         $subfields->{1}->{'Subfield_Mark'}='a';
1684         $subfields->{1}->{'Subfield_Value'}=$illus;
1685         $subfields->{2}->{'Subfield_Mark'}='e';
1686         $subfields->{2}->{'Subfield_Value'}='illustrator';
1687         my $tag='700';
1688         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1689     }
1690 # Subjects
1691     foreach (@subjects) {
1692         my $subject=$_;
1693         (next) unless ($subject);
1694         my $subfields;
1695         $subfields->{1}->{'Subfield_Mark'}='a';
1696         $subfields->{1}->{'Subfield_Value'}=$subject;
1697         my $tag='650';
1698         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1699     }
1700
1701
1702 # ISBN
1703     if ($isbn) {
1704         my $subfields;
1705         $subfields->{1}->{'Subfield_Mark'}='a';
1706         $subfields->{1}->{'Subfield_Value'}=$isbn;
1707         my $tag='020';
1708         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1709     }
1710 # LCCN
1711     if ($lccn) {
1712         my $subfields;
1713         $subfields->{1}->{'Subfield_Mark'}='a';
1714         $subfields->{1}->{'Subfield_Value'}=$lccn;
1715         my $tag='010';
1716         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1717     }
1718 # ISSN
1719     if ($issn) {
1720         my $subfields;
1721         $subfields->{1}->{'Subfield_Mark'}='a';
1722         $subfields->{1}->{'Subfield_Value'}=$issn;
1723         my $tag='022';
1724         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1725     }
1726 # DEWEY
1727     if ($dewey) {
1728         my $subfields;
1729         $subfields->{1}->{'Subfield_Mark'}='a';
1730         $subfields->{1}->{'Subfield_Value'}=$dewey;
1731         my $tag='082';
1732         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1733     }
1734 # DEWEY subclass and itemtype
1735     {
1736         my $subfields;
1737         $subfields->{1}->{'Subfield_Mark'}='a';
1738         $subfields->{1}->{'Subfield_Value'}=$itemtype;
1739         $subfields->{2}->{'Subfield_Mark'}='b';
1740         $subfields->{2}->{'Subfield_Value'}=$subclass;
1741         $subfields->{3}->{'Subfield_Mark'}='c';
1742         $subfields->{3}->{'Subfield_Value'}=$biblionumber;
1743         $subfields->{4}->{'Subfield_Mark'}='d';
1744         $subfields->{4}->{'Subfield_Value'}=$biblioitemnumber;
1745         my $tag='090';
1746         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1747     }
1748 # PUBLISHER
1749     {
1750         my $subfields;
1751         $subfields->{1}->{'Subfield_Mark'}='a';
1752         $subfields->{1}->{'Subfield_Value'}=$place;
1753         $subfields->{2}->{'Subfield_Mark'}='b';
1754         $subfields->{2}->{'Subfield_Value'}=$publishercode;
1755         $subfields->{3}->{'Subfield_Mark'}='c';
1756         $subfields->{3}->{'Subfield_Value'}=$publicationyear;
1757         if ($copyrightdate) {
1758             $subfields->{4}->{'Subfield_Mark'}='c';
1759             $subfields->{4}->{'Subfield_Value'}="c$copyrightdate";
1760         }
1761         my $tag='260';
1762         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1763     }
1764 # PHYSICAL
1765     if ($pages || $size) {
1766         my $subfields;
1767         $subfields->{1}->{'Subfield_Mark'}='a';
1768         $subfields->{1}->{'Subfield_Value'}=$pages;
1769         $subfields->{2}->{'Subfield_Mark'}='c';
1770         $subfields->{2}->{'Subfield_Value'}=$size;
1771         my $tag='300';
1772         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1773     }
1774 # Volume/Number
1775     if ($volume || $number) {
1776         my $subfields;
1777         $subfields->{1}->{'Subfield_Mark'}='v';
1778         $subfields->{1}->{'Subfield_Value'}=$volume;
1779         $subfields->{2}->{'Subfield_Mark'}='n';
1780         $subfields->{2}->{'Subfield_Value'}=$number;
1781         my $tag='440';
1782         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1783     }
1784 # Biblioitem Note
1785     if ($notes) {
1786         my $subfields;
1787         $subfields->{1}->{'Subfield_Mark'}='a';
1788         $subfields->{1}->{'Subfield_Value'}=$notes;
1789         $subfields->{2}->{'Subfield_Mark'}='3';
1790         $subfields->{2}->{'Subfield_Value'}='biblioitem';
1791         my $tag='500';
1792         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1793     }
1794     $sth->finish;
1795     $dbh->disconnect;
1796     return ($env, $Record_ID);
1797 }
1798
1799 sub OLD_MAYBE_DELETED_newItem {
1800     my ($env, $Record_ID, $item) = @_;
1801     my $dbh=&C4Connect;  
1802     my $barcode=$item->{'barcode'};
1803     my $q_barcode=$dbh->quote($barcode);
1804     my $biblionumber=$item->{'biblionumber'};
1805     my $biblioitemnumber=$item->{'biblioitemnumber'};
1806     my $dateaccessioned=$item->{'dateaccessioned'};
1807     my $booksellerid=$item->{'booksellerid'};
1808     my $q_booksellerid=$dbh->quote($booksellerid);
1809     my $homebranch=$item->{'homebranch'};
1810     my $q_homebranch=$dbh->quote($homebranch);
1811     my $holdingbranch=$item->{'holdingbranch'};
1812     my $price=$item->{'price'};
1813     my $replacementprice=$item->{'replacementprice'};
1814     my $replacementpricedate=$item->{'replacementpricedate'};
1815     my $q_replacementpricedate=$dbh->quote($replacementpricedate);
1816     my $notforloan=$item->{'notforloan'};
1817     my $itemlost=$item->{'itemlost'};
1818     my $wthdrawn=$item->{'wthdrawn'};
1819     my $restricted=$item->{'restricted'};
1820     my $itemnotes=$item->{'itemnotes'};
1821     my $q_itemnotes=$dbh->quote($itemnotes);
1822     my $itemtype=$item->{'itemtype'};
1823     my $subclass=$item->{'subclass'};
1824
1825 # KOHADB Section
1826
1827     unless ($env->{'marconly'}) {
1828         my $sth=$dbh->prepare("select max(itemnumber) from items");
1829         $sth->execute;
1830         my ($itemnumber) =$sth->fetchrow;
1831         $itemnumber++;
1832         $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)");
1833         $sth->execute;
1834     }
1835
1836
1837 # MARC SECTION
1838     my $subfields;
1839     $subfields->{1}->{'Subfield_Mark'}='p';
1840     $subfields->{1}->{'Subfield_Value'}=$barcode;
1841     $subfields->{2}->{'Subfield_Mark'}='d';
1842     $subfields->{2}->{'Subfield_Value'}=$dateaccessioned;
1843     $subfields->{3}->{'Subfield_Mark'}='e';
1844     $subfields->{3}->{'Subfield_Value'}=$booksellerid;
1845     $subfields->{4}->{'Subfield_Mark'}='b';
1846     $subfields->{4}->{'Subfield_Value'}=$homebranch;
1847     $subfields->{5}->{'Subfield_Mark'}='l';
1848     $subfields->{5}->{'Subfield_Value'}=$holdingbranch;
1849     $subfields->{6}->{'Subfield_Mark'}='c';
1850     $subfields->{6}->{'Subfield_Value'}=$price;
1851     $subfields->{7}->{'Subfield_Mark'}='c';
1852     $subfields->{7}->{'Subfield_Value'}=$replacementprice;
1853     $subfields->{8}->{'Subfield_Mark'}='d';
1854     $subfields->{8}->{'Subfield_Value'}=$replacementpricedate;
1855     if ($notforloan) {
1856         $subfields->{9}->{'Subfield_Mark'}='h';
1857         $subfields->{9}->{'Subfield_Value'}='Not for loan';
1858     }
1859     if ($notforloan) {
1860         $subfields->{10}->{'Subfield_Mark'}='j';
1861         $subfields->{10}->{'Subfield_Value'}='Item lost';
1862     }
1863     if ($notforloan) {
1864         $subfields->{11}->{'Subfield_Mark'}='j';
1865         $subfields->{11}->{'Subfield_Value'}='Item withdrawn';
1866     }
1867     if ($notforloan) {
1868         $subfields->{12}->{'Subfield_Mark'}='z';
1869         $subfields->{12}->{'Subfield_Value'}=$itemnotes;
1870     }
1871     my $tag='876';
1872     my $Tag_ID;
1873     $env->{'linkage'}=1;
1874     ($env, $Tag_ID) = addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
1875     $env->{'linkage'}=0;
1876     $env->{'linkid'}=$Tag_ID;
1877     $tag='852';
1878     my $subfields2;
1879     $subfields2->{1}->{'Subfield_Mark'}='a';
1880     $subfields2->{1}->{'Subfield_Value'}='Coast Mountains School District';
1881     $subfields2->{1}->{'Subfield_Mark'}='b';
1882     $subfields2->{1}->{'Subfield_Value'}=$homebranch;
1883     $subfields2->{1}->{'Subfield_Mark'}='c';
1884     $subfields2->{1}->{'Subfield_Value'}=$itemtype;
1885     $subfields2->{2}->{'Subfield_Mark'}='m';
1886     $subfields2->{2}->{'Subfield_Value'}=$subclass;
1887     addTag($env, $Record_ID, $tag, ' ', ' ', $subfields2);
1888     $env->{'linkid'}='';
1889 }
1890
1891 sub OLD_MAYBE_DELETED_updateBiblio {
1892 # Update the biblio with biblionumber $biblio->{'biblionumber'}
1893 # I guess this routine should search through all marc records for a record that
1894 # has the same biblionumber stored in it, and modify the MARC record as well as
1895 # the biblio table.
1896 #
1897 # Also, this subroutine should search through the $biblio object and compare it
1898 # to the existing record and _LOG ALL CHANGES MADE_ in some way.  I'd like for
1899 # this logging feature to be usable to undo changes easily.
1900
1901     my ($env, $biblio) = @_;
1902     my $Record_ID;
1903     my $biblionumber=$biblio->{'biblionumber'};
1904     my $dbh=&C4Connect;  
1905     my $sth=$dbh->prepare("select * from biblio where biblionumber=$biblionumber");
1906     $sth->execute;
1907     my $origbiblio=$sth->fetchrow_hashref;
1908     $sth=$dbh->prepare("select subtitle from bibliosubtitle where biblionumber=$biblionumber");
1909     $sth->execute;
1910     my ($subtitle)=$sth->fetchrow;
1911     $origbiblio->{'subtitle'}=$subtitle;
1912     $sth=$dbh->prepare("select author from additionalauthors where biblionumber=$biblionumber");
1913     $sth->execute;
1914     my $origadditionalauthors;
1915     while (my ($author) = $sth->fetchrow) {
1916         push (@{$origbiblio->{'additionalauthors'}}, $author);
1917         $origadditionalauthors->{$author}=1;
1918     }
1919     $sth=$dbh->prepare("select subject from bibliosubject where biblionumber=$biblionumber");
1920     $sth->execute;
1921     my $origsubjects;
1922     while (my ($subject) = $sth->fetchrow) {
1923         push (@{$origbiblio->{'subjects'}}, $subject);
1924         $origsubjects->{$subject}=1;
1925     }
1926
1927     
1928 # Obtain a list of MARC Record_ID's that are tied to this biblio
1929     $sth=$dbh->prepare("select bibid from marc_subfield_table where tag='090' and subfieldvalue=$biblionumber and subfieldcode='c'");
1930     $sth->execute;
1931     my @marcrecords;
1932     while (my ($bibid) = $sth->fetchrow) {
1933         push(@marcrecords, $bibid);
1934     }
1935
1936     my $bibid='';
1937     if ($biblio->{'author'} ne $origbiblio->{'author'}) {
1938         my $q_author=$dbh->quote($biblio->{'author'});
1939         logchange('kohadb', 'change', 'biblio', 'author', $origbiblio->{'author'}, $biblio->{'author'});
1940         my $sti=$dbh->prepare("update biblio set author=$q_author where biblionumber=$biblio->{'biblionumber'}");
1941         $sti->execute;
1942         foreach $bibid (@marcrecords) {
1943             logchange('marc', 'change', $bibid, '100', 'a', $origbiblio->{'author'}, $biblio->{'author'});
1944             changeSubfield($bibid, '100', 'a', $origbiblio->{'author'}, $biblio->{'author'});
1945         }
1946     }
1947     if ($biblio->{'title'} ne $origbiblio->{'title'}) {
1948         my $q_title=$dbh->quote($biblio->{'title'});
1949         logchange('kohadb', 'change', 'biblio', 'title', $origbiblio->{'title'}, $biblio->{'title'});
1950         my $sti=$dbh->prepare("update biblio set title=$q_title where biblionumber=$biblio->{'biblionumber'}");
1951         $sti->execute;
1952         foreach $Record_ID (@marcrecords) {
1953             logchange('marc', 'change', $Record_ID, '245', 'a', $origbiblio->{'title'}, $biblio->{'title'});
1954             changeSubfield($Record_ID, '245', 'a', $origbiblio->{'title'}, $biblio->{'title'});
1955         }
1956     }
1957     if ($biblio->{'subtitle'} ne $origbiblio->{'subtitle'}) {
1958         my $q_subtitle=$dbh->quote($biblio->{'subtitle'});
1959         logchange('kohadb', 'change', 'biblio', 'subtitle', $origbiblio->{'subtitle'}, $biblio->{'subtitle'});
1960         my $sti=$dbh->prepare("update bibliosubtitle set subtitle=$q_subtitle where biblionumber=$biblio->{'biblionumber'}");
1961         $sti->execute;
1962         foreach $Record_ID (@marcrecords) {
1963             logchange('marc', 'change', $Record_ID, '245', 'b', $origbiblio->{'subtitle'}, $biblio->{'subtitle'});
1964             changeSubfield($Record_ID, '245', 'b', $origbiblio->{'subtitle'}, $biblio->{'subtitle'});
1965         }
1966     }
1967     if ($biblio->{'unititle'} ne $origbiblio->{'unititle'}) {
1968         my $q_unititle=$dbh->quote($biblio->{'unititle'});
1969         logchange('kohadb', 'change', 'biblio', 'unititle', $origbiblio->{'unititle'}, $biblio->{'unititle'});
1970         my $sti=$dbh->prepare("update biblio set unititle=$q_unititle where biblionumber=$biblio->{'biblionumber'}");
1971         $sti->execute;
1972     }
1973     if ($biblio->{'notes'} ne $origbiblio->{'notes'}) {
1974         my $q_notes=$dbh->quote($biblio->{'notes'});
1975         logchange('kohadb', 'change', 'biblio', 'notes', $origbiblio->{'notes'}, $biblio->{'notes'});
1976         my $sti=$dbh->prepare("update biblio set notes=$q_notes where biblionumber=$biblio->{'biblionumber'}");
1977         $sti->execute;
1978         foreach $Record_ID (@marcrecords) {
1979             logchange('marc', 'change', $Record_ID, '500', 'a', $origbiblio->{'notes'}, $biblio->{'notes'});
1980             changeSubfield($Record_ID, '500', 'a', $origbiblio->{'notes'}, $biblio->{'notes'});
1981         }
1982     }
1983     if ($biblio->{'serial'} ne $origbiblio->{'serial'}) {
1984         my $q_serial=$dbh->quote($biblio->{'serial'});
1985         logchange('kohadb', 'change', 'biblio', 'serial', $origbiblio->{'serial'}, $biblio->{'serial'});
1986         my $sti=$dbh->prepare("update biblio set serial=$q_serial where biblionumber=$biblio->{'biblionumber'}");
1987         $sti->execute;
1988     }
1989     if ($biblio->{'seriestitle'} ne $origbiblio->{'seriestitle'}) {
1990         my $q_seriestitle=$dbh->quote($biblio->{'seriestitle'});
1991         logchange('kohadb', 'change', 'biblio', 'seriestitle', $origbiblio->{'seriestitle'}, $biblio->{'seriestitle'});
1992         my $sti=$dbh->prepare("update biblio set seriestitle=$q_seriestitle where biblionumber=$biblio->{'biblionumber'}");
1993         $sti->execute;
1994         foreach $Record_ID (@marcrecords) {
1995             logchange('marc', 'change', $Record_ID, '440', 'a', $origbiblio->{'seriestitle'}, $biblio->{'seriestitle'});
1996             changeSubfield($Record_ID, '440', 'a', $origbiblio->{'seriestitle'}, $biblio->{'seriestitle'});
1997         }
1998     }
1999     if ($biblio->{'copyrightdate'} ne $origbiblio->{'copyrightdate'}) {
2000         my $q_copyrightdate=$dbh->quote($biblio->{'copyrightdate'});
2001         logchange('kohadb', 'change', 'biblio', 'copyrightdate', $origbiblio->{'copyrightdate'}, $biblio->{'copyrightdate'});
2002         my $sti=$dbh->prepare("update biblio set copyrightdate=$q_copyrightdate where biblionumber=$biblio->{'biblionumber'}");
2003         $sti->execute;
2004         foreach $Record_ID (@marcrecords) {
2005             logchange('marc', 'change', $Record_ID, '260', 'c', "c$origbiblio->{'notes'}", "c$biblio->{'notes'}");
2006             changeSubfield($Record_ID, '260', 'c', "c$origbiblio->{'notes'}", "c$biblio->{'notes'}");
2007         }
2008     }
2009
2010 # Check for subject heading changes
2011     
2012     my $newsubject='';
2013     my $subjects;
2014     foreach $newsubject (@{$biblio->{'subject'}}) {
2015         $subjects->{$newsubject}=1;
2016         if ($origsubjects->{$newsubject}) {
2017             $subjects->{$newsubject}=2;
2018         } else {
2019             my $q_newsubject=$dbh->quote($newsubject);
2020             my $sth=$dbh->prepare("insert into bibliosubject (subject,biblionumber) values ($q_newsubject, $biblionumber)");
2021             $sth->execute;
2022             logchange('kohadb', 'add', 'biblio', 'subject', $newsubject);
2023             my $subfields;
2024             $subfields->{1}->{'Subfield_Mark'}='a';
2025             $subfields->{1}->{'Subfield_Value'}=$newsubject;
2026             my $tag='650';
2027             my $Record_ID;
2028             foreach $Record_ID (@marcrecords) {
2029                 addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
2030                 logchange('marc', 'add', $Record_ID, '650', 'a', $newsubject);
2031             }
2032         }
2033     }
2034     my $origsubject;
2035     foreach $origsubject (keys %$origsubjects) {
2036         if ($subjects->{$origsubject} == 1) {
2037             my $q_origsubject=$dbh->quote($origsubject);
2038             logchange('kohadb', 'delete', 'biblio', '$biblionumber', 'subject', $origsubject);
2039             my $sth=$dbh->prepare("delete from bibliosubject where biblionumber=$biblionumber and subject=$q_origsubject");
2040             $sth->execute;
2041         }
2042     }
2043 }
2044
2045 sub OLD_MAYBE_DELETED_updateBiblioItem {
2046 # Update the biblioitem with biblioitemnumber $biblioitem->{'biblioitemnumber'}
2047 #
2048 # This routine should also check to see which fields are actually being
2049 # modified, and log all changes.
2050
2051     my ($env, $biblioitem) = @_;
2052     my $dbh=&C4Connect;  
2053
2054     my $biblioitemnumber=$biblioitem->{'biblioitemnumber'};
2055     my $sth=$dbh->prepare("select * from biblioitems where biblioitemnumber=$biblioitemnumber");
2056 # obi = original biblioitem
2057     my $obi=$sth->fetchrow_hashref;
2058     $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");
2059     $sth->execute;
2060     my ($Record_ID) = $sth->fetchrow;
2061     if ($biblioitem->{'biblionumber'} ne $obi->{'biblionumber'}) {
2062         logchange('kohadb', 'change', 'biblioitems', 'biblionumber', $obi->{'biblionumber'}, $biblioitem->{'biblionumber'});
2063         my $sth=$dbh->prepare("update biblioitems set biblionumber=$biblioitem->{'biblionumber'} where biblioitemnumber=$biblioitemnumber");
2064         logchange('marc', 'change', $Record_ID, '090', 'c', $obi->{'biblionumber'}, $biblioitem->{'biblionumber'});
2065         changeSubfield($Record_ID, '090', 'c', $obi->{'biblionumber'}, $biblioitem->{'biblionumber'});
2066     }
2067     if ($biblioitem->{'volume'} ne $obi->{'volume'}) {
2068         logchange('kohadb', 'change', 'biblioitems', 'volume', $obi->{'volume'}, $biblioitem->{'volume'});
2069         my $q_volume=$dbh->quote($biblioitem->{'volume'});
2070         my $sth=$dbh->prepare("update biblioitems set volume=$q_volume where biblioitemnumber=$biblioitemnumber");
2071         logchange('marc', 'change', $Record_ID, '440', 'v', $obi->{'volume'}, $biblioitem->{'volume'});
2072         changeSubfield($Record_ID, '440', 'v', $obi->{'volume'}, $biblioitem->{'volume'});
2073     }
2074     if ($biblioitem->{'number'} ne $obi->{'number'}) {
2075         logchange('kohadb', 'change', 'biblioitems', 'number', $obi->{'number'}, $biblioitem->{'number'});
2076         my $q_number=$dbh->quote($biblioitem->{'number'});
2077         my $sth=$dbh->prepare("update biblioitems set number=$q_number where biblioitemnumber=$biblioitemnumber");
2078         logchange('marc', 'change', $Record_ID, '440', 'v', $obi->{'number'}, $biblioitem->{'number'});
2079         changeSubfield($Record_ID, '440', 'v', $obi->{'number'}, $biblioitem->{'number'});
2080     }
2081     if ($biblioitem->{'itemtype'} ne $obi->{'itemtype'}) {
2082         logchange('kohadb', 'change', 'biblioitems', 'itemtype', $obi->{'itemtype'}, $biblioitem->{'itemtype'});
2083         my $q_itemtype=$dbh->quote($biblioitem->{'itemtype'});
2084         my $sth=$dbh->prepare("update biblioitems set itemtype=$q_itemtype where biblioitemnumber=$biblioitemnumber");
2085         logchange('marc', 'change', $Record_ID, '090', 'a', $obi->{'itemtype'}, $biblioitem->{'itemtype'});
2086         changeSubfield($Record_ID, '090', 'a', $obi->{'itemtype'}, $biblioitem->{'itemtype'});
2087     }
2088     if ($biblioitem->{'isbn'} ne $obi->{'isbn'}) {
2089         logchange('kohadb', 'change', 'biblioitems', 'isbn', $obi->{'isbn'}, $biblioitem->{'isbn'});
2090         my $q_isbn=$dbh->quote($biblioitem->{'isbn'});
2091         my $sth=$dbh->prepare("update biblioitems set isbn=$q_isbn where biblioitemnumber=$biblioitemnumber");
2092         logchange('marc', 'change', $Record_ID, '020', 'a', $obi->{'isbn'}, $biblioitem->{'isbn'});
2093         changeSubfield($Record_ID, '020', 'a', $obi->{'isbn'}, $biblioitem->{'isbn'});
2094     }
2095     if ($biblioitem->{'issn'} ne $obi->{'issn'}) {
2096         logchange('kohadb', 'change', 'biblioitems', 'issn', $obi->{'issn'}, $biblioitem->{'issn'});
2097         my $q_issn=$dbh->quote($biblioitem->{'issn'});
2098         my $sth=$dbh->prepare("update biblioitems set issn=$q_issn where biblioitemnumber=$biblioitemnumber");
2099         logchange('marc', 'change', $Record_ID, '022', 'a', $obi->{'issn'}, $biblioitem->{'issn'});
2100         changeSubfield($Record_ID, '022', 'a', $obi->{'issn'}, $biblioitem->{'issn'});
2101     }
2102     if ($biblioitem->{'dewey'} ne $obi->{'dewey'}) {
2103         logchange('kohadb', 'change', 'biblioitems', 'dewey', $obi->{'dewey'}, $biblioitem->{'dewey'});
2104         my $sth=$dbh->prepare("update biblioitems set dewey=$biblioitem->{'dewey'} where biblioitemnumber=$biblioitemnumber");
2105         logchange('marc', 'change', $Record_ID, '082', 'a', $obi->{'dewey'}, $biblioitem->{'dewey'});
2106         changeSubfield($Record_ID, '082', 'a', $obi->{'dewey'}, $biblioitem->{'dewey'});
2107     }
2108     if ($biblioitem->{'subclass'} ne $obi->{'subclass'}) {
2109         logchange('kohadb', 'change', 'biblioitems', 'subclass', $obi->{'subclass'}, $biblioitem->{'subclass'});
2110         my $q_subclass=$dbh->quote($biblioitem->{'subclass'});
2111         my $sth=$dbh->prepare("update biblioitems set subclass=$q_subclass where biblioitemnumber=$biblioitemnumber");
2112         logchange('marc', 'change', $Record_ID, '090', 'b', $obi->{'subclass'}, $biblioitem->{'subclass'});
2113         changeSubfield($Record_ID, '090', 'b', $obi->{'subclass'}, $biblioitem->{'subclass'});
2114     }
2115     if ($biblioitem->{'place'} ne $obi->{'place'}) {
2116         logchange('kohadb', 'change', 'biblioitems', 'place', $obi->{'place'}, $biblioitem->{'place'});
2117         my $q_place=$dbh->quote($biblioitem->{'place'});
2118         my $sth=$dbh->prepare("update biblioitems set place=$q_place where biblioitemnumber=$biblioitemnumber");
2119         logchange('marc', 'change', $Record_ID, '260', 'a', $obi->{'place'}, $biblioitem->{'place'});
2120         changeSubfield($Record_ID, '260', 'a', $obi->{'place'}, $biblioitem->{'place'});
2121     }
2122     if ($biblioitem->{'publishercode'} ne $obi->{'publishercode'}) {
2123         logchange('kohadb', 'change', 'biblioitems', 'publishercode', $obi->{'publishercode'}, $biblioitem->{'publishercode'});
2124         my $q_publishercode=$dbh->quote($biblioitem->{'publishercode'});
2125         my $sth=$dbh->prepare("update biblioitems set publishercode=$q_publishercode where biblioitemnumber=$biblioitemnumber");
2126         logchange('marc', 'change', $Record_ID, '260', 'b', $obi->{'publishercode'}, $biblioitem->{'publishercode'});
2127         changeSubfield($Record_ID, '260', 'b', $obi->{'publishercode'}, $biblioitem->{'publishercode'});
2128     }
2129     if ($biblioitem->{'publicationyear'} ne $obi->{'publicationyear'}) {
2130         logchange('kohadb', 'change', 'biblioitems', 'publicationyear', $obi->{'publicationyear'}, $biblioitem->{'publicationyear'});
2131         my $q_publicationyear=$dbh->quote($biblioitem->{'publicationyear'});
2132         my $sth=$dbh->prepare("update biblioitems set publicationyear=$q_publicationyear where biblioitemnumber=$biblioitemnumber");
2133         logchange('marc', 'change', $Record_ID, '260', 'c', $obi->{'publicationyear'}, $biblioitem->{'publicationyear'});
2134         changeSubfield($Record_ID, '260', 'c', $obi->{'publicationyear'}, $biblioitem->{'publicationyear'});
2135     }
2136     if ($biblioitem->{'illus'} ne $obi->{'illus'}) {
2137         logchange('kohadb', 'change', 'biblioitems', 'illus', $obi->{'illus'}, $biblioitem->{'illus'});
2138         my $q_illus=$dbh->quote($biblioitem->{'illus'});
2139         my $sth=$dbh->prepare("update biblioitems set illus=$q_illus where biblioitemnumber=$biblioitemnumber");
2140         logchange('marc', 'change', $Record_ID, '700', 'a', $obi->{'illus'}, $biblioitem->{'illus'});
2141         changeSubfield($Record_ID, '700', 'a', $obi->{'illus'}, $biblioitem->{'illus'});
2142     }
2143     if ($biblioitem->{'pages'} ne $obi->{'pages'}) {
2144         logchange('kohadb', 'change', 'biblioitems', 'pages', $obi->{'pages'}, $biblioitem->{'pages'});
2145         my $q_pages=$dbh->quote($biblioitem->{'pages'});
2146         my $sth=$dbh->prepare("update biblioitems set pages=$q_pages where biblioitemnumber=$biblioitemnumber");
2147         logchange('marc', 'change', $Record_ID, '300', 'a', $obi->{'pages'}, $biblioitem->{'pages'});
2148         changeSubfield($Record_ID, '300', 'a', $obi->{'pages'}, $biblioitem->{'pages'});
2149     }
2150     if ($biblioitem->{'size'} ne $obi->{'size'}) {
2151         logchange('kohadb', 'change', 'biblioitems', 'size', $obi->{'size'}, $biblioitem->{'size'});
2152         my $q_size=$dbh->quote($biblioitem->{'size'});
2153         my $sth=$dbh->prepare("update biblioitems set size=$q_size where biblioitemnumber=$biblioitemnumber");
2154         logchange('marc', 'change', $Record_ID, '300', 'c', $obi->{'size'}, $biblioitem->{'size'});
2155         changeSubfield($Record_ID, '300', 'c', $obi->{'size'}, $biblioitem->{'size'});
2156     }
2157     if ($biblioitem->{'notes'} ne $obi->{'notes'}) {
2158         logchange('kohadb', 'change', 'biblioitems', 'notes', $obi->{'notes'}, $biblioitem->{'notes'});
2159         my $q_notes=$dbh->quote($biblioitem->{'notes'});
2160         my $sth=$dbh->prepare("update biblioitems set notes=$q_notes where biblioitemnumber=$biblioitemnumber");
2161         logchange('marc', 'change', $Record_ID, '500', 'a', $obi->{'notes'}, $biblioitem->{'notes'});
2162         changeSubfield($Record_ID, '500', 'a', $obi->{'notes'}, $biblioitem->{'notes'});
2163     }
2164     if ($biblioitem->{'lccn'} ne $obi->{'lccn'}) {
2165         logchange('kohadb', 'change', 'biblioitems', 'lccn', $obi->{'lccn'}, $biblioitem->{'lccn'});
2166         my $q_lccn=$dbh->quote($biblioitem->{'lccn'});
2167         my $sth=$dbh->prepare("update biblioitems set lccn=$q_lccn where biblioitemnumber=$biblioitemnumber");
2168         logchange('marc', 'change', $Record_ID, '010', 'a', $obi->{'lccn'}, $biblioitem->{'lccn'});
2169         changeSubfield($Record_ID, '010', 'a', $obi->{'lccn'}, $biblioitem->{'lccn'});
2170     }
2171     $sth->finish;
2172     $dbh->disconnect;
2173
2174 }
2175
2176 sub OLD_MAYBE_DELETED_updateItem {
2177 # Update the item with itemnumber $item->{'itemnumber'}
2178 # This routine should also modify the corresponding MARC record data. (852 and
2179 # 876 tags with 876p tag the same as $item->{'barcode'}
2180 #
2181 # This routine should also check to see which fields are actually being
2182 # modified, and log all changes.
2183
2184     my ($env, $item) = @_;
2185     my $dbh=&C4Connect;  
2186     my $itemnumber=$item->{'itemnumber'};
2187     my $biblionumber=$item->{'biblionumber'};
2188     my $biblioitemnumber=$item->{'biblioitemnumber'};
2189     my $barcode=$item->{'barcode'};
2190     my $dateaccessioned=$item->{'dateaccessioned'};
2191     my $booksellerid=$item->{'booksellerid'};
2192     my $homebranch=$item->{'homebranch'};
2193     my $price=$item->{'price'};
2194     my $replacementprice=$item->{'replacementprice'};
2195     my $replacementpricedate=$item->{'replacementpricedate'};
2196     my $multivolume=$item->{'multivolume'};
2197     my $stack=$item->{'stack'};
2198     my $notforloan=$item->{'notforloan'};
2199     my $itemlost=$item->{'itemlost'};
2200     my $wthdrawn=$item->{'wthdrawn'};
2201     my $bulk=$item->{'bulk'};
2202     my $restricted=$item->{'restricted'};
2203     my $binding=$item->{'binding'};
2204     my $itemnotes=$item->{'itemnotes'};
2205     my $holdingbranch=$item->{'holdingbranch'};
2206     my $interim=$item->{'interim'};
2207     my $sth=$dbh->prepare("select * from items where itemnumber=$itemnumber");
2208     $sth->execute;
2209     my $olditem=$sth->fetchrow_hashref;
2210     my $q_barcode=$dbh->quote($olditem->{'barcode'});
2211     $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");
2212     $sth->execute;
2213     my ($Subfield876_ID, $Record_ID) = $sth->fetchrow;
2214     $sth=$dbh->prepare("select Subfield_Value from 8XX_Subfield_Table where Subfield_Mark=8 and Subfield_ID=$Subfield876_ID");
2215     $sth->execute;
2216     my ($link) = $sth->fetchrow;
2217     $sth=$dbh->prepare("select Subfield_ID from 8XX_Subfield_Table where Subfield_Mark=8 and Subfield_Value=$link and !(Subfield_ID=$Subfield876_ID)");
2218     $sth->execute;
2219     my ($Subfield852_ID) = $sth->fetchrow;
2220     
2221     if ($item->{'barcode'} ne $olditem->{'barcode'}) {
2222         logchange('kohadb', 'change', 'items', 'barcode', $olditem->{'barcode'}, $item->{'barcode'});
2223         my $q_barcode=$dbh->quote($item->{'barcode'});
2224         my $sth=$dbh->prepare("update items set barcode=$q_barcode where itemnumber=$itemnumber");
2225         $sth->execute;
2226         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'p', $olditem->{'barcode'}, $item->{'barcode'}, $Subfield876_ID);
2227         logchange('marc', 'change', $Record_ID, '876', 'p', $Subfield_Key, $olditem->{'barcode'}, $item->{'barcode'});
2228     }
2229     if ($item->{'booksellerid'} ne $olditem->{'booksellerid'}) {
2230         logchange('kohadb', 'change', 'items', 'booksellerid', $olditem->{'booksellerid'}, $item->{'booksellerid'});
2231         my $q_booksellerid=$dbh->quote($item->{'booksellerid'});
2232         my $sth=$dbh->prepare("update items set booksellerid=$q_booksellerid where itemnumber=$itemnumber");
2233         $sth->execute;
2234         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'e', $olditem->{'booksellerid'}, $item->{'booksellerid'}, $Subfield876_ID);
2235         logchange('marc', 'change', $Record_ID, '876', 'e', $Subfield_Key, $olditem->{'booksellerid'}, $item->{'booksellerid'});
2236     }
2237     if ($item->{'dateaccessioned'} ne $olditem->{'dateaccessioned'}) {
2238         logchange('kohadb', 'change', 'items', 'dateaccessioned', $olditem->{'dateaccessioned'}, $item->{'dateaccessioned'});
2239         my $q_dateaccessioned=$dbh->quote($item->{'dateaccessioned'});
2240         my $sth=$dbh->prepare("update items set dateaccessioned=$q_dateaccessioned where itemnumber=$itemnumber");
2241         $sth->execute;
2242         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'd', $olditem->{'dateaccessioned'}, $item->{'dateaccessioned'}, $Subfield876_ID);
2243         logchange('marc', 'change', $Record_ID, '876', 'd', $Subfield_Key, $olditem->{'dateaccessioned'}, $item->{'dateaccessioned'});
2244     }
2245     if ($item->{'homebranch'} ne $olditem->{'homebranch'}) {
2246         logchange('kohadb', 'change', 'items', 'homebranch', $olditem->{'homebranch'}, $item->{'homebranch'});
2247         my $q_homebranch=$dbh->quote($item->{'homebranch'});
2248         my $sth=$dbh->prepare("update items set homebranch=$q_homebranch where itemnumber=$itemnumber");
2249         $sth->execute;
2250         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'b', $olditem->{'homebranch'}, $item->{'homebranch'}, $Subfield876_ID);
2251         logchange('marc', 'change', $Record_ID, '876', 'b', $Subfield_Key, $olditem->{'homebranch'}, $item->{'homebranch'});
2252     }
2253     if ($item->{'holdingbranch'} ne $olditem->{'holdingbranch'}) {
2254         logchange('kohadb', 'change', 'items', 'holdingbranch', $olditem->{'holdingbranch'}, $item->{'holdingbranch'});
2255         my $q_holdingbranch=$dbh->quote($item->{'holdingbranch'});
2256         my $sth=$dbh->prepare("update items set holdingbranch=$q_holdingbranch where itemnumber=$itemnumber");
2257         $sth->execute;
2258         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'l', $olditem->{'holdingbranch'}, $item->{'holdingbranch'}, $Subfield876_ID);
2259         logchange('marc', 'change', $Record_ID, '876', 'l', $Subfield_Key, $olditem->{'holdingbranch'}, $item->{'holdingbranch'});
2260     }
2261     if ($item->{'price'} ne $olditem->{'price'}) {
2262         logchange('kohadb', 'change', 'items', 'price', $olditem->{'price'}, $item->{'price'});
2263         my $q_price=$dbh->quote($item->{'price'});
2264         my $sth=$dbh->prepare("update items set price=$q_price where itemnumber=$itemnumber");
2265         $sth->execute;
2266         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'c', $olditem->{'price'}, $item->{'price'}, $Subfield876_ID);
2267         logchange('marc', 'change', $Record_ID, '876', 'c', $Subfield_Key, $olditem->{'price'}, $item->{'price'});
2268     }
2269     if ($item->{'itemnotes'} ne $olditem->{'itemnotes'}) {
2270         logchange('kohadb', 'change', 'items', 'itemnotes', $olditem->{'itemnotes'}, $item->{'itemnotes'});
2271         my $q_itemnotes=$dbh->quote($item->{'itemnotes'});
2272         my $sth=$dbh->prepare("update items set itemnotes=$q_itemnotes where itemnumber=$itemnumber");
2273         $sth->execute;
2274         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'c', $olditem->{'itemnotes'}, $item->{'itemnotes'}, $Subfield876_ID);
2275         logchange('marc', 'change', $Record_ID, '876', 'c', $Subfield_Key, $olditem->{'itemnotes'}, $item->{'itemnotes'});
2276     }
2277     if ($item->{'notforloan'} ne $olditem->{'notforloan'}) {
2278         logchange('kohadb', 'change', 'items', 'notforloan', $olditem->{'notforloan'}, $item->{'notforloan'});
2279         my $sth=$dbh->prepare("update items set notforloan=$notforloan where itemnumber=$itemnumber");
2280         $sth->execute;
2281         if ($item->{'notforloan'}) {
2282             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Not for loan', $Subfield876_ID);
2283             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Not for loan');
2284         } else {
2285             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Not for loan', $Subfield876_ID);
2286             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Not for loan');
2287         }
2288     }
2289     if ($item->{'itemlost'} ne $olditem->{'itemlost'}) {
2290         logchange('kohadb', 'change', 'items', 'itemlost', $olditem->{'itemlost'}, $item->{'itemlost'});
2291         my $sth=$dbh->prepare("update items set itemlost=$itemlost where itemnumber=$itemnumber");
2292         $sth->execute;
2293         if ($item->{'itemlost'}) {
2294             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Item lost', $Subfield876_ID);
2295             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Item lost');
2296         } else {
2297             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Item lost', $Subfield876_ID);
2298             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Item lost');
2299         }
2300     }
2301     if ($item->{'wthdrawn'} ne $olditem->{'wthdrawn'}) {
2302         logchange('kohadb', 'change', 'items', 'wthdrawn', $olditem->{'wthdrawn'}, $item->{'wthdrawn'});
2303         my $sth=$dbh->prepare("update items set wthdrawn=$wthdrawn where itemnumber=$itemnumber");
2304         $sth->execute;
2305         if ($item->{'wthdrawn'}) {
2306             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Withdrawn', $Subfield876_ID);
2307             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Withdrawn');
2308         } else {
2309             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Withdrawn', $Subfield876_ID);
2310             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Withdrawn');
2311         }
2312     }
2313     if ($item->{'restricted'} ne $olditem->{'restricted'}) {
2314         logchange('kohadb', 'change', 'items', 'restricted', $olditem->{'restricted'}, $item->{'restricted'});
2315         my $sth=$dbh->prepare("update items set restricted=$restricted where itemnumber=$itemnumber");
2316         $sth->execute;
2317         if ($item->{'restricted'}) {
2318             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Restricted', $Subfield876_ID);
2319             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Restricted');
2320         } else {
2321             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Restricted', $Subfield876_ID);
2322             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Restricted');
2323         }
2324     }
2325     $sth->finish;
2326     $dbh->disconnect;
2327 }
2328
2329 #
2330 #
2331 # END OF UNUSEFUL SUBs
2332 #
2333 #
2334
2335 END { }       # module clean-up code here (global destructor)