summaryrefslogtreecommitdiff
path: root/LedgerSMB/Tax.pm
blob: edf3bccc09eb2b0ccdfb22672d610d78513b12f0 (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 )
  77. if not $extract;
  78. $passtotal +=
  79. $tax->extract_tax( $form, $subtotal - $total, $passrate )
  80. if $extract;
  81. }
  82. $total += $passtotal;
  83. }
  84. return $total;
  85. }
  86. sub apply_taxes {
  87. my ( $taxes, $form, $subtotal ) = @_;
  88. return $subtotal + calculate_taxes( $taxes, $form, $subtotal, 0 );
  89. }
  90. sub extract_taxes {
  91. my ( $taxes, $form, $subtotal ) = @_;
  92. return $subtotal - calculate_taxes( $taxes, $form, $subtotal, 1 );
  93. }
  94. 1;