fix 'Can't call method "setup" on an undefined value' error when using into rates...
[freeside.git] / FS / FS / cust_bill_pkg_tax_location.pm
index 50e86eb..44dd6e3 100644 (file)
@@ -6,6 +6,9 @@ use FS::Record qw( qsearch qsearchs );
 use FS::cust_bill_pkg;
 use FS::cust_pkg;
 use FS::cust_location;
+use FS::cust_bill_pay_pkg;
+use FS::cust_credit_bill_pkg;
+use FS::cust_main_county;
 
 =head1 NAME
 
@@ -112,7 +115,7 @@ sub check {
     $self->ut_numbern('billpkgtaxlocationnum')
     || $self->ut_foreign_key('billpkgnum', 'cust_bill_pkg', 'billpkgnum' )
     || $self->ut_number('taxnum') #cust_bill_pkg/tax_rate key, based on taxtype
-    || $self->ut_enum('taxtype', [ qw( FS::cust_main::county FS::tax_rate ) ] )
+    || $self->ut_enum('taxtype', [ qw( FS::cust_main_county FS::tax_rate ) ] )
     || $self->ut_foreign_key('pkgnum', 'cust_pkg', 'pkgnum' )
     || $self->ut_foreign_key('locationnum', 'cust_location', 'locationnum' )
     || $self->ut_money('amount')
@@ -122,10 +125,96 @@ sub check {
   $self->SUPER::check;
 }
 
+=item cust_bill_pkg
+
+Returns the associated cust_bill_pkg object
+
+=cut
+
+sub cust_bill_pkg {
+  my $self = shift;
+  qsearchs( 'cust_bill_pkg', { 'billpkgnum' => $self->billpkgnum }  );
+}
+
+=item cust_location
+
+Returns the associated cust_location object
+
+=cut
+
+sub cust_location {
+  my $self = shift;
+  qsearchs( 'cust_location', { 'locationnum' => $self->locationnum }  );
+}
+
+=item desc
+
+Returns a description for this tax line item constituent.  Currently this
+is the desc of the associated line item followed by the state/county/city
+for the location in parentheses.
+
+=cut
+
+sub desc {
+  my $self = shift;
+  my $cust_location = $self->cust_location;
+  my $location = join('/', grep { $_ }                 # leave in?
+                           map { $cust_location->$_ }
+                           qw( state county city )     # country?
+  );
+  my $cust_bill_pkg_desc = $self->billpkgnum
+                         ? $self->cust_bill_pkg->desc
+                         : $self->cust_bill_pkg_desc;
+  "$cust_bill_pkg_desc ($location)";
+}
+
+=item owed
+
+Returns the amount owed (still outstanding) on this tax line item which is
+the amount of this record minus all payment applications and credit
+applications.
+
+=cut
+
+sub owed {
+  my $self = shift;
+  my $balance = $self->amount;
+  $balance -= $_->amount foreach ( $self->cust_bill_pay_pkg('setup') );
+  $balance -= $_->amount foreach ( $self->cust_credit_bill_pkg('setup') );
+  $balance = sprintf( '%.2f', $balance );
+  $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
+  $balance;
+}
+
+sub cust_bill_pay_pkg {
+  my $self = shift;
+  qsearch( 'cust_bill_pay_pkg',
+           { map { $_ => $self->$_ } qw( billpkgtaxlocationnum billpkgnum ) }
+         );
+}
+
+sub cust_credit_bill_pkg {
+  my $self = shift;
+  qsearch( 'cust_credit_bill_pkg',
+           { map { $_ => $self->$_ } qw( billpkgtaxlocationnum billpkgnum ) }
+         );
+}
+
+sub cust_main_county {
+  my $self = shift;
+  my $result;
+  if ( $self->taxtype eq 'FS::cust_main_county' ) {
+    $result = qsearchs( 'cust_main_county', { 'taxnum' => $self->taxnum } );
+  }
+}
+
 =back
 
 =head1 BUGS
 
+The presense of FS::cust_main_county::delete makes the cust_main_county method
+unreliable
+
 =head1 SEE ALSO
 
 L<FS::Record>, schema.html from the base documentation.