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