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