tax exemption by tax name, RT#5127
authorivan <ivan>
Mon, 22 Jun 2009 07:50:18 +0000 (07:50 +0000)
committerivan <ivan>
Mon, 22 Jun 2009 07:50:18 +0000 (07:50 +0000)
FS/FS.pm
FS/FS/Conf.pm
FS/FS/Schema.pm
FS/FS/cust_main.pm
FS/FS/cust_main_exemption.pm [new file with mode: 0644]
FS/MANIFEST
FS/t/cust_main_exemption.t [new file with mode: 0644]
httemplate/edit/cust_main/billing.html
httemplate/edit/process/cust_main.cgi
httemplate/view/cust_main/billing.html

index c4be977..e34b45d 100644 (file)
--- a/FS/FS.pm
+++ b/FS/FS.pm
@@ -228,6 +228,8 @@ L<FS::cust_main_Mixin> - Mixin class for records that contain fields from cust_m
 
 L<FS::cust_main_invoice> - Invoice destination class
 
+L<FS::cust_main_exemption> - Customer tax exemption class
+
 L<FS::cust_main_note> - Customer note class
 
 L<FS::banned_pay> - Banned payment information class
index 8065f3b..543c770 100644 (file)
@@ -2828,6 +2828,13 @@ worry that config_items is freeside-specific and icky.
   },
 
   {
+    'key'         => 'tax-cust_exempt-groups',
+    'section'     => '',
+    'description' => 'List of grouping possibilities for tax names, for per-customer exemption purposes, one tax name prefix for line.  For example, "GST" would indicate the ability to exempt customers individually from taxes starting with "GST" (but not other taxes).',
+    'type'        => 'textarea',
+  },
+
+  {
     'key'         => 'cust_main-default_view',
     'section'     => 'UI',
     'description' => 'Default customer view, for users who have not selected a default view in their preferences.',
index 69a96a9..0ca15a6 100644 (file)
@@ -788,6 +788,18 @@ sub tables_hashref {
       'index' => [ [ 'custnum' ], [ '_date' ], ],
     },
 
+    'cust_main_exemption' => {
+      'columns' => [
+        'exemptionnum', 'serial', '',      '', '', '',
+        'custnum',         'int', '',      '', '', '', 
+        'taxname',     'varchar', '', $char_d, '', '',
+        #start/end dates?  for reporting?
+      ],
+      'primary_key' => 'exemptionnum',
+      'unique'      => [],
+      'index'       => [ [ 'custnum' ] ],
+    },
+
     'cust_main_county' => { #county+state+country are checked off the
                             #cust_main_county for validation and to provide
                             # a tax rate.
index 9f9ae18..51ba209 100644 (file)
@@ -40,6 +40,7 @@ use FS::cust_refund;
 use FS::part_referral;
 use FS::cust_main_county;
 use FS::cust_location;
+use FS::cust_main_exemption;
 use FS::tax_rate;
 use FS::tax_rate_location;
 use FS::cust_tax_location;
@@ -363,7 +364,7 @@ invoicing_list destination to the newly-created svc_acct.  Here's an example:
 
   $cust_main->insert( {}, [ $email, 'POST' ] );
 
-Currently available options are: I<depend_jobnum> and I<noexport>.
+Currently available options are: I<depend_jobnum>, I<noexport> and I<tax_exemption>.
 
 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
 on the supplied jobnum (they will not run until the specific job completes).
@@ -374,6 +375,9 @@ The I<noexport> option is deprecated.  If I<noexport> is set true, no
 provisioning jobs (exports) are scheduled.  (You can schedule them later with
 the B<reexport> method.)
 
+The I<tax_exemption> option can be set to an arrayref of tax names.
+FS::cust_main_exemption records will be created and inserted.
+
 =cut
 
 sub insert {
@@ -459,6 +463,24 @@ sub insert {
     $self->invoicing_list( $invoicing_list );
   }
 
+  warn "  setting cust_main_exemption\n"
+    if $DEBUG > 1;
+
+  my $tax_exemption = delete $options{'tax_exemption'};
+  if ( $tax_exemption ) {
+    foreach my $taxname ( @$tax_exemption ) {
+      my $cust_main_exemption = new FS::cust_main_exemption {
+        'custnum' => $self->custnum,
+        'taxname' => $taxname,
+      };
+      my $error = $cust_main_exemption->insert;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return "inserting cust_main_exemption (transaction rolled back): $error";
+      }
+    }
+  }
+
   if (    $conf->config('cust_main-skeleton_tables')
        && $conf->config('cust_main-skeleton_custnum') ) {
 
@@ -1295,6 +1317,16 @@ sub delete {
     }
   }
 
+  foreach my $cust_main_exemption (
+    qsearch( 'cust_main_exemption', { 'custnum' => $self->custnum } )
+  ) {
+    my $error = $cust_main_exemption->delete;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
+    }
+  }
+
   my $error = $self->SUPER::delete;
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
@@ -1306,7 +1338,8 @@ sub delete {
 
 }
 
-=item replace [ OLD_RECORD ] [ INVOICING_LIST_ARYREF ]
+=item replace [ OLD_RECORD ] [ INVOICING_LIST_ARYREF ] [ , OPTION => VALUE ... ] ]
+
 
 Replaces the OLD_RECORD with this one in the database.  If there is an error,
 returns the error, otherwise returns false.
