UI hints for adding new nameservice records, too many to be intuitive now, RT#8933
[freeside.git] / FS / FS / cust_main.pm
index 1684034..f4b9c59 100644 (file)
@@ -31,6 +31,7 @@ use FS::UID qw( getotaker dbh driver_name );
 use FS::Record qw( qsearchs qsearch dbdef regexp_sql );
 use FS::Misc qw( generate_email send_email generate_ps do_print );
 use FS::Msgcat qw(gettext);
+use FS::CurrentUser;
 use FS::payby;
 use FS::cust_pkg;
 use FS::cust_svc;
@@ -1790,7 +1791,8 @@ sub check {
 
 
 # bad idea to disable, causes billing to fail because of no tax rates later
-#  unless ( $import ) {
+# except we don't fail any more
+  unless ( $import ) {
     unless ( qsearch('cust_main_county', {
       'country' => $self->country,
       'state'   => '',
@@ -1803,7 +1805,7 @@ sub check {
           'country' => $self->country,
         } );
     }
-#  }
+  }
 
   $error =
     $self->ut_phonen('daytime', $self->country)
@@ -2072,7 +2074,7 @@ sub check {
     $self->$flag($1);
   }
 
-  $self->otaker(getotaker) unless $self->otaker;
+  $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
 
   warn "$me check AFTER: \n". $self->_dump
     if $DEBUG > 2;
@@ -3020,7 +3022,7 @@ sub bill {
 
   foreach my $pass (@passes) { # keys %cust_bill_pkg ) {
 
-    my @cust_bill_pkg = @{ $cust_bill_pkg{$pass} };
+    my @cust_bill_pkg = _omit_zero_value_bundles(@{ $cust_bill_pkg{$pass} });
 
     next unless @cust_bill_pkg; #don't create an invoice w/o line items
 
@@ -3066,6 +3068,9 @@ sub bill {
           }
         }
 
+        # it's silly to have a zero value postal_pkg, but....
+        @cust_bill_pkg = _omit_zero_value_bundles(@cust_bill_pkg);
+
       }
 
     }
@@ -3140,24 +3145,12 @@ sub bill {
       return "can't create invoice for customer #". $self->custnum. ": $error";
     }
 
-    my @cust_bill_pkg_bundle = ();
     foreach my $cust_bill_pkg ( @cust_bill_pkg ) {
       $cust_bill_pkg->invnum($cust_bill->invnum); 
-      if (scalar(@cust_bill_pkg_bundle) && !$cust_bill_pkg->pkgpart_override) {
-        $error = $self->_insert_cust_bill_pkg_bundle( @cust_bill_pkg_bundle );
-        if ( $error ) {
-          $dbh->rollback if $oldAutoCommit;
-          return $error;
-        }
-        @cust_bill_pkg_bundle = ();
-      }
-      push @cust_bill_pkg_bundle, $cust_bill_pkg;
-    }
-    if (scalar(@cust_bill_pkg_bundle)) {
-      $error = $self->_insert_cust_bill_pkg_bundle( @cust_bill_pkg_bundle );
+      my $error = $cust_bill_pkg->insert;
       if ( $error ) {
         $dbh->rollback if $oldAutoCommit;
-        return $error;
+        return "can't create invoice line item: $error";
       }
     }
 
@@ -3177,19 +3170,25 @@ sub bill {
   ''; #no error
 }
 
-#insert line items while discarding bundled packages of 0 value
-sub _insert_cust_bill_pkg_bundle {
-  my $self = shift;
-  my @cust_bill_pkg = @_;
+#discard bundled packages of 0 value
+sub _omit_zero_value_bundles {
 
+  my @cust_bill_pkg = ();
+  my @cust_bill_pkg_bundle = ();
   my $sum = 0;
-  $sum += $_->setup + $_->recur foreach @cust_bill_pkg;
-  return '' unless $sum > 0;
 
-  foreach my $cust_bill_pkg ( @cust_bill_pkg ) {
-    my $error = $cust_bill_pkg->insert;
-    return "can't create invoice line item: $error" if $error;
+  foreach my $cust_bill_pkg ( @_ ) {
+    if (scalar(@cust_bill_pkg_bundle) && !$cust_bill_pkg->pkgpart_override) {
+      push @cust_bill_pkg, @cust_bill_pkg_bundle if $sum > 0;
+      @cust_bill_pkg_bundle = ();
+      $sum = 0;
+    }
+    $sum += $cust_bill_pkg->setup + $cust_bill_pkg->recur;
+    push @cust_bill_pkg_bundle, $cust_bill_pkg;
   }
+  push @cust_bill_pkg, @cust_bill_pkg_bundle if $sum > 0;
+
+  (@cust_bill_pkg);
 
 }
 
@@ -3476,6 +3475,7 @@ sub _make_lines {
     my %param = ( 'precommit_hooks'     => $precommit_hooks,
                   'increment_next_bill' => $increment_next_bill,
                   'discounts'           => \@discounts,
+                  'real_pkgpart'        => $real_pkgpart,
                 );
 
     my $method = $options{cancel} ? 'calc_cancel' : 'calc_recur';
@@ -4927,8 +4927,7 @@ sub realtime_bop {
 
   #false laziness w/misc/process/payment.cgi - check both to make sure working
   # correctly
-  if ( defined $self->dbdef_table->column('paycvv')
-       && length($self->paycvv)
+  if ( length($self->paycvv)
        && ! grep { $_ eq cardtype($options{payinfo}) } $conf->config('cvv-save')
   ) {
     my $error = $self->remove_cvv;
@@ -8866,7 +8865,17 @@ sub batch_charge {
   my $param = shift;
   #warn join('-',keys %$param);
   my $fh = $param->{filehandle};
-  my @fields = @{$param->{fields}};
+  my $agentnum = $param->{agentnum};
+  my $format = $param->{format};
+
+  my $extra_sql = ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
+
+  my @fields;
+  if ( $format eq 'simple' ) {
+    @fields = qw( custnum agent_custid amount pkg );
+  } else {
+    die "unknown format $format";
+  }
 
   eval "use Text::CSV_XS;";
   die $@ if $@;
@@ -8906,10 +8915,32 @@ sub batch_charge {
       $row{$field} = shift @columns;
     }
 
-    my $cust_main = qsearchs('cust_main', { 'custnum' => $row{'custnum'} } );
+    if ( $row{custnum} && $row{agent_custid} ) {
+      dbh->rollback if $oldAutoCommit;
+      return "can't specify custnum with agent_custid $row{agent_custid}";
+    }
+
+    my %hash = ();
+    if ( $row{agent_custid} && $agentnum ) {
+      %hash = ( 'agent_custid' => $row{agent_custid},
+                'agentnum'     => $agentnum,
+              );
+    }
+
+    if ( $row{custnum} ) {
+      %hash = ( 'custnum' => $row{custnum} );
+    }
+
+    unless ( scalar(keys %hash) ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "can't find customer without custnum or agent_custid and agentnum";
+    }
+
+    my $cust_main = qsearchs('cust_main', { %hash } );
     unless ( $cust_main ) {
       $dbh->rollback if $oldAutoCommit;
-      return "unknown custnum $row{'custnum'}";
+      my $custnum = $row{custnum} || $row{agent_custid};
+      return "unknown custnum $custnum";
     }
 
     if ( $row{'amount'} > 0 ) {