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