Kumara - predecessor to Koha
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
5.1 KiB

  1. package C4::Accounts2; #asummes C4/Accounts2
  2. #requires DBI.pm to be installed
  3. #uses DBD:Pg
  4. use strict;
  5. require Exporter;
  6. use DBI;
  7. use C4::Database;
  8. use C4::Stats;
  9. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  10. # set the version for version checking
  11. $VERSION = 0.01;
  12. @ISA = qw(Exporter);
  13. @EXPORT = qw(&recordpayment &fixaccounts &makepayment);
  14. %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
  15. # your exported package globals go here,
  16. # as well as any optionally exported functions
  17. @EXPORT_OK = qw($Var1 %Hashit);
  18. # non-exported package globals go here
  19. use vars qw(@more $stuff);
  20. # initalize package globals, first exported ones
  21. my $Var1 = '';
  22. my %Hashit = ();
  23. # then the others (which are still accessible as $Some::Module::stuff)
  24. my $stuff = '';
  25. my @more = ();
  26. # all file-scoped lexicals must be created before
  27. # the functions below that use them.
  28. # file-private lexicals go here
  29. my $priv_var = '';
  30. my %secret_hash = ();
  31. # here's a file-private function as a closure,
  32. # callable as &$priv_func; it cannot be prototyped.
  33. my $priv_func = sub {
  34. # stuff goes here.
  35. };
  36. # make all your functions, whether exported or not;
  37. sub displayaccounts{
  38. my ($env)=@_;
  39. }
  40. sub recordpayment{
  41. #here we update both the accountoffsets and the account lines
  42. my ($env,$bornumber,$data)=@_;
  43. my $dbh=C4Connect;
  44. my $updquery = "";
  45. my $newamtos = 0;
  46. my $accdata = "";
  47. my $amountleft = $data;
  48. # begin transaction
  49. my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
  50. # get lines with outstanding amounts to offset
  51. my $query = "select * from accountlines
  52. where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
  53. order by date";
  54. my $sth = $dbh->prepare($query);
  55. $sth->execute;
  56. # offset transactions
  57. while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
  58. if ($accdata->{'amountoutstanding'} < $amountleft) {
  59. $newamtos = 0;
  60. $amountleft = $amountleft - $accdata->{'amountoutstanding'};
  61. } else {
  62. $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
  63. $amountleft = 0;
  64. }
  65. my $thisacct = $accdata->{accountno};
  66. $updquery = "update accountlines set amountoutstanding= '$newamtos'
  67. where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
  68. my $usth = $dbh->prepare($updquery);
  69. $usth->execute;
  70. $usth->finish;
  71. $updquery = "insert into accountoffsets
  72. (borrowernumber, accountno, offsetaccount, offsetamount)
  73. values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
  74. my $usth = $dbh->prepare($updquery);
  75. $usth->execute;
  76. $usth->finish;
  77. }
  78. # create new line
  79. $updquery = "insert into accountlines
  80. (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
  81. values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
  82. 'Pay',0-$amountleft)";
  83. my $usth = $dbh->prepare($updquery);
  84. $usth->execute;
  85. $usth->finish;
  86. UpdateStats($env,'branch','payment',$data);
  87. $sth->finish;
  88. $dbh->disconnect;
  89. }
  90. sub makepayment{
  91. #here we update both the accountoffsets and the account lines
  92. my ($bornumber,$accountno,$amount)=@_;
  93. my $env;
  94. my $dbh=C4Connect;
  95. # begin transaction
  96. my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
  97. my $newamtos=0;
  98. my $updquery="Update accountlines set amountoutstanding=0 where
  99. borrowernumber=$bornumber and accountno=$accountno";
  100. my $sth=$dbh->prepare($updquery);
  101. $sth->execute;
  102. $sth->finish;
  103. # print $updquery;
  104. $updquery = "insert into accountoffsets
  105. (borrowernumber, accountno, offsetaccount, offsetamount)
  106. values ($bornumber,$accountno,$nextaccntno,$newamtos)";
  107. my $usth = $dbh->prepare($updquery);
  108. $usth->execute;
  109. $usth->finish;
  110. # create new line
  111. my $payment=0-$amount;
  112. $updquery = "insert into accountlines
  113. (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
  114. values ($bornumber,$nextaccntno,now(),$payment,'Payment,thanks', 'Pay',0)";
  115. my $usth = $dbh->prepare($updquery);
  116. $usth->execute;
  117. $usth->finish;
  118. UpdateStats($env,'branch','payment',$amount);
  119. $sth->finish;
  120. $dbh->disconnect;
  121. }
  122. sub getnextacctno {
  123. my ($env,$bornumber,$dbh)=@_;
  124. my $nextaccntno = 1;
  125. my $query = "select * from accountlines
  126. where (borrowernumber = '$bornumber')
  127. order by accountno desc";
  128. my $sth = $dbh->prepare($query);
  129. $sth->execute;
  130. if (my $accdata=$sth->fetchrow_hashref){
  131. $nextaccntno = $accdata->{'accountno'} + 1;
  132. }
  133. $sth->finish;
  134. return($nextaccntno);
  135. }
  136. sub fixaccounts {
  137. my ($borrowernumber,$accountno,$amount)=@_;
  138. my $dbh=C4Connect;
  139. my $query="Select * from accountlines where borrowernumber=$borrowernumber
  140. and accountno=$accountno";
  141. my $sth=$dbh->prepare($query);
  142. $sth->execute;
  143. my $data=$sth->fetchrow_hashref;
  144. my $diff=$amount-$data->{'amount'};
  145. my $outstanding=$data->{'amountoutstanding'}+$diff;
  146. $sth->finish;
  147. $query="Update accountlines set amount='$amount',amountoutstanding='$outstanding' where
  148. borrowernumber=$borrowernumber and accountno=$accountno";
  149. $sth=$dbh->prepare($query);
  150. # print $query;
  151. $sth->execute;
  152. $sth->finish;
  153. $dbh->disconnect;
  154. }
  155. END { } # module clean-up code here (global destructor)