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