Add functionality so that when the replacement cost of a lost item is paid
[koha.git] / C4 / Accounts2.pm
1 package C4::Accounts2; #asummes C4/Accounts2
2
3 #requires DBI.pm to be installed
4 #uses DBD:Pg
5
6 use strict;
7 require Exporter;
8 use DBI;
9 use C4::Database;
10 use C4::Stats;
11 use C4::Search;
12 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
13   
14 # set the version for version checking
15 $VERSION = 0.01;
16     
17 @ISA = qw(Exporter);
18 @EXPORT = qw(&recordpayment &fixaccounts &makepayment);
19 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
20                   
21 # your exported package globals go here,
22 # as well as any optionally exported functions
23
24 @EXPORT_OK   = qw($Var1 %Hashit);
25
26
27 # non-exported package globals go here
28 use vars qw(@more $stuff);
29         
30 # initalize package globals, first exported ones
31
32 my $Var1   = '';
33 my %Hashit = ();
34                     
35 # then the others (which are still accessible as $Some::Module::stuff)
36 my $stuff  = '';
37 my @more   = ();
38         
39 # all file-scoped lexicals must be created before
40 # the functions below that use them.
41                 
42 # file-private lexicals go here
43 my $priv_var    = '';
44 my %secret_hash = ();
45                             
46 # here's a file-private function as a closure,
47 # callable as &$priv_func;  it cannot be prototyped.
48 my $priv_func = sub {
49   # stuff goes here.
50 };
51                                                     
52 # make all your functions, whether exported or not;
53
54 sub displayaccounts{
55   my ($env)=@_;
56 }
57
58 sub recordpayment{
59   #here we update both the accountoffsets and the account lines
60   my ($env,$bornumber,$data)=@_;
61   my $dbh=C4Connect;
62   my $updquery = "";
63   my $newamtos = 0;
64   my $accdata = "";
65   my $branch=$env->{'branchcode'};
66   my $amountleft = $data;
67   # begin transaction
68   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
69   # get lines with outstanding amounts to offset
70   my $query = "select * from accountlines 
71   where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
72   order by date";
73   my $sth = $dbh->prepare($query);
74   $sth->execute;
75   # offset transactions
76   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
77      if ($accdata->{'amountoutstanding'} < $amountleft) {
78         $newamtos = 0;
79         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
80      }  else {
81         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
82         $amountleft = 0;
83      }
84      my $thisacct = $accdata->{accountno};
85      $updquery = "update accountlines set amountoutstanding= '$newamtos'
86      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
87      my $usth = $dbh->prepare($updquery);
88      $usth->execute;
89      $usth->finish;
90      $updquery = "insert into accountoffsets 
91      (borrowernumber, accountno, offsetaccount,  offsetamount)
92      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
93      my $usth = $dbh->prepare($updquery);
94      $usth->execute;
95      $usth->finish;
96   }
97   # create new line
98   $updquery = "insert into accountlines 
99   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)  
100   values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
101   'Pay',0-$amountleft)";
102   my $usth = $dbh->prepare($updquery);
103   $usth->execute;
104   $usth->finish;
105   UpdateStats($env,$branch,'payment',$data,'','','',$bornumber);
106   $sth->finish;
107   $dbh->disconnect;
108 }
109
110 sub makepayment{
111   #here we update both the accountoffsets and the account lines
112   #updated to check, if they are paying off a lost item, we return the item 
113   # from their card, and put a note on the item record
114   my ($bornumber,$accountno,$amount,$user)=@_;
115   my $env;
116   my $dbh=C4Connect;
117   # begin transaction
118   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
119   my $newamtos=0;
120   my $sel="Select * from accountlines where  borrowernumber=$bornumber and
121   accountno=$accountno";
122   my $sth=$dbh->prepare($sel);
123   $sth->execute;
124   my $data=$sth->fetchrow_hashref;
125   $sth->finish;
126   my $updquery="Update accountlines set amountoutstanding=0 where
127   borrowernumber=$bornumber and accountno=$accountno";
128   $sth=$dbh->prepare($updquery);
129   $sth->execute;
130   $sth->finish;
131 #  print $updquery;
132   $updquery = "insert into accountoffsets 
133   (borrowernumber, accountno, offsetaccount,  offsetamount)
134   values ($bornumber,$accountno,$nextaccntno,$newamtos)";
135   my $usth = $dbh->prepare($updquery);
136   $usth->execute;
137   $usth->finish;  
138   # create new line
139   my $payment=0-$amount;
140   $updquery = "insert into accountlines 
141   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)  
142   values ($bornumber,$nextaccntno,now(),$payment,'Payment,thanks - $user', 'Pay',0)";
143   my $usth = $dbh->prepare($updquery);
144   $usth->execute;
145   $usth->finish;
146   UpdateStats($env,$user,'payment',$amount,'','','',$bornumber);
147   $sth->finish;
148   $dbh->disconnect;
149   #check to see what accounttype
150   if ($data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L'){
151     returnlost($bornumber,$data->{'itemnumber'});
152   }
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 sub fixaccounts {
171   my ($borrowernumber,$accountno,$amount)=@_;
172   my $dbh=C4Connect;
173   my $query="Select * from accountlines where borrowernumber=$borrowernumber
174      and accountno=$accountno";
175   my $sth=$dbh->prepare($query);
176   $sth->execute;
177   my $data=$sth->fetchrow_hashref;
178   my $diff=$amount-$data->{'amount'};
179   my $outstanding=$data->{'amountoutstanding'}+$diff;
180   $sth->finish;
181   $query="Update accountlines set amount='$amount',amountoutstanding='$outstanding' where
182           borrowernumber=$borrowernumber and accountno=$accountno";
183    $sth=$dbh->prepare($query);
184 #   print $query;
185    $sth->execute;
186    $sth->finish;
187    $dbh->disconnect;
188  }
189
190 sub returnlost{
191   my ($borrnum,$itemnum)=@_;
192   my $dbh=C4Connect;
193   my $borrower=borrdata('',$borrnum); #from C4::Search;
194   my $upiss="Update issues set returndate=now() where
195   borrowernumber='$borrnum' and itemnumber='$itemnum' and returndate is null";
196   my $sth=$dbh->prepare($upiss);
197   $sth->execute;
198   $sth->finish;
199   my $date='2001-04-18';
200   my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
201   my $upitem="Update items set itemnotes='Paid for by $bor $date' where itemnumber='$itemnum'";
202   $sth=$dbh->prepare($upitem);
203   $sth->execute;
204   $sth->finish;
205   $dbh->disconnect;
206 }
207
208 END { }       # module clean-up code here (global destructor)