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