[freeside-commits] freeside/FS/FS cust_main.pm, 1.431, 1.432 cust_main_exemption.pm, NONE, 1.1 Schema.pm, 1.144, 1.145 Conf.pm, 1.290, 1.291

Ivan,,, ivan at wavetail.420.am
Mon Jun 22 00:50:20 PDT 2009


Update of /home/cvs/cvsroot/freeside/FS/FS
In directory wavetail.420.am:/tmp/cvs-serv11986/FS/FS

Modified Files:
	cust_main.pm Schema.pm Conf.pm 
Added Files:
	cust_main_exemption.pm 
Log Message:
tax exemption by tax name, RT#5127

--- NEW FILE: cust_main_exemption.pm ---
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: Schema.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Schema.pm,v
retrieving revision 1.144
retrieving revision 1.145
diff -u -d -r1.144 -r1.145
--- Schema.pm	21 Jun 2009 15:21:29 -0000	1.144
+++ Schema.pm	22 Jun 2009 07:50:18 -0000	1.145
@@ -788,6 +788,18 @@
       '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: Conf.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Conf.pm,v
retrieving revision 1.290
retrieving revision 1.291
diff -u -d -r1.290 -r1.291
--- Conf.pm	17 Jun 2009 02:39:08 -0000	1.290
+++ Conf.pm	22 Jun 2009 07:50:18 -0000	1.291
@@ -2828,6 +2828,13 @@
   },
 
   {
+    '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: cust_main.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_main.pm,v
retrieving revision 1.431
retrieving revision 1.432
diff -u -d -r1.431 -r1.432
--- cust_main.pm	19 Jun 2009 07:46:29 -0000	1.431
+++ cust_main.pm	22 Jun 2009 07:50:18 -0000	1.432
@@ -40,6 +40,7 @@
 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 @@
 
   $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 @@
 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 @@
     $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 @@
     }
   }
 
+  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 @@
 
 }
 
-=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 @@
 
   $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 @@
     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 @@
     $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 @@
 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 @@
     $total_bill += $cust_bill->owed;
   }
   sprintf( "%.2f", $total_bill );
+
 }
 
 =item total_paid
@@ -6287,6 +6376,20 @@
   }
 }
 
+=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



More information about the freeside-commits mailing list