summaryrefslogtreecommitdiff
path: root/LedgerSMB/Tax.pm
blob: 6d4c7956c72899a18ae720575becf0ee38643c69 (plain)
  1. #=====================================================================
  2. #
  3. # Tax support module for LedgerSMB
  4. # LedgerSMB::Tax
  5. # Default simple tax application
  6. #
  7. # LedgerSMB
  8. # Small Medium Business Accounting software
  9. # http://www.ledgersmb.org/
  10. #
  11. #
  12. # Copyright (C) 2006
  13. # This work contains copyrighted information from a number of sources all used
  14. # with permission. It is released under the GNU General Public License
  15. # Version 2 or, at your option, any later version. See COPYRIGHT file for
  16. # details.
  17. #
  18. #
  19. #======================================================================
  20. # This package contains tax related functions:
  21. #
  22. # apply_taxes - applies taxes to the given subtotal
  23. # extract_taxes - extracts taxes from the given total
  24. # initialize_taxes - loads taxes from the database
  25. # calculate_taxes - calculates taxes
  26. #
  27. #====================================================================
  28. package Tax;
  29. use Math::BigFloat;
  30. sub init_taxes {
  31. my ($form, $taxaccounts) = @_;
  32. my $dbh = $form->{dbh};
  33. @taxes = ();
  34. my @accounts = split / /, $taxaccounts;
  35. my $query = qq|SELECT t.taxnumber, c.description,
  36. t.rate, t.chart_id, t.pass, m.taxmodulename
  37. FROM tax t INNER JOIN chart c ON (t.chart_id = c.id)
  38. INNER JOIN taxmodule m ON (t.taxmodule_id = m.taxmodule_id)
  39. WHERE c.accno = ?|;
  40. my $sth = $dbh->prepare($query);
  41. foreach $taxaccount (@accounts) {
  42. $sth->execute($taxaccount) || $form->dberror($query);
  43. my $ref = $sth->fetchrow_hashref;
  44. my $module = $ref->{'taxmodulename'};
  45. require "LedgerSMB/Taxes/${module}.pm";
  46. $module =~ s/\//::/g;
  47. my $tax = (eval 'Taxes::'.$module)->new();
  48. $tax->pass($ref->{'pass'});
  49. $tax->account($taxaccount);
  50. $tax->rate(Math::BigFloat->new($ref->{'rate'}));
  51. $tax->taxnumber($ref->{'taxnumber'});
  52. $tax->chart($ref->{'chart'});
  53. $tax->description($ref->{'description'});
  54. $tax->value(Math::BigFloat->bzero());
  55. push @taxes, $tax;
  56. $sth->finish;
  57. }
  58. return @taxes;
  59. }
  60. sub calculate_taxes {
  61. my ($taxes, $form, $subtotal, $extract) = @_;
  62. my $total = Math::BigFloat->bzero();
  63. my %passes;
  64. foreach my $tax (@taxes) {
  65. push @{$passes{$tax->pass}}, $tax;
  66. }
  67. my @passkeys = sort keys %passes;
  68. @passkeys = reverse @passkeys if $extract;
  69. foreach my $pass (@passkeys) {
  70. my $passrate = Math::BigFloat->bzero();
  71. my $passtotal = Math::BigFloat->bzero();
  72. foreach my $tax (@{$passes{$pass}}) {
  73. $passrate += $tax->rate;
  74. }
  75. foreach my $tax (@{$passes{$pass}}) {
  76. $passtotal += $tax->apply_tax($form, $subtotal + $total) if not $extract;
  77. $passtotal += $tax->extract_tax($form, $subtotal - $total, $passrate) if $extract;
  78. }
  79. $total += $passtotal;
  80. }
  81. return $total;
  82. }
  83. sub apply_taxes {
  84. my ($taxes, $form, $subtotal) = @_;
  85. return $subtotal + calculate_taxes($taxes, $form, $subtotal, 0);
  86. }
  87. sub extract_taxes {
  88. my ($taxes, $form, $subtotal) = @_;
  89. return $subtotal - calculate_taxes($taxes, $form, $subtotal, 1);
  90. }
  91. 1;