test.pl, contains a script to test the Catalogue.pm stuff.
[koha.git] / C4 / Catalogue.pm
1 package C4::Catalogue; #asummes C4/Acquisitions.pm
2
3 # Continue working on updateItem!!!!!!
4 #
5 # updateItem is looking not bad.  Need to add addSubfield and deleteSubfield
6 # functions
7 #
8 # Trying to track down $dbh's that aren't disconnected....
9 #
10
11
12 use strict;
13 require Exporter;
14 use C4::Database;
15
16 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
17
18 # set the version for version checking
19 $VERSION = 0.01;
20
21 @ISA = qw(Exporter);
22 @EXPORT = qw(&newBiblio &newBiblioItem &newItem &updateBiblio &updateBiblioItem
23              &updateItem &changeSubfield &addSubfield &findSubfield 
24              &addMarcBiblio
25
26              &getorders &bookseller &breakdown &basket &newbasket &bookfunds
27              &ordersearch &newbiblio &newbiblioitem &newsubject &newsubtitle &neworder
28              &newordernum &modbiblio &modorder &getsingleorder &invoice &receiveorder
29              &bookfundbreakdown &curconvert &updatesup &insertsup &newitems &modbibitem
30              &getcurrencies &modsubtitle &modsubject &modaddauthor &moditem &countitems 
31              &findall &needsmod &delitem &deletebiblioitem &delbiblio &delorder &branches
32              &getallorders &getrecorders &updatecurrencies &getorder &getcurrency &updaterecorder
33              &updatecost &checkitems &modnote &getitemtypes &getbiblio
34              &getbiblioitembybiblionumber
35              &getbiblioitem &getitemsbybiblioitem &isbnsearch
36              &websitesearch &addwebsite &updatewebsite &deletewebsite);
37 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
38
39 # your exported package globals go here,
40 # as well as any optionally exported functions
41
42 @EXPORT_OK   = qw($Var1 %Hashit);
43
44
45 # non-exported package globals go here
46 use vars qw(@more $stuff);
47
48 # initalize package globals, first exported ones
49
50 my $Var1   = '';
51 my %Hashit = ();
52
53
54 # then the others (which are still accessible as $Some::Module::stuff)
55 my $stuff  = '';
56 my @more   = ();
57
58 # all file-scoped lexicals must be created before
59 # the functions below that use them.
60
61 # file-private lexicals go here
62 my $priv_var    = '';
63 my %secret_hash = ();
64
65 # here's a file-private function as a closure,
66 # callable as &$priv_func;  it cannot be prototyped.
67 my $priv_func = sub {
68   # stuff goes here.
69   };
70   
71 # make all your functions, whether exported or not;
72
73
74
75 sub newBiblio {
76 # This subroutine makes no modifications to the MARC tables.  MARC records are
77 # only created when new biblioitems are added.
78     my ($env, $biblio) = @_;
79     my $dbh=&C4Connect;  
80     my $subject=$biblio->{'subject'};
81     my $additionalauthors=$biblio->{'additionalauthors'};
82
83 # Why am I doing this?  This is a potential race condition.  At the very least,
84 # this needs code to ensure that two inserts didn't use the same
85 # biblionumber...
86
87     # Get next biblio number
88     my $sth=$dbh->prepare("select max(biblionumber) from biblio");
89     $sth->execute;
90     my ($biblionumber) = $sth->fetchrow;
91     $biblionumber++;
92
93     $sth=$dbh->prepare("insert into biblio 
94         (biblionumber,title,author,
95         unititle,copyrightdate,
96         serial,seriestitle,notes)
97          values (?, ?, ?, ?, ?, ?, ?, ?)");
98     $sth->execute($biblionumber, $biblio->{'title'}, $biblio->{'author'},
99         $biblio->{'unititle'}, $biblio->{'copyrightdate'}, 
100         $biblio->{'serial'}, $biblio->{'seriestitle'}, $biblio->{'notes'} );
101     $sth=$dbh->prepare("insert into bibliosubtitle 
102         (biblionumber,subtitle) 
103         values (?,?)");
104     $sth->execute($biblionumber, $biblio->{'subtitle'} );
105
106     my $sth=$dbh->prepare("insert into bibliosubject
107         (biblionumber,subject)
108         values (?,?) ");
109     foreach $_ (@$subject) {
110         $sth->execute($biblionumber,$_);
111     }
112     my $sth=$dbh->prepare("insert into additionalauthors 
113         (biblionumber,author)
114          values (?, ?)");
115     foreach $_ (@$additionalauthors) {
116         $sth->execute($biblionumber, $_ );
117     }
118 }
119
120
121 sub changeSubfield {
122 # Subroutine changes a subfield value given a subfieldid.
123     my ( $subfieldid, $subfieldvalue )=@_;
124
125     my $dbh=&C4Connect;
126     $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
127     my $sth1=$dbh->prepare("select valuebloblink from marc_subfield_table where subfieldid=?");
128     $sth1->execute($subfieldid);
129     my ($oldvaluebloblink)=$sth1->fetchrow;
130     $sth1->finish;
131     my $sth;
132     # if too long, use a bloblink
133     if (length($subfieldvalue)>255 ) {
134         # if already a bloblink, update it, otherwise, insert a new one.
135         if ($oldvaluebloblink) {
136             $sth=$dbh->prepare("update marc_blob_subfield set subfieldvalue=? where blobidlink=?");
137             $sth->execute($subfieldvalue,$oldvaluebloblink);
138         } else {
139             $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
140             $sth->execute($subfieldvalue);
141             $sth=$dbh->prepare("select max(blobidlink) from marc_blob_subfield");
142             $sth->execute;
143             my ($res)=$sth->fetchrow;
144             $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=null, valuebloblink=$res where subfieldid=?");
145             $sth->execute($subfieldid);
146         }
147     } else {
148         # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
149         $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?");
150         $sth->execute($subfieldvalue, $subfieldid);
151     }
152     $dbh->do("unlock tables");
153     $sth->finish;
154     $dbh->disconnect;
155     return($subfieldid, $subfieldvalue);
156 }
157
158 sub findSubfield {
159     my ($bibid,$tag,$subfieldcode,$subfieldvalue,$subfieldorder) = @_;
160     my $resultcounter=0;
161     my $subfieldid;
162     my $lastsubfieldid;
163     my $dbh=&C4Connect;
164     my $query="select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
165     if ($subfieldvalue) {
166         $query .= " and subfieldvalue=".$dbh->quote($subfieldvalue);
167     } else {
168         if ($subfieldorder<1) {
169             $subfieldorder=1;
170         }
171         $query .= " and subfieldorder=$subfieldorder";
172     }
173     my $sti=$dbh->prepare($query);
174     $sti->execute($bibid,$tag, $subfieldcode);
175     while (($subfieldid) = $sti->fetchrow) {
176         $resultcounter++;
177         $lastsubfieldid=$subfieldid;
178     }
179     if ($resultcounter>1) {
180         # Error condition.  Values given did not resolve into a unique record.  Don't know what to edit
181         # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
182         return -1;
183     } else {
184         return $lastsubfieldid;
185     }
186 }
187
188 sub addSubfield {
189 # Add a new subfield to a tag.
190     my $bibid=shift;
191     my $tagid=shift;
192     my $tagorder=shift;
193     my $subfieldcode=shift;
194     my $subfieldorder=shift;
195     my $subfieldvalue=shift;
196
197     my $dbh=&C4Connect;
198     unless ($subfieldorder) {
199         my $sth=$dbh->prepare("select max(subfieldorder) from marc_subfield_table where tagid=$tagid");
200         $sth->execute;
201         if ($sth->rows) {
202             ($subfieldorder) = $sth->fetchrow;
203             $subfieldorder++;
204         } else {
205             $subfieldorder=1;
206         }
207     }
208     if (length($subfieldvalue)>255) {
209         $dbh->do("lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE");
210         my $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
211         $sth->execute($subfieldvalue);
212         $sth=$dbh->prepare("select max(blobidlink)from marc_blob_subfield");
213         $sth->execute;
214         my ($res)=$sth->fetchrow;
215         my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tagid,tagorder,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?)");
216         $sth->execute($bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$res);
217         $dbh->do("unlock tables");
218     } else {
219         my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?)");
220         $sth->execute($bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
221     }
222 }
223
224 sub addMarcBiblio {
225 # pass the marcperlstructure to this function, and it will create the records in the marc tables
226     my ($marcstructure) = @_;
227     my $dbh=C4Connect;
228     my $tags;
229     my $i;
230     my $j;
231     # adding main table, and retrieving bibid
232     $dbh->do("lock tables marc_biblio WRITE");
233     my $sth=$dbh->prepare("insert into marc_biblio (datecreated,origincode) values (now(),?)");
234     $sth->execute($marcstructure->{origincode});
235     $sth=$dbh->prepare("select max(bibid) from marc_biblio");
236     $sth->execute;
237     ($marcstructure->{bibid})=$sth->fetchrow;
238     print "BIBID :::".$marcstructure->{bibid}."\n";
239     $sth->finish;
240     $dbh->do("unlock tables");
241     # now, add subfields...
242     foreach $tags ($marcstructure->{tags}) {
243         foreach $i (keys %{$tags}) {
244             foreach $j (keys %{$tags->{$i}->{subfields}}) {
245                 &addSubfield($marcstructure->{bibid},
246                              $tags->{$i}->{tag},
247                              $tags->{$i}->{tagorder},
248                              $tags->{$i}->{subfields}->{$j}->{mark},
249                              $tags->{$i}->{subfields}->{$j}->{subfieldorder},
250                              $tags->{$i}->{subfields}->{$j}->{value}
251                              );
252                 print $tags->{$i}->{tag}."//".$tags->{$i}->{subfields}->{$j}->{value}."\n";
253             }
254         }
255     }
256 }
257
258 sub updateBiblio {
259 # Update the biblio with biblionumber $biblio->{'biblionumber'}
260 # I guess this routine should search through all marc records for a record that
261 # has the same biblionumber stored in it, and modify the MARC record as well as
262 # the biblio table.
263 #
264 # Also, this subroutine should search through the $biblio object and compare it
265 # to the existing record and _LOG ALL CHANGES MADE_ in some way.  I'd like for
266 # this logging feature to be usable to undo changes easily.
267
268     my ($env, $biblio) = @_;
269     my $Record_ID;
270     my $biblionumber=$biblio->{'biblionumber'};
271     my $dbh=&C4Connect;  
272     my $sth=$dbh->prepare("select * from biblio where biblionumber=$biblionumber");
273     $sth->execute;
274     my $origbiblio=$sth->fetchrow_hashref;
275     $sth=$dbh->prepare("select subtitle from bibliosubtitle where biblionumber=$biblionumber");
276     $sth->execute;
277     my ($subtitle)=$sth->fetchrow;
278     $origbiblio->{'subtitle'}=$subtitle;
279     $sth=$dbh->prepare("select author from additionalauthors where biblionumber=$biblionumber");
280     $sth->execute;
281     my $origadditionalauthors;
282     while (my ($author) = $sth->fetchrow) {
283         push (@{$origbiblio->{'additionalauthors'}}, $author);
284         $origadditionalauthors->{$author}=1;
285     }
286     $sth=$dbh->prepare("select subject from bibliosubject where biblionumber=$biblionumber");
287     $sth->execute;
288     my $origsubjects;
289     while (my ($subject) = $sth->fetchrow) {
290         push (@{$origbiblio->{'subjects'}}, $subject);
291         $origsubjects->{$subject}=1;
292     }
293
294     
295 # Obtain a list of MARC Record_ID's that are tied to this biblio
296     $sth=$dbh->prepare("select bibid from marc_subfield_table where tag='090' and subfieldvalue=$biblionumber and subfieldcode='c'");
297     $sth->execute;
298     my @marcrecords;
299     while (my ($bibid) = $sth->fetchrow) {
300         push(@marcrecords, $bibid);
301     }
302
303
304
305     my $bibid='';
306     if ($biblio->{'author'} ne $origbiblio->{'author'}) {
307         my $q_author=$dbh->quote($biblio->{'author'});
308         logchange('kohadb', 'change', 'biblio', 'author', $origbiblio->{'author'}, $biblio->{'author'});
309         my $sti=$dbh->prepare("update biblio set author=$q_author where biblionumber=$biblio->{'biblionumber'}");
310         $sti->execute;
311         foreach $bibid (@marcrecords) {
312             logchange('marc', 'change', $bibid, '100', 'a', $origbiblio->{'author'}, $biblio->{'author'});
313             changeSubfield($bibid, '100', 'a', $origbiblio->{'author'}, $biblio->{'author'});
314         }
315     }
316     if ($biblio->{'title'} ne $origbiblio->{'title'}) {
317         my $q_title=$dbh->quote($biblio->{'title'});
318         logchange('kohadb', 'change', 'biblio', 'title', $origbiblio->{'title'}, $biblio->{'title'});
319         my $sti=$dbh->prepare("update biblio set title=$q_title where biblionumber=$biblio->{'biblionumber'}");
320         $sti->execute;
321         foreach $Record_ID (@marcrecords) {
322             logchange('marc', 'change', $Record_ID, '245', 'a', $origbiblio->{'title'}, $biblio->{'title'});
323             changeSubfield($Record_ID, '245', 'a', $origbiblio->{'title'}, $biblio->{'title'});
324         }
325     }
326     if ($biblio->{'subtitle'} ne $origbiblio->{'subtitle'}) {
327         my $q_subtitle=$dbh->quote($biblio->{'subtitle'});
328         logchange('kohadb', 'change', 'biblio', 'subtitle', $origbiblio->{'subtitle'}, $biblio->{'subtitle'});
329         my $sti=$dbh->prepare("update bibliosubtitle set subtitle=$q_subtitle where biblionumber=$biblio->{'biblionumber'}");
330         $sti->execute;
331         foreach $Record_ID (@marcrecords) {
332             logchange('marc', 'change', $Record_ID, '245', 'b', $origbiblio->{'subtitle'}, $biblio->{'subtitle'});
333             changeSubfield($Record_ID, '245', 'b', $origbiblio->{'subtitle'}, $biblio->{'subtitle'});
334         }
335     }
336     if ($biblio->{'unititle'} ne $origbiblio->{'unititle'}) {
337         my $q_unititle=$dbh->quote($biblio->{'unititle'});
338         logchange('kohadb', 'change', 'biblio', 'unititle', $origbiblio->{'unititle'}, $biblio->{'unititle'});
339         my $sti=$dbh->prepare("update biblio set unititle=$q_unititle where biblionumber=$biblio->{'biblionumber'}");
340         $sti->execute;
341     }
342     if ($biblio->{'notes'} ne $origbiblio->{'notes'}) {
343         my $q_notes=$dbh->quote($biblio->{'notes'});
344         logchange('kohadb', 'change', 'biblio', 'notes', $origbiblio->{'notes'}, $biblio->{'notes'});
345         my $sti=$dbh->prepare("update biblio set notes=$q_notes where biblionumber=$biblio->{'biblionumber'}");
346         $sti->execute;
347         foreach $Record_ID (@marcrecords) {
348             logchange('marc', 'change', $Record_ID, '500', 'a', $origbiblio->{'notes'}, $biblio->{'notes'});
349             changeSubfield($Record_ID, '500', 'a', $origbiblio->{'notes'}, $biblio->{'notes'});
350         }
351     }
352     if ($biblio->{'serial'} ne $origbiblio->{'serial'}) {
353         my $q_serial=$dbh->quote($biblio->{'serial'});
354         logchange('kohadb', 'change', 'biblio', 'serial', $origbiblio->{'serial'}, $biblio->{'serial'});
355         my $sti=$dbh->prepare("update biblio set serial=$q_serial where biblionumber=$biblio->{'biblionumber'}");
356         $sti->execute;
357     }
358     if ($biblio->{'seriestitle'} ne $origbiblio->{'seriestitle'}) {
359         my $q_seriestitle=$dbh->quote($biblio->{'seriestitle'});
360         logchange('kohadb', 'change', 'biblio', 'seriestitle', $origbiblio->{'seriestitle'}, $biblio->{'seriestitle'});
361         my $sti=$dbh->prepare("update biblio set seriestitle=$q_seriestitle where biblionumber=$biblio->{'biblionumber'}");
362         $sti->execute;
363         foreach $Record_ID (@marcrecords) {
364             logchange('marc', 'change', $Record_ID, '440', 'a', $origbiblio->{'seriestitle'}, $biblio->{'seriestitle'});
365             changeSubfield($Record_ID, '440', 'a', $origbiblio->{'seriestitle'}, $biblio->{'seriestitle'});
366         }
367     }
368     if ($biblio->{'copyrightdate'} ne $origbiblio->{'copyrightdate'}) {
369         my $q_copyrightdate=$dbh->quote($biblio->{'copyrightdate'});
370         logchange('kohadb', 'change', 'biblio', 'copyrightdate', $origbiblio->{'copyrightdate'}, $biblio->{'copyrightdate'});
371         my $sti=$dbh->prepare("update biblio set copyrightdate=$q_copyrightdate where biblionumber=$biblio->{'biblionumber'}");
372         $sti->execute;
373         foreach $Record_ID (@marcrecords) {
374             logchange('marc', 'change', $Record_ID, '260', 'c', "c$origbiblio->{'notes'}", "c$biblio->{'notes'}");
375             changeSubfield($Record_ID, '260', 'c', "c$origbiblio->{'notes'}", "c$biblio->{'notes'}");
376         }
377     }
378
379 # Check for subject heading changes
380     
381     my $newsubject='';
382     my $subjects;
383     foreach $newsubject (@{$biblio->{'subject'}}) {
384         $subjects->{$newsubject}=1;
385         if ($origsubjects->{$newsubject}) {
386             $subjects->{$newsubject}=2;
387         } else {
388             my $q_newsubject=$dbh->quote($newsubject);
389             my $sth=$dbh->prepare("insert into bibliosubject (subject,biblionumber) values ($q_newsubject, $biblionumber)");
390             $sth->execute;
391             logchange('kohadb', 'add', 'biblio', 'subject', $newsubject);
392             my $subfields;
393             $subfields->{1}->{'Subfield_Mark'}='a';
394             $subfields->{1}->{'Subfield_Value'}=$newsubject;
395             my $tag='650';
396             my $Record_ID;
397             foreach $Record_ID (@marcrecords) {
398                 addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
399                 logchange('marc', 'add', $Record_ID, '650', 'a', $newsubject);
400             }
401         }
402     }
403     my $origsubject;
404     foreach $origsubject (keys %$origsubjects) {
405         if ($subjects->{$origsubject} == 1) {
406             my $q_origsubject=$dbh->quote($origsubject);
407             logchange('kohadb', 'delete', 'biblio', '$biblionumber', 'subject', $origsubject);
408             my $sth=$dbh->prepare("delete from bibliosubject where biblionumber=$biblionumber and subject=$q_origsubject");
409             $sth->execute;
410         }
411     }
412
413 sub skip {
414 # At the moment this is just a straight copy of the subject code.  Needs heavy
415 # modification to work for additional authors, obviously.
416 # Check for additional author changes
417     
418     my $newadditionalauthor='';
419     my $additionalauthors;
420     foreach $newadditionalauthor (@{$biblio->{'additionalauthor'}}) {
421         $additionalauthors->{$newadditionalauthor}=1;
422         if ($origadditionalauthors->{$newadditionalauthor}) {
423             $additionalauthors->{$newadditionalauthor}=2;
424         } else {
425             my $q_newadditionalauthor=$dbh->quote($newadditionalauthor);
426             my $sth=$dbh->prepare("insert into biblioadditionalauthors (additionalauthor,biblionumber) values ($q_newadditionalauthor, $biblionumber)");
427             $sth->execute;
428             logchange('kohadb', 'add', 'biblio', 'additionalauthor', $newadditionalauthor);
429             my $subfields;
430             $subfields->{1}->{'Subfield_Mark'}='a';
431             $subfields->{1}->{'Subfield_Value'}=$newadditionalauthor;
432             my $tag='650';
433             my $Record_ID;
434             foreach $Record_ID (@marcrecords) {
435                 addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
436                 logchange('marc', 'add', $Record_ID, '650', 'a', $newadditionalauthor);
437             }
438         }
439     }
440     my $origadditionalauthor;
441     foreach $origadditionalauthor (keys %$origadditionalauthors) {
442         if ($additionalauthors->{$origadditionalauthor} == 1) {
443             my $q_origadditionalauthor=$dbh->quote($origadditionalauthor);
444             logchange('kohadb', 'delete', 'biblio', '$biblionumber', 'additionalauthor', $origadditionalauthor);
445             my $sth=$dbh->prepare("delete from biblioadditionalauthors where biblionumber=$biblionumber and additionalauthor=$q_origadditionalauthor");
446             $sth->execute;
447         }
448     }
449
450 }
451     $dbh->disconnect;
452 }
453
454 sub logchange {
455 # Subroutine to log changes to databases
456 # Eventually, this subroutine will be used to create a log of all changes made,
457 # with the possibility of "undo"ing some changes
458     my $database=shift;
459     if ($database eq 'kohadb') {
460         my $type=shift;
461         my $section=shift;
462         my $item=shift;
463         my $original=shift;
464         my $new=shift;
465         print STDERR "KOHA: $type $section $item $original $new\n";
466     } elsif ($database eq 'marc') {
467         my $type=shift;
468         my $Record_ID=shift;
469         my $tag=shift;
470         my $mark=shift;
471         my $subfield_ID=shift;
472         my $original=shift;
473         my $new=shift;
474         print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
475     }
476 }
477
478 sub addTag {
479 # Subroutine to add a tag to an existing MARC Record.  If a new linkage id is
480 # desired, set $env->{'linkage'} to 1.  If an existing linkage id should be
481 # set, set $env->{'linkid'} to the link number.
482     my ($env, $Record_ID, $tag, $Indicator1, $Indicator2, $subfields) = @_;
483     my $dbh=&C4Connect;  
484     ($Indicator1) || ($Indicator1=' ');
485     ($Indicator2) || ($Indicator2=' ');
486     my $firstdigit=substr($tag,0,1);
487     my $Subfield_ID;
488     foreach (sort keys %$subfields) {
489         my $Subfield_Mark=$subfields->{$_}->{'Subfield_Mark'};
490         my $Subfield_Value=$subfields->{$_}->{'Subfield_Value'};
491         my $q_Subfield_Value=$dbh->quote($Subfield_Value);
492         if ($Subfield_ID) {
493             my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_ID, Subfield_Mark, Subfield_Value) values ($Subfield_ID, '$Subfield_Mark', $q_Subfield_Value)");
494             $sth->execute;
495         } else {
496             my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_Mark, Subfield_Value) values ('$Subfield_Mark', $q_Subfield_Value)");
497             $sth->execute;
498             my $Subfield_Key=$dbh->{'mysql_insertid'};
499             $Subfield_ID=$Subfield_Key;
500             $sth=$dbh->prepare("update $firstdigit\XX_Subfield_Table set Subfield_ID=$Subfield_ID where Subfield_Key=$Subfield_Key");
501             $sth->execute;
502         }
503     }
504     if (my $linkid=$env->{'linkid'}) {
505         $env->{'linkage'}=0;
506         my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_ID, Subfield_Mark, Subfield_Value) values ($Subfield_ID, '8', '$linkid')");
507         $sth->execute;
508     }
509     my $sth=$dbh->prepare("insert into $firstdigit\XX_Tag_Table (Indicator1, Indicator2, Tag, Subfield_ID) values ('$Indicator1', '$Indicator2', '$tag', $Subfield_ID)");
510     $sth->execute;
511     my $Tag_Key=$dbh->{'mysql_insertid'};
512     my $Tag_ID=$Tag_Key;
513     $sth=$dbh->prepare("update $firstdigit\XX_Tag_Table set Tag_ID=$Tag_ID where Tag_Key=$Tag_Key");
514     $sth->execute;
515     $sth=$dbh->prepare("insert into Bib_Table (Record_ID, Tag_$firstdigit\XX_ID) values ($Record_ID, $Tag_ID)");
516     $sth->execute;
517     if ($env->{'linkage'}) {
518         my $sth=$dbh->prepare("insert into $firstdigit\XX_Subfield_Table (Subfield_ID, Subfield_Mark, Subfield_Value) values ($Subfield_ID, '8', '$Tag_ID')");
519         $sth->execute;
520         
521     }
522     $sth->finish;
523     $dbh->disconnect;
524     return ($env, $Tag_ID);
525 }
526
527 sub newBiblioItem {
528     my ($env, $biblioitem) = @_;
529     my $dbh=&C4Connect;  
530     my $biblionumber=$biblioitem->{'biblionumber'};
531     my $biblioitemnumber=$biblioitem->{'biblioitemnumber'};
532     my $volume=$biblioitem->{'volume'};
533     my $q_volume=$dbh->quote($volume);
534     my $number=$biblioitem->{'number'};
535     my $q_number=$dbh->quote($number);
536     my $classification=$biblioitem->{'classification'};
537     my $q_classification=$dbh->quote($classification);
538     my $itemtype=$biblioitem->{'itemtype'};
539     my $q_itemtype=$dbh->quote($itemtype);
540     my $isbn=$biblioitem->{'isbn'};
541     my $q_isbn=$dbh->quote($isbn);
542     my $issn=$biblioitem->{'issn'};
543     my $q_issn=$dbh->quote($issn);
544     my $dewey=$biblioitem->{'dewey'};
545     $dewey=~s/\.*0*$//;
546     ($dewey == 0) && ($dewey='');
547     my $subclass=$biblioitem->{'subclass'};
548     my $q_subclass=$dbh->quote($subclass);
549     my $publicationyear=$biblioitem->{'publicationyear'};
550     my $publishercode=$biblioitem->{'publishercode'};
551     my $q_publishercode=$dbh->quote($publishercode);
552     my $volumedate=$biblioitem->{'volumedate'};
553     my $q_volumedate=$dbh->quote($volumedate);
554     my $illus=$biblioitem->{'illus'};
555     my $q_illus=$dbh->quote($illus);
556     my $pages=$biblioitem->{'pages'};
557     my $q_pages=$dbh->quote($pages);
558     my $notes=$biblioitem->{'notes'};
559     my $q_notes=$dbh->quote($notes);
560     my $size=$biblioitem->{'size'};
561     my $q_size=$dbh->quote($size);
562     my $place=$biblioitem->{'place'};
563     my $q_place=$dbh->quote($place);
564     my $lccn=$biblioitem->{'lccn'};
565     my $q_lccn=$dbh->quote($lccn);
566
567
568 # Unless the $env->{'marconly'} flag is set, update the biblioitems table with
569 # the new data
570
571     unless ($env->{'marconly'}) {
572         #my $sth=$dbh->prepare("lock tables biblioitems write");
573         #$sth->execute;
574         my $sth=$dbh->prepare("select max(biblioitemnumber) from biblioitems");
575         $sth->execute;
576         my ($biblioitemnumber) =$sth->fetchrow;
577         $biblioitemnumber++;
578         $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)");
579         $sth->execute;
580         #my $sth=$dbh->prepare("unlock tables");
581         #$sth->execute;
582     }
583
584
585 # Should we check if there is already a biblioitem/marc with the
586 # same isbn/lccn/issn?
587
588     my $sth=$dbh->prepare("select title,unititle,seriestitle,copyrightdate,notes,author from biblio where biblionumber=$biblionumber");
589     $sth->execute;
590     my ($title, $unititle,$seriestitle,$copyrightdate,$biblionotes,$author) = $sth->fetchrow;
591     $sth=$dbh->prepare("select subtitle from bibliosubtitle where biblionumber=$biblionumber");
592     $sth->execute;
593     my ($subtitle) = $sth->fetchrow;
594     $sth=$dbh->prepare("select author from additionalauthors where biblionumber=$biblionumber");
595     $sth->execute;
596     my @additionalauthors;
597     while (my ($additionalauthor) = $sth->fetchrow) {
598         push (@additionalauthors, $additionalauthor);
599     }
600     $sth=$dbh->prepare("select subject from bibliosubject where biblionumber=$biblionumber");
601     $sth->execute;
602     my @subjects;
603     while (my ($subject) = $sth->fetchrow) {
604         push (@subjects, $subject);
605     }
606
607 # MARC SECTION
608
609     $sth=$dbh->prepare("insert into Resource_Table (Record_ID) values (0)");
610     $sth->execute;
611     my $Resource_ID=$dbh->{'mysql_insertid'};
612     my $Record_ID=$Resource_ID;
613     $sth=$dbh->prepare("update Resource_Table set Record_ID=$Record_ID where Resource_ID=$Resource_ID");
614     $sth->execute;
615
616 # Title
617     {
618         my $subfields;
619         $subfields->{1}->{'Subfield_Mark'}='a';
620         $subfields->{1}->{'Subfield_Value'}=$title;
621         if ($subtitle) {
622             $subfields->{2}->{'Subfield_Mark'}='b';
623             $subfields->{2}->{'Subfield_Value'}=$subtitle;
624         }
625         my $tag='245';
626         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
627     }
628
629 # author
630     {
631         my $subfields;
632         $subfields->{1}->{'Subfield_Mark'}='a';
633         $subfields->{1}->{'Subfield_Value'}=$author;
634         my $tag='100';
635         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
636     }
637 # Series Title
638     if ($seriestitle) {
639         my $subfields;
640         $subfields->{1}->{'Subfield_Mark'}='a';
641         $subfields->{1}->{'Subfield_Value'}=$seriestitle;
642         my $tag='440';
643         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
644     }
645 # Biblio Note
646     if ($biblionotes) {
647         my $subfields;
648         $subfields->{1}->{'Subfield_Mark'}='a';
649         $subfields->{1}->{'Subfield_Value'}=$biblionotes;
650         $subfields->{2}->{'Subfield_Mark'}='3';
651         $subfields->{2}->{'Subfield_Value'}='biblio';
652         my $tag='500';
653         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
654     }
655 # Additional Authors
656     foreach (@additionalauthors) {
657         my $author=$_;
658         (next) unless ($author);
659         my $subfields;
660         $subfields->{1}->{'Subfield_Mark'}='a';
661         $subfields->{1}->{'Subfield_Value'}=$author;
662         $subfields->{2}->{'Subfield_Mark'}='e';
663         $subfields->{2}->{'Subfield_Value'}='author';
664         my $tag='700';
665         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
666     }
667 # Illustrator
668     if ($illus) {
669         (next) unless ($illus);
670         my $subfields;
671         $subfields->{1}->{'Subfield_Mark'}='a';
672         $subfields->{1}->{'Subfield_Value'}=$illus;
673         $subfields->{2}->{'Subfield_Mark'}='e';
674         $subfields->{2}->{'Subfield_Value'}='illustrator';
675         my $tag='700';
676         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
677     }
678 # Subjects
679     foreach (@subjects) {
680         my $subject=$_;
681         (next) unless ($subject);
682         my $subfields;
683         $subfields->{1}->{'Subfield_Mark'}='a';
684         $subfields->{1}->{'Subfield_Value'}=$subject;
685         my $tag='650';
686         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
687     }
688
689
690 # ISBN
691     if ($isbn) {
692         my $subfields;
693         $subfields->{1}->{'Subfield_Mark'}='a';
694         $subfields->{1}->{'Subfield_Value'}=$isbn;
695         my $tag='020';
696         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
697     }
698 # LCCN
699     if ($lccn) {
700         my $subfields;
701         $subfields->{1}->{'Subfield_Mark'}='a';
702         $subfields->{1}->{'Subfield_Value'}=$lccn;
703         my $tag='010';
704         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
705     }
706 # ISSN
707     if ($issn) {
708         my $subfields;
709         $subfields->{1}->{'Subfield_Mark'}='a';
710         $subfields->{1}->{'Subfield_Value'}=$issn;
711         my $tag='022';
712         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
713     }
714 # DEWEY
715     if ($dewey) {
716         my $subfields;
717         $subfields->{1}->{'Subfield_Mark'}='a';
718         $subfields->{1}->{'Subfield_Value'}=$dewey;
719         my $tag='082';
720         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
721     }
722 # DEWEY subclass and itemtype
723     {
724         my $subfields;
725         $subfields->{1}->{'Subfield_Mark'}='a';
726         $subfields->{1}->{'Subfield_Value'}=$itemtype;
727         $subfields->{2}->{'Subfield_Mark'}='b';
728         $subfields->{2}->{'Subfield_Value'}=$subclass;
729         $subfields->{3}->{'Subfield_Mark'}='c';
730         $subfields->{3}->{'Subfield_Value'}=$biblionumber;
731         $subfields->{4}->{'Subfield_Mark'}='d';
732         $subfields->{4}->{'Subfield_Value'}=$biblioitemnumber;
733         my $tag='090';
734         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
735     }
736 # PUBLISHER
737     {
738         my $subfields;
739         $subfields->{1}->{'Subfield_Mark'}='a';
740         $subfields->{1}->{'Subfield_Value'}=$place;
741         $subfields->{2}->{'Subfield_Mark'}='b';
742         $subfields->{2}->{'Subfield_Value'}=$publishercode;
743         $subfields->{3}->{'Subfield_Mark'}='c';
744         $subfields->{3}->{'Subfield_Value'}=$publicationyear;
745         if ($copyrightdate) {
746             $subfields->{4}->{'Subfield_Mark'}='c';
747             $subfields->{4}->{'Subfield_Value'}="c$copyrightdate";
748         }
749         my $tag='260';
750         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
751     }
752 # PHYSICAL
753     if ($pages || $size) {
754         my $subfields;
755         $subfields->{1}->{'Subfield_Mark'}='a';
756         $subfields->{1}->{'Subfield_Value'}=$pages;
757         $subfields->{2}->{'Subfield_Mark'}='c';
758         $subfields->{2}->{'Subfield_Value'}=$size;
759         my $tag='300';
760         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
761     }
762 # Volume/Number
763     if ($volume || $number) {
764         my $subfields;
765         $subfields->{1}->{'Subfield_Mark'}='v';
766         $subfields->{1}->{'Subfield_Value'}=$volume;
767         $subfields->{2}->{'Subfield_Mark'}='n';
768         $subfields->{2}->{'Subfield_Value'}=$number;
769         my $tag='440';
770         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
771     }
772 # Biblioitem Note
773     if ($notes) {
774         my $subfields;
775         $subfields->{1}->{'Subfield_Mark'}='a';
776         $subfields->{1}->{'Subfield_Value'}=$notes;
777         $subfields->{2}->{'Subfield_Mark'}='3';
778         $subfields->{2}->{'Subfield_Value'}='biblioitem';
779         my $tag='500';
780         addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
781     }
782     $sth->finish;
783     $dbh->disconnect;
784     return ($env, $Record_ID);
785 }
786
787 sub updateBiblioItem {
788 # Update the biblioitem with biblioitemnumber $biblioitem->{'biblioitemnumber'}
789 #
790 # This routine should also check to see which fields are actually being
791 # modified, and log all changes.
792
793     my ($env, $biblioitem) = @_;
794     my $dbh=&C4Connect;  
795
796     my $biblioitemnumber=$biblioitem->{'biblioitemnumber'};
797     my $sth=$dbh->prepare("select * from biblioitems where biblioitemnumber=$biblioitemnumber");
798 # obi = original biblioitem
799     my $obi=$sth->fetchrow_hashref;
800     $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");
801     $sth->execute;
802     my ($Record_ID) = $sth->fetchrow;
803     if ($biblioitem->{'biblionumber'} ne $obi->{'biblionumber'}) {
804         logchange('kohadb', 'change', 'biblioitems', 'biblionumber', $obi->{'biblionumber'}, $biblioitem->{'biblionumber'});
805         my $sth=$dbh->prepare("update biblioitems set biblionumber=$biblioitem->{'biblionumber'} where biblioitemnumber=$biblioitemnumber");
806         logchange('marc', 'change', $Record_ID, '090', 'c', $obi->{'biblionumber'}, $biblioitem->{'biblionumber'});
807         changeSubfield($Record_ID, '090', 'c', $obi->{'biblionumber'}, $biblioitem->{'biblionumber'});
808     }
809     if ($biblioitem->{'volume'} ne $obi->{'volume'}) {
810         logchange('kohadb', 'change', 'biblioitems', 'volume', $obi->{'volume'}, $biblioitem->{'volume'});
811         my $q_volume=$dbh->quote($biblioitem->{'volume'});
812         my $sth=$dbh->prepare("update biblioitems set volume=$q_volume where biblioitemnumber=$biblioitemnumber");
813         logchange('marc', 'change', $Record_ID, '440', 'v', $obi->{'volume'}, $biblioitem->{'volume'});
814         changeSubfield($Record_ID, '440', 'v', $obi->{'volume'}, $biblioitem->{'volume'});
815     }
816     if ($biblioitem->{'number'} ne $obi->{'number'}) {
817         logchange('kohadb', 'change', 'biblioitems', 'number', $obi->{'number'}, $biblioitem->{'number'});
818         my $q_number=$dbh->quote($biblioitem->{'number'});
819         my $sth=$dbh->prepare("update biblioitems set number=$q_number where biblioitemnumber=$biblioitemnumber");
820         logchange('marc', 'change', $Record_ID, '440', 'v', $obi->{'number'}, $biblioitem->{'number'});
821         changeSubfield($Record_ID, '440', 'v', $obi->{'number'}, $biblioitem->{'number'});
822     }
823     if ($biblioitem->{'itemtype'} ne $obi->{'itemtype'}) {
824         logchange('kohadb', 'change', 'biblioitems', 'itemtype', $obi->{'itemtype'}, $biblioitem->{'itemtype'});
825         my $q_itemtype=$dbh->quote($biblioitem->{'itemtype'});
826         my $sth=$dbh->prepare("update biblioitems set itemtype=$q_itemtype where biblioitemnumber=$biblioitemnumber");
827         logchange('marc', 'change', $Record_ID, '090', 'a', $obi->{'itemtype'}, $biblioitem->{'itemtype'});
828         changeSubfield($Record_ID, '090', 'a', $obi->{'itemtype'}, $biblioitem->{'itemtype'});
829     }
830     if ($biblioitem->{'isbn'} ne $obi->{'isbn'}) {
831         logchange('kohadb', 'change', 'biblioitems', 'isbn', $obi->{'isbn'}, $biblioitem->{'isbn'});
832         my $q_isbn=$dbh->quote($biblioitem->{'isbn'});
833         my $sth=$dbh->prepare("update biblioitems set isbn=$q_isbn where biblioitemnumber=$biblioitemnumber");
834         logchange('marc', 'change', $Record_ID, '020', 'a', $obi->{'isbn'}, $biblioitem->{'isbn'});
835         changeSubfield($Record_ID, '020', 'a', $obi->{'isbn'}, $biblioitem->{'isbn'});
836     }
837     if ($biblioitem->{'issn'} ne $obi->{'issn'}) {
838         logchange('kohadb', 'change', 'biblioitems', 'issn', $obi->{'issn'}, $biblioitem->{'issn'});
839         my $q_issn=$dbh->quote($biblioitem->{'issn'});
840         my $sth=$dbh->prepare("update biblioitems set issn=$q_issn where biblioitemnumber=$biblioitemnumber");
841         logchange('marc', 'change', $Record_ID, '022', 'a', $obi->{'issn'}, $biblioitem->{'issn'});
842         changeSubfield($Record_ID, '022', 'a', $obi->{'issn'}, $biblioitem->{'issn'});
843     }
844     if ($biblioitem->{'dewey'} ne $obi->{'dewey'}) {
845         logchange('kohadb', 'change', 'biblioitems', 'dewey', $obi->{'dewey'}, $biblioitem->{'dewey'});
846         my $sth=$dbh->prepare("update biblioitems set dewey=$biblioitem->{'dewey'} where biblioitemnumber=$biblioitemnumber");
847         logchange('marc', 'change', $Record_ID, '082', 'a', $obi->{'dewey'}, $biblioitem->{'dewey'});
848         changeSubfield($Record_ID, '082', 'a', $obi->{'dewey'}, $biblioitem->{'dewey'});
849     }
850     if ($biblioitem->{'subclass'} ne $obi->{'subclass'}) {
851         logchange('kohadb', 'change', 'biblioitems', 'subclass', $obi->{'subclass'}, $biblioitem->{'subclass'});
852         my $q_subclass=$dbh->quote($biblioitem->{'subclass'});
853         my $sth=$dbh->prepare("update biblioitems set subclass=$q_subclass where biblioitemnumber=$biblioitemnumber");
854         logchange('marc', 'change', $Record_ID, '090', 'b', $obi->{'subclass'}, $biblioitem->{'subclass'});
855         changeSubfield($Record_ID, '090', 'b', $obi->{'subclass'}, $biblioitem->{'subclass'});
856     }
857     if ($biblioitem->{'place'} ne $obi->{'place'}) {
858         logchange('kohadb', 'change', 'biblioitems', 'place', $obi->{'place'}, $biblioitem->{'place'});
859         my $q_place=$dbh->quote($biblioitem->{'place'});
860         my $sth=$dbh->prepare("update biblioitems set place=$q_place where biblioitemnumber=$biblioitemnumber");
861         logchange('marc', 'change', $Record_ID, '260', 'a', $obi->{'place'}, $biblioitem->{'place'});
862         changeSubfield($Record_ID, '260', 'a', $obi->{'place'}, $biblioitem->{'place'});
863     }
864     if ($biblioitem->{'publishercode'} ne $obi->{'publishercode'}) {
865         logchange('kohadb', 'change', 'biblioitems', 'publishercode', $obi->{'publishercode'}, $biblioitem->{'publishercode'});
866         my $q_publishercode=$dbh->quote($biblioitem->{'publishercode'});
867         my $sth=$dbh->prepare("update biblioitems set publishercode=$q_publishercode where biblioitemnumber=$biblioitemnumber");
868         logchange('marc', 'change', $Record_ID, '260', 'b', $obi->{'publishercode'}, $biblioitem->{'publishercode'});
869         changeSubfield($Record_ID, '260', 'b', $obi->{'publishercode'}, $biblioitem->{'publishercode'});
870     }
871     if ($biblioitem->{'publicationyear'} ne $obi->{'publicationyear'}) {
872         logchange('kohadb', 'change', 'biblioitems', 'publicationyear', $obi->{'publicationyear'}, $biblioitem->{'publicationyear'});
873         my $q_publicationyear=$dbh->quote($biblioitem->{'publicationyear'});
874         my $sth=$dbh->prepare("update biblioitems set publicationyear=$q_publicationyear where biblioitemnumber=$biblioitemnumber");
875         logchange('marc', 'change', $Record_ID, '260', 'c', $obi->{'publicationyear'}, $biblioitem->{'publicationyear'});
876         changeSubfield($Record_ID, '260', 'c', $obi->{'publicationyear'}, $biblioitem->{'publicationyear'});
877     }
878     if ($biblioitem->{'illus'} ne $obi->{'illus'}) {
879         logchange('kohadb', 'change', 'biblioitems', 'illus', $obi->{'illus'}, $biblioitem->{'illus'});
880         my $q_illus=$dbh->quote($biblioitem->{'illus'});
881         my $sth=$dbh->prepare("update biblioitems set illus=$q_illus where biblioitemnumber=$biblioitemnumber");
882         logchange('marc', 'change', $Record_ID, '700', 'a', $obi->{'illus'}, $biblioitem->{'illus'});
883         changeSubfield($Record_ID, '700', 'a', $obi->{'illus'}, $biblioitem->{'illus'});
884     }
885     if ($biblioitem->{'pages'} ne $obi->{'pages'}) {
886         logchange('kohadb', 'change', 'biblioitems', 'pages', $obi->{'pages'}, $biblioitem->{'pages'});
887         my $q_pages=$dbh->quote($biblioitem->{'pages'});
888         my $sth=$dbh->prepare("update biblioitems set pages=$q_pages where biblioitemnumber=$biblioitemnumber");
889         logchange('marc', 'change', $Record_ID, '300', 'a', $obi->{'pages'}, $biblioitem->{'pages'});
890         changeSubfield($Record_ID, '300', 'a', $obi->{'pages'}, $biblioitem->{'pages'});
891     }
892     if ($biblioitem->{'size'} ne $obi->{'size'}) {
893         logchange('kohadb', 'change', 'biblioitems', 'size', $obi->{'size'}, $biblioitem->{'size'});
894         my $q_size=$dbh->quote($biblioitem->{'size'});
895         my $sth=$dbh->prepare("update biblioitems set size=$q_size where biblioitemnumber=$biblioitemnumber");
896         logchange('marc', 'change', $Record_ID, '300', 'c', $obi->{'size'}, $biblioitem->{'size'});
897         changeSubfield($Record_ID, '300', 'c', $obi->{'size'}, $biblioitem->{'size'});
898     }
899     if ($biblioitem->{'notes'} ne $obi->{'notes'}) {
900         logchange('kohadb', 'change', 'biblioitems', 'notes', $obi->{'notes'}, $biblioitem->{'notes'});
901         my $q_notes=$dbh->quote($biblioitem->{'notes'});
902         my $sth=$dbh->prepare("update biblioitems set notes=$q_notes where biblioitemnumber=$biblioitemnumber");
903         logchange('marc', 'change', $Record_ID, '500', 'a', $obi->{'notes'}, $biblioitem->{'notes'});
904         changeSubfield($Record_ID, '500', 'a', $obi->{'notes'}, $biblioitem->{'notes'});
905     }
906     if ($biblioitem->{'lccn'} ne $obi->{'lccn'}) {
907         logchange('kohadb', 'change', 'biblioitems', 'lccn', $obi->{'lccn'}, $biblioitem->{'lccn'});
908         my $q_lccn=$dbh->quote($biblioitem->{'lccn'});
909         my $sth=$dbh->prepare("update biblioitems set lccn=$q_lccn where biblioitemnumber=$biblioitemnumber");
910         logchange('marc', 'change', $Record_ID, '010', 'a', $obi->{'lccn'}, $biblioitem->{'lccn'});
911         changeSubfield($Record_ID, '010', 'a', $obi->{'lccn'}, $biblioitem->{'lccn'});
912     }
913     $sth->finish;
914     $dbh->disconnect;
915
916 }
917
918
919 sub newItem {
920     my ($env, $Record_ID, $item) = @_;
921     my $dbh=&C4Connect;  
922     my $barcode=$item->{'barcode'};
923     my $q_barcode=$dbh->quote($barcode);
924     my $biblionumber=$item->{'biblionumber'};
925     my $biblioitemnumber=$item->{'biblioitemnumber'};
926     my $dateaccessioned=$item->{'dateaccessioned'};
927     my $booksellerid=$item->{'booksellerid'};
928     my $q_booksellerid=$dbh->quote($booksellerid);
929     my $homebranch=$item->{'homebranch'};
930     my $q_homebranch=$dbh->quote($homebranch);
931     my $holdingbranch=$item->{'holdingbranch'};
932     my $price=$item->{'price'};
933     my $replacementprice=$item->{'replacementprice'};
934     my $replacementpricedate=$item->{'replacementpricedate'};
935     my $q_replacementpricedate=$dbh->quote($replacementpricedate);
936     my $notforloan=$item->{'notforloan'};
937     my $itemlost=$item->{'itemlost'};
938     my $wthdrawn=$item->{'wthdrawn'};
939     my $restricted=$item->{'restricted'};
940     my $itemnotes=$item->{'itemnotes'};
941     my $q_itemnotes=$dbh->quote($itemnotes);
942     my $itemtype=$item->{'itemtype'};
943     my $subclass=$item->{'subclass'};
944
945 # KOHADB Section
946
947     unless ($env->{'marconly'}) {
948         my $sth=$dbh->prepare("select max(itemnumber) from items");
949         $sth->execute;
950         my ($itemnumber) =$sth->fetchrow;
951         $itemnumber++;
952         $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)");
953         $sth->execute;
954     }
955
956
957 # MARC SECTION
958     my $subfields;
959     $subfields->{1}->{'Subfield_Mark'}='p';
960     $subfields->{1}->{'Subfield_Value'}=$barcode;
961     $subfields->{2}->{'Subfield_Mark'}='d';
962     $subfields->{2}->{'Subfield_Value'}=$dateaccessioned;
963     $subfields->{3}->{'Subfield_Mark'}='e';
964     $subfields->{3}->{'Subfield_Value'}=$booksellerid;
965     $subfields->{4}->{'Subfield_Mark'}='b';
966     $subfields->{4}->{'Subfield_Value'}=$homebranch;
967     $subfields->{5}->{'Subfield_Mark'}='l';
968     $subfields->{5}->{'Subfield_Value'}=$holdingbranch;
969     $subfields->{6}->{'Subfield_Mark'}='c';
970     $subfields->{6}->{'Subfield_Value'}=$price;
971     $subfields->{7}->{'Subfield_Mark'}='c';
972     $subfields->{7}->{'Subfield_Value'}=$replacementprice;
973     $subfields->{8}->{'Subfield_Mark'}='d';
974     $subfields->{8}->{'Subfield_Value'}=$replacementpricedate;
975     if ($notforloan) {
976         $subfields->{9}->{'Subfield_Mark'}='h';
977         $subfields->{9}->{'Subfield_Value'}='Not for loan';
978     }
979     if ($notforloan) {
980         $subfields->{10}->{'Subfield_Mark'}='j';
981         $subfields->{10}->{'Subfield_Value'}='Item lost';
982     }
983     if ($notforloan) {
984         $subfields->{11}->{'Subfield_Mark'}='j';
985         $subfields->{11}->{'Subfield_Value'}='Item withdrawn';
986     }
987     if ($notforloan) {
988         $subfields->{12}->{'Subfield_Mark'}='z';
989         $subfields->{12}->{'Subfield_Value'}=$itemnotes;
990     }
991     my $tag='876';
992     my $Tag_ID;
993     $env->{'linkage'}=1;
994     ($env, $Tag_ID) = addTag($env, $Record_ID, $tag, ' ', ' ', $subfields);
995     $env->{'linkage'}=0;
996     $env->{'linkid'}=$Tag_ID;
997     $tag='852';
998     my $subfields2;
999     $subfields2->{1}->{'Subfield_Mark'}='a';
1000     $subfields2->{1}->{'Subfield_Value'}='Coast Mountains School District';
1001     $subfields2->{1}->{'Subfield_Mark'}='b';
1002     $subfields2->{1}->{'Subfield_Value'}=$homebranch;
1003     $subfields2->{1}->{'Subfield_Mark'}='c';
1004     $subfields2->{1}->{'Subfield_Value'}=$itemtype;
1005     $subfields2->{2}->{'Subfield_Mark'}='m';
1006     $subfields2->{2}->{'Subfield_Value'}=$subclass;
1007     addTag($env, $Record_ID, $tag, ' ', ' ', $subfields2);
1008     $env->{'linkid'}='';
1009 }
1010
1011 sub updateItem {
1012 # Update the item with itemnumber $item->{'itemnumber'}
1013 # This routine should also modify the corresponding MARC record data. (852 and
1014 # 876 tags with 876p tag the same as $item->{'barcode'}
1015 #
1016 # This routine should also check to see which fields are actually being
1017 # modified, and log all changes.
1018
1019     my ($env, $item) = @_;
1020     my $dbh=&C4Connect;  
1021     my $itemnumber=$item->{'itemnumber'};
1022     my $biblionumber=$item->{'biblionumber'};
1023     my $biblioitemnumber=$item->{'biblioitemnumber'};
1024     my $barcode=$item->{'barcode'};
1025     my $dateaccessioned=$item->{'dateaccessioned'};
1026     my $booksellerid=$item->{'booksellerid'};
1027     my $homebranch=$item->{'homebranch'};
1028     my $price=$item->{'price'};
1029     my $replacementprice=$item->{'replacementprice'};
1030     my $replacementpricedate=$item->{'replacementpricedate'};
1031     my $multivolume=$item->{'multivolume'};
1032     my $stack=$item->{'stack'};
1033     my $notforloan=$item->{'notforloan'};
1034     my $itemlost=$item->{'itemlost'};
1035     my $wthdrawn=$item->{'wthdrawn'};
1036     my $bulk=$item->{'bulk'};
1037     my $restricted=$item->{'restricted'};
1038     my $binding=$item->{'binding'};
1039     my $itemnotes=$item->{'itemnotes'};
1040     my $holdingbranch=$item->{'holdingbranch'};
1041     my $interim=$item->{'interim'};
1042     my $sth=$dbh->prepare("select * from items where itemnumber=$itemnumber");
1043     $sth->execute;
1044     my $olditem=$sth->fetchrow_hashref;
1045     my $q_barcode=$dbh->quote($olditem->{'barcode'});
1046     $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");
1047     $sth->execute;
1048     my ($Subfield876_ID, $Record_ID) = $sth->fetchrow;
1049     $sth=$dbh->prepare("select Subfield_Value from 8XX_Subfield_Table where Subfield_Mark=8 and Subfield_ID=$Subfield876_ID");
1050     $sth->execute;
1051     my ($link) = $sth->fetchrow;
1052     $sth=$dbh->prepare("select Subfield_ID from 8XX_Subfield_Table where Subfield_Mark=8 and Subfield_Value=$link and !(Subfield_ID=$Subfield876_ID)");
1053     $sth->execute;
1054     my ($Subfield852_ID) = $sth->fetchrow;
1055     
1056     if ($item->{'barcode'} ne $olditem->{'barcode'}) {
1057         logchange('kohadb', 'change', 'items', 'barcode', $olditem->{'barcode'}, $item->{'barcode'});
1058         my $q_barcode=$dbh->quote($item->{'barcode'});
1059         my $sth=$dbh->prepare("update items set barcode=$q_barcode where itemnumber=$itemnumber");
1060         $sth->execute;
1061         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'p', $olditem->{'barcode'}, $item->{'barcode'}, $Subfield876_ID);
1062         logchange('marc', 'change', $Record_ID, '876', 'p', $Subfield_Key, $olditem->{'barcode'}, $item->{'barcode'});
1063     }
1064     if ($item->{'booksellerid'} ne $olditem->{'booksellerid'}) {
1065         logchange('kohadb', 'change', 'items', 'booksellerid', $olditem->{'booksellerid'}, $item->{'booksellerid'});
1066         my $q_booksellerid=$dbh->quote($item->{'booksellerid'});
1067         my $sth=$dbh->prepare("update items set booksellerid=$q_booksellerid where itemnumber=$itemnumber");
1068         $sth->execute;
1069         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'e', $olditem->{'booksellerid'}, $item->{'booksellerid'}, $Subfield876_ID);
1070         logchange('marc', 'change', $Record_ID, '876', 'e', $Subfield_Key, $olditem->{'booksellerid'}, $item->{'booksellerid'});
1071     }
1072     if ($item->{'dateaccessioned'} ne $olditem->{'dateaccessioned'}) {
1073         logchange('kohadb', 'change', 'items', 'dateaccessioned', $olditem->{'dateaccessioned'}, $item->{'dateaccessioned'});
1074         my $q_dateaccessioned=$dbh->quote($item->{'dateaccessioned'});
1075         my $sth=$dbh->prepare("update items set dateaccessioned=$q_dateaccessioned where itemnumber=$itemnumber");
1076         $sth->execute;
1077         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'd', $olditem->{'dateaccessioned'}, $item->{'dateaccessioned'}, $Subfield876_ID);
1078         logchange('marc', 'change', $Record_ID, '876', 'd', $Subfield_Key, $olditem->{'dateaccessioned'}, $item->{'dateaccessioned'});
1079     }
1080     if ($item->{'homebranch'} ne $olditem->{'homebranch'}) {
1081         logchange('kohadb', 'change', 'items', 'homebranch', $olditem->{'homebranch'}, $item->{'homebranch'});
1082         my $q_homebranch=$dbh->quote($item->{'homebranch'});
1083         my $sth=$dbh->prepare("update items set homebranch=$q_homebranch where itemnumber=$itemnumber");
1084         $sth->execute;
1085         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'b', $olditem->{'homebranch'}, $item->{'homebranch'}, $Subfield876_ID);
1086         logchange('marc', 'change', $Record_ID, '876', 'b', $Subfield_Key, $olditem->{'homebranch'}, $item->{'homebranch'});
1087     }
1088     if ($item->{'holdingbranch'} ne $olditem->{'holdingbranch'}) {
1089         logchange('kohadb', 'change', 'items', 'holdingbranch', $olditem->{'holdingbranch'}, $item->{'holdingbranch'});
1090         my $q_holdingbranch=$dbh->quote($item->{'holdingbranch'});
1091         my $sth=$dbh->prepare("update items set holdingbranch=$q_holdingbranch where itemnumber=$itemnumber");
1092         $sth->execute;
1093         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'l', $olditem->{'holdingbranch'}, $item->{'holdingbranch'}, $Subfield876_ID);
1094         logchange('marc', 'change', $Record_ID, '876', 'l', $Subfield_Key, $olditem->{'holdingbranch'}, $item->{'holdingbranch'});
1095     }
1096     if ($item->{'price'} ne $olditem->{'price'}) {
1097         logchange('kohadb', 'change', 'items', 'price', $olditem->{'price'}, $item->{'price'});
1098         my $q_price=$dbh->quote($item->{'price'});
1099         my $sth=$dbh->prepare("update items set price=$q_price where itemnumber=$itemnumber");
1100         $sth->execute;
1101         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'c', $olditem->{'price'}, $item->{'price'}, $Subfield876_ID);
1102         logchange('marc', 'change', $Record_ID, '876', 'c', $Subfield_Key, $olditem->{'price'}, $item->{'price'});
1103     }
1104     if ($item->{'itemnotes'} ne $olditem->{'itemnotes'}) {
1105         logchange('kohadb', 'change', 'items', 'itemnotes', $olditem->{'itemnotes'}, $item->{'itemnotes'});
1106         my $q_itemnotes=$dbh->quote($item->{'itemnotes'});
1107         my $sth=$dbh->prepare("update items set itemnotes=$q_itemnotes where itemnumber=$itemnumber");
1108         $sth->execute;
1109         my ($Subfield_ID, $Subfield_Key) = changeSubfield($Record_ID, '876', 'c', $olditem->{'itemnotes'}, $item->{'itemnotes'}, $Subfield876_ID);
1110         logchange('marc', 'change', $Record_ID, '876', 'c', $Subfield_Key, $olditem->{'itemnotes'}, $item->{'itemnotes'});
1111     }
1112     if ($item->{'notforloan'} ne $olditem->{'notforloan'}) {
1113         logchange('kohadb', 'change', 'items', 'notforloan', $olditem->{'notforloan'}, $item->{'notforloan'});
1114         my $sth=$dbh->prepare("update items set notforloan=$notforloan where itemnumber=$itemnumber");
1115         $sth->execute;
1116         if ($item->{'notforloan'}) {
1117             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Not for loan', $Subfield876_ID);
1118             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Not for loan');
1119         } else {
1120             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Not for loan', $Subfield876_ID);
1121             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Not for loan');
1122         }
1123     }
1124     if ($item->{'itemlost'} ne $olditem->{'itemlost'}) {
1125         logchange('kohadb', 'change', 'items', 'itemlost', $olditem->{'itemlost'}, $item->{'itemlost'});
1126         my $sth=$dbh->prepare("update items set itemlost=$itemlost where itemnumber=$itemnumber");
1127         $sth->execute;
1128         if ($item->{'itemlost'}) {
1129             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Item lost', $Subfield876_ID);
1130             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Item lost');
1131         } else {
1132             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Item lost', $Subfield876_ID);
1133             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Item lost');
1134         }
1135     }
1136     if ($item->{'wthdrawn'} ne $olditem->{'wthdrawn'}) {
1137         logchange('kohadb', 'change', 'items', 'wthdrawn', $olditem->{'wthdrawn'}, $item->{'wthdrawn'});
1138         my $sth=$dbh->prepare("update items set wthdrawn=$wthdrawn where itemnumber=$itemnumber");
1139         $sth->execute;
1140         if ($item->{'wthdrawn'}) {
1141             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Withdrawn', $Subfield876_ID);
1142             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Withdrawn');
1143         } else {
1144             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Withdrawn', $Subfield876_ID);
1145             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Withdrawn');
1146         }
1147     }
1148     if ($item->{'restricted'} ne $olditem->{'restricted'}) {
1149         logchange('kohadb', 'change', 'items', 'restricted', $olditem->{'restricted'}, $item->{'restricted'});
1150         my $sth=$dbh->prepare("update items set restricted=$restricted where itemnumber=$itemnumber");
1151         $sth->execute;
1152         if ($item->{'restricted'}) {
1153             my ($Subfield_ID, $Subfield_Key) = addSubfield($Record_ID, '876', 'h', 'Restricted', $Subfield876_ID);
1154             logchange('marc', 'add', $Record_ID, '876', 'h', $Subfield_Key, 'Restricted');
1155         } else {
1156             my ($Subfield_ID, $Subfield_Key) = deleteSubfield($Record_ID, '876', 'h', 'Restricted', $Subfield876_ID);
1157             logchange('marc', 'delete', $Record_ID, '876', 'h', $Subfield_Key, 'Restricted');
1158         }
1159     }
1160     $sth->finish;
1161     $dbh->disconnect;
1162 }
1163
1164
1165 sub getorders {
1166   my ($supplierid)=@_;
1167   my $dbh=C4Connect;
1168   my $query = "Select count(*),authorisedby,entrydate,basketno from aqorders where 
1169   booksellerid='$supplierid' and (quantity > quantityreceived or
1170   quantityreceived is NULL)
1171   and (datecancellationprinted is NULL or datecancellationprinted = '0000-00-00')";
1172   $query.=" group by basketno order by entrydate desc";
1173   #print $query;
1174   my $sth=$dbh->prepare($query);
1175   $sth->execute;
1176   my @results;
1177   my $i=0;
1178   while (my $data=$sth->fetchrow_hashref){
1179     $results[$i]=$data;
1180     $i++;
1181   }
1182   $sth->finish;
1183   $dbh->disconnect;
1184   return ($i,\@results);
1185 }
1186
1187 sub itemcount{
1188   my ($biblio)=@_;
1189   my $dbh=C4Connect;
1190   my $query="Select count(*) from items where biblionumber=$biblio";
1191 #  print $query;
1192   my $sth=$dbh->prepare($query);
1193   $sth->execute;
1194   my $data=$sth->fetchrow_hashref;
1195   $sth->finish;
1196   $dbh->disconnect;
1197   return($data->{'count(*)'});
1198 }
1199
1200 sub getorder{
1201   my ($bi,$bib)=@_;
1202   my $dbh=C4Connect;
1203   my $query="Select ordernumber from aqorders where biblionumber=$bib and
1204   biblioitemnumber='$bi'";
1205   my $sth=$dbh->prepare($query);
1206   $sth->execute;
1207   my $ordnum=$sth->fetchrow_hashref;
1208   $sth->finish;
1209   my $order=getsingleorder($ordnum->{'ordernumber'});
1210   $dbh->disconnect;
1211 #  print $query;
1212   return ($order,$ordnum->{'ordernumber'});
1213 }
1214
1215 sub getsingleorder {
1216   my ($ordnum)=@_;
1217   my $dbh=C4Connect;
1218   my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown 
1219   where aqorders.ordernumber='$ordnum' 
1220   and biblio.biblionumber=aqorders.biblionumber and
1221   biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
1222   aqorders.ordernumber=aqorderbreakdown.ordernumber";
1223   my $sth=$dbh->prepare($query);
1224   $sth->execute;
1225   my $data=$sth->fetchrow_hashref;
1226   $sth->finish;
1227   $dbh->disconnect;
1228   return($data);
1229 }
1230
1231 sub invoice {
1232   my ($invoice)=@_;
1233   my $dbh=C4Connect;
1234   my $query="Select * from aqorders,biblio,biblioitems where
1235   booksellerinvoicenumber='$invoice'
1236   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
1237   aqorders.biblioitemnumber group by aqorders.ordernumber,aqorders.biblioitemnumber";
1238   my $i=0;
1239   my @results;
1240   my $sth=$dbh->prepare($query);
1241   $sth->execute;
1242   while (my $data=$sth->fetchrow_hashref){
1243     $results[$i]=$data;
1244     $i++;
1245   }
1246   $sth->finish;
1247   $dbh->disconnect;
1248   return($i,@results);
1249 }
1250
1251 sub getallorders {
1252   #gets all orders from a certain supplier, orders them alphabetically
1253   my ($supid)=@_;
1254   my $dbh=C4Connect;
1255   my $query="Select * from aqorders,biblio,biblioitems where booksellerid='$supid'
1256   and (cancelledby is NULL or cancelledby = '')
1257   and (quantityreceived < quantity or quantityreceived is NULL)
1258   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
1259   aqorders.biblioitemnumber
1260   group by aqorders.biblioitemnumber
1261   order by
1262   biblio.title";
1263   my $i=0;
1264   my @results;
1265   my $sth=$dbh->prepare($query);
1266   $sth->execute;
1267   while (my $data=$sth->fetchrow_hashref){
1268     $results[$i]=$data;
1269     $i++;
1270   }
1271   $sth->finish;
1272   $dbh->disconnect;
1273   return($i,@results);
1274 }
1275
1276 sub getrecorders {
1277   #gets all orders from a certain supplier, orders them alphabetically
1278   my ($supid)=@_;
1279   my $dbh=C4Connect;
1280   my $query="Select * from aqorders,biblio,biblioitems where booksellerid='$supid'
1281   and (cancelledby is NULL or cancelledby = '')
1282   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=                    
1283   aqorders.biblioitemnumber and
1284   aqorders.quantityreceived>0
1285   and aqorders.datereceived >=now()
1286   group by aqorders.biblioitemnumber 
1287   order by
1288   biblio.title";
1289   my $i=0;
1290   my @results;
1291   my $sth=$dbh->prepare($query);
1292   $sth->execute;
1293   while (my $data=$sth->fetchrow_hashref){
1294     $results[$i]=$data;
1295     $i++;
1296   }
1297   $sth->finish;
1298   $dbh->disconnect;
1299   return($i,@results);
1300 }
1301
1302 sub ordersearch {
1303   my ($search,$biblio,$catview) = @_;
1304   my $dbh   = C4Connect;
1305   my $query = "Select *,biblio.title from aqorders,biblioitems,biblio
1306 where aqorders.biblioitemnumber = biblioitems.biblioitemnumber
1307 and biblio.biblionumber=aqorders.biblionumber
1308 and ((datecancellationprinted is NULL)
1309 or (datecancellationprinted = '0000-00-00'))
1310 and ((";
1311   my @data  = split(' ',$search);
1312   my $count = @data;
1313   for (my $i = 0; $i < $count; $i++) {
1314     $query .= "(biblio.title like '$data[$i]%' or biblio.title like '% $data[$i]%') and ";
1315   }
1316   $query=~ s/ and $//;
1317   $query.=" ) or biblioitems.isbn='$search'
1318   or (aqorders.ordernumber='$search' and aqorders.biblionumber='$biblio')) ";
1319   if ($catview ne 'yes'){
1320     $query.=" and (quantityreceived < quantity or quantityreceived is NULL)";
1321   }
1322   $query.=" group by aqorders.ordernumber";
1323   my $sth=$dbh->prepare($query);
1324   $sth->execute;
1325   my $i=0;
1326   my @results;
1327   while (my $data=$sth->fetchrow_hashref){
1328      my $sth2=$dbh->prepare("Select * from biblio where
1329      biblionumber='$data->{'biblionumber'}'");
1330      $sth2->execute;
1331      my $data2=$sth2->fetchrow_hashref;
1332      $sth2->finish;
1333      $data->{'author'}=$data2->{'author'};
1334      $data->{'seriestitle'}=$data2->{'seriestitle'};
1335      $sth2=$dbh->prepare("Select * from aqorderbreakdown where
1336     ordernumber=$data->{'ordernumber'}");
1337     $sth2->execute;
1338     $data2=$sth2->fetchrow_hashref;
1339     $sth2->finish;
1340     $data->{'branchcode'}=$data2->{'branchcode'};
1341     $data->{'bookfundid'}=$data2->{'bookfundid'};
1342     $results[$i]=$data;
1343     $i++;
1344   }
1345   $sth->finish;
1346   $dbh->disconnect;
1347   return($i,@results);
1348 }
1349
1350
1351 sub bookseller {
1352   my ($searchstring)=@_;
1353   my $dbh=C4Connect;
1354   my $query="Select * from aqbooksellers where name like '%$searchstring%' or
1355   id = '$searchstring'";
1356   my $sth=$dbh->prepare($query);
1357   $sth->execute;
1358   my @results;
1359   my $i=0;
1360   while (my $data=$sth->fetchrow_hashref){
1361     $results[$i]=$data;
1362     $i++;
1363   }
1364   $sth->finish;
1365   $dbh->disconnect;
1366   return($i,@results);
1367 }
1368
1369 sub breakdown {
1370   my ($id)=@_;
1371   my $dbh=C4Connect;
1372   my $query="Select * from aqorderbreakdown where ordernumber='$id'";
1373   my $sth=$dbh->prepare($query);
1374   $sth->execute;
1375   my @results;
1376   my $i=0;
1377   while (my $data=$sth->fetchrow_hashref){
1378     $results[$i]=$data;
1379     $i++;
1380   }
1381   $sth->finish;
1382   $dbh->disconnect;
1383   return($i,\@results);
1384 }
1385
1386 sub basket {
1387   my ($basketno,$supplier)=@_;
1388   my $dbh=C4Connect;
1389   my $query="Select *,biblio.title from aqorders,biblio,biblioitems 
1390   where basketno='$basketno'
1391   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber
1392   =aqorders.biblioitemnumber 
1393   and (datecancellationprinted is NULL or datecancellationprinted =
1394   '0000-00-00')";
1395   if ($supplier ne ''){
1396     $query.=" and aqorders.booksellerid='$supplier'";
1397   }
1398   $query.=" group by aqorders.ordernumber";
1399   my $sth=$dbh->prepare($query);
1400   $sth->execute;
1401   my @results;
1402 #  print $query;
1403   my $i=0;
1404   while (my $data=$sth->fetchrow_hashref){
1405     $results[$i]=$data;
1406     $i++;
1407   }
1408   $sth->finish;
1409   $dbh->disconnect;
1410   return($i,@results);
1411 }
1412
1413 sub newbasket {
1414   my $dbh=C4Connect;
1415   my $query="Select max(basketno) from aqorders";
1416   my $sth=$dbh->prepare($query);
1417   $sth->execute;
1418   my $data=$sth->fetchrow_arrayref;
1419   my $basket=$$data[0];
1420   $basket++;
1421   $sth->finish;
1422   $dbh->disconnect;
1423   return($basket);
1424 }
1425
1426 sub bookfunds {
1427   my $dbh=C4Connect;
1428   my $query="Select * from aqbookfund,aqbudget where aqbookfund.bookfundid
1429   =aqbudget.bookfundid
1430   group by aqbookfund.bookfundid order by bookfundname";
1431   my $sth=$dbh->prepare($query);
1432   $sth->execute;
1433   my @results;
1434   my $i=0;
1435   while (my $data=$sth->fetchrow_hashref){
1436     $results[$i]=$data;
1437     $i++;
1438   }
1439   $sth->finish;
1440   $dbh->disconnect;
1441   return($i,@results);
1442 }
1443
1444 sub branches {
1445     my $dbh   = C4Connect;
1446     my $query = "Select * from branches";
1447     my $sth   = $dbh->prepare($query);
1448     my $i     = 0;
1449     my @results;
1450
1451     $sth->execute;
1452     while (my $data = $sth->fetchrow_hashref) {
1453         $results[$i] = $data;
1454         $i++;
1455     } # while
1456
1457     $sth->finish;
1458     $dbh->disconnect;
1459     return($i, @results);
1460 } # sub branches
1461
1462 sub bookfundbreakdown {
1463   my ($id)=@_;
1464   my $dbh=C4Connect;
1465   my $query="Select quantity,datereceived,freight,unitprice,listprice,ecost,quantityreceived,subscription
1466   from aqorders,aqorderbreakdown where bookfundid='$id' and 
1467   aqorders.ordernumber=aqorderbreakdown.ordernumber
1468   and (datecancellationprinted is NULL or
1469   datecancellationprinted='0000-00-00')";
1470   my $sth=$dbh->prepare($query);
1471   $sth->execute;
1472   my $comtd=0;
1473   my $spent=0;
1474   while (my $data=$sth->fetchrow_hashref){
1475     if ($data->{'subscription'} == 1){
1476       $spent+=$data->{'quantity'}*$data->{'unitprice'};
1477     } else {
1478       my $leftover=$data->{'quantity'}-$data->{'quantityreceived'};
1479       $comtd+=($data->{'ecost'})*$leftover;
1480       $spent+=($data->{'unitprice'})*$data->{'quantityreceived'};
1481     }
1482   }
1483   $sth->finish;
1484   $dbh->disconnect;
1485   return($spent,$comtd);
1486 }
1487       
1488
1489 sub newbiblio {
1490   my ($biblio) = @_;
1491   my $dbh    = &C4Connect;
1492   my $query  = "Select max(biblionumber) from biblio";
1493   my $sth    = $dbh->prepare($query);
1494   $sth->execute;
1495   my $data   = $sth->fetchrow_arrayref;
1496   my $bibnum = $$data[0] + 1;
1497   my $series = 0;
1498
1499   $biblio->{'title'}       = $dbh->quote($biblio->{'title'});
1500   $biblio->{'author'}      = $dbh->quote($biblio->{'author'});
1501   $biblio->{'copyright'}   = $dbh->quote($biblio->{'copyright'});
1502   $biblio->{'seriestitle'} = $dbh->quote($biblio->{'seriestitle'});
1503   $biblio->{'notes'}       = $dbh->quote($biblio->{'notes'});
1504   $biblio->{'abstract'}    = $dbh->quote($biblio->{'abstract'});
1505   if ($biblio->{'seriestitle'}) { $series = 1 };
1506
1507   $sth->finish;
1508   $query = "insert into biblio set
1509 biblionumber  = $bibnum,
1510 title         = $biblio->{'title'},
1511 author        = $biblio->{'author'},
1512 copyrightdate = $biblio->{'copyright'},
1513 serial        = $series,
1514 seriestitle   = $biblio->{'seriestitle'},
1515 notes         = $biblio->{'notes'},
1516 abstract      = $biblio->{'abstract'}";
1517
1518   $sth = $dbh->prepare($query);
1519   $sth->execute;
1520
1521   $sth->finish;
1522   $dbh->disconnect;
1523   return($bibnum);
1524 }
1525
1526
1527 sub modbiblio {
1528   my ($biblio) = @_;
1529   my $dbh   = C4Connect;
1530   my $query;
1531   my $sth;
1532   
1533   $biblio->{'title'}         = $dbh->quote($biblio->{'title'});
1534   $biblio->{'author'}        = $dbh->quote($biblio->{'author'});
1535   $biblio->{'abstract'}      = $dbh->quote($biblio->{'abstract'});
1536   $biblio->{'copyrightdate'} = $dbh->quote($biblio->{'copyrightdate'});
1537   $biblio->{'seriestitle'}   = $dbh->quote($biblio->{'serirestitle'});
1538   $biblio->{'serial'}        = $dbh->quote($biblio->{'serial'});
1539   $biblio->{'unititle'}      = $dbh->quote($biblio->{'unititle'});
1540   $biblio->{'notes'}         = $dbh->quote($biblio->{'notes'});
1541
1542   $query = "Update biblio set
1543 title         = $biblio->{'title'},
1544 author        = $biblio->{'author'},
1545 abstract      = $biblio->{'abstract'},
1546 copyrightdate = $biblio->{'copyrightdate'},
1547 seriestitle   = $biblio->{'seriestitle'},
1548 serial        = $biblio->{'serial'},
1549 unititle      = $biblio->{'unititle'},
1550 notes         = $biblio->{'notes'}
1551 where biblionumber = $biblio->{'biblionumber'}";
1552   $sth   = $dbh->prepare($query);
1553
1554   $sth->execute;
1555
1556   $sth->finish;
1557   $dbh->disconnect;
1558   return($biblio->{'biblionumber'});
1559 } # sub modbiblio
1560
1561
1562 sub modsubtitle {
1563   my ($bibnum, $subtitle) = @_;
1564   my $dbh   = C4Connect;
1565   my $query = "update bibliosubtitle set
1566 subtitle = '$subtitle'
1567 where biblionumber = $bibnum";
1568   my $sth   = $dbh->prepare($query);
1569
1570   $sth->execute;
1571   $sth->finish;
1572   $dbh->disconnect;
1573 } # sub modsubtitle
1574
1575
1576 sub modaddauthor {
1577     my ($bibnum, $author) = @_;
1578     my $dbh   = C4Connect;
1579     my $query = "Delete from additionalauthors where biblionumber = $bibnum";
1580     my $sth = $dbh->prepare($query);
1581
1582     $sth->execute;
1583     $sth->finish;
1584
1585     if ($author ne '') {
1586         $query = "Insert into additionalauthors set
1587 author       = '$author',
1588 biblionumber = '$bibnum'";
1589         $sth   = $dbh->prepare($query);
1590
1591         $sth->execute;
1592
1593         $sth->finish;
1594     } # if
1595
1596   $dbh->disconnect;
1597 } # sub modaddauthor
1598
1599
1600 sub modsubject {
1601   my ($bibnum, $force, @subject) = @_;
1602   my $dbh   = C4Connect;
1603   my $count = @subject;
1604   my $error;
1605   for (my $i = 0; $i < $count; $i++) {
1606     $subject[$i] =~ s/^ //g;
1607     $subject[$i] =~ s/ $//g;
1608     my $query = "select * from catalogueentry
1609 where entrytype = 's'
1610 and catalogueentry = '$subject[$i]'";
1611     my $sth   = $dbh->prepare($query);
1612     $sth->execute;
1613
1614     if (my $data = $sth->fetchrow_hashref) {
1615     } else {
1616       if ($force eq $subject[$i]) {
1617
1618          # subject not in aut, chosen to force anway
1619          # so insert into cataloguentry so its in auth file
1620          $query = "Insert into catalogueentry
1621 (entrytype,catalogueentry)
1622 values ('s','$subject[$i]')";
1623          my $sth2 = $dbh->prepare($query);
1624
1625          $sth2->execute;
1626          $sth2->finish;
1627
1628       } else {
1629
1630         $error = "$subject[$i]\n does not exist in the subject authority file";
1631         $query = "Select * from catalogueentry
1632 where entrytype = 's'
1633 and (catalogueentry like '$subject[$i] %'
1634 or catalogueentry like '% $subject[$i] %'
1635 or catalogueentry like '% $subject[$i]')";
1636         my $sth2 = $dbh->prepare($query);
1637
1638         $sth2->execute;
1639         while (my $data = $sth2->fetchrow_hashref) {
1640           $error = $error."<br>$data->{'catalogueentry'}";
1641         } # while
1642         $sth2->finish;
1643       } # else
1644     } # else
1645     $sth->finish;
1646   } # else
1647
1648   if ($error eq '') {
1649     my $query = "Delete from bibliosubject where biblionumber = $bibnum";
1650     my $sth   = $dbh->prepare($query);
1651
1652     $sth->execute;
1653     $sth->finish;
1654
1655     for (my $i = 0; $i < $count; $i++) {
1656       $sth = $dbh->prepare("Insert into bibliosubject
1657 values ('$subject[$i]', $bibnum)");
1658
1659       $sth->execute;
1660       $sth->finish;
1661     } # for
1662   } # if
1663
1664   $dbh->disconnect;
1665   return($error);
1666 } # sub modsubject
1667
1668
1669 sub modbibitem {
1670     my ($biblioitem) = @_;
1671     my $dbh   = C4Connect;
1672     my $query;
1673
1674     $biblioitem->{'itemtype'}        = $dbh->quote($biblioitem->{'itemtype'});
1675     $biblioitem->{'url'}             = $dbh->quote($biblioitem->{'url'});
1676     $biblioitem->{'isbn'}            = $dbh->quote($biblioitem->{'isbn'});
1677     $biblioitem->{'publishercode'}   = $dbh->quote($biblioitem->{'publishercode'});
1678     $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1679     $biblioitem->{'classification'}  = $dbh->quote($biblioitem->{'classification'});
1680     $biblioitem->{'dewey'}           = $dbh->quote($biblioitem->{'dewey'});
1681     $biblioitem->{'subclass'}        = $dbh->quote($biblioitem->{'subclass'});
1682     $biblioitem->{'illus'}           = $dbh->quote($biblioitem->{'illus'});
1683     $biblioitem->{'pages'}           = $dbh->quote($biblioitem->{'pages'});
1684     $biblioitem->{'volumeddesc'}     = $dbh->quote($biblioitem->{'volumeddesc'});
1685     $biblioitem->{'notes'}           = $dbh->quote($biblioitem->{'notes'});
1686     $biblioitem->{'size'}            = $dbh->quote($biblioitem->{'size'});
1687     $biblioitem->{'place'}           = $dbh->quote($biblioitem->{'place'});
1688
1689     $query = "Update biblioitems set
1690 itemtype        = $biblioitem->{'itemtype'},
1691 url             = $biblioitem->{'url'},
1692 isbn            = $biblioitem->{'isbn'},
1693 publishercode   = $biblioitem->{'publishercode'},
1694 publicationyear = $biblioitem->{'publicationyear'},
1695 classification  = $biblioitem->{'classification'},
1696 dewey           = $biblioitem->{'dewey'},
1697 subclass        = $biblioitem->{'subclass'},
1698 illus           = $biblioitem->{'illus'},
1699 pages           = $biblioitem->{'pages'},
1700 volumeddesc     = $biblioitem->{'volumeddesc'},
1701 notes           = $biblioitem->{'notes'},
1702 size            = $biblioitem->{'size'},
1703 place           = $biblioitem->{'place'}
1704 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
1705
1706     $dbh->do($query);
1707
1708     $dbh->disconnect;
1709 } # sub modbibitem
1710
1711
1712 sub modnote {
1713   my ($bibitemnum,$note)=@_;
1714   my $dbh=C4Connect;
1715   my $query="update biblioitems set notes='$note' where
1716   biblioitemnumber='$bibitemnum'";
1717   my $sth=$dbh->prepare($query);
1718   $sth->execute;
1719   $sth->finish;
1720   $dbh->disconnect;
1721 }
1722
1723 sub newbiblioitem {
1724   my ($biblioitem) = @_;
1725   my $dbh   = C4Connect;
1726   my $query = "Select max(biblioitemnumber) from biblioitems";
1727   my $sth   = $dbh->prepare($query);
1728   my $data;
1729   my $bibitemnum;
1730
1731   $biblioitem->{'volume'}          = $dbh->quote($biblioitem->{'volume'});
1732   $biblioitem->{'number'}          = $dbh->quote($biblioitem->{'number'});
1733   $biblioitem->{'classification'}  = $dbh->quote($biblioitem->{'classification'});
1734   $biblioitem->{'itemtype'}        = $dbh->quote($biblioitem->{'itemtype'});
1735   $biblioitem->{'url'}             = $dbh->quote($biblioitem->{'url'});
1736   $biblioitem->{'isbn'}            = $dbh->quote($biblioitem->{'isbn'});
1737   $biblioitem->{'issn'}            = $dbh->quote($biblioitem->{'issn'});
1738   $biblioitem->{'dewey'}           = $dbh->quote($biblioitem->{'dewey'});
1739   $biblioitem->{'subclass'}        = $dbh->quote($biblioitem->{'subclass'});
1740   $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1741   $biblioitem->{'publishercode'}   = $dbh->quote($biblioitem->{'publishercode'});
1742   $biblioitem->{'volumedate'}      = $dbh->quote($biblioitem->{'volumedate'});
1743   $biblioitem->{'volumeddesc'}     = $dbh->quote($biblioitem->{'volumeddesc'});  $biblioitem->{'illus'}            = $dbh->quote($biblioitem->{'illus'});
1744   $biblioitem->{'illus'}           = $dbh->quote($biblioitem->{'illus'});
1745   $biblioitem->{'pages'}           = $dbh->quote($biblioitem->{'pages'});
1746   $biblioitem->{'notes'}           = $dbh->quote($biblioitem->{'notes'});
1747   $biblioitem->{'size'}            = $dbh->quote($biblioitem->{'size'});
1748   $biblioitem->{'place'}           = $dbh->quote($biblioitem->{'place'});
1749   $biblioitem->{'lccn'}            = $dbh->quote($biblioitem->{'lccn'});
1750   $biblioitem->{'marc'}            = $dbh->quote($biblioitem->{'marc'});
1751   
1752   $sth->execute;
1753   $data       = $sth->fetchrow_arrayref;
1754   $bibitemnum = $$data[0] + 1;
1755
1756   $sth->finish;
1757
1758   $query = "insert into biblioitems set
1759 biblioitemnumber = $bibitemnum,
1760 biblionumber     = $biblioitem->{'biblionumber'},
1761 volume           = $biblioitem->{'volume'},
1762 number           = $biblioitem->{'number'},
1763 classification   = $biblioitem->{'classification'},
1764 itemtype         = $biblioitem->{'itemtype'},
1765 url              = $biblioitem->{'url'},
1766 isbn             = $biblioitem->{'isbn'},
1767 issn             = $biblioitem->{'issn'},
1768 dewey            = $biblioitem->{'dewey'},
1769 subclass         = $biblioitem->{'subclass'},
1770 publicationyear  = $biblioitem->{'publicationyear'},
1771 publishercode    = $biblioitem->{'publishercode'},
1772 volumedate       = $biblioitem->{'volumedate'},
1773 volumeddesc      = $biblioitem->{'volumeddesc'},
1774 illus            = $biblioitem->{'illus'},
1775 pages            = $biblioitem->{'pages'},
1776 notes            = $biblioitem->{'notes'},
1777 size             = $biblioitem->{'size'},
1778 lccn             = $biblioitem->{'lccn'},
1779 marc             = $biblioitem->{'marc'},
1780 place            = $biblioitem->{'place'}";
1781
1782   $sth = $dbh->prepare($query);
1783   $sth->execute;
1784
1785   $sth->finish;
1786   $dbh->disconnect;
1787   return($bibitemnum);
1788 }
1789
1790 sub newsubject {
1791   my ($bibnum)=@_;
1792   my $dbh=C4Connect;
1793   my $query="insert into bibliosubject (biblionumber) values
1794   ($bibnum)";
1795   my $sth=$dbh->prepare($query);
1796 #  print $query;
1797   $sth->execute;
1798   $sth->finish;
1799   $dbh->disconnect;
1800 }
1801
1802 sub newsubtitle {
1803   my ($bibnum, $subtitle) = @_;
1804   my $dbh   = C4Connect;
1805   $subtitle = $dbh->quote($subtitle);
1806   my $query = "insert into bibliosubtitle set
1807 biblionumber = $bibnum,
1808 subtitle = $subtitle";
1809   my $sth   = $dbh->prepare($query);
1810
1811   $sth->execute;
1812
1813   $sth->finish;
1814   $dbh->disconnect;
1815 }
1816
1817 sub neworder {
1818   my ($bibnum,$title,$ordnum,$basket,$quantity,$listprice,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$sub,$invoice)=@_;
1819   if ($budget eq 'now'){
1820     $budget="now()";
1821   } else {
1822     $budget="'2001-07-01'";
1823   }
1824   if ($sub eq 'yes'){
1825     $sub=1;
1826   } else {
1827     $sub=0;
1828   }
1829   my $dbh=C4Connect;
1830   my $query="insert into aqorders (biblionumber,title,basketno,
1831   quantity,listprice,booksellerid,entrydate,requisitionedby,authorisedby,notes,
1832   biblioitemnumber,rrp,ecost,gst,unitprice,subscription,booksellerinvoicenumber)
1833
1834   values
1835   ($bibnum,'$title',$basket,$quantity,$listprice,'$supplier',now(),
1836   '$who','$who','$notes',$bibitemnum,'$rrp','$ecost','$gst','$cost',
1837   '$sub','$invoice')";
1838   my $sth=$dbh->prepare($query);
1839 #  print $query;
1840   $sth->execute;
1841   $sth->finish;
1842   $query="select * from aqorders where
1843   biblionumber=$bibnum and basketno=$basket and ordernumber >=$ordnum";
1844   $sth=$dbh->prepare($query);
1845   $sth->execute;
1846   my $data=$sth->fetchrow_hashref;
1847   $sth->finish;
1848   $ordnum=$data->{'ordernumber'};
1849   $query="insert into aqorderbreakdown (ordernumber,bookfundid) values
1850   ($ordnum,'$bookfund')";
1851   $sth=$dbh->prepare($query);
1852 #  print $query;
1853   $sth->execute;
1854   $sth->finish;
1855   $dbh->disconnect;
1856 }
1857
1858 sub delorder {
1859   my ($bibnum,$ordnum)=@_;
1860   my $dbh=C4Connect;
1861   my $query="update aqorders set datecancellationprinted=now()
1862   where biblionumber='$bibnum' and
1863   ordernumber='$ordnum'";
1864   my $sth=$dbh->prepare($query);
1865   #print $query;
1866   $sth->execute;
1867   $sth->finish;
1868   my $count=itemcount($bibnum);
1869   if ($count == 0){
1870     delbiblio($bibnum);
1871   }
1872   $dbh->disconnect;
1873 }
1874
1875 sub modorder {
1876   my ($title,$ordnum,$quantity,$listprice,$bibnum,$basketno,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$invoice)=@_;
1877   my $dbh=C4Connect;
1878   my $query="update aqorders set title='$title',
1879   quantity='$quantity',listprice='$listprice',basketno='$basketno', 
1880   rrp='$rrp',ecost='$ecost',unitprice='$cost',
1881   booksellerinvoicenumber='$invoice'
1882   where
1883   ordernumber=$ordnum and biblionumber=$bibnum";
1884   my $sth=$dbh->prepare($query);
1885 #  print $query;
1886   $sth->execute;
1887   $sth->finish;
1888   $query="update aqorderbreakdown set bookfundid=$bookfund where
1889   ordernumber=$ordnum";
1890   $sth=$dbh->prepare($query);
1891 #  print $query;
1892   $sth->execute;
1893   $sth->finish;
1894   $dbh->disconnect;
1895 }
1896
1897 sub newordernum {
1898   my $dbh=C4Connect;
1899   my $query="Select max(ordernumber) from aqorders";
1900   my $sth=$dbh->prepare($query);
1901   $sth->execute;
1902   my $data=$sth->fetchrow_arrayref;
1903   my $ordnum=$$data[0];
1904   $ordnum++;
1905   $sth->finish;
1906   $dbh->disconnect;
1907   return($ordnum);
1908 }
1909
1910 sub receiveorder {
1911   my ($biblio,$ordnum,$quantrec,$user,$cost,$invoiceno,$bibitemno,$freight,$bookfund,$rrp)=@_;
1912   my $dbh=C4Connect;
1913   my $query="update aqorders set quantityreceived='$quantrec',
1914   datereceived=now(),booksellerinvoicenumber='$invoiceno',
1915   biblioitemnumber=$bibitemno,unitprice='$cost',freight='$freight',
1916   rrp='$rrp'
1917   where biblionumber=$biblio and ordernumber=$ordnum
1918   ";
1919 #  print $query;
1920   my $sth=$dbh->prepare($query);
1921   $sth->execute;
1922   $sth->finish;
1923   $query="update aqorderbreakdown set bookfundid=$bookfund where
1924   ordernumber=$ordnum";
1925   $sth=$dbh->prepare($query);
1926 #  print $query;
1927   $sth->execute;
1928   $sth->finish;  
1929   $dbh->disconnect;
1930 }
1931 sub updaterecorder{
1932   my($biblio,$ordnum,$user,$cost,$bookfund,$rrp)=@_;
1933   my $dbh=C4Connect;
1934   my $query="update aqorders set
1935   unitprice='$cost', rrp='$rrp'
1936   where biblionumber=$biblio and ordernumber=$ordnum
1937   ";
1938 #  print $query;
1939   my $sth=$dbh->prepare($query);
1940   $sth->execute;
1941   $sth->finish;
1942   $query="update aqorderbreakdown set bookfundid=$bookfund where
1943   ordernumber=$ordnum";
1944   $sth=$dbh->prepare($query);
1945 #  print $query;
1946   $sth->execute;
1947   $sth->finish;  
1948   $dbh->disconnect;
1949 }
1950
1951 sub curconvert {
1952   my ($currency,$price)=@_;
1953   my $dbh=C4Connect;
1954   my $query="Select rate from currency where currency='$currency'";
1955   my $sth=$dbh->prepare($query);
1956   $sth->execute;
1957   my $data=$sth->fetchrow_hashref;
1958   $sth->finish;
1959   $dbh->disconnect;
1960   my $cur=$data->{'rate'};
1961   if ($cur==0){
1962     $cur=1;
1963   }
1964   my $price=$price / $cur;
1965   return($price);
1966 }
1967
1968 sub getcurrencies {
1969   my $dbh=C4Connect;
1970   my $query="Select * from currency";
1971   my $sth=$dbh->prepare($query);
1972   $sth->execute;
1973   my @results;
1974   my $i=0;
1975   while (my $data=$sth->fetchrow_hashref){
1976     $results[$i]=$data;
1977     $i++;
1978   }
1979   $sth->finish;
1980   $dbh->disconnect;
1981   return($i,\@results);
1982
1983
1984 sub getcurrency {
1985   my ($cur)=@_;
1986   my $dbh=C4Connect;
1987   my $query="Select * from currency where currency='$cur'";
1988   my $sth=$dbh->prepare($query);
1989   $sth->execute;
1990
1991   my $data=$sth->fetchrow_hashref;
1992   $sth->finish;
1993   $dbh->disconnect;
1994   return($data);
1995
1996
1997 sub updatecurrencies {
1998   my ($currency,$rate)=@_;
1999   my $dbh=C4Connect;
2000   my $query="update currency set rate=$rate where currency='$currency'";
2001   my $sth=$dbh->prepare($query);
2002   $sth->execute;
2003   $sth->finish;
2004   $dbh->disconnect;
2005
2006
2007 sub updatesup {
2008    my ($data)=@_;
2009    my $dbh=C4Connect;
2010    my $query="Update aqbooksellers set
2011    name='$data->{'name'}',address1='$data->{'address1'}',address2='$data->{'address2'}',
2012    address3='$data->{'address3'}',address4='$data->{'address4'}',postal='$data->{'postal'}',
2013    phone='$data->{'phone'}',fax='$data->{'fax'}',url='$data->{'url'}',
2014    contact='$data->{'contact'}',contpos='$data->{'contpos'}',
2015    contphone='$data->{'contphone'}', contfax='$data->{'contfax'}', contaltphone=
2016    '$data->{'contaltphone'}', contemail='$data->{'contemail'}', contnotes=
2017    '$data->{'contnotes'}', active=$data->{'active'},
2018    listprice='$data->{'listprice'}', invoiceprice='$data->{'invoiceprice'}',
2019    gstreg=$data->{'gstreg'}, listincgst=$data->{'listincgst'},
2020    invoiceincgst=$data->{'invoiceincgst'}, specialty='$data->{'specialty'}',
2021    discount='$data->{'discount'}',invoicedisc='$data->{'invoicedisc'}',
2022    nocalc='$data->{'nocalc'}'
2023    where id='$data->{'id'}'";
2024    my $sth=$dbh->prepare($query);
2025    $sth->execute;
2026    $sth->finish;
2027    $dbh->disconnect;
2028 #   print $query;
2029 }
2030
2031 sub insertsup {
2032   my ($data)=@_;
2033   my $dbh=C4Connect;
2034   my $sth=$dbh->prepare("Select max(id) from aqbooksellers");
2035   $sth->execute;
2036   my $data2=$sth->fetchrow_hashref;
2037   $sth->finish;
2038   $data2->{'max(id)'}++;
2039   $sth=$dbh->prepare("Insert into aqbooksellers (id) values ($data2->{'max(id)'})");
2040   $sth->execute;
2041   $sth->finish;
2042   $data->{'id'}=$data2->{'max(id)'};
2043   $dbh->disconnect;
2044   updatesup($data);
2045   return($data->{'id'});
2046 }
2047
2048
2049 sub newitems {
2050   my ($item, @barcodes) = @_;
2051   my $dbh   = C4Connect;
2052   my $query = "Select max(itemnumber) from items";
2053   my $sth   = $dbh->prepare($query);
2054   my $data;
2055   my $itemnumber;
2056   my $error;
2057
2058   $sth->execute;
2059   $data       = $sth->fetchrow_hashref;
2060   $itemnumber = $data->{'max(itemnumber)'} + 1;
2061   $sth->finish;
2062   
2063   $item->{'booksellerid'}     = $dbh->quote($item->{'booksellerid'});
2064   $item->{'homebranch'}       = $dbh->quote($item->{'homebranch'});
2065   $item->{'price'}            = $dbh->quote($item->{'price'});
2066   $item->{'replacementprice'} = $dbh->quote($item->{'replacementprice'});
2067   $item->{'itemnotes'}        = $dbh->quote($item->{'itemnotes'});
2068
2069   foreach my $barcode (@barcodes) {
2070     $barcode = uc($barcode);
2071     $barcode = $dbh->quote($barcode);
2072     $query   = "Insert into items set
2073 itemnumber           = $itemnumber,
2074 biblionumber         = $item->{'biblionumber'},
2075 biblioitemnumber     = $item->{'biblioitemnumber'},
2076 barcode              = $barcode,
2077 booksellerid         = $item->{'booksellerid'},
2078 dateaccessioned      = NOW(),
2079 homebranch           = $item->{'homebranch'},
2080 holdingbranch        = $item->{'homebranch'},
2081 price                = $item->{'price'},
2082 replacementprice     = $item->{'replacementprice'},
2083 replacementpricedate = NOW(),
2084 itemnotes            = $item->{'itemnotes'}";
2085
2086     if ($item->{'loan'}) {
2087       $query .= ",
2088 notforloan           = $item->{'loan'}";
2089     } # if
2090
2091     $sth = $dbh->prepare($query);
2092     $sth->execute;
2093
2094     $error .= $sth->errstr;
2095
2096     $sth->finish;
2097     $itemnumber++;
2098   } # for
2099
2100   $dbh->disconnect;
2101   return($error);
2102 }
2103
2104 sub checkitems{
2105   my ($count,@barcodes)=@_;
2106   my $dbh=C4Connect;
2107   my $error;
2108   for (my $i=0;$i<$count;$i++){
2109     $barcodes[$i]=uc $barcodes[$i];
2110     my $query="Select * from items where barcode='$barcodes[$i]'";
2111     my $sth=$dbh->prepare($query);
2112     $sth->execute;
2113     if (my $data=$sth->fetchrow_hashref){
2114       $error.=" Duplicate Barcode: $barcodes[$i]";
2115     }
2116     $sth->finish;
2117   }
2118   $dbh->disconnect;
2119   return($error);
2120 }
2121
2122 sub moditem {
2123   my ($loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
2124   my $dbh=C4Connect;
2125   my $query="update items set biblioitemnumber=$bibitemnum,
2126   barcode='$barcode',itemnotes='$notes'
2127   where itemnumber=$itemnum";
2128   if ($barcode eq ''){
2129     $query="update items set biblioitemnumber=$bibitemnum,notforloan=$loan where itemnumber=$itemnum";
2130   }
2131   if ($lost ne ''){
2132     $query="update items set biblioitemnumber=$bibitemnum,
2133       barcode='$barcode',itemnotes='$notes',homebranch='$homebranch',
2134       itemlost='$lost',wthdrawn='$wthdrawn' where itemnumber=$itemnum";
2135   }
2136   if ($replacement ne ''){
2137     $query=~ s/ where/,replacementprice='$replacement' where/;
2138   }
2139
2140   my $sth=$dbh->prepare($query);
2141   $sth->execute;
2142   $sth->finish;
2143   $dbh->disconnect;
2144 }
2145
2146 sub updatecost{
2147   my($price,$rrp,$itemnum)=@_;
2148   my $dbh=C4Connect;
2149   my $query="update items set price='$price',replacementprice='$rrp'
2150   where itemnumber=$itemnum";
2151   my $sth=$dbh->prepare($query);
2152   $sth->execute;
2153   $sth->finish;
2154   $dbh->disconnect;
2155 }
2156 sub countitems{
2157   my ($bibitemnum)=@_;
2158   my $dbh=C4Connect;
2159   my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
2160   my $sth=$dbh->prepare($query);
2161   $sth->execute;
2162   my $data=$sth->fetchrow_hashref;
2163   $sth->finish;
2164   $dbh->disconnect;
2165   return($data->{'count(*)'});
2166 }
2167
2168 sub findall {
2169   my ($biblionumber)=@_;
2170   my $dbh=C4Connect;
2171   my $query="Select * from biblioitems,items,itemtypes where 
2172   biblioitems.biblionumber=$biblionumber 
2173   and biblioitems.biblioitemnumber=items.biblioitemnumber and
2174   itemtypes.itemtype=biblioitems.itemtype
2175   order by items.biblioitemnumber";
2176   my $sth=$dbh->prepare($query);
2177   $sth->execute;
2178   my @results;
2179   my $i;
2180   while (my $data=$sth->fetchrow_hashref){
2181     $results[$i]=$data;
2182     $i++;
2183   }
2184   $sth->finish;
2185   $dbh->disconnect;
2186   return(@results);
2187 }
2188
2189 sub needsmod{
2190   my ($bibitemnum,$itemtype)=@_;
2191   my $dbh=C4Connect;
2192   my $query="Select * from biblioitems where biblioitemnumber=$bibitemnum
2193   and itemtype='$itemtype'";
2194   my $sth=$dbh->prepare($query);
2195   $sth->execute;
2196   my $result=0;
2197   if (my $data=$sth->fetchrow_hashref){
2198     $result=1;
2199   }
2200   $sth->finish;
2201   $dbh->disconnect;
2202   return($result);
2203 }
2204
2205 sub delitem{
2206   my ($itemnum)=@_;
2207   my $dbh=C4Connect;
2208   my $query="select * from items where itemnumber=$itemnum";
2209   my $sth=$dbh->prepare($query);
2210   $sth->execute;
2211   my @data=$sth->fetchrow_array;
2212   $sth->finish;
2213   $query="Insert into deleteditems values (";
2214   foreach my $temp (@data){
2215     $query=$query."'$temp',";
2216   }
2217   $query=~ s/\,$/\)/;
2218 #  print $query;
2219   $sth=$dbh->prepare($query);
2220   $sth->execute;
2221   $sth->finish;
2222   $query = "Delete from items where itemnumber=$itemnum";
2223   $sth=$dbh->prepare($query);
2224   $sth->execute;
2225   $sth->finish;
2226   $dbh->disconnect;
2227 }
2228 sub deletebiblioitem {
2229     my ($biblioitemnumber) = @_;
2230     my $dbh   = C4Connect;
2231     my $query = "Select * from biblioitems
2232 where biblioitemnumber = $biblioitemnumber";
2233     my $sth   = $dbh->prepare($query);
2234     my @results;
2235
2236     $sth->execute;
2237   
2238     if (@results = $sth->fetchrow_array) {
2239
2240         $query = "Insert into deletedbiblioitems values (";
2241         foreach my $value (@results) {
2242             $value  = $dbh->quote($value);
2243             $query .= "$value,";
2244         } # foreach
2245
2246         $query =~ s/\,$/\)/;
2247         $dbh->do($query);
2248
2249         $query = "Delete from biblioitems
2250 where biblioitemnumber = $biblioitemnumber";
2251         $dbh->do($query);
2252     } # if
2253
2254     $sth->finish;
2255
2256 # Now delete all the items attached to the biblioitem
2257
2258     $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
2259     $sth   = $dbh->prepare($query);
2260
2261     $sth->execute;
2262
2263     while (@results = $sth->fetchrow_array) {
2264
2265         $query = "Insert into deleteditems values (";
2266         foreach my $value (@results) {
2267             $value  = $dbh->quote($value);
2268             $query .= "$value,";
2269         } # foreach
2270
2271         $query =~ s/\,$/\)/;
2272         $dbh->do($query);
2273     } # while
2274
2275     $sth->finish;
2276
2277     $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
2278     $dbh->do($query);
2279     
2280     $dbh->disconnect;
2281 } # sub deletebiblioitem
2282
2283
2284 sub delbiblio{
2285   my ($biblio)=@_;
2286   my $dbh=C4Connect;
2287   my $query="select * from biblio where biblionumber=$biblio";
2288   my $sth=$dbh->prepare($query);
2289   $sth->execute;
2290   if (my @data=$sth->fetchrow_array){
2291     $sth->finish;
2292     $query="Insert into deletedbiblio values (";
2293     foreach my $temp (@data){
2294       $temp=~ s/\'/\\\'/g;
2295       $query=$query."'$temp',";
2296     }
2297     $query=~ s/\,$/\)/;
2298 #   print $query;
2299     $sth=$dbh->prepare($query);
2300     $sth->execute;
2301     $sth->finish;
2302     $query = "Delete from biblio where biblionumber=$biblio";
2303     $sth=$dbh->prepare($query);
2304     $sth->execute;
2305     $sth->finish;
2306   }
2307
2308   $sth->finish;
2309   $dbh->disconnect;
2310 }
2311
2312
2313 sub getitemtypes {
2314   my $dbh   = C4Connect;
2315   my $query = "select * from itemtypes";
2316   my $sth   = $dbh->prepare($query);
2317     # || die "Cannot prepare $query" . $dbh->errstr;
2318   my $count = 0;
2319   my @results;
2320   
2321   $sth->execute;
2322     # || die "Cannot execute $query\n" . $sth->errstr;
2323   while (my $data = $sth->fetchrow_hashref) {
2324     @results[$count] = $data;
2325     $count++;
2326   } # while
2327   
2328   $sth->finish;
2329   $dbh->disconnect;
2330   return($count, @results);
2331 } # sub getitemtypes
2332
2333
2334 sub getbiblio {
2335     my ($biblionumber) = @_;
2336     my $dbh   = C4Connect;
2337     my $query = "Select * from biblio where biblionumber = $biblionumber";
2338     my $sth   = $dbh->prepare($query);
2339       # || die "Cannot prepare $query\n" . $dbh->errstr;
2340     my $count = 0;
2341     my @results;
2342     
2343     $sth->execute;
2344       # || die "Cannot execute $query\n" . $sth->errstr;
2345     while (my $data = $sth->fetchrow_hashref) {
2346       $results[$count] = $data;
2347       $count++;
2348     } # while
2349     
2350     $sth->finish;
2351     $dbh->disconnect;
2352     return($count, @results);
2353 } # sub getbiblio
2354
2355
2356 sub getbiblioitem {
2357     my ($biblioitemnum) = @_;
2358     my $dbh   = C4Connect;
2359     my $query = "Select * from biblioitems where
2360 biblioitemnumber = $biblioitemnum";
2361     my $sth   = $dbh->prepare($query);
2362     my $count = 0;
2363     my @results;
2364
2365     $sth->execute;
2366
2367     while (my $data = $sth->fetchrow_hashref) {
2368         $results[$count] = $data;
2369         $count++;
2370     } # while
2371
2372     $sth->finish;
2373     $dbh->disconnect;
2374     return($count, @results);
2375 } # sub getbiblioitem
2376
2377
2378 sub getbiblioitembybiblionumber {
2379     my ($biblionumber) = @_;
2380     my $dbh   = C4Connect;
2381     my $query = "Select * from biblioitems where biblionumber =
2382 $biblionumber";
2383     my $sth   = $dbh->prepare($query);
2384     my $count = 0;
2385     my @results;
2386
2387     $sth->execute;
2388
2389     while (my $data = $sth->fetchrow_hashref) {
2390         $results[$count] = $data;
2391         $count++;
2392     } # while
2393
2394     $sth->finish;
2395     $dbh->disconnect;
2396     return($count, @results);
2397 } # sub
2398
2399
2400 sub getitemsbybiblioitem {
2401     my ($biblioitemnum) = @_;
2402     my $dbh   = C4Connect;
2403     my $query = "Select * from items, biblio where
2404 biblio.biblionumber = items.biblionumber and biblioitemnumber
2405 = $biblioitemnum";
2406     my $sth   = $dbh->prepare($query);
2407       # || die "Cannot prepare $query\n" . $dbh->errstr;
2408     my $count = 0;
2409     my @results;
2410     
2411     $sth->execute;
2412       # || die "Cannot execute $query\n" . $sth->errstr;
2413     while (my $data = $sth->fetchrow_hashref) {
2414       $results[$count] = $data;
2415       $count++;
2416     } # while
2417     
2418     $sth->finish;
2419     $dbh->disconnect;
2420     return($count, @results);
2421 } # sub getitemsbybiblioitem
2422
2423
2424 sub isbnsearch {
2425     my ($isbn) = @_;
2426     my $dbh   = C4Connect;
2427     my $count = 0;
2428     my $query;
2429     my $sth;
2430     my @results;
2431     
2432     $isbn  = $dbh->quote($isbn);
2433     $query = "Select biblio.* from biblio, biblioitems where
2434 biblio.biblionumber = biblioitems.biblionumber
2435 and isbn = $isbn";
2436     $sth   = $dbh->prepare($query);
2437     
2438     $sth->execute;
2439     while (my $data = $sth->fetchrow_hashref) {
2440         $results[$count] = $data;
2441         $count++;
2442     } # while
2443
2444     $sth->finish;
2445     $dbh->disconnect;
2446     return($count, @results);
2447 } # sub isbnsearch
2448
2449
2450 sub websitesearch {
2451     my ($keywordlist) = @_;
2452     my $dbh   = C4Connect;
2453     my $query = "Select distinct biblio.* from biblio, biblioitems where
2454 biblio.biblionumber = biblioitems.biblionumber and (";
2455     my $count = 0;
2456     my $sth;
2457     my @results;
2458     my @keywords = split(/ +/, $keywordlist);
2459     my $keyword = shift(@keywords);
2460
2461     $keyword =~ s/%/\\%/g;
2462     $keyword =~ s/_/\\_/;
2463     $keyword = "%" . $keyword . "%";
2464     $keyword = $dbh->quote($keyword);
2465     $query  .= " (url like $keyword)";
2466
2467     foreach $keyword (@keywords) {
2468         $keyword =~ s/%/\\%/;
2469         $keyword =~ s/_/\\_/;
2470         $keyword = "%" . $keyword . "%";
2471         $keyword = $dbh->quote($keyword);
2472         $query  .= " or (url like $keyword)";
2473     } # foreach
2474
2475     $query .= ")";
2476     $sth    = $dbh->prepare($query);
2477     $sth->execute;
2478
2479     while (my $data = $sth->fetchrow_hashref) {
2480         $results[$count] = $data;
2481         $count++;
2482     } # while
2483
2484     $sth->finish;
2485     $dbh->disconnect;
2486     return($count, @results);
2487 } # sub websitesearch
2488
2489
2490 sub addwebsite {
2491     my ($website) = @_;
2492     my $dbh = C4Connect;
2493     my $query;
2494     
2495     $website->{'biblionumber'} = $dbh->quote($website->{'biblionumber'});
2496     $website->{'title'}        = $dbh->quote($website->{'title'});
2497     $website->{'description'}  = $dbh->quote($website->{'description'});
2498     $website->{'url'}          = $dbh->quote($website->{'url'});
2499     
2500     $query = "Insert into websites set
2501 biblionumber = $website->{'biblionumber'},
2502 title        = $website->{'title'},
2503 description  = $website->{'description'},
2504 url          = $website->{'url'}";
2505     
2506     $dbh->do($query);
2507     
2508     $dbh->disconnect;
2509 } # sub website
2510
2511
2512 sub updatewebsite {
2513     my ($website) = @_;
2514     my $dbh = C4Connect;
2515     my $query;
2516     
2517     $website->{'title'}      = $dbh->quote($website->{'title'});
2518     $website->{'description'} = $dbh->quote($website->{'description'});
2519     $website->{'url'}        = $dbh->quote($website->{'url'});
2520     
2521     $query = "Update websites set
2522 title       = $website->{'title'},
2523 description = $website->{'description'},
2524 url         = $website->{'url'}
2525 where websitenumber = $website->{'websitenumber'}";
2526
2527     $dbh->do($query);
2528     
2529     $dbh->disconnect;
2530 } # sub updatewebsite
2531
2532
2533 sub deletewebsite {
2534     my ($websitenumber) = @_;
2535     my $dbh = C4Connect;
2536     my $query = "Delete from websites where websitenumber = $websitenumber";
2537     
2538     $dbh->do($query);
2539     
2540     $dbh->disconnect;
2541 } # sub deletewebsite
2542
2543 END { }       # module clean-up code here (global destructor)