New set of routines for HEAD.
[koha.git] / C4 / Accounts2.pm
1 package C4::Accounts2; #assumes C4/Accounts2
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::Context;
24 use C4::Stats;
25 use C4::Search;
26 use C4::Circulation::Circ2;
27 use C4::Members;
28 use vars qw($VERSION @ISA @EXPORT);
29
30 # set the version for version checking
31 $VERSION = 0.01;        # FIXME - Should probably be different from
32                         # the version for C4::Accounts
33
34 =head1 NAME
35
36 C4::Accounts - Functions for dealing with Koha accounts
37
38 =head1 SYNOPSIS
39
40   use C4::Accounts2;
41
42 =head1 DESCRIPTION
43
44 The functions in this module deal with the monetary aspect of Koha,
45 including looking up and modifying the amount of money owed by a
46 patron.
47
48 =head1 FUNCTIONS
49
50 =over 2
51
52 =cut
53
54 @ISA = qw(Exporter);
55 @EXPORT = qw(&checkaccount      &recordpayment &fixaccounts &makepayment &manualinvoice
56                                 &getnextacctno &manualcredit
57                                 
58                                 &dailyAccountBalance &addDailyAccountOp &getDailyAccountOp);
59
60 =item checkaccount
61
62   $owed = &checkaccount($env, $borrowernumber, $dbh, $date);
63
64 Looks up the total amount of money owed by a borrower (fines, etc.).
65
66 C<$borrowernumber> specifies the borrower to look up.
67
68 C<$dbh> is a DBI::db handle for the Koha database.
69
70 C<$env> is ignored.
71
72 =cut
73 #'
74 sub checkaccount  {
75   #take borrower number
76   #check accounts and list amounts owing
77         my ($env,$bornumber,$dbh,$date)=@_;
78         my $select="SELECT SUM(amountoutstanding) AS total
79                         FROM accountlines
80                 WHERE borrowernumber = ?
81                         AND amountoutstanding<>0";
82         my @bind = ($bornumber);
83         if ($date ne ''){
84         $select.=" AND date < ?";
85         push(@bind,$date);
86         }
87         #  print $select;
88         my $sth=$dbh->prepare($select);
89         $sth->execute(@bind);
90         my $data=$sth->fetchrow_hashref;
91         my $total = $data->{'total'};
92         $sth->finish;
93         # output(1,2,"borrower owes $total");
94         #if ($total > 0){
95         #  # output(1,2,"borrower owes $total");
96         #  if ($total > 5){
97         #    reconcileaccount($env,$dbh,$bornumber,$total);
98         #  }
99         #}
100         #  pause();
101         return($total);
102 }
103
104 =item recordpayment
105
106   &recordpayment($env, $borrowernumber, $payment);
107
108 Record payment by a patron. C<$borrowernumber> is the patron's
109 borrower number. C<$payment> is a floating-point number, giving the
110 amount that was paid. C<$env> is a reference-to-hash;
111 C<$env-E<gt>{branchcode}> is the code of the branch where payment was
112 made.
113
114 Amounts owed are paid off oldest first. That is, if the patron has a
115 $1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment
116 of $1.50, then the oldest fine will be paid off in full, and $0.50
117 will be credited to the next one.
118
119 =cut
120 #'
121 sub recordpayment{
122   #here we update both the accountoffsets and the account lines
123   my ($env,$bornumber,$data)=@_;
124   my $dbh = C4::Context->dbh;
125   my $newamtos = 0;
126   my $accdata = "";
127   my $branch=$env->{'branchcode'};
128   my $amountleft = $data;
129   # begin transaction
130   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
131   # get lines with outstanding amounts to offset
132   my $sth = $dbh->prepare("select * from accountlines
133   where (borrowernumber = ?) and (amountoutstanding<>0)
134   order by date");
135   $sth->execute($bornumber);
136   # offset transactions
137   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
138      if ($accdata->{'amountoutstanding'} < $amountleft) {
139         $newamtos = 0;
140         $amountleft -= $accdata->{'amountoutstanding'};
141      }  else {
142         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
143         $amountleft = 0;
144      }
145      my $thisacct = $accdata->{accountno};
146      my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
147      where (borrowernumber = ?) and (accountno=?)");
148      $usth->execute($newamtos,$bornumber,$thisacct);
149      $usth->finish;
150  #    $usth = $dbh->prepare("insert into accountoffsets
151   #   (borrowernumber, accountno, offsetaccount,  offsetamount)
152    #  values (?,?,?,?)");
153     # $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
154     # $usth->finish;
155   }
156   # create new line
157   my $usth = $dbh->prepare("insert into accountlines
158   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
159   values (?,?,now(),?,'Payment,thanks','Pay',?)");
160   $usth->execute($bornumber,$nextaccntno,0-$data,0-$amountleft);
161   $usth->finish;
162 #  UpdateStats($env,$branch,'payment',$data,'','','',$bornumber);
163   $sth->finish;
164 }
165
166 =item makepayment
167
168   &makepayment($borrowernumber, $acctnumber, $amount, $branchcode);
169
170 Records the fact that a patron has paid off the entire amount he or
171 she owes.
172
173 C<$borrowernumber> is the patron's borrower number. C<$acctnumber> is
174 the account that was credited. C<$amount> is the amount paid (this is
175 only used to record the payment. It is assumed to be equal to the
176 amount owed). C<$branchcode> is the code of the branch where payment
177 was made.
178
179 =cut
180 #'
181 # FIXME - I'm not at all sure about the above, because I don't
182 # understand what the acct* tables in the Koha database are for.
183
184 sub makepayment{
185   #here we update  the account lines
186   #updated to check, if they are paying off a lost item, we return the item
187   # from their card, and put a note on the item record
188   my ($bornumber,$accountno,$amount,$user,$type)=@_;
189   my $env;
190 my $desc;
191 my $pay;
192 if ($type eq "Pay"){
193  $desc="Payment,received by -". $user;
194  $pay="Pay";
195 }else{
196  $desc="Written-off -by". $user;
197  $pay="W";
198 }
199   my $dbh = C4::Context->dbh;
200   # begin transaction
201   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
202   my $newamtos=0;
203   my $sth=$dbh->prepare("Select * from accountlines where  borrowernumber=? and accountno=?");
204   $sth->execute($bornumber,$accountno);
205   my $data=$sth->fetchrow_hashref;
206   $sth->finish;
207
208   $dbh->do(<<EOT);
209         UPDATE  accountlines
210         SET     amountoutstanding = amountoutstanding-$amount
211         WHERE   borrowernumber = $bornumber
212           AND   accountno = $accountno
213 EOT
214
215 #  print $updquery;
216 #  $dbh->do(<<EOT);
217 #       INSERT INTO     accountoffsets
218 #                       (borrowernumber, accountno, offsetaccount,
219 #                        offsetamount)
220 #       VALUES          ($bornumber, $accountno, $nextaccntno, $newamtos)
221 # EOT
222
223   # create new line
224   my $payment=0-$amount;
225 if ($data->{'itemnumber'}){
226 $desc.=" ".$data->{'itemnumber'};
227
228   $dbh->do(<<EOT);
229         INSERT INTO     accountlines
230                         (borrowernumber, accountno, itemnumber,date, amount,
231                          description, accounttype, amountoutstanding,offset)
232         VALUES          ($bornumber, $nextaccntno, $data->{'itemnumber'},now(), $payment,
233                         '$desc', '$pay', 0,$accountno)
234 EOT
235 }else{
236   $dbh->do(<<EOT);
237 INSERT INTO     accountlines
238                         (borrowernumber, accountno, date, amount,
239                          description, accounttype, amountoutstanding,offset)
240         VALUES          ($bornumber, $nextaccntno, now(), $payment,
241                         '$desc', '$pay', 0,$accountno)
242 EOT
243 }
244
245   # FIXME - The second argument to &UpdateStats is supposed to be the
246   # branch code.
247 #  UpdateStats($env,'MAIN',$pay,$amount,'','','',$bornumber);
248   $sth->finish;
249   #check to see what accounttype
250   if ($data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L'){
251     returnlost($bornumber,$data->{'itemnumber'});
252   }
253 }
254
255 =item getnextacctno
256
257   $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
258
259 Returns the next unused account number for the patron with the given
260 borrower number.
261
262 C<$dbh> is a DBI::db handle to the Koha database.
263
264 C<$env> is ignored.
265
266 =cut
267 #'
268 # FIXME - Okay, so what does the above actually _mean_?
269 sub getnextacctno {
270   my ($env,$bornumber,$dbh)=@_;
271   my $nextaccntno = 1;
272   my $sth = $dbh->prepare("select * from accountlines
273   where (borrowernumber = ?)
274   order by accountno desc");
275   $sth->execute($bornumber);
276   if (my $accdata=$sth->fetchrow_hashref){
277     $nextaccntno = $accdata->{'accountno'} + 1;
278   }
279   $sth->finish;
280   return($nextaccntno);
281 }
282
283 =item fixaccounts
284
285   &fixaccounts($borrowernumber, $accountnumber, $amount);
286
287 =cut
288 #'
289 # FIXME - I don't understand what this function does.
290 sub fixaccounts {
291   my ($borrowernumber,$accountno,$amount)=@_;
292   my $dbh = C4::Context->dbh;
293   my $sth=$dbh->prepare("Select * from accountlines where borrowernumber=?
294      and accountno=?");
295   $sth->execute($borrowernumber,$accountno);
296   my $data=$sth->fetchrow_hashref;
297         # FIXME - Error-checking
298   my $diff=$amount-$data->{'amount'};
299   my $outstanding=$data->{'amountoutstanding'}+$diff;
300   $sth->finish;
301
302   $dbh->do(<<EOT);
303         UPDATE  accountlines
304         SET     amount = '$amount',
305                 amountoutstanding = '$outstanding'
306         WHERE   borrowernumber = $borrowernumber
307           AND   accountno = $accountno
308 EOT
309  }
310
311 # FIXME - Never used, but not exported, either.
312 sub returnlost{
313   my ($borrnum,$itemnum)=@_;
314   my $dbh = C4::Context->dbh;
315   my $borrower=borrdata('',$borrnum); #from C4::Search;
316   my $sth=$dbh->prepare("Update issues set returndate=now() where
317   borrowernumber=? and itemnumber=? and returndate is null");
318   $sth->execute($borrnum,$itemnum);
319   $sth->finish;
320   my @datearr = localtime(time);
321   my $date = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
322   my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
323   $sth=$dbh->prepare("Update items set paidfor=? where itemnumber=?");
324   $sth->execute("Paid for by $bor $date",$itemnum);
325   $sth->finish;
326 }
327
328 =item manualinvoice
329
330   &manualinvoice($borrowernumber, $itemnumber, $description, $type,
331                  $amount, $user);
332
333 C<$borrowernumber> is the patron's borrower number.
334 C<$description> is a description of the transaction.
335 C<$type> may be one of C<CS>, C<CB>, C<CW>, C<CF>, C<CL>, C<N>, C<L>,
336 or C<REF>.
337 C<$itemnumber> is the item involved, if pertinent; otherwise, it
338 should be the empty string.
339
340 =cut
341 #'
342 # FIXME - Okay, so what does this function do, really?
343 sub manualinvoice{
344   my ($bornum,$itemnum,$desc,$type,$amount,$user)=@_;
345   my $dbh = C4::Context->dbh;
346   my $insert;
347   $itemnum=~ s/ //g;
348   my %env;
349   my $accountno=getnextacctno('',$bornum,$dbh);
350   my $amountleft=$amount;
351
352
353   if ($type eq 'N'){
354     $desc.="New Card";
355   }
356
357   if ($type eq 'L' && $desc eq ''){
358     $desc="Lost Item";
359   }
360  if ($type eq 'REF'){
361  $desc="Cash refund";
362     $amountleft=refund('',$bornum,$amount);
363   }
364   if ($itemnum ne ''){
365
366     $desc.=" ".$itemnum;
367     my $sth=$dbh->prepare("INSERT INTO  accountlines
368                         (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber)
369         VALUES (?, ?, now(), ?,?, ?,?,?)");
370      $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft, $itemnum);
371   } else {
372     $desc=$dbh->quote($desc);
373     my $sth=$dbh->prepare("INSERT INTO  accountlines
374                         (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding)
375                         VALUES (?, ?, now(), ?, ?, ?, ?)");
376     $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft);
377   }
378 }
379 sub manualcredit{
380   my ($bornum,$itemnum,$desc,$type,$amount,$user,$oldaccount)=@_;
381   my $dbh = C4::Context->dbh;
382   my $insert;
383   $itemnum=~ s/ //g;
384   my %env;
385   my $accountno=getnextacctno('',$bornum,$dbh);
386 #  my $amountleft=$amount;
387 my $amountleft;
388 my $noerror;
389   if ($type eq 'CN' || $type eq 'CA'  || $type eq 'CR' 
390   || $type eq 'CF' || $type eq 'CL' || $type eq 'CM'){
391     my $amount2=$amount*-1;     # FIXME - $amount2 = -$amount
392    ( $amountleft, $noerror,$oldaccount)=fixcredit(\%env,$bornum,$amount2,$itemnum,$type,$user);
393   }
394  if ($noerror>0){
395   if ($type eq 'CN'){
396     $desc.="Card fee credited by:".$user;
397   }
398 if ($type eq 'CM'){
399     $desc.="Other fees credited by:".$user;
400   }
401 if ($type eq 'CR'){
402     $desc.="Resrvation fee credited by:".$user;
403   }
404 if ($type eq 'CA'){
405     $desc.="Managenent fee credited by:".$user;
406   }
407   if ($type eq 'CL' && $desc eq ''){
408     $desc="Lost Item credited by:".$user;
409   }
410  
411   if ($itemnum ne ''){
412
413     $desc.=" Credited for overdue item:".$itemnum. " by:".$user;
414     my $sth=$dbh->prepare("INSERT INTO  accountlines
415                         (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber,offset)
416         VALUES (?, ?, now(), ?,?, ?,?,?,?)");
417      $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft, $itemnum,$oldaccount);
418   } else {
419 #    $desc=$dbh->quote($desc);
420     my $sth=$dbh->prepare("INSERT INTO  accountlines
421                         (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding,offset)
422                         VALUES (?, ?, now(), ?, ?, ?, ?,?)");
423     $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft,$oldaccount);
424
425   }
426 return ("0");
427 } else {
428         return("1");
429 }
430 }
431 # fixcredit
432 # $amountleft = &fixcredit($env, $bornumber, $data, $barcode, $type, $user);
433 #
434 # This function is only used internally.
435 # FIXME - Figure out what this function does, and write it down.
436 sub fixcredit{
437   #here we update both the accountoffsets and the account lines
438   my ($env,$bornumber,$data,$barcode,$type,$user)=@_;
439   my $dbh = C4::Context->dbh;
440   my $newamtos = 0;
441   my $accdata = "";
442   my $amountleft = $data;
443  
444  #   my $item=getiteminformation($env,'',$barcode);
445     my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
446     my $query="Select * from accountlines where (borrowernumber=?
447     and amountoutstanding > 0)";
448 my $exectype;
449           if ($type eq 'CL'){
450             $query.=" and (accounttype = 'L' or accounttype = 'Rep')";
451          } elsif ($type eq 'CF'){
452            $query.=" and ( itemnumber= ? and (accounttype = 'FU' or accounttype='F') )";
453                 $exectype=1;
454           } elsif ($type eq 'CN'){
455             $query.=" and ( accounttype = 'N' )";
456           } elsif ($type eq 'CR'){
457            $query.=" and ( itemnumber= ? and ( accounttype='Res' or accounttype='Rent'))";
458                 $exectype=1;
459         }elsif ($type eq 'CM'){
460             $query.=" and ( accounttype = 'M' )";
461           }elsif ($type eq 'CA'){
462             $query.=" and ( accounttype = 'A' )";
463           }
464 #    print $query;
465     my $sth=$dbh->prepare($query);
466  if ($exectype && $barcode ne ''){
467     $sth->execute($bornumber,$barcode);
468         }else{
469          $sth->execute($bornumber);
470         }
471     $accdata=$sth->fetchrow_hashref;
472     $sth->finish;
473
474 if ($accdata){
475           if ($accdata->{'amountoutstanding'} < $amountleft) {
476               $newamtos = 0;
477                 $amountleft -= $accdata->{'amountoutstanding'};
478            }  else {
479               $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
480         $amountleft = 0;
481            }
482           my $thisacct = $accdata->{accountno};
483      my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
484      where (borrowernumber = ?) and (accountno=?)");
485      $usth->execute($newamtos,$bornumber,$thisacct);
486      $usth->finish;
487 #     $usth = $dbh->prepare("insert into accountoffsets
488  #    (borrowernumber, accountno, offsetaccount,  offsetamount)
489   #   values (?,?,?,?)");
490 #     $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
491  #    $usth->finish;
492   
493   # begin transaction
494   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
495   # get lines with outstanding amounts to offset
496   my $sth = $dbh->prepare("select * from accountlines
497   where (borrowernumber = ?) and (amountoutstanding >0)
498   order by date");
499   $sth->execute($bornumber);
500 #  print $query;
501   # offset transactions
502   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
503      if ($accdata->{'amountoutstanding'} < $amountleft) {
504         $newamtos = 0;
505         $amountleft -= $accdata->{'amountoutstanding'};
506      }  else {
507         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
508         $amountleft = 0;
509      }
510      my $thisacct = $accdata->{accountno};
511      my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
512      where (borrowernumber = ?) and (accountno=?)");
513      $usth->execute($newamtos,$bornumber,$thisacct);
514      $usth->finish;
515 #     $usth = $dbh->prepare("insert into accountoffsets
516  #    (borrowernumber, accountno, offsetaccount,  offsetamount)
517  #    values (?,?,?,?)");
518   #   $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
519    #  $usth->finish;
520   }
521   $sth->finish;
522 #  $env->{'branch'}=$user;
523  # $type="Credit ".$type;
524  # UpdateStats($env,$user,$type,$data,$user,'','',$bornumber);
525   $amountleft*=-1;
526   return($amountleft,1,$accdata->{'accountno'});
527 }else{
528 return("",0)
529 }
530 }
531
532 # FIXME - Figure out what this function does, and write it down.
533 sub refund{
534   #here we update both the accountoffsets and the account lines
535   my ($env,$bornumber,$data)=@_;
536   my $dbh = C4::Context->dbh;
537   my $newamtos = 0;
538   my $accdata = "";
539 #  my $branch=$env->{'branchcode'};
540   my $amountleft = $data *-1;
541
542   # begin transaction
543   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
544   # get lines with outstanding amounts to offset
545   my $sth = $dbh->prepare("select * from accountlines
546   where (borrowernumber = ?) and (amountoutstanding<0)
547   order by date");
548   $sth->execute($bornumber);
549 #  print $amountleft;
550   # offset transactions
551   while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
552      if ($accdata->{'amountoutstanding'} > $amountleft) {
553         $newamtos = 0;
554         $amountleft -= $accdata->{'amountoutstanding'};
555      }  else {
556         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
557         $amountleft = 0;
558      }
559 #     print $amountleft;
560      my $thisacct = $accdata->{accountno};
561      my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
562      where (borrowernumber = ?) and (accountno=?)");
563      $usth->execute($newamtos,$bornumber,$thisacct);
564      $usth->finish;
565 #     $usth = $dbh->prepare("insert into accountoffsets
566 #     (borrowernumber, accountno, offsetaccount,  offsetamount)
567  #    values (?,?,?,?)");
568 #     $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
569 #     $usth->finish;
570   }
571   $sth->finish;
572   return($amountleft);
573 }
574
575 #Funtion to manage the daily account#
576
577 sub dailyAccountBalance {
578         my ($date) = @_;
579         my $dbh = C4::Context->dbh;
580         my $sth;
581         
582         if ($date) {
583
584                 $sth = $dbh->prepare("SELECT * FROM dailyaccountbalance WHERE balanceDate = ?");
585                 $sth->execute($date);
586                 my $data = $sth->fetchrow_hashref;
587                 if (!$data->{'balanceDate'}) {
588                         $data->{'noentry'} = 1;
589                 }
590                 return ($data);
591
592         } else {
593                 
594                 $sth = $dbh->prepare("SELECT * FROM dailyaccountbalance WHERE balanceDate = CURRENT_DATE()");
595                 $sth->execute();
596         
597                 if ($sth->rows) {
598                         return ($sth->fetchrow_hashref);        
599                 } else  {
600                         my %hash;
601                 
602                         $sth = $dbh->prepare("SELECT currentBalanceInHand FROM dailyaccountbalance ORDER BY balanceDate DESC LIMIT 1");
603                         $sth->execute();
604                         if ($sth->rows) {
605                                 ($hash{'initialBalanceInHand'}) = $sth->fetchrow_array;
606                                 $hash{'currentBalanceInHand'} = $hash{'initialBalanceInHand'};
607                         } else {
608                                 $hash{'initialBalanceInHand'} = 0;
609                                 $hash{'currentBalanceInHand'} = 0;
610                         }
611                         #gets the current date.
612                         my @nowarr = localtime();
613                         my $date = (1900+$nowarr[5])."-".($nowarr[4]+1)."-".$nowarr[3]; 
614
615                         $hash{'balanceDate'} = $date;
616                         $hash{'initialBalanceInHand'} = sprintf  ("%.2f", $hash{'initialBalanceInHand'});
617                         $hash{'currentBalanceInHand'} = sprintf  ("%.2f", $hash{'currentBalanceInHand'});
618                         return \%hash;
619                 }
620
621         }
622 }
623
624 sub addDailyAccountOp {
625         my ($description, $amount, $type, $invoice) = @_;
626         my $dbh = C4::Context->dbh;
627         unless ($invoice) { $invoice = undef};
628         my $sth = $dbh->prepare("INSERT INTO dailyaccount (date, description, amount, type, invoice) VALUES (CURRENT_DATE(), ?, ?, ?, ?)");
629         $sth->execute($description, $amount, $type, $invoice);
630         my $accountop = $dbh->{'mysql_insertid'};
631         $sth = $dbh->prepare("SELECT * FROM dailyaccountbalance WHERE balanceDate = CURRENT_DATE()");
632         $sth->execute();
633         if (!$sth->rows) {
634                 $sth = $dbh->prepare("SELECT currentBalanceInHand FROM dailyaccountbalance ORDER BY balanceDate DESC LIMIT 1");
635                 $sth->execute();
636                 my ($blc) = $sth->fetchrow_array;
637                 unless ($blc) {$blc = 0}
638                 $sth = $dbh->prepare("INSERT INTO dailyaccountbalance (balanceDate, initialBalanceInHand, currentBalanceInHand) VALUES (CURRENT_DATE(), ?, ?)");
639                 $sth->execute($blc, $blc);
640         }
641         if ($type eq 'D') {
642                 $amount = -1 * $amount;
643         } 
644         $sth = $dbh->prepare("UPDATE dailyaccountbalance SET currentBalanceInHand = currentBalanceInHand + ? WHERE balanceDate = CURRENT_DATE()");
645         $sth->execute($amount);
646         return $accountop; 
647 }
648
649 sub getDailyAccountOp {
650         my ($date) = @_;
651         my $dbh = C4::Context->dbh;
652         my $sth;
653         if ($date) {
654                 $sth = $dbh->prepare("SELECT * FROM dailyaccount WHERE date = ?");
655                 $sth->execute($date);   
656         } else {
657                 $sth = $dbh->prepare("SELECT * FROM dailyaccount WHERE date = CURRENT_DATE()");
658                 $sth->execute();
659         }
660         my @operations; 
661         my $count = 1;
662         while (my $row = $sth->fetchrow_hashref) {
663                 $row->{'num'} = $count++; 
664                 $row->{$row->{'type'}} = 1;
665                 
666                 $row->{'invoice'} =~ /(\w*)\-(\w*)\-(\w*)/; 
667                 $row->{'invoiceNumber'} = $1;
668                 $row->{'invoiceSupplier'} = $2;
669                 $row->{'invoiceType'} = $3;
670                         
671                 push @operations, $row;
672         }
673         return (scalar(@operations), \@operations);
674 }
675
676 END { }       # module clean-up code here (global destructor)
677
678 1;
679 __END__
680
681 =back
682
683 =head1 SEE ALSO
684
685 DBI(3)
686
687 =cut