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