[freeside-commits] freeside/FS/FS Schema.pm, 1.101, 1.102 cust_bill_pkg.pm, 1.22, 1.23 cust_main.pm, 1.361, 1.362 part_pkg.pm, 1.75, 1.76

Jeff Finucane,420,, jeff at wavetail.420.am
Thu Aug 28 00:38:41 PDT 2008


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

Modified Files:
	Schema.pm cust_bill_pkg.pm cust_main.pm part_pkg.pm 
Log Message:
multiple usage classes checkpoint

Index: Schema.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Schema.pm,v
retrieving revision 1.101
retrieving revision 1.102
diff -u -d -r1.101 -r1.102
--- Schema.pm	25 Aug 2008 05:53:11 -0000	1.101
+++ Schema.pm	28 Aug 2008 07:38:38 -0000	1.102
@@ -502,6 +502,7 @@
         'unitrecur', @money_typen, '', '', 
         'duplicate',  'char', 'NULL', 1, '', '',
         'post_total', 'char', 'NULL', 1, '', '',
+        'type',       'char', 'NULL', 1, '', '',
       ],
       'primary_key' => 'billpkgnum',
       'unique' => [],
@@ -514,7 +515,9 @@
         'billpkgnum', 'int', 'NULL', '', '', '',        # should not be nullable
         'pkgnum',  'int', 'NULL', '', '', '',           # deprecated
         'invnum',  'int', 'NULL', '', '', '',           # deprecated
+        'amount',  @money_typen, '', '', 
         'format',  'char', 'NULL', 1, '', '',
+        'classnum', 'char', 'NULL', 1, '', '',
         'detail',  'varchar', '', $char_d, '', '', 
       ],
       'primary_key' => 'detailnum',
@@ -1103,6 +1106,7 @@
         'taxoverridenum', 'serial', '', '', '', '',
         'pkgpart',        'serial', '', '', '', '',
         'taxclassnum',    'serial', '', '', '', '',
+        'usage_class',    'varchar', 'NULL', $char_d, '', '', 
       ],
       'primary_key' => 'taxoverridenum',
       'unique' => [],

Index: cust_bill_pkg.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_bill_pkg.pm,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -d -r1.22 -r1.23
--- cust_bill_pkg.pm	25 Aug 2008 05:53:13 -0000	1.22
+++ cust_bill_pkg.pm	28 Aug 2008 07:38:38 -0000	1.23
@@ -47,6 +47,8 @@
 =item pkgnum - package (see L<FS::cust_pkg>) or 0 for the special virtual sales tax package, or -1 for the virtual line item (itemdesc is used for the line)
 
 =item pkgpart_override - optional package definition (see L<FS::part_pkg>) override
+=item type - can be set to U for usage; more later
+
 =item setup - setup fee
 
 =item recur - recurring fee
@@ -137,6 +139,8 @@
       'billpkgnum' => $self->billpkgnum,
       'format'     => (ref($detail) ? $detail->[0] : '' ),
       'detail'     => (ref($detail) ? $detail->[1] : $detail ),
+      'charge'     => (ref($detail) ? $detail->[2] : '' ),
+      'classnum'   => (ref($detail) ? $detail->[3] : '' ),
     };
     $error = $cust_bill_pkg_detail->insert;
     if ( $error ) {
@@ -195,6 +199,7 @@
       || $self->ut_textn('section')
       || $self->ut_enum('duplicate', [ '', 'Y' ])
       || $self->ut_enum('post_total', [ '', 'Y' ])
+      || $self->ut_enum('type', [ '', 'U' ])      #only usage for now
   ;
   return $error if $error;
 
@@ -458,6 +463,70 @@
   $self->pkgnum && $self->section ne $self->part_pkg->categoryname;
 }
 
