Modifications for new acqui.simple
[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   $sth->execute;
345   my @results;
346   my $i=0;
347   while (my $data=$sth->fetchrow_hashref){
348     $results[$i]=$data;
349     $i++;
350   }
351   $sth->finish;
352   $dbh->disconnect;
353   return($i,@results);
354 }
355
356 sub bookfundbreakdown {
357   my ($id)=@_;
358   my $dbh=C4Connect;
359   my $query="Select quantity,datereceived,freight,unitprice,listprice,ecost,quantityreceived,subscription
360   from aqorders,aqorderbreakdown where bookfundid='$id' and 
361   aqorders.ordernumber=aqorderbreakdown.ordernumber and ((budgetdate >=
362   '2001-07-01' and budgetdate <'2002-07-01') or
363   (datereceived >= '2001-07-01' and datereceived < '2002-07-01'))
364   and (datecancellationprinted is NULL or
365   datecancellationprinted='0000-00-00')";
366   my $sth=$dbh->prepare($query);
367   $sth->execute;
368   my $comtd=0;
369   my $spent=0;
370   while (my $data=$sth->fetchrow_hashref){
371     if ($data->{'subscription'} == 1){
372       $spent+=$data->{'quantity'}*$data->{'unitprice'};
373     } else {
374       my $leftover=$data->{'quantity'}-$data->{'quantityreceived'};
375       $comtd+=($data->{'ecost'})*$leftover;
376       $spent+=($data->{'unitprice'})*$data->{'quantityreceived'};
377     }
378   }
379   $sth->finish;
380   $dbh->disconnect;
381   return($spent,$comtd);
382 }
383       
384
385 sub newbiblio {
386   my ($biblio) = @_;
387   my $dbh    = &C4Connect;
388   my $query  = "Select max(biblionumber) from biblio";
389   my $sth    = $dbh->prepare($query);
390   $sth->execute;
391   my $data   = $sth->fetchrow_arrayref;
392   my $bibnum = $$data[0] + 1;
393   my $series = 0;
394
395   $biblio->{'title'}       = $dbh->quote($biblio->{'title'});
396   $biblio->{'author'}      = $dbh->quote($biblio->{'author'});
397   $biblio->{'copyright'}   = $dbh->quote($biblio->{'copyright'});
398   $biblio->{'seriestitle'} = $dbh->quote($biblio->{'seriestitle'});
399   $biblio->{'notes'}       = $dbh->quote($biblio->{'notes'});
400   $biblio->{'abstract'}    = $dbh->quote($biblio->{'abstract'});
401   if ($biblio->{'seriestitle'}) { $series = 1 };
402
403   $sth->finish;
404   $query = "insert into biblio set
405 biblionumber  = $bibnum,
406 title         = $biblio->{'title'},
407 author        = $biblio->{'author'},
408 copyrightdate = $biblio->{'copyright'},
409 series        = $series;
410 seriestitle   = $biblio->{'seriestitle'},
411 notes         = $biblio->{'notes'},
412 abstract      = $biblio->{'abstract'}";
413
414   $sth = $dbh->prepare($query);
415 #  print $query;
416   $sth->execute;
417
418   $sth->finish;
419   $dbh->disconnect;
420   return($bibnum);
421 }
422
423 sub modbiblio {
424   my ($bibnum, $title, $author, $copyright, $seriestitle, $serial, $unititle, $notes)=@_;
425   my $dbh   = C4Connect;
426   my $query = "Update biblio set
427 title         = '$title',
428 author        = '$author',
429 copyrightdate = '$copyright',
430 seriestitle   = '$seriestitle',
431 serial        = '$serial',
432 unititle      = '$unititle',
433 notes         = '$notes'
434 where biblionumber = $bibnum";
435   my $sth   = $dbh->prepare($query);
436
437   $sth->execute;
438
439   $sth->finish;
440   $dbh->disconnect;
441   return($bibnum);
442 }
443
444 sub modsubtitle {
445   my ($bibnum,$subtitle)=@_;
446   my $dbh=C4Connect;
447   my $query="update bibliosubtitle set subtitle='$subtitle' where biblionumber=$bibnum";
448   my $sth=$dbh->prepare($query);
449   $sth->execute;
450   $sth->finish;
451   $dbh->disconnect;
452 }
453
454 sub modaddauthor {
455     my ($bibnum, $author) = @_;
456     my $dbh   = C4Connect;
457     my $query = "Delete from additionalauthors where biblionumber = $bibnum";
458     my $sth = $dbh->prepare($query);
459
460     $sth->execute;
461     $sth->finish;
462
463     if ($author ne '') {
464         $query = "Insert into additionalauthors set
465 author       = '$author',
466 biblionumber = '$bibnum'";
467         $sth   = $dbh->prepare($query);
468
469         $sth->execute;
470
471         $sth->finish;
472     } # if
473
474   $dbh->disconnect;
475 } # sub modaddauthor
476
477
478 sub modsubject {
479   my ($bibnum,$force,@subject)=@_;
480   my $dbh=C4Connect;
481   my $count=@subject;
482   my $error;
483   for (my $i=0;$i<$count;$i++){
484     $subject[$i]=~ s/^ //g;
485     $subject[$i]=~ s/ $//g;
486     my $query="select * from catalogueentry where entrytype='s' and
487     catalogueentry='$subject[$i]'";
488     my $sth=$dbh->prepare($query);
489     $sth->execute;
490     if (my $data=$sth->fetchrow_hashref){
491       
492     } else {
493       if ($force eq $subject[$i]){
494          #subject not in aut, chosen to force anway
495          #so insert into cataloguentry so its in auth file
496          $query="Insert into catalogueentry (entrytype,catalogueentry)
497          values ('s','$subject[$i]')";
498          my $sth2=$dbh->prepare($query);
499 #        print $query;
500          $sth2->execute;
501          $sth2->finish;
502       } else {      
503         $error="$subject[$i]\n does not exist in the subject authority file";
504         $query= "Select * from catalogueentry where
505         entrytype='s' and (catalogueentry like '$subject[$i] %' or 
506         catalogueentry like '% $subject[$i] %' or catalogueentry like
507         '% $subject[$i]')";
508         my $sth2=$dbh->prepare($query);
509 #        print $query;
510         $sth2->execute;
511         while (my $data=$sth2->fetchrow_hashref){
512           $error=$error."<br>$data->{'catalogueentry'}";
513         }
514         $sth2->finish;
515 #       $error=$error."<br>$query";
516      }
517    }
518     $sth->finish;
519   }
520   if ($error eq ''){  
521     my $query="Delete from bibliosubject where biblionumber=$bibnum";
522 #  print $query;
523     my $sth=$dbh->prepare($query);
524 #  print $query;
525     $sth->execute;
526     $sth->finish;
527     for (my $i=0;$i<$count;$i++){
528       $sth=$dbh->prepare("Insert into bibliosubject values ('$subject[$i]',$bibnum)");
529 #     print $subject[$i];
530       $sth->execute;
531       $sth->finish;
532     }
533   }
534   $dbh->disconnect;
535   return($error);
536 }
537
538 sub modbibitem {
539   my ($bibitemnum,$itemtype,$isbn,$publishercode,$publicationdate,$classification,$dewey,$subclass,$illus,$pages,$volumeddesc,$notes,$size,$place)=@_;
540   my $dbh=C4Connect;
541   my $query="update biblioitems set itemtype='$itemtype',
542   isbn='$isbn',publishercode='$publishercode',publicationyear='$publicationdate',
543   classification='$classification',dewey='$dewey',subclass='$subclass',illus='$illus',
544   pages='$pages',volumeddesc='$volumeddesc',notes='$notes',size='$size',place='$place'
545   where
546   biblioitemnumber=$bibitemnum";
547   my $sth=$dbh->prepare($query);
548 #    print $query;
549   $sth->execute;
550   $sth->finish;
551   $dbh->disconnect;
552 }
553
554 sub modnote {
555   my ($bibitemnum,$note)=@_;
556   my $dbh=C4Connect;
557   my $query="update biblioitems set notes='$note' where
558   biblioitemnumber='$bibitemnum'";
559   my $sth=$dbh->prepare($query);
560   $sth->execute;
561   $sth->finish;
562   $dbh->disconnect;
563 }
564
565 sub newbiblioitem {
566   my ($biblioitem) = @_;
567   my $dbh   = C4Connect;
568   my $query = "Select max(biblioitemnumber) from biblioitems";
569   my $sth   = $dbh->prepare($query);
570   my $data;
571   my $bibitemnum;
572   
573   $biblioitem->{'volume'}          = $dbh->quote($biblioitem->{'volume'});
574   $biblioitem->{'number'}          = $dbh->quote($biblioitem->{'number'});
575   $biblioitem->{'classification'}  = $dbh->quote($biblioitem->{'classification'});
576   $biblioitem->{'itemtype'}        = $dbh->quote($biblioitem->{'itemtype'});
577   $biblioitem->{'url'}             = $dbh->quote($biblioitem->{'url'});
578   $biblioitem->{'isbn'}            = $dbh->quote($biblioitem->{'isbn'});
579   $biblioitem->{'issn'}            = $dbh->quote($biblioitem->{'issn'});
580   $biblioitem->{'dewey'}           = $dbh->quote($biblioitem->{'dewey'});
581   $biblioitem->{'subclass'}        = $dbh->quote($biblioitem->{'subclass'});
582   $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
583   $biblioitem->{'publishercode'}   = $dbh->quote($biblioitem->{'publishercode'});
584   $biblioitem->{'volumedate'}      = $dbh->quote($biblioitem->{'volumedate'});
585   $biblioitem->{'volumeddesc'}     = $dbh->quote($biblioitem->{'volumeddesc'});  $biblioitem->{'illus'}            = $dbh->quote($biblioitem->{'illus'});
586   $biblioitem->{'pages'}           = $dbh->quote($biblioitem->{'pages'});
587   $biblioitem->{'notes'}           = $dbh->quote($biblioitem->{'notes'});
588   $biblioitem->{'size'}            = $dbh->quote($biblioitem->{'size'});
589   $biblioitem->{'place'}           = $dbh->quote($biblioitem->{'place'});
590   
591   $sth->execute;
592   $data       = $sth->fetchrow_arrayref;
593   $bibitemnum = $$data[0] + 1;
594
595   $sth->finish;
596
597   $query = "insert into biblioitems set
598 biblioitemnumber = $bibitemnum,
599 biblionumber     = $biblioitem->{'biblionumber'},
600 volume           = $biblioitem->{'volume'},
601 number           = $biblioitem->{'number'},
602 classification   = $biblioitem->{'classification'},
603 itemtype         = $biblioitem->{'itemtype'},
604 url              = $biblioitem->{'url'},
605 isbn             = $biblioitem->{'isbn'},
606 issn             = $biblioitem->{'issn'},
607 dewey            = $biblioitem->{'dewey'},
608 subclass         = $biblioitem->{'subclass'},
609 publicationyear  = $biblioitem->{'publicationyear'},
610 publishercode    = $biblioitem->{'publishercode'},
611 volumedate       = $biblioitem->{'volumedate'},
612 volumeddesc      = $biblioitem->{'volumeddesc'},
613 illus            = $biblioitem->{'illus'},
614 pages            = $biblioitem->{'pages'},
615 notes            = $biblioitem->{'notes'},
616 size             = $biblioitem->{'size'},
617 place            = $biblioitem->{'place'}";
618
619   $sth = $dbh->prepare($query);
620   $sth->execute;
621
622   $sth->finish;
623   $dbh->disconnect;
624   return($bibitemnum);
625 }
626
627 sub newsubject {
628   my ($bibnum)=@_;
629   my $dbh=C4Connect;
630   my $query="insert into bibliosubject (biblionumber) values
631   ($bibnum)";
632   my $sth=$dbh->prepare($query);
633 #  print $query;
634   $sth->execute;
635   $sth->finish;
636   $dbh->disconnect;
637 }
638
639 sub newsubtitle {
640   my ($bibnum, $subtitle) = @_;
641   my $dbh   = C4Connect;
642   $subtitle = $dbh->quote($subtitle);
643   my $query = "insert into bibliosubtitle set
644 biblionumber = $bibnum,
645 subtitle = $subtitle";
646   my $sth   = $dbh->prepare($query);
647
648   $sth->execute;
649
650   $sth->finish;
651   $dbh->disconnect;
652 }
653
654 sub neworder {
655   my ($bibnum,$title,$ordnum,$basket,$quantity,$listprice,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$sub,$invoice)=@_;
656   if ($budget eq 'now'){
657     $budget="now()";
658   } else {
659     $budget="'2001-07-01'";
660   }
661   if ($sub eq 'yes'){
662     $sub=1;
663   } else {
664     $sub=0;
665   }
666   my $dbh=C4Connect;
667   my $query="insert into aqorders (biblionumber,title,basketno,
668   quantity,listprice,booksellerid,entrydate,requisitionedby,authorisedby,notes,
669   biblioitemnumber,rrp,ecost,gst,budgetdate,unitprice,subscription,booksellerinvoicenumber)
670
671   values
672   ($bibnum,'$title',$basket,$quantity,$listprice,'$supplier',now(),
673   '$who','$who','$notes',$bibitemnum,'$rrp','$ecost','$gst',$budget,'$cost',
674   '$sub','$invoice')";
675   my $sth=$dbh->prepare($query);
676 #  print $query;
677   $sth->execute;
678   $sth->finish;
679   $query="select * from aqorders where
680   biblionumber=$bibnum and basketno=$basket and ordernumber >=$ordnum";
681   $sth=$dbh->prepare($query);
682   $sth->execute;
683   my $data=$sth->fetchrow_hashref;
684   $sth->finish;
685   $ordnum=$data->{'ordernumber'};
686   $query="insert into aqorderbreakdown (ordernumber,bookfundid) values
687   ($ordnum,'$bookfund')";
688   $sth=$dbh->prepare($query);
689 #  print $query;
690   $sth->execute;
691   $sth->finish;
692   $dbh->disconnect;
693 }
694
695 sub delorder {
696   my ($bibnum,$ordnum)=@_;
697   my $dbh=C4Connect;
698   my $query="update aqorders set datecancellationprinted=now()
699   where biblionumber='$bibnum' and
700   ordernumber='$ordnum'";
701   my $sth=$dbh->prepare($query);
702   #print $query;
703   $sth->execute;
704   $sth->finish;
705   my $count=itemcount($bibnum);
706   if ($count == 0){
707     delbiblio($bibnum);
708   }
709   $dbh->disconnect;
710 }
711
712 sub modorder {
713   my ($title,$ordnum,$quantity,$listprice,$bibnum,$basketno,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$invoice)=@_;
714   my $dbh=C4Connect;
715   my $query="update aqorders set title='$title',
716   quantity='$quantity',listprice='$listprice',basketno='$basketno', 
717   rrp='$rrp',ecost='$ecost',unitprice='$cost',
718   booksellerinvoicenumber='$invoice'
719   where
720   ordernumber=$ordnum and biblionumber=$bibnum";
721   my $sth=$dbh->prepare($query);
722 #  print $query;
723   $sth->execute;
724   $sth->finish;
725   $query="update aqorderbreakdown set bookfundid=$bookfund where
726   ordernumber=$ordnum";
727   $sth=$dbh->prepare($query);
728 #  print $query;
729   $sth->execute;
730   $sth->finish;
731   $dbh->disconnect;
732 }
733
734 sub newordernum {
735   my $dbh=C4Connect;
736   my $query="Select max(ordernumber) from aqorders";
737   my $sth=$dbh->prepare($query);
738   $sth->execute;
739   my $data=$sth->fetchrow_arrayref;
740   my $ordnum=$$data[0];
741   $ordnum++;
742   $sth->finish;
743   $dbh->disconnect;
744   return($ordnum);
745 }
746
747 sub receiveorder {
748   my ($biblio,$ordnum,$quantrec,$user,$cost,$invoiceno,$bibitemno,$freight,$bookfund,$rrp)=@_;
749   my $dbh=C4Connect;
750   my $query="update aqorders set quantityreceived='$quantrec',
751   datereceived=now(),booksellerinvoicenumber='$invoiceno',
752   biblioitemnumber=$bibitemno,unitprice='$cost',freight='$freight',
753   rrp='$rrp'
754   where biblionumber=$biblio and ordernumber=$ordnum
755   ";
756 #  print $query;
757   my $sth=$dbh->prepare($query);
758   $sth->execute;
759   $sth->finish;
760   $query="update aqorderbreakdown set bookfundid=$bookfund where
761   ordernumber=$ordnum";
762   $sth=$dbh->prepare($query);
763 #  print $query;
764   $sth->execute;
765   $sth->finish;  
766   $dbh->disconnect;
767 }
768 sub updaterecorder{
769   my($biblio,$ordnum,$user,$cost,$bookfund,$rrp)=@_;
770   my $dbh=C4Connect;
771   my $query="update aqorders set
772   unitprice='$cost', rrp='$rrp'
773   where biblionumber=$biblio and ordernumber=$ordnum
774   ";
775 #  print $query;
776   my $sth=$dbh->prepare($query);
777   $sth->execute;
778   $sth->finish;
779   $query="update aqorderbreakdown set bookfundid=$bookfund where
780   ordernumber=$ordnum";
781   $sth=$dbh->prepare($query);
782 #  print $query;
783   $sth->execute;
784   $sth->finish;  
785   $dbh->disconnect;
786 }
787
788 sub curconvert {
789   my ($currency,$price)=@_;
790   my $dbh=C4Connect;
791   my $query="Select rate from currency where currency='$currency'";
792   my $sth=$dbh->prepare($query);
793   $sth->execute;
794   my $data=$sth->fetchrow_hashref;
795   $sth->finish;
796   $dbh->disconnect;
797   my $cur=$data->{'rate'};
798   if ($cur==0){
799     $cur=1;
800   }
801   my $price=$price / $cur;
802   return($price);
803 }
804
805 sub getcurrencies {
806   my $dbh=C4Connect;
807   my $query="Select * from currency";
808   my $sth=$dbh->prepare($query);
809   $sth->execute;
810   my @results;
811   my $i=0;
812   while (my $data=$sth->fetchrow_hashref){
813     $results[$i]=$data;
814     $i++;
815   }
816   $sth->finish;
817   $dbh->disconnect;
818   return($i,\@results);
819
820
821 sub getcurrency {
822   my ($cur)=@_;
823   my $dbh=C4Connect;
824   my $query="Select * from currency where currency='$cur'";
825   my $sth=$dbh->prepare($query);
826   $sth->execute;
827
828   my $data=$sth->fetchrow_hashref;
829   $sth->finish;
830   $dbh->disconnect;
831   return($data);
832
833
834 sub updatecurrencies {
835   my ($currency,$rate)=@_;
836   my $dbh=C4Connect;
837   my $query="update currency set rate=$rate where currency='$currency'";
838   my $sth=$dbh->prepare($query);
839   $sth->execute;
840   $sth->finish;
841   $dbh->disconnect;
842
843
844 sub updatesup {
845    my ($data)=@_;
846    my $dbh=C4Connect;
847    my $query="Update aqbooksellers set
848    name='$data->{'name'}',address1='$data->{'address1'}',address2='$data->{'address2'}',
849    address3='$data->{'address3'}',address4='$data->{'address4'}',postal='$data->{'postal'}',
850    phone='$data->{'phone'}',fax='$data->{'fax'}',url='$data->{'url'}',
851    contact='$data->{'contact'}',contpos='$data->{'contpos'}',
852    contphone='$data->{'contphone'}', contfax='$data->{'contfax'}', contaltphone=
853    '$data->{'contaltphone'}', contemail='$data->{'contemail'}', contnotes=
854    '$data->{'contnotes'}', active=$data->{'active'},
855    listprice='$data->{'listprice'}', invoiceprice='$data->{'invoiceprice'}',
856    gstreg=$data->{'gstreg'}, listincgst=$data->{'listincgst'},
857    invoiceincgst=$data->{'invoiceincgst'}, specialty='$data->{'specialty'}',
858    discount='$data->{'discount'}',invoicedisc='$data->{'invoicedisc'}',
859    nocalc='$data->{'nocalc'}'
860    where id='$data->{'id'}'";
861    my $sth=$dbh->prepare($query);
862    $sth->execute;
863    $sth->finish;
864    $dbh->disconnect;
865 #   print $query;
866 }
867
868 sub insertsup {
869   my ($data)=@_;
870   my $dbh=C4Connect;
871   my $sth=$dbh->prepare("Select max(id) from aqbooksellers");
872   $sth->execute;
873   my $data2=$sth->fetchrow_hashref;
874   $sth->finish;
875   $data2->{'max(id)'}++;
876   $sth=$dbh->prepare("Insert into aqbooksellers (id) values ($data2->{'max(id)'})");
877   $sth->execute;
878   $sth->finish;
879   $data->{'id'}=$data2->{'max(id)'};
880   $dbh->disconnect;
881   updatesup($data);
882   return($data->{'id'});
883 }
884
885
886 sub newitems {
887   my ($item, @barcodes) = @_;
888   my $dbh   = C4Connect;
889   my $query = "Select max(itemnumber) from items";
890   my $sth   = $dbh->prepare($query);
891   my $data;
892   my $itemnumber;
893   my $error;
894
895   $sth->execute;
896   $data       = $sth->fetchrow_hashref;
897   $itemnumber = $data->{'max(itemnumber)'} + 1;
898   $sth->finish;
899   
900   $item->{'booksellerid'}     = $dbh->quote($item->{'bookselletid'});
901   $item->{'homebranch'}       = $dbh->quote($item->{'homebranch'});
902   $item->{'price'}            = $dbh->quote($item->{'price'});
903   $item->{'replacementprice'} = $dbh->quote($item->{'replacementprice'});
904   $item->{'itemnotes'}        = $dbh->quote($item->{'itemnotes'});
905
906   foreach my $barcode (@barcodes) {
907     $barcode = uc($barcode);
908     $query   = "Insert into items set
909 itemnumber           = $itemnumber,
910 biblionumber         = $item->{'biblionumber'},
911 biblioitemnumber     = $item->{'biblioitemnumber'},
912 barcode              = $barcode,
913 booksellerid         = $item->{'booksellerid'},
914 dateaccessioned      = NOW(),
915 homebranch           = $item->{'branch'},
916 holdingbranch        = $item->{'branch'},
917 price                = $item->{'price'},
918 replacementprice     = $item->{'replacementprice'},
919 replacementpricedate = NOW(),
920 notforloan           = $item->{'loan'},
921 itemnotes            = $item->{'itemnotes'}";
922
923     $sth = $dbh->prepare($query);
924     $sth->execute;
925
926     $error .= $sth->errstr;
927
928     $sth->finish;
929     $itemnumber++;
930   } # for
931
932   $dbh->disconnect;
933   return($error);
934 }
935
936 sub checkitems{
937   my ($count,@barcodes)=@_;
938   my $dbh=C4Connect;
939   my $error;
940   for (my $i=0;$i<$count;$i++){
941     $barcodes[$i]=uc $barcodes[$i];
942     my $query="Select * from items where barcode='$barcodes[$i]'";
943     my $sth=$dbh->prepare($query);
944     $sth->execute;
945     if (my $data=$sth->fetchrow_hashref){
946       $error.=" Duplicate Barcode: $barcodes[$i]";
947     }
948     $sth->finish;
949   }
950   $dbh->disconnect;
951   return($error);
952 }
953
954 sub moditem {
955   my ($loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
956   my $dbh=C4Connect;
957   my $query="update items set biblioitemnumber=$bibitemnum,
958   barcode='$barcode',itemnotes='$notes'
959   where itemnumber=$itemnum";
960   if ($barcode eq ''){
961     $query="update items set biblioitemnumber=$bibitemnum,notforloan=$loan where itemnumber=$itemnum";
962   }
963   if ($lost ne ''){
964     $query="update items set biblioitemnumber=$bibitemnum,
965       barcode='$barcode',itemnotes='$notes',homebranch='$homebranch',
966       itemlost='$lost',wthdrawn='$wthdrawn' where itemnumber=$itemnum";
967   }
968   if ($replacement ne ''){
969     $query=~ s/ where/,replacementprice='$replacement' where/;
970   }
971
972   my $sth=$dbh->prepare($query);
973   $sth->execute;
974   $sth->finish;
975   $dbh->disconnect;
976 }
977
978 sub updatecost{
979   my($price,$rrp,$itemnum)=@_;
980   my $dbh=C4Connect;
981   my $query="update items set price='$price',replacementprice='$rrp'
982   where itemnumber=$itemnum";
983   my $sth=$dbh->prepare($query);
984   $sth->execute;
985   $sth->finish;
986   $dbh->disconnect;
987 }
988 sub countitems{
989   my ($bibitemnum)=@_;
990   my $dbh=C4Connect;
991   my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
992   my $sth=$dbh->prepare($query);
993   $sth->execute;
994   my $data=$sth->fetchrow_hashref;
995   $sth->finish;
996   $dbh->disconnect;
997   return($data->{'count(*)'});
998 }
999
1000 sub findall {
1001   my ($biblionumber)=@_;
1002   my $dbh=C4Connect;
1003   my $query="Select * from biblioitems,items,itemtypes where 
1004   biblioitems.biblionumber=$biblionumber 
1005   and biblioitems.biblioitemnumber=items.biblioitemnumber and
1006   itemtypes.itemtype=biblioitems.itemtype
1007   order by items.biblioitemnumber";
1008   my $sth=$dbh->prepare($query);
1009   $sth->execute;
1010   my @results;
1011   my $i;
1012   while (my $data=$sth->fetchrow_hashref){
1013     $results[$i]=$data;
1014     $i++;
1015   }
1016   $sth->finish;
1017   $dbh->disconnect;
1018   return(@results);
1019 }
1020
1021 sub needsmod{
1022   my ($bibitemnum,$itemtype)=@_;
1023   my $dbh=C4Connect;
1024   my $query="Select * from biblioitems where biblioitemnumber=$bibitemnum
1025   and itemtype='$itemtype'";
1026   my $sth=$dbh->prepare($query);
1027   $sth->execute;
1028   my $result=0;
1029   if (my $data=$sth->fetchrow_hashref){
1030     $result=1;
1031   }
1032   $sth->finish;
1033   $dbh->disconnect;
1034   return($result);
1035 }
1036
1037 sub delitem{
1038   my ($itemnum)=@_;
1039   my $dbh=C4Connect;
1040   my $query="select * from items where itemnumber=$itemnum";
1041   my $sth=$dbh->prepare($query);
1042   $sth->execute;
1043   my @data=$sth->fetchrow_array;
1044   $sth->finish;
1045   $query="Insert into deleteditems values (";
1046   foreach my $temp (@data){
1047     $query=$query."'$temp',";
1048   }
1049   $query=~ s/\,$/\)/;
1050 #  print $query;
1051   $sth=$dbh->prepare($query);
1052   $sth->execute;
1053   $sth->finish;
1054   $query = "Delete from items where itemnumber=$itemnum";
1055   $sth=$dbh->prepare($query);
1056   $sth->execute;
1057   $sth->finish;
1058   $dbh->disconnect;
1059 }
1060
1061 sub delbibitem{
1062   my ($itemnum)=@_;
1063   my $dbh=C4Connect;
1064   my $query="select * from biblioitems where biblioitemnumber=$itemnum";
1065   my $sth=$dbh->prepare($query);
1066   $sth->execute;
1067   if (my @data=$sth->fetchrow_array){
1068     $sth->finish;
1069     $query="Insert into deletedbiblioitems values (";
1070     foreach my $temp (@data){
1071       $temp=~ s/\'/\\\'/g;
1072       $query=$query."'$temp',";
1073     }
1074     $query=~ s/\,$/\)/;
1075 #   print $query;
1076     $sth=$dbh->prepare($query);
1077     $sth->execute;
1078     $sth->finish;
1079     $query = "Delete from biblioitems where biblioitemnumber=$itemnum";
1080     $sth=$dbh->prepare($query);
1081     $sth->execute;
1082     $sth->finish;
1083   }
1084   $sth->finish;
1085   $dbh->disconnect;
1086 }
1087
1088 sub delbiblio{
1089   my ($biblio)=@_;
1090   my $dbh=C4Connect;
1091   my $query="select * from biblio where biblionumber=$biblio";
1092   my $sth=$dbh->prepare($query);
1093   $sth->execute;
1094   if (my @data=$sth->fetchrow_array){
1095     $sth->finish;
1096     $query="Insert into deletedbiblio values (";
1097     foreach my $temp (@data){
1098       $temp=~ s/\'/\\\'/g;
1099       $query=$query."'$temp',";
1100     }
1101     $query=~ s/\,$/\)/;
1102 #   print $query;
1103     $sth=$dbh->prepare($query);
1104     $sth->execute;
1105     $sth->finish;
1106     $query = "Delete from biblio where biblionumber=$biblio";
1107     $sth=$dbh->prepare($query);
1108     $sth->execute;
1109     $sth->finish;
1110   }
1111
1112   $sth->finish;
1113   $dbh->disconnect;
1114 }
1115
1116 sub getitemtypes {
1117   my $dbh   = C4Connect;
1118   my $query = "select * from itemtypes";
1119   my $sth   = $dbh->prepare($query);
1120     # || die "Cannot prepare $query" . $dbh->errstr;
1121   my $count = 0;
1122   my @results;
1123   
1124   $sth->execute;
1125     # || die "Cannot execute $query\n" . $sth->errstr;
1126   while (my $data = $sth->fetchrow_hashref) {
1127     @results[$count] = $data;
1128     $count++;
1129   } # while
1130   
1131   $sth->finish;
1132   $dbh->disconnect;
1133   return($count, @results);
1134 } # sub getitemtypes
1135
1136
1137 sub getbiblio {
1138     my ($biblionumber) = @_;
1139     my $dbh   = C4Connect;
1140     my $query = "Select * from biblio where biblionumber = $biblionumber";
1141     my $sth   = $dbh->prepare($query);
1142       # || die "Cannot prepare $query\n" . $dbh->errstr;
1143     my $count = 0;
1144     my @results;
1145     
1146     $sth->execute;
1147       # || die "Cannot execute $query\n" . $sth->errstr;
1148     while (my $data = $sth->fetchrow_hashref) {
1149       $results[$count] = $data;
1150       $count++;
1151     } # while
1152     
1153     $sth->finish;
1154     $dbh->disconnect;
1155     return($count, @results);
1156 } # sub getbiblio
1157
1158
1159 sub getbiblioitem {
1160     my ($biblioitemnum) = @_;
1161     my $dbh   = C4Connect;
1162     my $query = "Select * from biblioitems where
1163 biblioitemnumber = $biblioitemnum";
1164     my $sth   = $dbh->prepare($query);
1165     my $count = 0;
1166     my @results;
1167
1168     $sth->execute;
1169
1170     while (my $data = $sth->fetchrow_hashref) {
1171         $results[$count] = $data;
1172         $count++;
1173     } # while
1174
1175     $sth->finish;
1176     $dbh->disconnect;
1177     return($count, @results);
1178 } # sub getbiblioitem
1179
1180
1181 sub getitemsbybiblioitem {
1182     my ($biblioitemnum) = @_;
1183     my $dbh   = C4Connect;
1184     my $query = "Select * from items, biblio where
1185 biblio.biblionumber = items.biblionumber and biblioitemnumber
1186 = $biblioitemnum";
1187     my $sth   = $dbh->prepare($query);
1188       # || die "Cannot prepare $query\n" . $dbh->errstr;
1189     my $count = 0;
1190     my @results;
1191     
1192     $sth->execute;
1193       # || die "Cannot execute $query\n" . $sth->errstr;
1194     while (my $data = $sth->fetchrow_hashref) {
1195       $results[$count] = $data;
1196       $count++;
1197     } # while
1198     
1199     $sth->finish;
1200     $dbh->disconnect;
1201     return($count, @results);
1202 } # sub getitemsbybiblioitem
1203
1204
1205 sub isbnsearch {
1206     my ($isbn) = @_;
1207     my $dbh   = C4Connect;
1208     my $count = 0;
1209     my $query;
1210     my $sth;
1211     my @results;
1212     
1213     $isbn  = $dbh->quote($isbn);
1214     $query = "Select * from biblioitems where isbn = $isbn";
1215     $sth   = $dbh->prepare($query);
1216     
1217     $sth->execute;
1218     while (my $data = $sth->fetchrow_hashref) {
1219         $results[$count] = $data;
1220         $count++;
1221     } # while
1222
1223     $sth->finish;
1224     $dbh->disconnect;
1225     return($count, @results);
1226 } # sub isbnsearch
1227
1228
1229 sub keywordsearch {
1230   my ($keywordlist) = @_;
1231   my $dbh   = C4Connect;
1232   my $query = "Select * from biblio where";
1233   my $count = 0;
1234   my $sth;
1235   my @results;
1236   my @keywords = split(/ +/, $keywordlist);
1237   my $keyword = shift(@keywords);
1238
1239   $keyword =~ s/%/\\%/g;
1240   $keyword =~ s/_/\\_/;
1241   $keyword = "%" . $keyword . "%";
1242   $keyword = $dbh->quote($keyword);
1243   $query  .= " (author like $keyword) or
1244 (title like $keyword) or (unititle like $keyword) or
1245 (notes like $keyword) or (seriestitle like $keyword) or
1246 (abstract like $keyword)";
1247
1248   foreach $keyword (@keywords) {
1249     $keyword =~ s/%/\\%/;
1250     $keyword =~ s/_/\\_/;
1251     $keyword = "%" . $keyword . "%";
1252     $keyword = $dbh->quote($keyword);
1253     $query  .= " or (author like $keyword) or
1254 (title like $keyword) or (unititle like $keyword) or 
1255 (notes like $keyword) or (seriestitle like $keyword) or
1256 (abstract like $keyword)";
1257   } # foreach
1258   
1259   $sth = $dbh->prepare($query);
1260   $sth->execute;
1261   
1262   while (my $data = $sth->fetchrow_hashref) {
1263     $results[$count] = $data;
1264     $count++;
1265   } # while
1266   
1267   $sth->finish;
1268   $dbh->disconnect;
1269   return($count, @results);
1270 } # sub keywordsearch
1271
1272
1273 sub websitesearch {
1274     my ($keywordlist) = @_;
1275     my $dbh   = C4Connect;
1276     my $query = "Select distinct biblio.* from biblio, biblioitems where
1277 biblio.biblionumber = biblioitems.biblionumber and (";
1278     my $count = 0;
1279     my $sth;
1280     my @results;
1281     my @keywords = split(/ +/, $keywordlist);
1282     my $keyword = shift(@keywords);
1283
1284     $keyword =~ s/%/\\%/g;
1285     $keyword =~ s/_/\\_/;
1286     $keyword = "%" . $keyword . "%";
1287     $keyword = $dbh->quote($keyword);
1288     $query  .= " (url like $keyword)";
1289
1290     foreach $keyword (@keywords) {
1291         $keyword =~ s/%/\\%/;
1292         $keyword =~ s/_/\\_/;
1293         $keyword = "%" . $keyword . "%";
1294         $keyword = $dbh->quote($keyword);
1295         $query  .= " or (url like $keyword)";
1296     } # foreach
1297
1298     $query .= ")";
1299     $sth    = $dbh->prepare($query);
1300     $sth->execute;
1301
1302     while (my $data = $sth->fetchrow_hashref) {
1303         $results[$count] = $data;
1304         $count++;
1305     } # while
1306
1307     $sth->finish;
1308     $dbh->disconnect;
1309     return($count, @results);
1310 } # sub websitesearch
1311
1312
1313 END { }       # module clean-up code here (global destructor)