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