Update to catalogue to allow change of biblio abstracts.
[koha.git] / C4 / Acquisitions.pm
1 package C4::Acquisitions; #asummes C4/Acquisitions.pm
2
3 use strict;
4 require Exporter;
5 use C4::Database;
6
7 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
8
9 # set the version for version checking
10 $VERSION = 0.01;
11
12 @ISA = qw(Exporter);
13 @EXPORT = qw(&getorders &bookseller &breakdown &basket &newbasket &bookfunds
14 &ordersearch &newbiblio &newbiblioitem &newsubject &newsubtitle &neworder
15 &newordernum &modbiblio &modorder &getsingleorder &invoice &receiveorder
16 &bookfundbreakdown &curconvert &updatesup &insertsup &newitems &modbibitem
17 &getcurrencies &modsubtitle &modsubject &modaddauthor &moditem &countitems 
18 &findall &needsmod &delitem &delbibitem &delbiblio &delorder &branches
19 &getallorders &getrecorders &updatecurrencies &getorder &getcurrency &updaterecorder
20 &updatecost &checkitems &modnote &getitemtypes &getbiblio
21 &getbiblioitem &getitemsbybiblioitem &isbnsearch &keywordsearch
22 &websitesearch);
23 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
24
25 # your exported package globals go here,
26 # as well as any optionally exported functions
27
28 @EXPORT_OK   = qw($Var1 %Hashit);
29
30
31 # non-exported package globals go here
32 use vars qw(@more $stuff);
33
34 # initalize package globals, first exported ones
35
36 my $Var1   = '';
37 my %Hashit = ();
38
39
40
41 # then the others (which are still accessible as $Some::Module::stuff)
42 my $stuff  = '';
43 my @more   = ();
44
45 # all file-scoped lexicals must be created before
46 # the functions below that use them.
47
48 # file-private lexicals go here
49 my $priv_var    = '';
50 my %secret_hash = ();
51
52 # here's a file-private function as a closure,
53 # callable as &$priv_func;  it cannot be prototyped.
54 my $priv_func = sub {
55   # stuff goes here.
56   };
57   
58 # make all your functions, whether exported or not;
59
60 sub getorders {
61   my ($supplierid)=@_;
62   my $dbh=C4Connect;
63   my $query = "Select count(*),authorisedby,entrydate,basketno from aqorders where 
64   booksellerid='$supplierid' and (quantity > quantityreceived or
65   quantityreceived is NULL)
66   and (datecancellationprinted is NULL or datecancellationprinted = '0000-00-00')";
67   $query.=" group by basketno order by entrydate desc";
68   #print $query;
69   my $sth=$dbh->prepare($query);
70   $sth->execute;
71   my @results;
72   my $i=0;
73   while (my $data=$sth->fetchrow_hashref){
74     $results[$i]=$data;
75     $i++;
76   }
77   $sth->finish;
78   $dbh->disconnect;
79   return ($i,\@results);
80 }
81
82 sub itemcount{
83   my ($biblio)=@_;
84   my $dbh=C4Connect;
85   my $query="Select count(*) from items where biblionumber=$biblio";
86 #  print $query;
87   my $sth=$dbh->prepare($query);
88   $sth->execute;
89   my $data=$sth->fetchrow_hashref;
90   $sth->finish;
91   $dbh->disconnect;
92   return($data->{'count(*)'});
93 }
94
95 sub getorder{
96   my ($bi,$bib)=@_;
97   my $dbh=C4Connect;
98   my $query="Select ordernumber from aqorders where biblionumber=$bib and
99   biblioitemnumber='$bi'";
100   my $sth=$dbh->prepare($query);
101   $sth->execute;
102   my $ordnum=$sth->fetchrow_hashref;
103   $sth->finish;
104   my $order=getsingleorder($ordnum->{'ordernumber'});
105   $dbh->disconnect;
106 #  print $query;
107   return ($order,$ordnum->{'ordernumber'});
108 }
109
110 sub getsingleorder {
111   my ($ordnum)=@_;
112   my $dbh=C4Connect;
113   my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown 
114   where aqorders.ordernumber='$ordnum' 
115   and biblio.biblionumber=aqorders.biblionumber and
116   biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
117   aqorders.ordernumber=aqorderbreakdown.ordernumber";
118   my $sth=$dbh->prepare($query);
119   $sth->execute;
120   my $data=$sth->fetchrow_hashref;
121   $sth->finish;
122   $dbh->disconnect;
123   return($data);
124 }
125
126 sub invoice {
127   my ($invoice)=@_;
128   my $dbh=C4Connect;
129   my $query="Select * from aqorders,biblio,biblioitems where
130   booksellerinvoicenumber='$invoice' 
131   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
132   aqorders.biblioitemnumber group by aqorders.ordernumber,aqorders.biblioitemnumber";
133   my $i=0;
134   my @results;
135   my $sth=$dbh->prepare($query);
136   $sth->execute;
137   while (my $data=$sth->fetchrow_hashref){
138     $results[$i]=$data;
139     $i++;
140   }
141   $sth->finish;
142   $dbh->disconnect;
143   return($i,@results);
144 }
145
146 sub getallorders {
147   #gets all orders from a certain supplier, orders them alphabetically
148   my ($supid)=@_;
149   my $dbh=C4Connect;
150   my $query="Select * from aqorders,biblio,biblioitems where booksellerid='$supid'
151   and (cancelledby is NULL or cancelledby = '')
152   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=                    
153   aqorders.biblioitemnumber 
154   group by aqorders.biblioitemnumber 
155   order by
156   biblio.title";
157   my $i=0;
158   my @results;
159   my $sth=$dbh->prepare($query);
160   $sth->execute;
161   while (my $data=$sth->fetchrow_hashref){
162     $results[$i]=$data;
163     $i++;
164   }
165   $sth->finish;
166   $dbh->disconnect;
167   return($i,@results);
168 }
169
170 sub getrecorders {
171   #gets all orders from a certain supplier, orders them alphabetically
172   my ($supid)=@_;
173   my $dbh=C4Connect;
174   my $query="Select * from aqorders,biblio,biblioitems where booksellerid='$supid'
175   and (cancelledby is NULL or cancelledby = '')
176   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=                    
177   aqorders.biblioitemnumber and
178   aqorders.quantityreceived>0
179   and aqorders.datereceived >=now()
180   group by aqorders.biblioitemnumber 
181   order by
182   biblio.title";
183   my $i=0;
184   my @results;
185   my $sth=$dbh->prepare($query);
186   $sth->execute;
187   while (my $data=$sth->fetchrow_hashref){
188     $results[$i]=$data;
189     $i++;
190   }
191   $sth->finish;
192   $dbh->disconnect;
193   return($i,@results);
194 }
195
196 sub ordersearch {
197   my ($search,$biblio,$catview) = @_;
198   my $dbh   = C4Connect;
199   my $query = "Select *,biblio.title from aqorders,biblioitems,biblio
200 where aqorders.biblioitemnumber = biblioitems.biblioitemnumber
201 and biblio.biblionumber=aqorders.biblionumber
202 and ((datecancellationprinted is NULL)
203 or (datecancellationprinted = '0000-00-00')
204 and ((";
205   my @data  = split(' ',$search);
206   my $count = @data;
207   for (my $i = 0; $i < $count; $i++) {
208     $query .= "(biblio.title like '$data[$i]%' or biblio.title like '% $data[$i]%') and ";
209   }
210   $query=~ s/ and $//;
211   $query.=" ) or biblioitems.isbn='$search' 
212   or (aqorders.ordernumber='$search' and aqorders.biblionumber='$biblio')) ";
213   if ($catview ne 'yes'){
214     $query.=" and (quantityreceived < quantity or quantityreceived is NULL)";
215   }
216   $query.=" group by aqorders.ordernumber";
217   my $sth=$dbh->prepare($query);
218 #  print $query;
219   $sth->execute;
220   my $i=0;
221   my @results;
222   while (my $data=$sth->fetchrow_hashref){
223      my $sth2=$dbh->prepare("Select * from biblio where
224      biblionumber='$data->{'biblionumber'}'");
225      $sth2->execute;
226      my $data2=$sth2->fetchrow_hashref;
227      $sth2->finish;
228      $data->{'author'}=$data2->{'author'};
229      $data->{'seriestitle'}=$data2->{'seriestitle'};
230      $sth2=$dbh->prepare("Select * from aqorderbreakdown where
231     ordernumber=$data->{'ordernumber'}");
232     $sth2->execute;
233     $data2=$sth2->fetchrow_hashref;
234     $sth2->finish;
235     $data->{'branchcode'}=$data2->{'branchcode'};
236     $data->{'bookfundid'}=$data2->{'bookfundid'};
237     $results[$i]=$data;
238     $i++;
239   }
240   $sth->finish;
241   $dbh->disconnect;
242   return($i,@results);
243 }
244
245
246 sub bookseller {
247   my ($searchstring)=@_;
248   my $dbh=C4Connect;
249   my $query="Select * from aqbooksellers where name like '%$searchstring%' or
250   id = '$searchstring'";
251   my $sth=$dbh->prepare($query);
252   $sth->execute;
253   my @results;
254   my $i=0;
255   while (my $data=$sth->fetchrow_hashref){
256     $results[$i]=$data;
257     $i++;
258   }
259   $sth->finish;
260   $dbh->disconnect;
261   return($i,@results);
262 }
263
264 sub breakdown {
265   my ($id)=@_;
266   my $dbh=C4Connect;
267   my $query="Select * from aqorderbreakdown where ordernumber='$id'";
268   my $sth=$dbh->prepare($query);
269   $sth->execute;
270   my @results;
271   my $i=0;
272   while (my $data=$sth->fetchrow_hashref){
273     $results[$i]=$data;
274     $i++;
275   }
276   $sth->finish;
277   $dbh->disconnect;
278   return($i,\@results);
279 }
280
281 sub basket {
282   my ($basketno,$supplier)=@_;
283   my $dbh=C4Connect;
284   my $query="Select *,biblio.title from aqorders,biblio,biblioitems 
285   where basketno='$basketno'
286   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber
287   =aqorders.biblioitemnumber 
288   and (datecancellationprinted is NULL or datecancellationprinted =
289   '0000-00-00')";
290   if ($supplier ne ''){
291     $query.=" and aqorders.booksellerid='$supplier'";
292   } 
293   $query.=" group by aqorders.ordernumber";
294   my $sth=$dbh->prepare($query);
295   $sth->execute;
296   my @results;
297 #  print $query;
298   my $i=0;
299   while (my $data=$sth->fetchrow_hashref){
300     $results[$i]=$data;
301     $i++;
302   }
303   $sth->finish;
304   $dbh->disconnect;
305   return($i,@results);
306 }
307
308 sub newbasket {
309   my $dbh=C4Connect;
310   my $query="Select max(basketno) from aqorders";
311   my $sth=$dbh->prepare($query);
312   $sth->execute;
313   my $data=$sth->fetchrow_arrayref;
314   my $basket=$$data[0];
315   $basket++;
316   $sth->finish;
317   $dbh->disconnect;
318   return($basket);
319 }
320
321 sub bookfunds {
322   my $dbh=C4Connect;
323   my $query="Select * from aqbookfund,aqbudget where aqbookfund.bookfundid
324   =aqbudget.bookfundid 
325   and aqbudget.startdate='2001=07-01' 
326   group by aqbookfund.bookfundid order by bookfundname";
327   my $sth=$dbh->prepare($query);
328   $sth->execute;
329   my @results;
330   my $i=0;
331   while (my $data=$sth->fetchrow_hashref){
332     $results[$i]=$data;
333     $i++;
334   }
335   $sth->finish;
336   $dbh->disconnect;
337   return($i,@results);
338 }
339
340 sub branches {
341     my $dbh   = C4Connect;
342     my $query = "Select * from branches";
343     my $sth   = $dbh->prepare($query);
344     my $i     = 0;
345     my @results;
346
347     $sth->execute;
348     while (my $data = $sth->fetchrow_hashref) {
349         $results[$i] = $data;
350         $i++;
351     } # while
352
353     $sth->finish;
354     $dbh->disconnect;
355     return($i, @results);
356 } # sub branches
357
358 sub bookfundbreakdown {
359   my ($id)=@_;
360   my $dbh=C4Connect;
361   my $query="Select quantity,datereceived,freight,unitprice,listprice,ecost,quantityreceived,subscription
362   from aqorders,aqorderbreakdown where bookfundid='$id' and 
363   aqorders.ordernumber=aqorderbreakdown.ordernumber and ((budgetdate >=
364   '2001-07-01' and budgetdate <'2002-07-01') or
365   (datereceived >= '2001-07-01' and datereceived < '2002-07-01'))
366   and (datecancellationprinted is NULL or
367   datecancellationprinted='0000-00-00')";
368   my $sth=$dbh->prepare($query);
369   $sth->execute;
370   my $comtd=0;
371   my $spent=0;
372   while (my $data=$sth->fetchrow_hashref){
373     if ($data->{'subscription'} == 1){
374       $spent+=$data->{'quantity'}*$data->{'unitprice'};
375     } else {
376       my $leftover=$data->{'quantity'}-$data->{'quantityreceived'};
377       $comtd+=($data->{'ecost'})*$leftover;
378       $spent+=($data->{'unitprice'})*$data->{'quantityreceived'};
379     }
380   }
381   $sth->finish;
382   $dbh->disconnect;
383   return($spent,$comtd);
384 }
385       
386
387 sub newbiblio {
388   my ($biblio) = @_;
389   my $dbh    = &C4Connect;
390   my $query  = "Select max(biblionumber) from biblio";
391   my $sth    = $dbh->prepare($query);
392   $sth->execute;
393   my $data   = $sth->fetchrow_arrayref;
394   my $bibnum = $$data[0] + 1;
395   my $series = 0;
396
397   $biblio->{'title'}       = $dbh->quote($biblio->{'title'});
398   $biblio->{'author'}      = $dbh->quote($biblio->{'author'});
399   $biblio->{'copyright'}   = $dbh->quote($biblio->{'copyright'});
400   $biblio->{'seriestitle'} = $dbh->quote($biblio->{'seriestitle'});
401   $biblio->{'notes'}       = $dbh->quote($biblio->{'notes'});
402   $biblio->{'abstract'}    = $dbh->quote($biblio->{'abstract'});
403   if ($biblio->{'seriestitle'}) { $series = 1 };
404
405   $sth->finish;
406   $query = "insert into biblio set
407 biblionumber  = $bibnum,
408 title         = $biblio->{'title'},
409 author        = $biblio->{'author'},
410 copyrightdate = $biblio->{'copyright'},
411 serial        = $series,
412 seriestitle   = $biblio->{'seriestitle'},
413 notes         = $biblio->{'notes'},
414 abstract      = $biblio->{'abstract'}";
415
416   $sth = $dbh->prepare($query);
417   $sth->execute;
418
419   $sth->finish;
420   $dbh->disconnect;
421   return($bibnum);
422 }
423
424
425 sub modbiblio {
426   my ($biblio) = @_;
427   my $dbh   = C4Connect;
428   my $query;
429   my $sth;
430   
431   $biblio->{'title'}         = $dbh->quote($biblio->{'title'});
432   $biblio->{'author'}        = $dbh->quote($biblio->{'author'});
433   $biblio->{'abstract'}      = $dbh->quote($biblio->{'abstract'});
434   $biblio->{'copyrightdate'} = $dbh->quote($biblio->{'copyrightdate'});
435   $biblio->{'seriestitle'}   = $dbh->quote($biblio->{'serirestitle'});
436   $biblio->{'serial'}        = $dbh->quote($biblio->{'serial'});
437   $biblio->{'unititle'}      = $dbh->quote($biblio->{'unititle'});
438   $biblio->{'notes'}         = $dbh->quote($biblio->{'notes'});
439
440   $query = "Update biblio set
441 title         = $biblio->{'title'},
442 author        = $biblio->{'author'},
443 abstract      = $biblio->{'abstract'},
444 copyrightdate = $biblio->{'copyrightdate'},
445 seriestitle   = $biblio->{'seriestitle'},
446 serial        = $biblio->{'serial'},
447 unititle      = $biblio->{'unititle'},
448 notes         = $biblio->{'notes'}
449 where biblionumber = $biblio->{'biblionumber'}";
450   $sth   = $dbh->prepare($query);
451
452   $sth->execute;
453
454   $sth->finish;
455   $dbh->disconnect;
456   return($biblio->{'biblionumber'});
457 } # sub modbiblio
458
459
460 sub modsubtitle {
461   my ($bibnum, $subtitle) = @_;
462   my $dbh   = C4Connect;
463   my $query = "update bibliosubtitle set
464 subtitle = '$subtitle'
465 where biblionumber = $bibnum";
466   my $sth   = $dbh->prepare($query);
467
468   $sth->execute;
469   $sth->finish;
470   $dbh->disconnect;
471 } # sub modsubtitle
472
473
474 sub modaddauthor {
475     my ($bibnum, $author) = @_;
476     my $dbh   = C4Connect;
477     my $query = "Delete from additionalauthors where biblionumber = $bibnum";
478     my $sth = $dbh->prepare($query);
479
480     $sth->execute;
481     $sth->finish;
482
483     if ($author ne '') {
484         $query = "Insert into additionalauthors set
485 author       = '$author',
486 biblionumber = '$bibnum'";
487         $sth   = $dbh->prepare($query);
488
489         $sth->execute;
490
491         $sth->finish;
492     } # if
493
494   $dbh->disconnect;
495 } # sub modaddauthor
496
497
498 sub modsubject {
499   my ($bibnum, $force, @subject) = @_;
500   my $dbh   = C4Connect;
501   my $count = @subject;
502   my $error;
503   for (my $i = 0; $i < $count; $i++) {
504     $subject[$i] =~ s/^ //g;
505     $subject[$i] =~ s/ $//g;
506     my $query = "select * from catalogueentry
507 where entrytype = 's'
508 and catalogueentry = '$subject[$i]'";
509     my $sth   = $dbh->prepare($query);
510     $sth->execute;
511
512     if (my $data = $sth->fetchrow_hashref) {
513     } else {
514       if ($force eq $subject[$i]) {
515
516          # subject not in aut, chosen to force anway
517          # so insert into cataloguentry so its in auth file
518          $query = "Insert into catalogueentry
519 (entrytype,catalogueentry)
520 values ('s','$subject[$i]')";
521          my $sth2 = $dbh->prepare($query);
522
523          $sth2->execute;
524          $sth2->finish;
525
526       } else {
527
528         $error = "$subject[$i]\n does not exist in the subject authority file";
529         $query = "Select * from catalogueentry
530 where entrytype = 's'
531 and (catalogueentry like '$subject[$i] %'
532 or catalogueentry like '% $subject[$i] %'
533 or catalogueentry like '% $subject[$i]')";
534         my $sth2 = $dbh->prepare($query);
535
536         $sth2->execute;
537         while (my $data = $sth2->fetchrow_hashref) {
538           $error = $error."<br>$data->{'catalogueentry'}";
539         } # while
540         $sth2->finish;
541       } # else
542     } # else
543     $sth->finish;
544   } # else
545
546   if ($error eq '') {
547     my $query = "Delete from bibliosubject where biblionumber = $bibnum";
548     my $sth   = $dbh->prepare($query);
549
550     $sth->execute;
551     $sth->finish;
552
553     for (my $i = 0; $i < $count; $i++) {
554       $sth = $dbh->prepare("Insert into bibliosubject
555 values ('$subject[$i]', $bibnum)");
556
557       $sth->execute;
558       $sth->finish;
559     } # for
560   } # if
561
562   $dbh->disconnect;
563   return($error);
564 } # sub modsubject
565
566 sub modbibitem {
567   my ($bibitemnum,$itemtype,$isbn,$publishercode,$publicationdate,$classification,$dewey,$subclass,$illus,$pages,$volumeddesc,$notes,$size,$place)=@_;
568   my $dbh=C4Connect;
569   my $query="update biblioitems set itemtype='$itemtype',
570   isbn='$isbn',publishercode='$publishercode',publicationyear='$publicationdate',
571   classification='$classification',dewey='$dewey',subclass='$subclass',illus='$illus',
572   pages='$pages',volumeddesc='$volumeddesc',notes='$notes',size='$size',place='$place'
573   where
574   biblioitemnumber=$bibitemnum";
575   my $sth=$dbh->prepare($query);
576 #    print $query;
577   $sth->execute;
578   $sth->finish;
579   $dbh->disconnect;
580 }
581
582 sub modnote {
583   my ($bibitemnum,$note)=@_;
584   my $dbh=C4Connect;
585   my $query="update biblioitems set notes='$note' where
586   biblioitemnumber='$bibitemnum'";
587   my $sth=$dbh->prepare($query);
588   $sth->execute;
589   $sth->finish;
590   $dbh->disconnect;
591 }
592
593 sub newbiblioitem {
594   my ($biblioitem) = @_;
595   my $dbh   = C4Connect;
596   my $query = "Select max(biblioitemnumber) from biblioitems";
597   my $sth   = $dbh->prepare($query);
598   my $data;
599   my $bibitemnum;
600   
601   $biblioitem->{'volume'}          = $dbh->quote($biblioitem->{'volume'});
602   $biblioitem->{'number'}          = $dbh->quote($biblioitem->{'number'});
603   $biblioitem->{'classification'}  = $dbh->quote($biblioitem->{'classification'});
604   $biblioitem->{'itemtype'}        = $dbh->quote($biblioitem->{'itemtype'});
605   $biblioitem->{'url'}             = $dbh->quote($biblioitem->{'url'});
606   $biblioitem->{'isbn'}            = $dbh->quote($biblioitem->{'isbn'});
607   $biblioitem->{'issn'}            = $dbh->quote($biblioitem->{'issn'});
608   $biblioitem->{'dewey'}           = $dbh->quote($biblioitem->{'dewey'});
609   $biblioitem->{'subclass'}        = $dbh->quote($biblioitem->{'subclass'});
610   $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
611   $biblioitem->{'publishercode'}   = $dbh->quote($biblioitem->{'publishercode'});
612   $biblioitem->{'volumedate'}      = $dbh->quote($biblioitem->{'volumedate'});
613   $biblioitem->{'volumeddesc'}     = $dbh->quote($biblioitem->{'volumeddesc'});  $biblioitem->{'illus'}            = $dbh->quote($biblioitem->{'illus'});
614   $biblioitem->{'pages'}           = $dbh->quote($biblioitem->{'pages'});
615   $biblioitem->{'notes'}           = $dbh->quote($biblioitem->{'notes'});
616   $biblioitem->{'size'}            = $dbh->quote($biblioitem->{'size'});
617   $biblioitem->{'place'}           = $dbh->quote($biblioitem->{'place'});
618   
619   $sth->execute;
620   $data       = $sth->fetchrow_arrayref;
621   $bibitemnum = $$data[0] + 1;
622
623   $sth->finish;
624
625   $query = "insert into biblioitems set
626 biblioitemnumber = $bibitemnum,
627 biblionumber     = $biblioitem->{'biblionumber'},
628 volume           = $biblioitem->{'volume'},
629 number           = $biblioitem->{'number'},
630 classification   = $biblioitem->{'classification'},
631 itemtype         = $biblioitem->{'itemtype'},
632 url              = $biblioitem->{'url'},
633 isbn             = $biblioitem->{'isbn'},
634 issn             = $biblioitem->{'issn'},
635 dewey            = $biblioitem->{'dewey'},
636 subclass         = $biblioitem->{'subclass'},
637 publicationyear  = $biblioitem->{'publicationyear'},
638 publishercode    = $biblioitem->{'publishercode'},
639 volumedate       = $biblioitem->{'volumedate'},
640 volumeddesc      = $biblioitem->{'volumeddesc'},
641 illus            = $biblioitem->{'illus'},
642 pages            = $biblioitem->{'pages'},
643 notes            = $biblioitem->{'notes'},
644 size             = $biblioitem->{'size'},
645 place            = $biblioitem->{'place'}";
646
647   $sth = $dbh->prepare($query);
648   $sth->execute;
649
650   $sth->finish;
651   $dbh->disconnect;
652   return($bibitemnum);
653 }
654
655 sub newsubject {
656   my ($bibnum)=@_;
657   my $dbh=C4Connect;
658   my $query="insert into bibliosubject (biblionumber) values
659   ($bibnum)";
660   my $sth=$dbh->prepare($query);
661 #  print $query;
662   $sth->execute;
663   $sth->finish;
664   $dbh->disconnect;
665 }
666
667 sub newsubtitle {
668   my ($bibnum, $subtitle) = @_;
669   my $dbh   = C4Connect;
670   $subtitle = $dbh->quote($subtitle);
671   my $query = "insert into bibliosubtitle set
672 biblionumber = $bibnum,
673 subtitle = $subtitle";
674   my $sth   = $dbh->prepare($query);
675
676   $sth->execute;
677
678   $sth->finish;
679   $dbh->disconnect;
680 }
681
682 sub neworder {
683   my ($bibnum,$title,$ordnum,$basket,$quantity,$listprice,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$sub,$invoice)=@_;
684   if ($budget eq 'now'){
685     $budget="now()";
686   } else {
687     $budget="'2001-07-01'";
688   }
689   if ($sub eq 'yes'){
690     $sub=1;
691   } else {
692     $sub=0;
693   }
694   my $dbh=C4Connect;
695   my $query="insert into aqorders (biblionumber,title,basketno,
696   quantity,listprice,booksellerid,entrydate,requisitionedby,authorisedby,notes,
697   biblioitemnumber,rrp,ecost,gst,budgetdate,unitprice,subscription,booksellerinvoicenumber)
698
699   values
700   ($bibnum,'$title',$basket,$quantity,$listprice,'$supplier',now(),
701   '$who','$who','$notes',$bibitemnum,'$rrp','$ecost','$gst',$budget,'$cost',
702   '$sub','$invoice')";
703   my $sth=$dbh->prepare($query);
704 #  print $query;
705   $sth->execute;
706   $sth->finish;
707   $query="select * from aqorders where
708   biblionumber=$bibnum and basketno=$basket and ordernumber >=$ordnum";
709   $sth=$dbh->prepare($query);
710   $sth->execute;
711   my $data=$sth->fetchrow_hashref;
712   $sth->finish;
713   $ordnum=$data->{'ordernumber'};
714   $query="insert into aqorderbreakdown (ordernumber,bookfundid) values
715   ($ordnum,'$bookfund')";
716   $sth=$dbh->prepare($query);
717 #  print $query;
718   $sth->execute;
719   $sth->finish;
720   $dbh->disconnect;
721 }
722
723 sub delorder {
724   my ($bibnum,$ordnum)=@_;
725   my $dbh=C4Connect;
726   my $query="update aqorders set datecancellationprinted=now()
727   where biblionumber='$bibnum' and
728   ordernumber='$ordnum'";
729   my $sth=$dbh->prepare($query);
730   #print $query;
731   $sth->execute;
732   $sth->finish;
733   my $count=itemcount($bibnum);
734   if ($count == 0){
735     delbiblio($bibnum);
736   }
737   $dbh->disconnect;
738 }
739
740 sub modorder {
741   my ($title,$ordnum,$quantity,$listprice,$bibnum,$basketno,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$invoice)=@_;
742   my $dbh=C4Connect;
743   my $query="update aqorders set title='$title',
744   quantity='$quantity',listprice='$listprice',basketno='$basketno', 
745   rrp='$rrp',ecost='$ecost',unitprice='$cost',
746   booksellerinvoicenumber='$invoice'
747   where
748   ordernumber=$ordnum and biblionumber=$bibnum";
749   my $sth=$dbh->prepare($query);
750 #  print $query;
751   $sth->execute;
752   $sth->finish;
753   $query="update aqorderbreakdown set bookfundid=$bookfund where
754   ordernumber=$ordnum";
755   $sth=$dbh->prepare($query);
756 #  print $query;
757   $sth->execute;
758   $sth->finish;
759   $dbh->disconnect;
760 }
761
762 sub newordernum {
763   my $dbh=C4Connect;
764   my $query="Select max(ordernumber) from aqorders";
765   my $sth=$dbh->prepare($query);
766   $sth->execute;
767   my $data=$sth->fetchrow_arrayref;
768   my $ordnum=$$data[0];
769   $ordnum++;
770   $sth->finish;
771   $dbh->disconnect;
772   return($ordnum);
773 }
774
775 sub receiveorder {
776   my ($biblio,$ordnum,$quantrec,$user,$cost,$invoiceno,$bibitemno,$freight,$bookfund,$rrp)=@_;
777   my $dbh=C4Connect;
778   my $query="update aqorders set quantityreceived='$quantrec',
779   datereceived=now(),booksellerinvoicenumber='$invoiceno',
780   biblioitemnumber=$bibitemno,unitprice='$cost',freight='$freight',
781   rrp='$rrp'
782   where biblionumber=$biblio and ordernumber=$ordnum
783   ";
784 #  print $query;
785   my $sth=$dbh->prepare($query);
786   $sth->execute;
787   $sth->finish;
788   $query="update aqorderbreakdown set bookfundid=$bookfund where
789   ordernumber=$ordnum";
790   $sth=$dbh->prepare($query);
791 #  print $query;
792   $sth->execute;
793   $sth->finish;  
794   $dbh->disconnect;
795 }
796 sub updaterecorder{
797   my($biblio,$ordnum,$user,$cost,$bookfund,$rrp)=@_;
798   my $dbh=C4Connect;
799   my $query="update aqorders set
800   unitprice='$cost', rrp='$rrp'
801   where biblionumber=$biblio and ordernumber=$ordnum
802   ";
803 #  print $query;
804   my $sth=$dbh->prepare($query);
805   $sth->execute;
806   $sth->finish;
807   $query="update aqorderbreakdown set bookfundid=$bookfund where
808   ordernumber=$ordnum";
809   $sth=$dbh->prepare($query);
810 #  print $query;
811   $sth->execute;
812   $sth->finish;  
813   $dbh->disconnect;
814 }
815
816 sub curconvert {
817   my ($currency,$price)=@_;
818   my $dbh=C4Connect;
819   my $query="Select rate from currency where currency='$currency'";
820   my $sth=$dbh->prepare($query);
821   $sth->execute;
822   my $data=$sth->fetchrow_hashref;
823   $sth->finish;
824   $dbh->disconnect;
825   my $cur=$data->{'rate'};
826   if ($cur==0){
827     $cur=1;
828   }
829   my $price=$price / $cur;
830   return($price);
831 }
832
833 sub getcurrencies {
834   my $dbh=C4Connect;
835   my $query="Select * from currency";
836   my $sth=$dbh->prepare($query);
837   $sth->execute;
838   my @results;
839   my $i=0;
840   while (my $data=$sth->fetchrow_hashref){
841     $results[$i]=$data;
842     $i++;
843   }
844   $sth->finish;
845   $dbh->disconnect;
846   return($i,\@results);
847
848
849 sub getcurrency {
850   my ($cur)=@_;
851   my $dbh=C4Connect;
852   my $query="Select * from currency where currency='$cur'";
853   my $sth=$dbh->prepare($query);
854   $sth->execute;
855
856   my $data=$sth->fetchrow_hashref;
857   $sth->finish;
858   $dbh->disconnect;
859   return($data);
860
861
862 sub updatecurrencies {
863   my ($currency,$rate)=@_;
864   my $dbh=C4Connect;
865   my $query="update currency set rate=$rate where currency='$currency'";
866   my $sth=$dbh->prepare($query);
867   $sth->execute;
868   $sth->finish;
869   $dbh->disconnect;
870
871
872 sub updatesup {
873    my ($data)=@_;
874    my $dbh=C4Connect;
875    my $query="Update aqbooksellers set
876    name='$data->{'name'}',address1='$data->{'address1'}',address2='$data->{'address2'}',
877    address3='$data->{'address3'}',address4='$data->{'address4'}',postal='$data->{'postal'}',
878    phone='$data->{'phone'}',fax='$data->{'fax'}',url='$data->{'url'}',
879    contact='$data->{'contact'}',contpos='$data->{'contpos'}',
880    contphone='$data->{'contphone'}', contfax='$data->{'contfax'}', contaltphone=
881    '$data->{'contaltphone'}', contemail='$data->{'contemail'}', contnotes=
882    '$data->{'contnotes'}', active=$data->{'active'},
883    listprice='$data->{'listprice'}', invoiceprice='$data->{'invoiceprice'}',
884    gstreg=$data->{'gstreg'}, listincgst=$data->{'listincgst'},
885    invoiceincgst=$data->{'invoiceincgst'}, specialty='$data->{'specialty'}',
886    discount='$data->{'discount'}',invoicedisc='$data->{'invoicedisc'}',
887    nocalc='$data->{'nocalc'}'
888    where id='$data->{'id'}'";
889    my $sth=$dbh->prepare($query);
890    $sth->execute;
891    $sth->finish;
892    $dbh->disconnect;
893 #   print $query;
894 }
895
896 sub insertsup {
897   my ($data)=@_;
898   my $dbh=C4Connect;
899   my $sth=$dbh->prepare("Select max(id) from aqbooksellers");
900   $sth->execute;
901   my $data2=$sth->fetchrow_hashref;
902   $sth->finish;
903   $data2->{'max(id)'}++;
904   $sth=$dbh->prepare("Insert into aqbooksellers (id) values ($data2->{'max(id)'})");
905   $sth->execute;
906   $sth->finish;
907   $data->{'id'}=$data2->{'max(id)'};
908   $dbh->disconnect;
909   updatesup($data);
910   return($data->{'id'});
911 }
912
913
914 sub newitems {
915   my ($item, @barcodes) = @_;
916   my $dbh   = C4Connect;
917   my $query = "Select max(itemnumber) from items";
918   my $sth   = $dbh->prepare($query);
919   my $data;
920   my $itemnumber;
921   my $error;
922
923   $sth->execute;
924   $data       = $sth->fetchrow_hashref;
925   $itemnumber = $data->{'max(itemnumber)'} + 1;
926   $sth->finish;
927   
928   $item->{'booksellerid'}     = $dbh->quote($item->{'bookselletid'});
929   $item->{'homebranch'}       = $dbh->quote($item->{'homebranch'});
930   $item->{'price'}            = $dbh->quote($item->{'price'});
931   $item->{'replacementprice'} = $dbh->quote($item->{'replacementprice'});
932   $item->{'itemnotes'}        = $dbh->quote($item->{'itemnotes'});
933
934   foreach my $barcode (@barcodes) {
935     $barcode = uc($barcode);
936     $query   = "Insert into items set
937 itemnumber           = $itemnumber,
938 biblionumber         = $item->{'biblionumber'},
939 biblioitemnumber     = $item->{'biblioitemnumber'},
940 barcode              = $barcode,
941 booksellerid         = $item->{'booksellerid'},
942 dateaccessioned      = NOW(),
943 homebranch           = $item->{'homebranch'},
944 holdingbranch        = $item->{'homebranch'},
945 price                = $item->{'price'},
946 replacementprice     = $item->{'replacementprice'},
947 replacementpricedate = NOW(),
948 itemnotes            = $item->{'itemnotes'}";
949
950     if ($item->{'loan'}) {
951       $query .= ",
952 notforloan           = $item->{'loan'}";
953     } # if
954
955     $sth = $dbh->prepare($query);
956     $sth->execute;
957
958     $error .= $sth->errstr;
959
960     $sth->finish;
961     $itemnumber++;
962   } # for
963
964   $dbh->disconnect;
965   return($error);
966 }
967
968 sub checkitems{
969   my ($count,@barcodes)=@_;
970   my $dbh=C4Connect;
971   my $error;
972   for (my $i=0;$i<$count;$i++){
973     $barcodes[$i]=uc $barcodes[$i];
974     my $query="Select * from items where barcode='$barcodes[$i]'";
975     my $sth=$dbh->prepare($query);
976     $sth->execute;
977     if (my $data=$sth->fetchrow_hashref){
978       $error.=" Duplicate Barcode: $barcodes[$i]";
979     }
980     $sth->finish;
981   }
982   $dbh->disconnect;
983   return($error);
984 }
985
986 sub moditem {
987   my ($loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
988   my $dbh=C4Connect;
989   my $query="update items set biblioitemnumber=$bibitemnum,
990   barcode='$barcode',itemnotes='$notes'
991   where itemnumber=$itemnum";
992   if ($barcode eq ''){
993     $query="update items set biblioitemnumber=$bibitemnum,notforloan=$loan where itemnumber=$itemnum";
994   }
995   if ($lost ne ''){
996     $query="update items set biblioitemnumber=$bibitemnum,
997       barcode='$barcode',itemnotes='$notes',homebranch='$homebranch',
998       itemlost='$lost',wthdrawn='$wthdrawn' where itemnumber=$itemnum";
999   }
1000   if ($replacement ne ''){
1001     $query=~ s/ where/,replacementprice='$replacement' where/;
1002   }
1003
1004   my $sth=$dbh->prepare($query);
1005   $sth->execute;
1006   $sth->finish;
1007   $dbh->disconnect;
1008 }
1009
1010 sub updatecost{
1011   my($price,$rrp,$itemnum)=@_;
1012   my $dbh=C4Connect;
1013   my $query="update items set price='$price',replacementprice='$rrp'
1014   where itemnumber=$itemnum";
1015   my $sth=$dbh->prepare($query);
1016   $sth->execute;
1017   $sth->finish;
1018   $dbh->disconnect;
1019 }
1020 sub countitems{
1021   my ($bibitemnum)=@_;
1022   my $dbh=C4Connect;
1023   my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
1024   my $sth=$dbh->prepare($query);
1025   $sth->execute;
1026   my $data=$sth->fetchrow_hashref;
1027   $sth->finish;
1028   $dbh->disconnect;
1029   return($data->{'count(*)'});
1030 }
1031
1032 sub findall {
1033   my ($biblionumber)=@_;
1034   my $dbh=C4Connect;
1035   my $query="Select * from biblioitems,items,itemtypes where 
1036   biblioitems.biblionumber=$biblionumber 
1037   and biblioitems.biblioitemnumber=items.biblioitemnumber and
1038   itemtypes.itemtype=biblioitems.itemtype
1039   order by items.biblioitemnumber";
1040   my $sth=$dbh->prepare($query);
1041   $sth->execute;
1042   my @results;
1043   my $i;
1044   while (my $data=$sth->fetchrow_hashref){
1045     $results[$i]=$data;
1046     $i++;
1047   }
1048   $sth->finish;
1049   $dbh->disconnect;
1050   return(@results);
1051 }
1052
1053 sub needsmod{
1054   my ($bibitemnum,$itemtype)=@_;
1055   my $dbh=C4Connect;
1056   my $query="Select * from biblioitems where biblioitemnumber=$bibitemnum
1057   and itemtype='$itemtype'";
1058   my $sth=$dbh->prepare($query);
1059   $sth->execute;
1060   my $result=0;
1061   if (my $data=$sth->fetchrow_hashref){
1062     $result=1;
1063   }
1064   $sth->finish;
1065   $dbh->disconnect;
1066   return($result);
1067 }
1068
1069 sub delitem{
1070   my ($itemnum)=@_;
1071   my $dbh=C4Connect;
1072   my $query="select * from items where itemnumber=$itemnum";
1073   my $sth=$dbh->prepare($query);
1074   $sth->execute;
1075   my @data=$sth->fetchrow_array;
1076   $sth->finish;
1077   $query="Insert into deleteditems values (";
1078   foreach my $temp (@data){
1079     $query=$query."'$temp',";
1080   }
1081   $query=~ s/\,$/\)/;
1082 #  print $query;
1083   $sth=$dbh->prepare($query);
1084   $sth->execute;
1085   $sth->finish;
1086   $query = "Delete from items where itemnumber=$itemnum";
1087   $sth=$dbh->prepare($query);
1088   $sth->execute;
1089   $sth->finish;
1090   $dbh->disconnect;
1091 }
1092
1093 sub delbibitem{
1094   my ($itemnum)=@_;
1095   my $dbh=C4Connect;
1096   my $query="select * from biblioitems where biblioitemnumber=$itemnum";
1097   my $sth=$dbh->prepare($query);
1098   $sth->execute;
1099   if (my @data=$sth->fetchrow_array){
1100     $sth->finish;
1101     $query="Insert into deletedbiblioitems values (";
1102     foreach my $temp (@data){
1103       $temp=~ s/\'/\\\'/g;
1104       $query=$query."'$temp',";
1105     }
1106     $query=~ s/\,$/\)/;
1107 #   print $query;
1108     $sth=$dbh->prepare($query);
1109     $sth->execute;
1110     $sth->finish;
1111     $query = "Delete from biblioitems where biblioitemnumber=$itemnum";
1112     $sth=$dbh->prepare($query);
1113     $sth->execute;
1114     $sth->finish;
1115   }
1116   $sth->finish;
1117   $dbh->disconnect;
1118 }
1119
1120 sub delbiblio{
1121   my ($biblio)=@_;
1122   my $dbh=C4Connect;
1123   my $query="select * from biblio where biblionumber=$biblio";
1124   my $sth=$dbh->prepare($query);
1125   $sth->execute;
1126   if (my @data=$sth->fetchrow_array){
1127     $sth->finish;
1128     $query="Insert into deletedbiblio values (";
1129     foreach my $temp (@data){
1130       $temp=~ s/\'/\\\'/g;
1131       $query=$query."'$temp',";
1132     }
1133     $query=~ s/\,$/\)/;
1134 #   print $query;
1135     $sth=$dbh->prepare($query);
1136     $sth->execute;
1137     $sth->finish;
1138     $query = "Delete from biblio where biblionumber=$biblio";
1139     $sth=$dbh->prepare($query);
1140     $sth->execute;
1141     $sth->finish;
1142   }
1143
1144   $sth->finish;
1145   $dbh->disconnect;
1146 }
1147
1148 sub getitemtypes {
1149   my $dbh   = C4Connect;
1150   my $query = "select * from itemtypes";
1151   my $sth   = $dbh->prepare($query);
1152     # || die "Cannot prepare $query" . $dbh->errstr;
1153   my $count = 0;
1154   my @results;
1155   
1156   $sth->execute;
1157     # || die "Cannot execute $query\n" . $sth->errstr;
1158   while (my $data = $sth->fetchrow_hashref) {
1159     @results[$count] = $data;
1160     $count++;
1161   } # while
1162   
1163   $sth->finish;
1164   $dbh->disconnect;
1165   return($count, @results);
1166 } # sub getitemtypes
1167
1168
1169 sub getbiblio {
1170     my ($biblionumber) = @_;
1171     my $dbh   = C4Connect;
1172     my $query = "Select * from biblio where biblionumber = $biblionumber";
1173     my $sth   = $dbh->prepare($query);
1174       # || die "Cannot prepare $query\n" . $dbh->errstr;
1175     my $count = 0;
1176     my @results;
1177     
1178     $sth->execute;
1179       # || die "Cannot execute $query\n" . $sth->errstr;
1180     while (my $data = $sth->fetchrow_hashref) {
1181       $results[$count] = $data;
1182       $count++;
1183     } # while
1184     
1185     $sth->finish;
1186     $dbh->disconnect;
1187     return($count, @results);
1188 } # sub getbiblio
1189
1190
1191 sub getbiblioitem {
1192     my ($biblioitemnum) = @_;
1193     my $dbh   = C4Connect;
1194     my $query = "Select * from biblioitems where
1195 biblioitemnumber = $biblioitemnum";
1196     my $sth   = $dbh->prepare($query);
1197     my $count = 0;
1198     my @results;
1199
1200     $sth->execute;
1201
1202     while (my $data = $sth->fetchrow_hashref) {
1203         $results[$count] = $data;
1204         $count++;
1205     } # while
1206
1207     $sth->finish;
1208     $dbh->disconnect;
1209     return($count, @results);
1210 } # sub getbiblioitem
1211
1212
1213 sub getitemsbybiblioitem {
1214     my ($biblioitemnum) = @_;
1215     my $dbh   = C4Connect;
1216     my $query = "Select * from items, biblio where
1217 biblio.biblionumber = items.biblionumber and biblioitemnumber
1218 = $biblioitemnum";
1219     my $sth   = $dbh->prepare($query);
1220       # || die "Cannot prepare $query\n" . $dbh->errstr;
1221     my $count = 0;
1222     my @results;
1223     
1224     $sth->execute;
1225       # || die "Cannot execute $query\n" . $sth->errstr;
1226     while (my $data = $sth->fetchrow_hashref) {
1227       $results[$count] = $data;
1228       $count++;
1229     } # while
1230     
1231     $sth->finish;
1232     $dbh->disconnect;
1233     return($count, @results);
1234 } # sub getitemsbybiblioitem
1235
1236
1237 sub isbnsearch {
1238     my ($isbn) = @_;
1239     my $dbh   = C4Connect;
1240     my $count = 0;
1241     my $query;
1242     my $sth;
1243     my @results;
1244     
1245     $isbn  = $dbh->quote($isbn);
1246     $query = "Select * from biblioitems where isbn = $isbn";
1247     $sth   = $dbh->prepare($query);
1248     
1249     $sth->execute;
1250     while (my $data = $sth->fetchrow_hashref) {
1251         $results[$count] = $data;
1252         $count++;
1253     } # while
1254
1255     $sth->finish;
1256     $dbh->disconnect;
1257     return($count, @results);
1258 } # sub isbnsearch
1259
1260
1261 sub keywordsearch {
1262   my ($keywordlist) = @_;
1263   my $dbh   = C4Connect;
1264   my $query = "Select * from biblio where";
1265   my $count = 0;
1266   my $sth;
1267   my @results;
1268   my @keywords = split(/ +/, $keywordlist);
1269   my $keyword = shift(@keywords);
1270
1271   $keyword =~ s/%/\\%/g;
1272   $keyword =~ s/_/\\_/;
1273   $keyword = "%" . $keyword . "%";
1274   $keyword = $dbh->quote($keyword);
1275   $query  .= " (author like $keyword) or
1276 (title like $keyword) or (unititle like $keyword) or
1277 (notes like $keyword) or (seriestitle like $keyword) or
1278 (abstract like $keyword)";
1279
1280   foreach $keyword (@keywords) {
1281     $keyword =~ s/%/\\%/;
1282     $keyword =~ s/_/\\_/;
1283     $keyword = "%" . $keyword . "%";
1284     $keyword = $dbh->quote($keyword);
1285     $query  .= " or (author like $keyword) or
1286 (title like $keyword) or (unititle like $keyword) or 
1287 (notes like $keyword) or (seriestitle like $keyword) or
1288 (abstract like $keyword)";
1289   } # foreach
1290   
1291   $sth = $dbh->prepare($query);
1292   $sth->execute;
1293   
1294   while (my $data = $sth->fetchrow_hashref) {
1295     $results[$count] = $data;
1296     $count++;
1297   } # while
1298   
1299   $sth->finish;
1300   $dbh->disconnect;
1301   return($count, @results);
1302 } # sub keywordsearch
1303
1304
1305 sub websitesearch {
1306     my ($keywordlist) = @_;
1307     my $dbh   = C4Connect;
1308     my $query = "Select distinct biblio.* from biblio, biblioitems where
1309 biblio.biblionumber = biblioitems.biblionumber and (";
1310     my $count = 0;
1311     my $sth;
1312     my @results;
1313     my @keywords = split(/ +/, $keywordlist);
1314     my $keyword = shift(@keywords);
1315
1316     $keyword =~ s/%/\\%/g;
1317     $keyword =~ s/_/\\_/;
1318     $keyword = "%" . $keyword . "%";
1319     $keyword = $dbh->quote($keyword);
1320     $query  .= " (url like $keyword)";
1321
1322     foreach $keyword (@keywords) {
1323         $keyword =~ s/%/\\%/;
1324         $keyword =~ s/_/\\_/;
1325         $keyword = "%" . $keyword . "%";
1326         $keyword = $dbh->quote($keyword);
1327         $query  .= " or (url like $keyword)";
1328     } # foreach
1329
1330     $query .= ")";
1331     $sth    = $dbh->prepare($query);
1332     $sth->execute;
1333
1334     while (my $data = $sth->fetchrow_hashref) {
1335         $results[$count] = $data;
1336         $count++;
1337     } # while
1338
1339     $sth->finish;
1340     $dbh->disconnect;
1341     return($count, @results);
1342 } # sub websitesearch
1343
1344
1345 END { }       # module clean-up code here (global destructor)