summaryrefslogtreecommitdiff
path: root/FS/FS
diff options
context:
space:
mode:
Diffstat (limited to 'FS/FS')
-rw-r--r--FS/FS/Conf.pm7
-rw-r--r--FS/FS/Schema.pm12
-rw-r--r--FS/FS/cust_main.pm109
-rw-r--r--FS/FS/cust_main_exemption.pm128
4 files changed, 253 insertions, 3 deletions
diff --git a/FS/FS/Conf.pm b/FS/FS/Conf.pm
index 8065f3b..543c770 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 69a96a9..0ca15a6 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 9f9ae18..51ba209 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<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
index 0000000..06d22b7
--- /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<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;
+