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