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