+=item usage CLASSNUM
+
+Returns the amount of the charge associated with usage class CLASSNUM if
+CLASSNUM is defined.  Otherwise returns the total charge associated with
+usage.
+  
+=cut
+
+sub usage {
+  my( $self, $classnum ) = @_;
+  my $sum = 0;
+  my @values = ();
+
+  if ( $self->get('details') ) {
+
+    @values = 
+      map { $_->[2] }
+      grep { ref($_) && ( defined($classnum) ? $_->[3] eq $classnum : 1 ) }
+      $self->get('details');
+
+  }else{
+
+    my $hashref = { 'billpkgnum' => $self->billpkgnum };
+    $hashref->{ 'classnum' } = $classnum if defined($classnum);
+    @values = map { $_->charge } qsearch('cust_bill_pkg_detail', $hashref);
+
+  }
+
+  foreach ( @values ) {
+    $sum += $_ if $_;
+  }
+  $sum;
+}
+
+=item usage_classes
+
+Returns a list of usage classnums associated with this invoice line's
+details.
+  
+=cut
+
+sub usage_classes {
+  my( $self ) = @_;
+
+  if ( $self->get('details') ) {
+
+    my %seen = ();
+    foreach my $detail ( grep { ref($_) } @{$self->get('details')} ) {
+      $seen{ $detail->[3] } = 1;
+    }
+    keys %seen;
+
+  }else{
+
+    map { $_->classnum }
+        qsearch({ table   => 'cust_bill_pkg_detail',
+                  hashref => { billpkgnum => $self->billpkgnum },
+                  select  => 'DISTINCT classnum',
+               });
+
+  }
+
+}
+
 =back
 
 =head1 BUGS

Index: part_pkg.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/part_pkg.pm,v
retrieving revision 1.75
retrieving revision 1.76
diff -u -d -r1.75 -r1.76
--- part_pkg.pm	2 Aug 2008 04:10:01 -0000	1.75
+++ part_pkg.pm	28 Aug 2008 07:38:39 -0000	1.76
@@ -801,19 +801,84 @@
   );
 }
 
-=item part_pkg_taxoverride
+=item part_pkg_taxoverride [ CLASS ]
 
 Returns all associated FS::part_pkg_taxoverride objects (see
-L<FS::part_pkg_taxoverride>).
+L<FS::part_pkg_taxoverride>).  Limits the returned set to those
+of class CLASS if defined.  Class may be one of 'setup', 'recur',
+the empty string (default), or a usage class number (see L<FS::usage_class>).
+When a class is specified, the empty string class (default) is returned
+if no more specific values exist.
 
 =cut
 
 sub part_pkg_taxoverride {
   my $self = shift;
-  qsearch('part_pkg_taxoverride', { 'pkgpart' => $self->pkgpart } );
+  my $class = shift;
+
+  my $hashref = { 'pkgpart' => $self->pkgpart };
+  $hashref->{'usage_class'} = $class if defined($class);
+  my @overrides = qsearch('part_pkg_taxoverride', $hashref );
+
+  unless ( scalar(@overrides) || !defined($class) || !$class ){
+    $hashref->{'usage_class'} = '';
+    @overrides = qsearch('part_pkg_taxoverride', $hashref );
+  }
+
+  @overrides;
 }
 
-=item taxproduct_description
+=item has_taxproduct
+
+Returns true if this package has any taxproduct associated with it.  
+
+=cut
+
+sub has_taxproduct {
+  my $self = shift;
+
+  $self->taxproductnum ||
+  scalar(grep { $_ =~/^usage_taxproductnum_/ } keys %{ {$self->options} } )
+
+}
+
+
+=item taxproduct [ CLASS ]
+
+Returns the associated tax product for this package definition (see
+L<FS::part_pkg_taxproduct>).  CLASS may be one of 'setup', 'recur' or
+the usage classnum (see L<FS::usage_class>).  Returns the default
+tax product for this record if the more specific CLASS value does
+not exist.
+
+=cut
+
+sub taxproduct {
+  my $self = shift;
+  my $class = shift;
+
+  my $part_pkg_taxproduct;
+
+  my $taxproductnum = $self->taxproductnum;
+  if ($class) { 
+    my $class_taxproductnum = $self->option("usage_taxproductnum_$class", 1);
+    $taxproductnum = $class_taxproductnum
+      if $class_taxproductnum
+  }
+  
+  $part_pkg_taxproduct =
+    qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
+
+  unless ($part_pkg_taxproduct || $taxproductnum eq $self->taxproductnum ) {
+    $taxproductnum = $self->taxproductnum;
+    $part_pkg_taxproduct =
+      qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
+  }
+
+  $part_pkg_taxproduct;
+}
+
+=item taxproduct_description [ CLASS ]
 
 Returns the description of the associated tax product for this package
 definition (see L<FS::part_pkg_taxproduct>).
@@ -822,26 +887,24 @@
 
 sub taxproduct_description {
   my $self = shift;
-  my $part_pkg_taxproduct =
-    qsearchs( 'part_pkg_taxproduct',
-              { 'taxproductnum' => $self->taxproductnum }
-            );
+  my $part_pkg_taxproduct = $self->taxproduct(@_);
   $part_pkg_taxproduct ? $part_pkg_taxproduct->description : '';
 }
 
