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