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