summaryrefslogtreecommitdiff
path: root/LedgerSMB/Tax.pm
blob: bc0ba5fe5d5bc93b1c8922b51b336e5d83c5ddfb (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, $taxaccounts2 ) = @_;
  32. my $dbh = $form->{dbh};
  33. @taxes = ();
  34. my @accounts = split / /, $taxaccounts;
  35. my $transdate = $form->{transdate} || 'now()';
  36. if ( defined $taxaccounts2 ) {
  37. my @tmpaccounts = @accounts;
  38. $#accounts = -1;
  39. for my $acct ( split / /, $taxaccounts2 ) {
  40. if ( $taxaccounts =~ /\b$acct\b/ ) {
  41. push @accounts, $acct;
  42. }
  43. }
  44. }
  45. my $query = qq|SELECT t.taxnumber, c.description,
  46. t.rate, t.chart_id, t.pass, m.taxmodulename
  47. FROM tax t INNER JOIN chart c ON (t.chart_id = c.id)
  48. INNER JOIN taxmodule m ON (t.taxmodule_id = m.taxmodule_id)
  49. WHERE c.accno = ? AND coalesce(validto, ?) >= ?
  50. ORDER BY coalesce(validto, now()) DESC|;
  51. my $sth = $dbh->prepare($query);
  52. foreach $taxaccount (@accounts) {
  53. next if ( !defined $taxaccount );
  54. if ( defined $taxaccounts2 ) {
  55. next if $taxaccounts2 !~ /\b$taxaccount\b/;
  56. }
  57. $sth->execute($taxaccount, $transdate, $transdate)
  58. || $form->dberror($query);
  59. my $ref = $sth->fetchrow_hashref;
  60. my $module = $ref->{'taxmodulename'};
  61. require "LedgerSMB/Taxes/${module}.pm";
  62. $module =~ s/\//::/g;
  63. my $tax = ( eval 'Taxes::' . $module )->new();
  64. $tax->pass( $ref->{'pass'} );
  65. $tax->account($taxaccount);
  66. $tax->rate( Math::BigFloat->new( $ref->{'rate'} ) );
  67. $tax->taxnumber( $ref->{'taxnumber'} );
  68. $tax->chart( $ref->{'chart'} );
  69. $tax->description( $ref->{'description'} );
  70. $tax->value( Math::BigFloat->bzero() );
  71. push @taxes, $tax;
  72. $sth->finish;
  73. }
  74. return @taxes;
  75. }
  76. sub calculate_taxes {
  77. my ( $taxes, $form, $subtotal, $extract ) = @_;
  78. my $total = Math::BigFloat->bzero();
  79. my %passes;
  80. foreach my $tax (@taxes) {
  81. push @{ $passes{ $tax->pass } }, $tax;
  82. }
  83. my @passkeys = sort keys %passes;
  84. @passkeys = reverse @passkeys if $extract;
  85. foreach my $pass (@passkeys) {
  86. my $passrate = Math::BigFloat->bzero();
  87. my $passtotal = Math::BigFloat->bzero();
  88. foreach my $tax ( @{ $passes{$pass} } ) {
  89. $passrate += $tax->rate;
  90. }
  91. foreach my $tax ( @{ $passes{$pass} } ) {
  92. $passtotal += $tax->apply_tax( $form, $subtotal + $total )
  93. if not $extract;
  94. $passtotal +=
  95. $tax->extract_tax( $form, $subtotal - $total, $passrate )
  96. if $extract;
  97. }
  98. $total += $passtotal;
  99. }
  100. return $total;
  101. }
  102. sub apply_taxes {
  103. my ( $taxes, $form, $subtotal ) = @_;
  104. return $subtotal + calculate_taxes( $taxes, $form, $subtotal, 0 );
  105. }
  106. sub extract_taxes {
  107. my ( $taxes, $form, $subtotal ) = @_;
  108. return $subtotal - calculate_taxes( $taxes, $form, $subtotal, 1 );
  109. }
  110. 1;