okay, finally fix all the weirdness with shipping adresses. whew.
[freeside.git] / FS / FS / cust_main.pm
index 94fd97f..b6d3075 100644 (file)
@@ -512,6 +512,8 @@ and repalce methods.
 sub check {
   my $self = shift;
 
+  #warn "BEFORE: \n". $self->_dump;
+
   my $error =
     $self->ut_numbern('custnum')
     || $self->ut_number('agentnum')
@@ -553,7 +555,9 @@ sub check {
     $self->ss("$1-$2-$3");
   }
 
-  unless ( $import ) {
+
+# bad idea to disable, causes billing to fail because of no tax rates later
+#  unless ( $import ) {
     unless ( qsearchs('cust_main_county', {
       'country' => $self->country,
       'state'   => '',
@@ -566,7 +570,7 @@ sub check {
           'country' => $self->country,
         } );
     }
-  }
+#  }
 
   $error =
     $self->ut_phonen('daytime', $self->country)
@@ -582,8 +586,9 @@ sub check {
   );
 
   if ( defined $self->dbdef_table->column('ship_last') ) {
-    if ( grep { $self->getfield($_) ne $self->getfield("ship_$_") } @addfields
-         && grep $self->getfield("ship_$_"), grep $_ ne 'state', @addfields
+    if ( scalar ( grep { $self->getfield($_) ne $self->getfield("ship_$_") }
+                       @addfields )
+         && scalar ( grep { $self->getfield("ship_$_") ne '' } @addfields )
        )
     {
       my $error =
@@ -694,6 +699,8 @@ sub check {
 
   $self->otaker(getotaker);
 
+  #warn "AFTER: \n". $self->_dump;
+
   ''; #no error
 }
 
@@ -913,6 +920,9 @@ sub bill {
       };
       $recur_prog = $1;
 
+      # shared with $recur_prog
+      $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
+
         #my $cpt = new Safe;
         ##$cpt->permit(); #what is necessary?
         #$cpt->share(qw( $cust_pkg )); #can $cpt now use $cust_pkg methods?
@@ -925,11 +935,14 @@ sub bill {
       }
       #change this bit to use Date::Manip? CAREFUL with timezones (see
       # mailing list archive)
-      #$sdate=$cust_pkg->bill || time;
-      #$sdate=$cust_pkg->bill || $time;
-      $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
       my ($sec,$min,$hour,$mday,$mon,$year) =
         (localtime($sdate) )[0,1,2,3,4,5];
+
+      #pro-rating magic - if $recur_prog fiddles $sdate, want to use that
+      # only for figuring next bill date, nothing else, so, reset $sdate again
+      # here
+      $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
+
       $mon += $part_pkg->getfield('freq');
       until ( $mon < 12 ) { $mon -= 12; $year++; }
       $cust_pkg->setfield('bill',
@@ -970,10 +983,10 @@ sub bill {
         $total_recur += $recur;
         $taxable_setup += $setup
           unless $part_pkg->dbdef_table->column('setuptax')
-                 || $part_pkg->setuptax =~ /^Y$/i;
+                 && $part_pkg->setuptax =~ /^Y$/i;
         $taxable_recur += $recur
           unless $part_pkg->dbdef_table->column('recurtax')
-                 || $part_pkg->recurtax =~ /^Y$/i;
+                 && $part_pkg->recurtax =~ /^Y$/i;
       }
     }
 
@@ -994,7 +1007,8 @@ sub bill {
         'state'   => $self->state,
         'county'  => $self->county,
         'country' => $self->country,
-    } );
+    } ) or die "fatal: can't find tax rate for state/county/country ".
+               $self->state. "/". $self->county. "/". $self->country. "\n";
     my $tax = sprintf( "%.2f",
       $taxable_charged * ( $cust_main_county->getfield('tax') / 100 )
     );
@@ -1118,7 +1132,7 @@ sub collect {
       sort {    $a->seconds   <=> $b->seconds
              || $a->weight    <=> $b->weight
              || $a->eventpart <=> $b->eventpart }
-        grep { $_->seconds > ( $invoice_time - ( $cust_bill->_date || 0 ) )
+        grep { $_->seconds <= ( $invoice_time - $cust_bill->_date )
                && ! qsearchs( 'cust_bill_event', {
                                 'invnum'    => $cust_bill->invnum,
                                 'eventpart' => $_->eventpart       } )
@@ -1518,11 +1532,23 @@ sub referral_cust_main {
   @cust_main;
 }
 
+=item referral_cust_main_ncancelled
+
+Same as referral_cust_main, except only returns customers with uncancelled
+packages.
+
+=cut
+
+sub referral_cust_main_ncancelled {
+  my $self = shift;
+  grep { scalar($_->ncancelled_pkgs) } $self->referral_cust_main;
+}
+
 =item referral_cust_pkg [ DEPTH ]
 
-Like referral_cust_main, except returns a flat list of all unsuspended packages
-for each customer.  The number of items in this list may be useful for
-comission calculations (perhaps after a grep).
+Like referral_cust_main, except returns a flat list of all unsuspended (and
+uncancelled) packages for each customer.  The number of items in this list may
+be useful for comission calculations (perhaps after a C<grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } @commission_worthy_pkgparts> } $cust_main-> ).
 
 =cut
 
@@ -1564,7 +1590,7 @@ sub charge {
 
   my $part_pkg = new FS::part_pkg ( {
     'pkg'      => $pkg || 'One-time charge',
-    'comment'  => $comment,
+    'comment'  => $comment || '$'. sprintf("%.2f".$amount),
     'setup'    => $amount,
     'freq'     => 0,
     'recur'    => '0',