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