Removed "use warnings;" as it does not work with perl 5.005
[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 C4::Biblio;
7
8 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
9
10 # set the version for version checking
11 $VERSION = 0.01;
12
13 @ISA = qw(Exporter);
14 @EXPORT = qw(
15         &getorders &bookseller &breakdown &basket &newbasket &bookfunds
16         &ordersearch &neworder &newordernum 
17         &modorder &getsingleorder &invoice &receiveorder
18         &delorder 
19         &bookfundbreakdown &curconvert &updatesup &insertsup 
20         &getcurrencies 
21         &branches &getallorders &getrecorders &updatecurrencies 
22         &getorder &getcurrency &updaterecorder
23
24 );
25 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
26
27 # your exported package globals go here,
28 # as well as any optionally exported functions
29
30 @EXPORT_OK   = qw($Var1 %Hashit);
31
32
33 # non-exported package globals go here
34 use vars qw(@more $stuff);
35
36 # initalize package globals, first exported ones
37
38 my $Var1   = '';
39 my %Hashit = ();
40
41
42
43 # then the others (which are still accessible as $Some::Module::stuff)
44 my $stuff  = '';
45 my @more   = ();
46
47 # all file-scoped lexicals must be created before
48 # the functions below that use them.
49
50 # file-private lexicals go here
51 my $priv_var    = '';
52 my %secret_hash = ();
53
54 # here's a file-private function as a closure,
55 # callable as &$priv_func;  it cannot be prototyped.
56 my $priv_func = sub {
57   # stuff goes here.
58   };
59   
60 # make all your functions, whether exported or not;
61
62 sub getorders {
63   my ($supplierid)=@_;
64   my $dbh=C4Connect;
65   my $query = "Select count(*),authorisedby,entrydate,basketno from aqorders where 
66   booksellerid='$supplierid' and (quantity > quantityreceived or
67   quantityreceived is NULL)
68   and (datecancellationprinted is NULL or datecancellationprinted = '0000-00-00')";
69   $query.=" group by basketno order by entrydate desc";
70   #print $query;
71   my $sth=$dbh->prepare($query);
72   $sth->execute;
73   my @results;
74   my $i=0;
75   while (my $data=$sth->fetchrow_hashref){
76     $results[$i]=$data;
77     $i++;
78   }
79   $sth->finish;
80   $dbh->disconnect;
81   return ($i,\@results);
82 }
83
84 sub itemcount{
85   my ($biblio)=@_;
86   my $dbh=C4Connect;
87   my $query="Select count(*) from items where biblionumber=$biblio";
88 #  print $query;
89   my $sth=$dbh->prepare($query);
90   $sth->execute;
91   my $data=$sth->fetchrow_hashref;
92   $sth->finish;
93   $dbh->disconnect;
94   return($data->{'count(*)'});
95 }
96
97 sub getorder{
98   my ($bi,$bib)=@_;
99   my $dbh=C4Connect;
100   my $query="Select ordernumber 
101         from aqorders 
102         where biblionumber=? and biblioitemnumber=?";
103   my $sth=$dbh->prepare($query);
104   $sth->execute($bib,$bi);
105   my $ordnum=$sth->fetchrow_hashref;
106   $sth->finish;
107   my $order=getsingleorder($ordnum->{'ordernumber'});
108   $dbh->disconnect;
109 #  print $query;
110   return ($order,$ordnum->{'ordernumber'});
111 }
112
113 sub getsingleorder {
114   my ($ordnum)=@_;
115   my $dbh=C4Connect;
116   my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown 
117   where aqorders.ordernumber=?
118   and biblio.biblionumber=aqorders.biblionumber and
119   biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
120   aqorders.ordernumber=aqorderbreakdown.ordernumber";
121   my $sth=$dbh->prepare($query);
122   $sth->execute($ordnum);
123   my $data=$sth->fetchrow_hashref;
124   $sth->finish;
125   $dbh->disconnect;
126   return($data);
127 }
128
129 sub invoice {
130   my ($invoice)=@_;
131   my $dbh=C4Connect;
132   my $query="Select * from aqorders,biblio,biblioitems where
133   booksellerinvoicenumber='$invoice' 
134   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
135   aqorders.biblioitemnumber group by aqorders.ordernumber,aqorders.biblioitemnumber";
136   my $i=0;
137   my @results;
138   my $sth=$dbh->prepare($query);
139   $sth->execute;
140   while (my $data=$sth->fetchrow_hashref){
141     $results[$i]=$data;
142     $i++;
143   }
144   $sth->finish;
145   $dbh->disconnect;
146   return($i,@results);
147 }
148
149 sub getallorders {
150   #gets all orders from a certain supplier, orders them alphabetically
151   my ($supid)=@_;
152   my $dbh=C4Connect;
153   my $query="Select * from aqorders,biblio,biblioitems where booksellerid='$supid'
154   and (cancelledby is NULL or cancelledby = '')
155   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=                    
156   aqorders.biblioitemnumber 
157   group by aqorders.biblioitemnumber 
158   order by
159   biblio.title";
160   my $i=0;
161   my @results;
162   my $sth=$dbh->prepare($query);
163   $sth->execute;
164   while (my $data=$sth->fetchrow_hashref){
165     $results[$i]=$data;
166     $i++;
167   }
168   $sth->finish;
169   $dbh->disconnect;
170   return($i,@results);
171 }
172
173 sub getrecorders {
174   #gets all orders from a certain supplier, orders them alphabetically
175   my ($supid)=@_;
176   my $dbh=C4Connect;
177   my $query="Select * from aqorders,biblio,biblioitems where booksellerid='$supid'
178   and (cancelledby is NULL or cancelledby = '')
179   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=                    
180   aqorders.biblioitemnumber and
181   aqorders.quantityreceived>0
182   and aqorders.datereceived >=now()
183   group by aqorders.biblioitemnumber 
184   order by
185   biblio.title";
186   my $i=0;
187   my @results;
188   my $sth=$dbh->prepare($query);
189   $sth->execute;
190   while (my $data=$sth->fetchrow_hashref){
191     $results[$i]=$data;
192     $i++;
193   }
194   $sth->finish;
195   $dbh->disconnect;
196   return($i,@results);
197 }
198
199 sub ordersearch {
200   my ($search,$biblio,$catview)=@_;
201   my $dbh=C4Connect;
202   my $query="Select *,biblio.title from aqorders,biblioitems,biblio
203         where aqorders.biblioitemnumber = biblioitems.biblioitemnumber
204         and biblio.biblionumber=aqorders.biblionumber
205         and ((datecancellationprinted is NULL)
206         or (datecancellationprinted = '0000-00-00')
207   and ((";
208   my @data=split(' ',$search);
209   my $count=@data;
210   for (my $i=0;$i<$count;$i++){
211     $query.= "(biblio.title like '$data[$i]%' or biblio.title like '% $data[$i]%') and ";
212   }
213   $query=~ s/ and $//;
214   $query.=" ) or biblioitems.isbn='$search' 
215   or (aqorders.ordernumber='$search' and aqorders.biblionumber='$biblio')) ";
216   if ($catview ne 'yes'){
217     $query.=" and (quantityreceived < quantity or quantityreceived is NULL)";
218   }
219   $query.=" group by aqorders.ordernumber";
220   my $sth=$dbh->prepare($query);
221 #  print $query;
222   $sth->execute;
223   my $i=0;
224   my @results;
225   while (my $data=$sth->fetchrow_hashref){
226      my $sth2=$dbh->prepare("Select * from biblio where
227      biblionumber='$data->{'biblionumber'}'");
228      $sth2->execute;
229      my $data2=$sth2->fetchrow_hashref;
230      $sth2->finish;
231      $data->{'author'}=$data2->{'author'};
232      $data->{'seriestitle'}=$data2->{'seriestitle'};
233      $sth2=$dbh->prepare("Select * from aqorderbreakdown where
234     ordernumber=$data->{'ordernumber'}");
235     $sth2->execute;
236     $data2=$sth2->fetchrow_hashref;
237     $sth2->finish;
238     $data->{'branchcode'}=$data2->{'branchcode'};
239     $data->{'bookfundid'}=$data2->{'bookfundid'};
240     $results[$i]=$data;
241     $i++;
242   }
243   $sth->finish;
244   $dbh->disconnect;
245   return($i,@results);
246 }
247
248
249 sub bookseller {
250   my ($searchstring)=@_;
251   my $dbh=C4Connect;
252   my $query="Select * from aqbooksellers where name like '%$searchstring%' or
253   id = '$searchstring'";
254   my $sth=$dbh->prepare($query);
255   $sth->execute;
256   my @results;
257   my $i=0;
258   while (my $data=$sth->fetchrow_hashref){
259     $results[$i]=$data;
260     $i++;
261   }
262   $sth->finish;
263   $dbh->disconnect;
264   return($i,@results);
265 }
266
267 sub breakdown {
268   my ($id)=@_;
269   my $dbh=C4Connect;
270   my $query="Select * from aqorderbreakdown where ordernumber='$id'";
271   my $sth=$dbh->prepare($query);
272   $sth->execute;
273   my @results;
274   my $i=0;
275   while (my $data=$sth->fetchrow_hashref){
276     $results[$i]=$data;
277     $i++;
278   }
279   $sth->finish;
280   $dbh->disconnect;
281   return($i,\@results);
282 }
283
284 sub basket {
285   my ($basketno,$supplier)=@_;
286   my $dbh=C4Connect;
287   my $query="Select *,biblio.title from aqorders,biblio,biblioitems 
288   where basketno='$basketno'
289   and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber
290   =aqorders.biblioitemnumber 
291   and (datecancellationprinted is NULL or datecancellationprinted =
292   '0000-00-00')";
293   if ($supplier ne ''){
294     $query.=" and aqorders.booksellerid='$supplier'";
295   } 
296   $query.=" group by aqorders.ordernumber";
297   my $sth=$dbh->prepare($query);
298   $sth->execute;
299   my @results;
300 #  print $query;
301   my $i=0;
302   while (my $data=$sth->fetchrow_hashref){
303     $results[$i]=$data;
304     $i++;
305   }
306   $sth->finish;
307   $dbh->disconnect;
308   return($i,@results);
309 }
310
311 sub newbasket {
312   my $dbh=C4Connect;
313   my $query="Select max(basketno) from aqorders";
314   my $sth=$dbh->prepare($query);
315   $sth->execute;
316   my $data=$sth->fetchrow_arrayref;
317   my $basket=$$data[0];
318   $basket++;
319   $sth->finish;
320   $dbh->disconnect;
321   return($basket);
322 }
323
324 sub bookfunds {
325   my $dbh=C4Connect;
326   my $query="Select * from aqbookfund,aqbudget where aqbookfund.bookfundid
327   =aqbudget.bookfundid 
328   and aqbudget.startdate='2001-07-01' 
329   group by aqbookfund.bookfundid order by bookfundname";
330   my $sth=$dbh->prepare($query);
331   $sth->execute;
332   my @results;
333   my $i=0;
334   while (my $data=$sth->fetchrow_hashref){
335     $results[$i]=$data;
336     $i++;
337   }
338   $sth->finish;
339   $dbh->disconnect;
340   return($i,@results);
341 }
342
343 sub branches {
344   my $dbh=C4Connect;
345   my $query="Select * from branches";
346   my $sth=$dbh->prepare($query);
347   my $i=0;
348     my @results;
349
350     $sth->execute;
351   while (my $data=$sth->fetchrow_hashref){
352     $results[$i]=$data;
353     $i++;
354     } # while
355
356   $sth->finish;
357   $dbh->disconnect;
358   return($i,@results);
359 } # sub branches
360
361 sub bookfundbreakdown {
362   my ($id)=@_;
363   my $dbh=C4Connect;
364   my $query="Select quantity,datereceived,freight,unitprice,listprice,ecost,quantityreceived,subscription
365   from aqorders,aqorderbreakdown where bookfundid='$id' and 
366   aqorders.ordernumber=aqorderbreakdown.ordernumber and ((budgetdate >=
367   '2001-07-01' and budgetdate <'2002-07-01') or
368   (datereceived >= '2001-07-01' and datereceived < '2002-07-01'))
369   and (datecancellationprinted is NULL or
370   datecancellationprinted='0000-00-00')";
371   my $sth=$dbh->prepare($query);
372   $sth->execute;
373   my $comtd=0;
374   my $spent=0;
375   while (my $data=$sth->fetchrow_hashref){
376     if ($data->{'subscription'} == 1){
377       $spent+=$data->{'quantity'}*$data->{'unitprice'};
378     } else {
379       my $leftover=$data->{'quantity'}-$data->{'quantityreceived'};
380       $comtd+=($data->{'ecost'})*$leftover;
381       $spent+=($data->{'unitprice'})*$data->{'quantityreceived'};
382     }
383   }
384   $sth->finish;
385   $dbh->disconnect;
386   return($spent,$comtd);
387 }
388       
389
390 sub neworder {
391   my ($bibnum,$title,$ordnum,$basket,$quantity,$listprice,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$sub,$invoice)=@_;
392   if ($budget eq 'now'){
393     $budget="now()";
394   } else {
395     $budget="'2001-07-01'";
396   }
397   if ($sub eq 'yes'){
398     $sub=1;
399   } else {
400     $sub=0;
401   }
402   my $dbh=C4Connect;
403   my $query="insert into aqorders (biblionumber,title,basketno,
404   quantity,listprice,booksellerid,entrydate,requisitionedby,authorisedby,notes,
405   biblioitemnumber,rrp,ecost,gst,budgetdate,unitprice,subscription,booksellerinvoicenumber)
406
407   values
408   ($bibnum,'$title',$basket,$quantity,$listprice,'$supplier',now(),
409   '$who','$who','$notes',$bibitemnum,'$rrp','$ecost','$gst',$budget,'$cost',
410   '$sub','$invoice')";
411   my $sth=$dbh->prepare($query);
412 #  print $query;
413   $sth->execute;
414   $sth->finish;
415   $query="select * from aqorders where
416   biblionumber=$bibnum and basketno=$basket and ordernumber >=$ordnum";
417   $sth=$dbh->prepare($query);
418   $sth->execute;
419   my $data=$sth->fetchrow_hashref;
420   $sth->finish;
421   $ordnum=$data->{'ordernumber'};
422   $query="insert into aqorderbreakdown (ordernumber,bookfundid) values
423   ($ordnum,'$bookfund')";
424   $sth=$dbh->prepare($query);
425 #  print $query;
426   $sth->execute;
427   $sth->finish;
428   $dbh->disconnect;
429 }
430
431 sub delorder {
432   my ($bibnum,$ordnum)=@_;
433   my $dbh=C4Connect;
434   my $query="update aqorders set datecancellationprinted=now()
435   where biblionumber='$bibnum' and
436   ordernumber='$ordnum'";
437   my $sth=$dbh->prepare($query);
438   #print $query;
439   $sth->execute;
440   $sth->finish;
441   my $count=itemcount($bibnum);
442   if ($count == 0){
443     delbiblio($bibnum);
444   }
445   $dbh->disconnect;
446 }
447
448 sub modorder {
449   my ($title,$ordnum,$quantity,$listprice,$bibnum,$basketno,$supplier,$who,$notes,$bookfund,$bibitemnum,$rrp,$ecost,$gst,$budget,$cost,$invoice)=@_;
450   my $dbh=C4Connect;
451   my $query="update aqorders set title='$title',
452   quantity='$quantity',listprice='$listprice',basketno='$basketno', 
453   rrp='$rrp',ecost='$ecost',unitprice='$cost',
454   booksellerinvoicenumber='$invoice'
455   where
456   ordernumber=$ordnum and biblionumber=$bibnum";
457   my $sth=$dbh->prepare($query);
458 #  print $query;
459   $sth->execute;
460   $sth->finish;
461   $query="update aqorderbreakdown set bookfundid=$bookfund where
462   ordernumber=$ordnum";
463   $sth=$dbh->prepare($query);
464 #  print $query;
465   $sth->execute;
466   $sth->finish;
467   $dbh->disconnect;
468 }
469
470 sub newordernum {
471   my $dbh=C4Connect;
472   my $query="Select max(ordernumber) from aqorders";
473   my $sth=$dbh->prepare($query);
474   $sth->execute;
475   my $data=$sth->fetchrow_arrayref;
476   my $ordnum=$$data[0];
477   $ordnum++;
478   $sth->finish;
479   $dbh->disconnect;
480   return($ordnum);
481 }
482
483 sub receiveorder {
484   my ($biblio,$ordnum,$quantrec,$user,$cost,$invoiceno,$bibitemno,$freight,$bookfund,$rrp)=@_;
485   my $dbh=C4Connect;
486   my $query="update aqorders set quantityreceived='$quantrec',
487   datereceived=now(),booksellerinvoicenumber='$invoiceno',
488   biblioitemnumber=$bibitemno,unitprice='$cost',freight='$freight',
489   rrp='$rrp'
490   where biblionumber=$biblio and ordernumber=$ordnum
491   ";
492 #  print $query;
493   my $sth=$dbh->prepare($query);
494   $sth->execute;
495   $sth->finish;
496   $query="update aqorderbreakdown set bookfundid=$bookfund where
497   ordernumber=$ordnum";
498   $sth=$dbh->prepare($query);
499 #  print $query;
500   $sth->execute;
501   $sth->finish;  
502   $dbh->disconnect;
503 }
504 sub updaterecorder{
505   my($biblio,$ordnum,$user,$cost,$bookfund,$rrp)=@_;
506   my $dbh=C4Connect;
507   my $query="update aqorders set
508   unitprice='$cost', rrp='$rrp'
509   where biblionumber=$biblio and ordernumber=$ordnum
510   ";
511 #  print $query;
512   my $sth=$dbh->prepare($query);
513   $sth->execute;
514   $sth->finish;
515   $query="update aqorderbreakdown set bookfundid=$bookfund where
516   ordernumber=$ordnum";
517   $sth=$dbh->prepare($query);
518 #  print $query;
519   $sth->execute;
520   $sth->finish;  
521   $dbh->disconnect;
522 }
523
524 sub curconvert {
525   my ($currency,$price)=@_;
526   my $convertedprice;
527   my $dbh=C4Connect;
528   my $query="Select rate from currency where currency='$currency'";
529   my $sth=$dbh->prepare($query);
530   $sth->execute;
531   my $data=$sth->fetchrow_hashref;
532   $sth->finish;
533   $dbh->disconnect;
534   my $cur=$data->{'rate'};
535   if ($cur==0){
536     $cur=1;
537   }
538   $convertedprice=$price / $cur;
539   return($convertedprice);
540 }
541
542 sub getcurrencies {
543   my $dbh=C4Connect;
544   my $query="Select * from currency";
545   my $sth=$dbh->prepare($query);
546   $sth->execute;
547   my @results;
548   my $i=0;
549   while (my $data=$sth->fetchrow_hashref){
550     $results[$i]=$data;
551     $i++;
552   }
553   $sth->finish;
554   $dbh->disconnect;
555   return($i,\@results);
556
557
558 sub getcurrency {
559   my ($cur)=@_;
560   my $dbh=C4Connect;
561   my $query="Select * from currency where currency='$cur'";
562   my $sth=$dbh->prepare($query);
563   $sth->execute;
564
565   my $data=$sth->fetchrow_hashref;
566   $sth->finish;
567   $dbh->disconnect;
568   return($data);
569
570
571 sub updatecurrencies {
572   my ($currency,$rate)=@_;
573   my $dbh=C4Connect;
574   my $query="update currency set rate=$rate where currency='$currency'";
575   my $sth=$dbh->prepare($query);
576   $sth->execute;
577   $sth->finish;
578   $dbh->disconnect;
579
580
581 sub updatesup {
582    my ($data)=@_;
583    my $dbh=C4Connect;
584    my $query="Update aqbooksellers set
585    name='$data->{'name'}',address1='$data->{'address1'}',address2='$data->{'address2'}',
586    address3='$data->{'address3'}',address4='$data->{'address4'}',postal='$data->{'postal'}',
587    phone='$data->{'phone'}',fax='$data->{'fax'}',url='$data->{'url'}',
588    contact='$data->{'contact'}',contpos='$data->{'contpos'}',
589    contphone='$data->{'contphone'}', contfax='$data->{'contfax'}', contaltphone=
590    '$data->{'contaltphone'}', contemail='$data->{'contemail'}', contnotes=
591    '$data->{'contnotes'}', active=$data->{'active'},
592    listprice='$data->{'listprice'}', invoiceprice='$data->{'invoiceprice'}',
593    gstreg=$data->{'gstreg'}, listincgst=$data->{'listincgst'},
594    invoiceincgst=$data->{'invoiceincgst'}, specialty='$data->{'specialty'}',
595    discount='$data->{'discount'}',invoicedisc='$data->{'invoicedisc'}',
596    nocalc='$data->{'nocalc'}'
597    where id='$data->{'id'}'";
598    my $sth=$dbh->prepare($query);
599    $sth->execute;
600    $sth->finish;
601    $dbh->disconnect;
602 #   print $query;
603 }
604
605 sub insertsup {
606   my ($data)=@_;
607   my $dbh=C4Connect;
608   my $sth=$dbh->prepare("Select max(id) from aqbooksellers");
609   $sth->execute;
610   my $data2=$sth->fetchrow_hashref;
611   $sth->finish;
612   $data2->{'max(id)'}++;
613   $sth=$dbh->prepare("Insert into aqbooksellers (id) values ($data2->{'max(id)'})");
614   $sth->execute;
615   $sth->finish;
616   $data->{'id'}=$data2->{'max(id)'};
617   $dbh->disconnect;
618   updatesup($data);
619   return($data->{'id'});
620 }
621
622
623 END { }       # module clean-up code here (global destructor)