Added PODs.
[koha.git] / C4 / Accounts.pm
1 package C4::Accounts; #assumes C4/Accounts
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 DBI;
24 use C4::Database;
25 use C4::Format;
26 use C4::Search;
27 use C4::Stats;
28 use C4::InterfaceCDK;
29 use C4::Interface::AccountsCDK;
30 use vars qw($VERSION @ISA @EXPORT);
31   
32 # set the version for version checking
33 $VERSION = 0.01;
34     
35 @ISA = qw(Exporter);
36 @EXPORT = qw(&checkaccount &reconcileaccount &getnextacctno);
37 sub displayaccounts{
38   my ($env)=@_;
39 }
40
41 sub checkaccount  {
42   #take borrower number
43   #check accounts and list amounts owing
44   my ($env,$bornumber,$dbh)=@_;
45   my $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
46   borrowernumber=$bornumber and amountoutstanding<>0");
47   $sth->execute;
48   my $total=0;
49   while (my $data=$sth->fetchrow_hashref){
50     $total=$total+$data->{'sum(amountoutstanding)'};
51   }
52   $sth->finish;
53   # output(1,2,"borrower owes $total");
54   #if ($total > 0){
55   #  # output(1,2,"borrower owes $total");
56   #  if ($total > 5){
57   #    reconcileaccount($env,$dbh,$bornumber,$total);
58   #  }
59   #}
60   #  pause();
61   return($total);
62 }    
63
64 sub reconcileaccount {
65   #print put money owing give person opportunity to pay it off
66   my ($env,$dummy,$bornumber,$total)=@_;
67   my $dbh = &C4Connect;
68   #get borrower record
69   my $sth=$dbh->prepare("select * from borrowers
70     where borrowernumber=$bornumber");
71   $sth->execute;
72   my $borrower=$sth->fetchrow_hashref;
73   $sth->finish();
74   #get borrower information
75   $sth=$dbh->prepare("Select * from accountlines where 
76   borrowernumber=$bornumber and amountoutstanding<>0 order by date");   
77   $sth->execute;     
78   #display account information
79   &clearscreen();
80   #&helptext('F11 quits');
81   output(20,0,"Accounts");
82   my @accountlines;
83   my $row=4;
84   my $i=0;
85   my $text;
86   #output (1,2,"Account Info");
87   #output (1,3,"Item\tDate      \tAmount\tDescription");
88   while (my $data=$sth->fetchrow_hashref){
89     my $line=$i+1;
90     my $amount=0+$data->{'amountoutstanding'};
91     my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
92     $line= $data->{'accountno'}." ".$data->{'date'}." ".$data->{'accounttype'}." ";
93     my $title = $itemdata->{'title'};
94     if (length($title) > 15 ) {$title = substr($title,0,15);}
95     $line= $line.$itemdata->{'barcode'}." $title ".$data->{'description'};
96     $line = fmtstr($env,$line,"L65")." ".fmtdec($env,$amount,"52");
97     push @accountlines,$line;
98     $i++;
99   }
100   #get amount paid and update database
101   my ($data,$reason)=
102     &accountsdialog($env,"Payment Entry",$borrower,\@accountlines,$total); 
103   if ($data>0) {
104     &recordpayment($env,$bornumber,$dbh,$data);
105     #Check if the borrower still owes
106     $total=&checkaccount($env,$bornumber,$dbh);
107   }
108   $dbh->disconnect;
109   return($total);
110
111 }
112
113 sub recordpayment{
114   #here we update both the accountoffsets and the account lines
115   my ($env,$bornumber,$dbh,$data)=@_;
116   my $updquery = "";
117   my $newamtos = 0;
118   my $accdata = "";
119   my $amountleft = $data;
120   # begin transaction
121 #  my $sth = $dbh->prepare("begin");
122 #  $sth->execute;
123   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
124   # get lines with outstanding amounts to offset
125   my $query = "select * from accountlines 
126   where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
127   order by date";
128   my $sth = $dbh->prepare($query);
129   $sth->execute;
130   # offset transactions
131   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
132      if ($accdata->{'amountoutstanding'} < $amountleft) {
133         $newamtos = 0;
134         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
135      }  else {
136         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
137         $amountleft = 0;
138      }
139      my $thisacct = $accdata->{accountno};
140      $updquery = "update accountlines set amountoutstanding= '$newamtos'
141      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
142      my $usth = $dbh->prepare($updquery);
143      $usth->execute;
144      $usth->finish;
145      $updquery = "insert into accountoffsets 
146      (borrowernumber, accountno, offsetaccount,  offsetamount)
147      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
148      my $usth = $dbh->prepare($updquery);
149 #     print $updquery
150      $usth->execute;
151      $usth->finish;
152   }
153   # create new line
154   #$updquery = "insert into accountlines (borrowernumber,
155   #accountno,date,amount,description,accounttype,amountoutstanding) values
156   #($bornumber,$nextaccntno,datetime('now'::abstime),0-$data,'Payment,thanks',
157   #'Pay',0-$amountleft)";
158   $updquery = "insert into accountlines 
159   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)  
160   values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
161   'Pay',0-$amountleft)";
162   my $usth = $dbh->prepare($updquery);
163   $usth->execute;
164   $usth->finish;
165   UpdateStats($env,'branch','payment',$data)
166 #  $sth->finish;
167 #  $query = "commit";
168 #  $sth = $dbh->prepare;
169 #  $sth->execute;
170 #  $sth-finish;
171 }
172
173 sub getnextacctno {
174   my ($env,$bornumber,$dbh)=@_;
175   my $nextaccntno = 1;
176   my $query = "select * from accountlines
177   where (borrowernumber = '$bornumber')
178   order by accountno desc";
179   my $sth = $dbh->prepare($query);
180   $sth->execute;
181   if (my $accdata=$sth->fetchrow_hashref){
182     $nextaccntno = $accdata->{'accountno'} + 1;
183   }
184   $sth->finish;
185   return($nextaccntno);
186 }
187                         
188 END { }       # module clean-up code here (global destructor)