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