@@ -1318,6 +1351,11 @@ check_invoicing_list first.  Here's an example:
 
   $new_cust_main->replace( $old_cust_main, [ $email, 'POST' ] );
 
+Currently available options are: I<tax_exemption>.
+
+The I<tax_exemption> option can be set to an arrayref of tax names.
+FS::cust_main_exemption records will be deleted and inserted as appropriate.
+
 =cut
 
 sub replace {
@@ -1364,7 +1402,7 @@ sub replace {
     return $error;
   }
 
-  if ( @param ) { # INVOICING_LIST_ARYREF
+  if ( @param && ref($param[0]) eq 'ARRAY' ) { # INVOICING_LIST_ARYREF
     my $invoicing_list = shift @param;
     $error = $self->check_invoicing_list( $invoicing_list );
     if ( $error ) {
@@ -1374,6 +1412,40 @@ sub replace {
     $self->invoicing_list( $invoicing_list );
   }
 
+  my %options = @param;
+
+  my $tax_exemption = delete $options{'tax_exemption'};
+  if ( $tax_exemption ) {
+
+    my %cust_main_exemption =
+      map { $_->taxname => $_ }
+          qsearch('cust_main_exemption', { 'custnum' => $old->custnum } );
+
+    foreach my $taxname ( @$tax_exemption ) {
+
+      next if delete $cust_main_exemption{$taxname};
+
+      my $cust_main_exemption = new FS::cust_main_exemption {
+        'custnum' => $self->custnum,
+        'taxname' => $taxname,
+      };
+      my $error = $cust_main_exemption->insert;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return "inserting cust_main_exemption (transaction rolled back): $error";
+      }
+    }
+
+    foreach my $cust_main_exemption ( values %cust_main_exemption ) {
+      my $error = $cust_main_exemption->delete;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return "deleting cust_main_exemption (transaction rolled back): $error";
+      }
+    }
+
+  }
+
   if ( $self->payby =~ /^(CARD|CHEK|LECB)$/ &&
        grep { $self->get($_) ne $old->get($_) } qw(payinfo paydate payname) ) {
     # card/check/lec info has changed, want to retry realtime_ invoice events
@@ -6054,6 +6126,22 @@ see L<Time::Local> and L<Date::Parse> for conversion functions.
 sub total_owed_date {
   my $self = shift;
   my $time = shift;
+
+#  my $custnum = $self->custnum;
+#
+#  my $owed_sql = FS::cust_bill->owed_sql;
+#
+#  my $sql = "
+#    SELECT SUM($owed_sql) FROM cust_bill
+#      WHERE custnum = $custnum
+#        AND _date <= $time
+#  ";
+#
+#  my $sth = dbh->prepare($sql) or die dbh->errstr;
+#  $sth->execute() or die $sth->errstr;
+#
+#  return sprintf( '%.2f', $sth->fetchrow_arrayref->[0] );
+
   my $total_bill = 0;
   foreach my $cust_bill (
     grep { $_->_date <= $time }
@@ -6062,6 +6150,7 @@ sub total_owed_date {
     $total_bill += $cust_bill->owed;
   }
   sprintf( "%.2f", $total_bill );
+
 }
 
 =item total_paid
@@ -6287,6 +6376,20 @@ sub paydate_monthyear {
   }
 }
 
+=item tax_exemption TAXNAME
+
+=cut
+
+sub tax_exemption {
+  my( $self, $taxname ) = @_;
+
+  qsearchs( 'cust_main_exemption', { 'custnum' => $self->custnum,
+                                     'taxname' => { 'op'    => 'LIKE',
+                                                    'value' => $taxname.'%' },
+                                    },
+          );
+}
+
 =item invoicing_list [ ARRAYREF ]
 
 If an arguement is given, sets these email addresses as invoice recipients
diff --git a/FS/FS/cust_main_exemption.pm b/FS/FS/cust_main_exemption.pm
new file mode 100644 (file)
index 0000000..06d22b7
--- /dev/null
@@ -0,0 +1,128 @@
+package FS::cust_main_exemption;
+
+use strict;
+use base qw( FS::Record );
+use FS::Record qw( qsearch qsearchs );
+use FS::cust_main;
+
+=head1 NAME
+
+FS::cust_main_exemption - Object methods for cust_main_exemption records
+
+=head1 SYNOPSIS
+
+  use FS::cust_main_exemption;
+
+  $record = new FS::cust_main_exemption \%hash;
+  $record = new FS::cust_main_exemption { 'column' => 'value' };
+
+  $error = $record->insert;
+
+  $error = $new_record->replace($old_record);
+
+  $error = $record->delete;
+
+  $error = $record->check;
+
+=head1 DESCRIPTION
+
+An FS::cust_main_exemption object represents a customer tax exemption from a
+specific tax name (prefix).  FS::cust_main_exemption inherits from
+FS::Record.  The following fields are currently supported:
+
+=over 4
+
+=item exemptionnum
+
+Primary key
+
+=item custnum
+
+Customer (see L<FS::cust_main>)
+
+=item taxname
+
+taxname
+
+
+=back
+
+=head1 METHODS
+
+=over 4
+
+=item new HASHREF
+
+Creates a new record.  To add the record to the database, see L<"insert">.
+
+Note that this stores the hash reference, not a distinct copy of the hash it
+points to.  You can ask the object for a copy with the I<hash> method.
+
+=cut
+
+# the new method can be inherited from FS::Record, if a table method is defined
+
+sub table { 'cust_main_exemption'; }
+
+=item insert
+
+Adds this record to the database.  If there is an error, returns the error,
+otherwise returns false.
+
+=cut
+
+# the insert method can be inherited from FS::Record
+
+=item delete
+
+Delete this record from the database.
+
+=cut
+
+# the delete method can be inherited from FS::Record
+
+=item replace OLD_RECORD
+
+Replaces the OLD_RECORD with this one in the database.  If there is an error,
+returns the error, otherwise returns false.
+
+=cut
+
+# the replace method can be inherited from FS::Record
+
+=item check
+
+Checks all fields to make sure this is a valid record.  If there is
+an error, returns the error, otherwise returns false.  Called by the insert
+and replace methods.
+
+=cut
+
+# the check method should currently be supplied - FS::Record contains some
+# data checking routines
+
+sub check {
+  my $self = shift;
+
+  my $error = 
+    $self->ut_numbern('exemptionnum')
+    || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
+    || $self->ut_text('taxname')
+  ;
+  return $error if $error;
+
+  $self->SUPER::check;
+}
+
+=back
+
+=head1 BUGS
+
+=head1 SEE ALSO
+
+L<FS::cust_main>, L<FS::Record>, schema.html from the base documentation.
+
+=cut
+
+1;
+
index b77d5d8..b52d7b3 100644 (file)
@@ -440,3 +440,5 @@ FS/cust_bill_pkg_tax_rate_location.pm
 t/cust_bill_pkg_tax_rate_location.t
 FS/cust_recon.pm
 t/cust_recon.t
+FS/cust_main_exemption.pm
+t/cust_main_exemption.t
diff --git a/FS/t/cust_main_exemption.t b/FS/t/cust_main_exemption.t
new file mode 100644 (file)
index 0000000..fec6d19
--- /dev/null
@@ -0,0 +1,5 @@
+BEGIN { $| = 1; print "1..1\n" }
+END {print "not ok 1\n" unless $loaded;}
+use FS::cust_main_exemption;
+$loaded=1;
+print "ok 1\n";
index 353f2b9..f0d9b85 100644 (file)
 
     <TR><TD>&nbsp;</TD></TR>
 
+%   my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
+
     <TR>
-      <TD WIDTH="608" COLSPAN="2"><INPUT TYPE="checkbox" NAME="tax" VALUE="Y" <% $cust_main->tax eq "Y" ? 'CHECKED' : '' %>> Tax Exempt</TD>
+      <TD WIDTH="608" COLSPAN="2"><INPUT TYPE="checkbox" NAME="tax" VALUE="Y" <% $cust_main->tax eq "Y" ? 'CHECKED' : '' %>> Tax Exempt<% @exempt_groups ? ' (all taxes)' : '' %></TD>
     </TR>
 
+%   foreach my $exempt_group ( @exempt_groups ) {
+%     #escape $exempt_group for NAME
+      <TR>
+        <TD WIDTH="608" COLSPAN="2">&nbsp;&nbsp;<INPUT TYPE="checkbox" NAME="tax_<% $exempt_group %>" VALUE="Y" <% $cust_main->tax_exemption($exempt_group) ? 'CHECKED' : '' %>> Tax Exempt (<% $exempt_group %> taxes)<TD>
+      </TR>
+%   }
+
 % unless ( $conf->exists('emailinvoiceonly') ) {
 
     <TR>
index 7a0c67b..1709752 100755 (executable)
@@ -19,6 +19,8 @@ my $DEBUG = 0;
 die "access denied"
   unless $FS::CurrentUser::CurrentUser->access_right('Edit customer');
 
+my $conf = new FS::Conf;
+
 my $error = '';
 
 #unmunge stuff
@@ -72,7 +74,6 @@ if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
 }
 
 if ( $cgi->param('birthdate') && $cgi->param('birthdate') =~ /^([ 0-9\-\/]{0,10})$/) {
-  my $conf = new FS::Conf;
   my $format = $conf->config('date_format') || "%m/%d/%Y";
   my $parser = DateTime::Format::Strptime->new(pattern => $format,
                                                time_zone => 'floating',
@@ -91,6 +92,9 @@ if ( $cgi->param('birthdate') && $cgi->param('birthdate') =~ /^([ 0-9\-\/]{0,10}
 $new->setfield('paid', $cgi->param('paid') )
   if $cgi->param('paid');
 
+my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
+my @tax_exempt = grep { $cgi->param("tax_$_") eq 'Y' } @exempt_groups;
+
 #perhaps this stuff should go to cust_main.pm
 if ( $new->custnum eq '' ) {
 
@@ -179,7 +183,9 @@ if ( $new->custnum eq '' ) {
   use Tie::RefHash;
   tie my %hash, 'Tie::RefHash';
   %hash = ( $cust_pkg => [ $svc ] ) if $cust_pkg;
-  $error ||= $new->insert( \%hash, \@invoicing_list );
+  $error ||= $new->insert( \%hash, \@invoicing_list,
+                           'tax_exemption' => \@tax_exempt,
+                         );
 
   my $conf = new FS::Conf;
   if ( $conf->exists('backend-realtime') && ! $error ) {
@@ -222,7 +228,9 @@ if ( $new->custnum eq '' ) {
   local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
   local($FS::Record::DEBUG)    = $DEBUG if $DEBUG;
 
-  $error ||= $new->replace($old, \@invoicing_list);
+  $error ||= $new->replace( $old, \@invoicing_list,
+                            'tax_exemption' => \@tax_exempt,
+                          );
 
   warn "$me returned from replace" if $DEBUG;
   
index 4b2425b..e02c048 100644 (file)
@@ -159,11 +159,18 @@ Billing information
 </TR>
 % } 
 
-
+% my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
 <TR>
-  <TD ALIGN="right">Tax&nbsp;exempt</TD>
+  <TD ALIGN="right">Tax&nbsp;exempt<% @exempt_groups ? ' (all taxes)' : '' %></TD>
   <TD BGCOLOR="#ffffff"><% $cust_main->tax ? 'yes' : 'no' %></TD>
 </TR>
+% foreach my $exempt_group ( @exempt_groups ) {
+<TR>
+  <TD ALIGN="right">Tax&nbsp;exempt (<% $exempt_group %> taxes)</TD>
+  <TD BGCOLOR="#ffffff"><% $cust_main->tax_exemption($exempt_group) ? 'yes' : 'no' %></TD>
+</TR>
+% }
+
 % if ( $conf->exists('enable_taxproducts') ) {
 <TR>
   <TD ALIGN="right">Tax&nbsp;location</TD>