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