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