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