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