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