add cust_main.agent_custid (at least to schema and customer view, no manual editing...
[freeside.git] / FS / FS / cust_main.pm
index 8956d5b..3f67c62 100644 (file)
@@ -944,10 +944,13 @@ sub replace {
     $old = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
   }
 
-  if ( $self->payby eq 'COMP' && $self->payby ne $old->payby
-       && $conf->config('users-allow_comp')                  ) {
-    return "You are not permitted to create complimentary accounts."
-      unless grep { $_ eq getotaker } $conf->config('users-allow_comp');
+  my $curuser = $FS::CurrentUser::CurrentUser;
+  if (    $self->payby eq 'COMP'
+       && $self->payby ne $old->payby
+       && ! $curuser->access_right('Complimentary customer')
+     )
+  {
+    return "You are not permitted to create complimentary accounts.";
   }
 
   local($ignore_expired_card) = 1
@@ -1057,6 +1060,7 @@ sub check {
   my $error =
     $self->ut_numbern('custnum')
     || $self->ut_number('agentnum')
+    || $self->ut_textn('agent_custid')
     || $self->ut_number('refnum')
     || $self->ut_name('last')
     || $self->ut_name('first')
@@ -1302,9 +1306,12 @@ sub check {
 
   } elsif ( $self->payby eq 'COMP' ) {
 
-    if ( !$self->custnum && $conf->config('users-allow_comp') ) {
+    my $curuser = $FS::CurrentUser::CurrentUser;
+    if (    ! $self->custnum
+         && ! $curuser->access_right('Complimentary customer')
+       )
+    {
       return "You are not permitted to create complimentary accounts."
-        unless grep { $_ eq getotaker } $conf->config('users-allow_comp');
     }
 
     $error = $self->ut_textn('payinfo');
@@ -2040,6 +2047,8 @@ quiet - set true to surpress email card/ACH decline notices.
 freq - "1d" for the traditional, daily events (the default), or "1m" for the
 new monthly events
 
+payby - allows for one time override of normal customer billing method
+
 =cut
 
 sub collect {
@@ -2110,7 +2119,10 @@ sub collect {
              }
           qsearch( {
             'table'     => 'part_bill_event',
-            'hashref'   => { 'payby'    => $self->payby,
+            'hashref'   => { 'payby'    => (exists($options{'payby'})
+                                            ? $options{'payby'}
+                                            : $self->payby
+                                          ),
                              'disabled' => '',           },
             'extra_sql' => $extra_sql,
           } )
@@ -3137,6 +3149,29 @@ sub balance_date {
   );
 }
 
+=item in_transit_payments
+
+Returns the total of requests for payments for this customer pending in 
+batches in transit to the bank.  See L<FS::pay_batch> and L<FS::cust_pay_batch>
+
+=cut
+
+sub in_transit_payments {
+  my $self = shift;
+  my $in_transit_payments = 0;
+  foreach my $pay_batch ( qsearch('pay_batch', {
+    'status' => 'I',
+  } ) ) {
+    foreach my $cust_pay_batch ( qsearch('cust_pay_batch', {
+      'batchnum' => $pay_batch->batchnum,
+      'custnum' => $self->custnum,
+    } ) ) {
+      $in_transit_payments += $cust_pay_batch->amount;
+    }
+  }
+  sprintf( "%.2f", $in_transit_payments );
+}
+
 =item paydate_monthyear
 
 Returns a two-element list consisting of the month and year of this customer's
@@ -3664,18 +3699,17 @@ Returns a hex triplet color string for this customer's status.
 
 =cut
 
+use vars qw(%statuscolor);
+%statuscolor = (
+  'prospect'  => '7e0079', #'000000', #black?  naw, purple
+  'active'    => '00CC00', #green
+  'inactive'  => '0000CC', #blue
+  'suspended' => 'FF9900', #yellow
+  'cancelled' => 'FF0000', #red
+);
 
 sub statuscolor {
   my $self = shift;
-
-  my %statuscolor = (
-    'prospect'  => '7e0079', #'000000', #black?  naw, purple
-    'active'    => '00CC00', #green
-    'inactive'  => '0000CC', #blue
-    'suspended' => 'FF9900', #yellow
-    'cancelled' => 'FF0000', #red
-  );
-
   $statuscolor{$self->status};
 }
 
@@ -3697,6 +3731,10 @@ $select_count_pkgs =
   "SELECT COUNT(*) FROM cust_pkg
     WHERE cust_pkg.custnum = cust_main.custnum";
 
+sub select_count_pkgs_sql {
+  $select_count_pkgs;
+}
+
 sub prospect_sql { "
   0 = ( $select_count_pkgs )
 "; }
@@ -3857,6 +3895,11 @@ sub smart_search {
   } elsif ( $search =~ /^\s*(\S.*\S)\s*$/ ) { #value search
 
     my $value = lc($1);
+
+    # remove "(Last, First)" in "Company (Last, First"), otherwise the
+    # full strings the browser remembers won't work
+    $value =~ s/\([\w \,\.\-\']*\)$//; #false laziness w/Record::ut_name
+    
     my $q_value = dbh->quote($value);
 
     #exact
@@ -3880,7 +3923,7 @@ sub smart_search {
       push @cust_main, qsearch( {
         'table'     => 'cust_main',
         'hashref'   => { 'last'     => { 'op'    => 'ILIKE',
-                                         'value' => "%$q_value%" },
+                                         'value' => "%$value%" },
                          %options,
                        },
         'extra_sql' => " AND $agentnums_sql", #agent virtualizaiton
@@ -3888,7 +3931,7 @@ sub smart_search {
       push @cust_main, qsearch( {
         'table'     => 'cust_main',
         'hashref'   => { 'ship_last' => { 'op'     => 'ILIKE',
-                                          'value' => "%$q_value%" },
+                                          'value' => "%$value%" },
                          %options, 
                        },
         'extra_sql' => " AND $agentnums_sql", #agent virtualization
@@ -3898,7 +3941,7 @@ sub smart_search {
       push @cust_main, qsearch( {
         'table'     => 'cust_main',
         'hashref'   => { 'company'  => { 'op'    => 'ILIKE',
-                                         'value' => "%$q_value%" },
+                                         'value' => "%$value%" },
                          %options,
                        },
         'extra_sql' => " AND $agentnums_sql", #agent virtualization
@@ -3906,7 +3949,7 @@ sub smart_search {
       push @cust_main, qsearch(  {
         'table'     => 'cust_main',
         'hashref'   => { 'ship_company' => { 'op'    => 'ILIKE',
-                                             'value' => "%$q_value%" },
+                                             'value' => "%$value%" },
                          %options,
                        },
         'extra_sql' => " AND $agentnums_sql", #agent virtualization
@@ -3929,6 +3972,10 @@ sub smart_search {
 
     }
 
+    #eliminate duplicates
+    my %saw = ();
+    @cust_main = grep { !$saw{$_->custnum}++ } @cust_main;
+
   }
 
   @cust_main;