-=item part_pkg_taxrate DATA_PROVIDER, GEOCODE
+=item part_pkg_taxrate DATA_PROVIDER, GEOCODE, [ CLASS ]
 
 Returns the package to taxrate m2m records for this package in the location
-specified by GEOCODE (see L<FS::part_pkg_taxrate> and ).
+specified by GEOCODE (see L<FS::part_pkg_taxrate>) and usage class CLASS.
+CLASS may be one of 'setup', 'recur', or one of the usage classes numbers
+(see L<FS::usage_class>).
 
 =cut
 
 sub _expand_cch_taxproductnum {
   my $self = shift;
-  my $part_pkg_taxproduct =
-    qsearchs( 'part_pkg_taxproduct',
-              { 'taxproductnum' => $self->taxproductnum }
-            );
+  my $class = shift;
+  my $part_pkg_taxproduct = $self->taxproduct($class);
+
   my ($a,$b,$c,$d) = ( $part_pkg_taxproduct
                          ? ( split ':', $part_pkg_taxproduct->taxproduct )
                          : ()
@@ -859,7 +922,7 @@
 
 sub part_pkg_taxrate {
   my $self = shift;
-  my ($data_vendor, $geocode) = @_;
+  my ($data_vendor, $geocode, $class) = @_;
 
   my $dbh = dbh;
   my $extra_sql = 'WHERE part_pkg_taxproduct.data_vendor = '.
@@ -873,7 +936,9 @@
     ')';
   # much more CCH oddness in m2m -- this is kludgy
   $extra_sql .= ' AND ('.
-    join(' OR ', map{ "taxproductnum = $_" } $self->_expand_cch_taxproductnum).
+    join(' OR ', map{ "taxproductnum = $_" }
+                 $self->_expand_cch_taxproductnum($class)
+        ).
     ')';
 
   my $addl_from = 'LEFT JOIN part_pkg_taxproduct USING ( taxproductnum )';

Index: cust_main.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_main.pm,v
retrieving revision 1.361
retrieving revision 1.362
diff -u -d -r1.361 -r1.362
--- cust_main.pm	27 Aug 2008 00:15:23 -0000	1.361
+++ cust_main.pm	28 Aug 2008 07:38:38 -0000	1.362
@@ -2047,6 +2047,7 @@
 sub bill {
   my( $self, %options ) = @_;
   return '' if $self->payby eq 'COMP';
+  local $DEBUG = 1;
   warn "$me bill customer ". $self->custnum. "\n"
     if $DEBUG;
 
@@ -2460,7 +2461,6 @@
       };
       $cust_bill_pkg->pkgpart_override($part_pkg->pkgpart)
         unless $part_pkg->pkgpart == $real_pkgpart;
-      push @$cust_bill_pkgs, $cust_bill_pkg;
 
       $$total_setup += $setup;
       $$total_recur += $recur;
@@ -2471,7 +2471,14 @@
 
       unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
 
-        $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg);
+        #some garbage disappears on cust_bill_pkg refactor
+        my $err_or_cust_bill_pkg =
+          $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg);
+
+        return $err_or_cust_bill_pkg
+          unless ( ref($err_or_cust_bill_pkg) );
+
+        push @$cust_bill_pkgs, @$err_or_cust_bill_pkg;
 
       } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
 
@@ -2493,7 +2500,6 @@
 
       $cust_bill_pkg->pkgpart_override($part_pkg->pkgpart)
         unless $part_pkg->pkgpart == $real_pkgpart;
-      push @$appended_cust_bill_pkg, $cust_bill_pkg;
 
       unless ($cust_bill_pkg->duplicate) {
         $$total_setup += $cust_bill_pkg->setup;
@@ -2505,7 +2511,14 @@
 
         unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
 
-          $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg);
+          #some garbage disappears on cust_bill_pkg refactor
+          my $err_or_cust_bill_pkg =
+            $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg);
+
+          return $err_or_cust_bill_pkg
+            unless ( ref($err_or_cust_bill_pkg) );
+
+          push @$appended_cust_bill_pkg, @$err_or_cust_bill_pkg;
 
         } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
       }
@@ -2520,39 +2533,35 @@
   my $taxlisthash = shift;
   my $cust_bill_pkg = shift;
 
