From 73e382b838f031512684138fedb7d813684ddd28 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 22 Jun 2009 07:50:18 +0000 Subject: [PATCH] tax exemption by tax name, RT#5127 --- FS/FS.pm | 2 + FS/FS/Conf.pm | 7 ++ FS/FS/Schema.pm | 12 ++++ FS/FS/cust_main.pm | 109 +++++++++++++++++++++++++++- FS/FS/cust_main_exemption.pm | 128 +++++++++++++++++++++++++++++++++ FS/MANIFEST | 2 + FS/t/cust_main_exemption.t | 5 ++ httemplate/edit/cust_main/billing.html | 11 ++- httemplate/edit/process/cust_main.cgi | 14 +++- httemplate/view/cust_main/billing.html | 11 ++- 10 files changed, 292 insertions(+), 9 deletions(-) create mode 100644 FS/FS/cust_main_exemption.pm create mode 100644 FS/t/cust_main_exemption.t diff --git a/FS/FS.pm b/FS/FS.pm index c4be977f1..e34b45db6 100644 --- a/FS/FS.pm +++ b/FS/FS.pm @@ -228,6 +228,8 @@ L - Mixin class for records that contain fields from cust_m L - Invoice destination class +L - Customer tax exemption class + L - Customer note class L - Banned payment information class diff --git a/FS/FS/Conf.pm b/FS/FS/Conf.pm index 8065f3b47..543c77027 100644 --- a/FS/FS/Conf.pm +++ b/FS/FS/Conf.pm @@ -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.', diff --git a/FS/FS/Schema.pm b/FS/FS/Schema.pm index 69a96a99a..0ca15a64d 100644 --- a/FS/FS/Schema.pm +++ b/FS/FS/Schema.pm @@ -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. diff --git a/FS/FS/cust_main.pm b/FS/FS/cust_main.pm index 9f9ae1869..51ba20920 100644 --- a/FS/FS/cust_main.pm +++ b/FS/FS/cust_main.pm @@ -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 and I. +Currently available options are: I, I and I. If I 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 option is deprecated. If I is set true, no provisioning jobs (exports) are scheduled. (You can schedule them later with the B method.) +The I 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. + +The I 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 and L 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 index 000000000..06d22b7e0 --- /dev/null +++ b/FS/FS/cust_main_exemption.pm @@ -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) + +=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 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, L, schema.html from the base documentation. + +=cut + +1; + diff --git a/FS/MANIFEST b/FS/MANIFEST index b77d5d892..b52d7b318 100644 --- a/FS/MANIFEST +++ b/FS/MANIFEST @@ -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 index 000000000..fec6d197e --- /dev/null +++ b/FS/t/cust_main_exemption.t @@ -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"; diff --git a/httemplate/edit/cust_main/billing.html b/httemplate/edit/cust_main/billing.html index 353f2b9a0..f0d9b853a 100644 --- a/httemplate/edit/cust_main/billing.html +++ b/httemplate/edit/cust_main/billing.html @@ -359,10 +359,19 @@   +% my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups'); + - tax eq "Y" ? 'CHECKED' : '' %>> Tax Exempt + tax eq "Y" ? 'CHECKED' : '' %>> Tax Exempt<% @exempt_groups ? ' (all taxes)' : '' %> +% foreach my $exempt_group ( @exempt_groups ) { +% #escape $exempt_group for NAME + +   tax_exemption($exempt_group) ? 'CHECKED' : '' %>> Tax Exempt (<% $exempt_group %> taxes) + +% } + % unless ( $conf->exists('emailinvoiceonly') ) { diff --git a/httemplate/edit/process/cust_main.cgi b/httemplate/edit/process/cust_main.cgi index 7a0c67b4a..1709752fb 100755 --- a/httemplate/edit/process/cust_main.cgi +++ b/httemplate/edit/process/cust_main.cgi @@ -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; diff --git a/httemplate/view/cust_main/billing.html b/httemplate/view/cust_main/billing.html index 4b2425b33..e02c04868 100644 --- a/httemplate/view/cust_main/billing.html +++ b/httemplate/view/cust_main/billing.html @@ -159,11 +159,18 @@ Billing information % } - +% my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups'); - Tax exempt + Tax exempt<% @exempt_groups ? ' (all taxes)' : '' %> <% $cust_main->tax ? 'yes' : 'no' %> +% foreach my $exempt_group ( @exempt_groups ) { + + Tax exempt (<% $exempt_group %> taxes) + <% $cust_main->tax_exemption($exempt_group) ? 'yes' : 'no' %> + +% } + % if ( $conf->exists('enable_taxproducts') ) { Tax location -- 2.11.0