summaryrefslogtreecommitdiff
path: root/LedgerSMB/Tax.pm
blob: 9de046bf3a8c3e2a3e453bc24a414404a6dcb141 (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, 'now()') >= ?
  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) || $form->dberror($query);
  58. my $ref = $sth->fetchrow_hashref;
  59. my $module = $ref->{'taxmodulename'};
  60. require "LedgerSMB/Taxes/${module}.pm";
  61. $module =~ s/\//::/g;
  62. my $tax = ( eval 'Taxes::' . $module )->new();
  63. $tax->pass( $ref->{'pass'} );
  64. $tax->account($taxaccount);
  65. $tax->rate( Math::BigFloat->new( $ref->{'rate'} ) );
  66. $tax->taxnumber( $ref->{'taxnumber'} );
  67. $tax->chart( $ref->{'chart'} );
  68. $tax->description( $ref->{'description'} );
  69. $tax->value( Math::BigFloat->bzero() );
  70. push @taxes, $tax;
  71. $sth->finish;
  72. }
  73. return @taxes;
  74. }
  75. sub calculate_taxes {
  76. my ( $taxes, $form, $subtotal, $extract ) = @_;
  77. my $total = Math::BigFloat->bzero();
  78. my %passes;
  79. foreach my $tax (@taxes) {
  80. push @{ $passes{ $tax->pass } }, $tax;
  81. }
  82. my @passkeys = sort keys %passes;
  83. @passkeys = reverse @passkeys if $extract;
  84. foreach my $pass (@passkeys) {
  85. my $passrate = Math::BigFloat->bzero();
  86. my $passtotal = Math::BigFloat->bzero();
  87. foreach my $tax ( @{ $passes{$pass} } ) {
  88. $passrate += $tax->rate;
  89. }
  90. foreach my $tax ( @{ $passes{$pass} } ) {
  91. $passtotal += $tax->apply_tax( $form, $subtotal + $total )
  92. if not $extract;
  93. $passtotal +=
  94. $tax->extract_tax( $form, $subtotal - $total, $passrate )
  95. if $extract;
  96. }
  97. $total += $passtotal;
  98. }
  99. return $total;
  100. }
  101. sub apply_taxes {
  102. my ( $taxes, $form, $subtotal ) = @_;
  103. return $subtotal + calculate_taxes( $taxes, $form, $subtotal, 0 );
  104. }
  105. sub extract_taxes {
  106. my ( $taxes, $form, $subtotal ) = @_;
  107. return $subtotal - calculate_taxes( $taxes, $form, $subtotal, 1 );
  108. }
  109. 1;