-  my @taxes = ();
-  my @taxoverrides = $part_pkg->part_pkg_taxoverride;
+  my %cust_bill_pkg = ();
+  my %taxes = ();
     
   my $prefix = 
     ( $conf->exists('tax-ship_address') && length($self->ship_last) )
     ? 'ship_'
     : '';
 
+  my @classes;
+  push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->type eq 'U';
+  push @classes, 'setup' if $cust_bill_pkg->setup;
+  push @classes, 'recur' if $cust_bill_pkg->recur;
+
   if ( $conf->exists('enable_taxproducts')
-       && (scalar(@taxoverrides) || $part_pkg->taxproductnum )
+       && (scalar($part_pkg->part_pkg_taxoverride) || $part_pkg->has_taxproduct)
      )
   { 
 
-    my @taxclassnums = ();
-    my $geocode = $self->geocode('cch');
-
-    if ( scalar( @taxoverrides ) ) {
-      @taxclassnums = map { $_->taxclassnum } @taxoverrides;
-    }elsif ( $part_pkg->taxproductnum ) {
-      @taxclassnums = map { $_->taxclassnum }
-                      $part_pkg->part_pkg_taxrate('cch', $geocode);
+    foreach my $class (@classes) {
+      my $err_or_ref = $self->_gather_taxes( $part_pkg, $class, $prefix );
+      return $err_or_ref unless ref($err_or_ref);
+      $taxes{$class} = $err_or_ref;
     }
 
-    my $extra_sql =
-      "AND (".
-      join(' OR ', map { "taxclassnum = $_" } @taxclassnums ). ")";
-
-    @taxes = qsearch({ 'table' => 'tax_rate',
-                       'hashref' => { 'geocode' => $geocode, },
-                       'extra_sql' => $extra_sql,
-                    })
-      if scalar(@taxclassnums);
-
+    unless (exists $taxes{''}) {
+      my $err_or_ref = $self->_gather_taxes( $part_pkg, '', $prefix );
+      return $err_or_ref unless ref($err_or_ref);
+      $taxes{''} = $err_or_ref;
+    }
 
   }else{
 
@@ -2561,7 +2570,7 @@
 
     $taxhash{'taxclass'} = $part_pkg->taxclass;
 
-    @taxes = qsearch( 'cust_main_county', \%taxhash );
+    my @taxes = qsearch( 'cust_main_county', \%taxhash );
 
     unless ( @taxes ) {
       $taxhash{'taxclass'} = '';
@@ -2574,39 +2583,132 @@
       @taxes =  qsearch( 'cust_main_county', \%taxhash );
     }
 
-  } #if $conf->exists('enable_taxproducts') 
+    $taxes{''} = [ @taxes ];
+    $taxes{'setup'} = [ @taxes ];
+    $taxes{'recur'} = [ @taxes ];
+    $taxes{$_} = [ @taxes ] foreach (@classes);
 
-  # maybe eliminate this entirely, along with all the 0% records
-  unless ( @taxes ) {
-    my $error;
-    if ( $conf->exists('enable_taxproducts') ) { 
-      $error = 
-        "fatal: can't find tax rate for zip/taxproduct/pkgpart ".
-        join('/', ( map $self->get("$prefix$_"),
-                        qw(zip)
-                  ),
-                  $part_pkg->taxproduct_description,
-                  $part_pkg->pkgpart ). "\n";
-    } else {
-      $error = 
+    # maybe eliminate this entirely, along with all the 0% records
+    unless ( @taxes ) {
+      return
         "fatal: can't find tax rate for state/county/country/taxclass ".
         join('/', ( map $self->get("$prefix$_"),
                         qw(state county country)
                   ),
                   $part_pkg->taxclass ). "\n";
     }
-    return $error;
+
+  } #if $conf->exists('enable_taxproducts') 
+
+  # XXX all this goes away with cust_bill_pay refactor
+
+  $cust_bill_pkg{setup} = $cust_bill_pkg if $cust_bill_pkg->setup;
+  $cust_bill_pkg{recur} = $cust_bill_pkg if $cust_bill_pkg->recur;
+    
+  #split setup and recur
+  if ($cust_bill_pkg->setup && $cust_bill_pkg->recur) {
+    my $cust_bill_pkg_recur = new FS::cust_bill_pkg { $cust_bill_pkg->hash };
+    $cust_bill_pkg->set('details', []);
+    $cust_bill_pkg->recur(0);
+    $cust_bill_pkg->type('');
+    $cust_bill_pkg{recur} = $cust_bill_pkg_recur;
   }
 
-  foreach my $tax ( @taxes ) {
-    my $taxname = ref( $tax ). ' '. $tax->taxnum;
-    if ( exists( $taxlisthash->{ $taxname } ) ) {
-      push @{ $taxlisthash->{ $taxname  } }, $cust_bill_pkg;
-    }else{
-      $taxlisthash->{ $taxname } = [ $tax, $cust_bill_pkg ];
+  #split usage from recur
+  my $usage = $cust_bill_pkg->usage;
+  if ($usage) {
+    my $cust_bill_pkg_usage =
+        new FS::cust_bill_pkg { $cust_bill_pkg{recur}->hash };
+    $cust_bill_pkg_usage->recur($usage);
+    $cust_bill_pkg{recur}->recur( $cust_bill_pkg{recur}->recur - $usage );
+    $cust_bill_pkg{recur}->type( '' );
+    $cust_bill_pkg{''} = $cust_bill_pkg_usage;
+  }
+
+  #subdivide usage by usage_class
+  if (exists($cust_bill_pkg{''})) {
+    foreach my $class (grep {$_} @classes) {
+      my $usage = $cust_bill_pkg{''}->usage($class);
+      my $cust_bill_pkg_usage =
+          new FS::cust_bill_pkg { $cust_bill_pkg{''}->hash };
+      $cust_bill_pkg_usage->recur($usage);
+      $cust_bill_pkg{''}->recur( $cust_bill_pkg{''}->recur - $usage );
+      $cust_bill_pkg{$class} = $cust_bill_pkg_usage;
+    }
+    $cust_bill_pkg{''}->set('details', []);
+    delete $cust_bill_pkg{''} unless $cust_bill_pkg{''}->recur;
+  }
+
+  foreach my $key (keys %cust_bill_pkg) {
+    my @taxes = @{ $taxes{$key} };
+    my $cust_bill_pkg = $cust_bill_pkg{$key};
+
+    foreach my $tax ( @taxes ) {
+      my $taxname = ref( $tax ). ' '. $tax->taxnum;
+      if ( exists( $taxlisthash->{ $taxname } ) ) {
+        push @{ $taxlisthash->{ $taxname  } }, $cust_bill_pkg;
+      }else{
+        $taxlisthash->{ $taxname } = [ $tax, $cust_bill_pkg ];
+      }
     }
   }
 
+  # sort setup,recur,'', and the rest numeric && return
+  my @result = map { $cust_bill_pkg{$_} }
+               sort { my $ad = ($a=~/^\d+$/); my $bd = ($b=~/^\d+$/);
+                      ( $ad cmp $bd ) || ( $ad ? $a<=>$b : $b cmp $a )
+                    }
+               keys %cust_bill_pkg;
+
+  \@result;
+}
+
+sub _gather_taxes {
+  my $self = shift;
+  my $part_pkg = shift;
+  my $class = shift;
+  my $prefix = shift;
+
+  my @taxes = ();
+  my $geocode = $self->geocode('cch');
+
+  my @taxclassnums = map { $_->taxclassnum }
+                     $part_pkg->part_pkg_taxoverride($class);
+
+  unless (@taxclassnums) {
+    @taxclassnums = map { $_->taxclassnum }
+                    $part_pkg->part_pkg_taxrate('cch', $geocode, $class);
+  }
+  warn "Found taxclassnum values of ". join(',', @taxclassnums)
+    if $DEBUG;
+
+  my $extra_sql =
+    "AND (".
+    join(' OR ', map { "taxclassnum = $_" } @taxclassnums ). ")";
+
+  @taxes = qsearch({ 'table' => 'tax_rate',
+                     'hashref' => { 'geocode' => $geocode, },
+                     'extra_sql' => $extra_sql,
+                  })
+    if scalar(@taxclassnums);
+
+  # maybe eliminate this entirely, along with all the 0% records
+  unless ( @taxes ) {
+    return 
+      "fatal: can't find tax rate for zip/taxproduct/pkgpart ".
+      join('/', ( map $self->get("$prefix$_"),
+                      qw(zip)
+                ),
+                $part_pkg->taxproduct_description,
+                $part_pkg->pkgpart ). "\n";
+  }
+
+  warn "Found taxes ".
+       join(',', map{ ref($_). " ". $_->get($_->primary_key) } @taxes). "\n" 
+   if $DEBUG;
+
+  [ @taxes ];
+
 }
 
 =item collect OPTIONS



More information about the freeside-commits mailing list