agent-ize invoice_default_terms, RT#32513
authorIvan Kohler <ivan@freeside.biz>
Fri, 5 Dec 2014 18:45:26 +0000 (10:45 -0800)
committerIvan Kohler <ivan@freeside.biz>
Fri, 5 Dec 2014 18:45:26 +0000 (10:45 -0800)
FS/FS/Conf.pm
FS/FS/Template_Mixin.pm
FS/FS/cust_bill.pm
FS/FS/part_event/Condition/cust_bill_past_due.pm
httemplate/edit/cust_main/billing.html
httemplate/edit/quick-charge.html
httemplate/elements/select-terms.html
httemplate/search/report_cust_main.html
httemplate/view/cust_main/billing.html

index 8fb6220..97659bc 100644 (file)
@@ -1539,6 +1539,7 @@ and customer address. Include units.',
     'section'     => 'invoicing',
     'description' => 'Optional default invoice term, used to calculate a due date printed on invoices.',
     'type'        => 'select',
+    'per_agent'   => 1,
     'select_enum' => [ 
       '', 'Payable upon receipt', 'Net 0', 'Net 3', 'Net 5', 'Net 9', 'Net 10', 'Net 14', 
       'Net 15', 'Net 18', 'Net 20', 'Net 21', 'Net 25', 'Net 30', 'Net 45', 
index 346e781..f67de76 100644 (file)
@@ -1856,8 +1856,15 @@ sub terms {
   my $cust_main = $self->cust_main;
   return $cust_main->invoice_terms if $cust_main && $cust_main->invoice_terms;
 
+  my $agentnum = '';
+  if ( $cust_main ) {
+    $agentnum = $cust_main->agentnum;
+  } elsif ( my $prospect_main = $self->prospect_main ) {
+    $agentnum = $prospect_main->agentnum;
+  }
+
   #use configured default
-  $conf->config('invoice_default_terms') || '';
+  $conf->config('invoice_default_terms', $agentnum) || '';
 }
 
 sub due_date {
@@ -1891,8 +1898,8 @@ sub balance_due_date {
   my $self = shift;
   my $conf = $self->conf;
   my $duedate = '';
-  if (    $conf->exists('invoice_default_terms') 
-       && $conf->config('invoice_default_terms')=~ /^\s*Net\s*(\d+)\s*$/ ) {
+  my $terms = $self->terms;
+  if ( $terms =~ /^\s*Net\s*(\d+)\s*$/ ) {
     $duedate = $self->time2str_local('rdate', $self->_date + ($1*86400) );
   }
   $duedate;
index 5be984e..0c07e9a 100644 (file)
@@ -3323,6 +3323,13 @@ Currently only supported on PostgreSQL.
 =cut
 
 sub due_date_sql {
+  die "don't use: doesn't account for agent-specific invoice_default_terms";
+
+  #we're passed a $conf but not a specific customer (that's in the query), so
+  # to make this work we'd need an agentnum-aware "condition_sql_conf" like
+  # "condition_sql_option" that retreives a conf value with SQL in an agent-
+  # aware fashion
+
   my $conf = new FS::Conf;
 'COALESCE(
   SUBSTRING(
index 47a90cf..5e24acf 100644 (file)
@@ -35,13 +35,14 @@ sub condition {
   $as_of >= ($cust_bill->due_date || $cust_bill->_date);
 }
 
-sub condition_sql {
-  my( $class, $table, %opt ) = @_;
-  return 'true' if $opt{'driver_name'} ne 'Pg';
-  my $delay = $class->condition_sql_option_integer('delay', 'Pg');
-  my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($opt{'time'}))[0..5];
-  my $as_of = timelocal(0,0,0,$mday,$mon,$year) . " - ($delay * 86400)";
-  "( $as_of ) >= ".FS::cust_bill->due_date_sql;
-}
+#->due_date_sql can't cope with agent-specific invoice_default_terms
+#sub condition_sql {
+#  my( $class, $table, %opt ) = @_;
+#  return 'true' if $opt{'driver_name'} ne 'Pg';
+#  my $delay = $class->condition_sql_option_integer('delay', 'Pg');
+#  my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($opt{'time'}))[0..5];
+#  my $as_of = timelocal(0,0,0,$mday,$mon,$year) . " - ($delay * 86400)";
+#  "( $as_of ) >= ".FS::cust_bill->due_date_sql;
+#}
 
 1;
index 1f44905..fcd8f01 100644 (file)
       <TD ALIGN="right" WIDTH="200"><% mt('Invoice terms') |h %> </TD>
       <TD WIDTH="408">
         <& /elements/select-terms.html,
-                     'curr_value' => $cust_main->invoice_terms,
+             'curr_value' => $cust_main->invoice_terms,
+             'agentnum'   => $cust_main->agentnum,
         &>
       </TD>
     </TR>
index 0db1543..da9ac6e 100644 (file)
@@ -200,8 +200,9 @@ function bill_now_changed (what) {
     >
     <% mt('with terms') |h %> 
     <& /elements/select-terms.html,
-                 'curr_value'  => scalar($cgi->param('invoice_terms')),
-                 'disabled'    => ( $cgi->param('bill_now') ? 0 : 1 ),
+         'curr_value' => scalar($cgi->param('invoice_terms')),
+         'disabled'   => ( $cgi->param('bill_now') ? 0 : 1 ),
+         'agentnum'   => $cust_main->agentnum,
     &>
   </TD>
 </TR>
@@ -429,9 +430,12 @@ my $default_terms;
 if ( $cust_main->invoice_terms ) {
   $default_terms = emt("Customer default ([_1])", $cust_main->invoice_terms);
 } else {
-  $default_terms = emt("Default ([_1])",
-            ($conf->config('invoice_default_terms') || emt('Payable upon receipt'))
-                      );
+  $default_terms =
+    emt( "Default ([_1])",
+         ( $conf->config('invoice_default_terms', $cust_main->agentnum)
+             || emt('Payable upon receipt')
+         )
+       );
 }
 
 my @description;
index 8839692..716832f 100644 (file)
@@ -26,9 +26,11 @@ my $conf = new FS::Conf;
 
 my $empty_label =
   $opt{'empty_label'}
-  || emt('Default').' ('.
-       ($conf->config('invoice_default_terms') || emt('Payable upon receipt')).
-     ')';
+    || emt('Default').' ('.
+         ( $conf->config('invoice_default_terms', $opt{'agentnum'})
+             || emt('Payable upon receipt')
+         ).
+       ')';
 
 my $empty_value = $opt{'empty_value'} || '';
 
index 994f950..2b40672 100755 (executable)
     <TR>
       <TD ALIGN="right"><% mt('Invoice terms') |h %></TD>
       <TD>
+%       my @agentnums = $FS::CurrentUser::CurrentUser->agentnums;
+%       my $agentnum = scalar(@agentnums) == 1 ? $agentnums[0] : '';
         <& /elements/select-terms.html,
-                      'pre_options' => [ '' => emt('all') ],
-                      'empty_value' => 'NULL',
+             'pre_options' => [ '' => emt('all') ],
+             'empty_value' => 'NULL',
+             'agentnum'    => $agentnum,
         &>
       </TD>
     </TR>
index 2c4b3fb..64ec591 100644 (file)
 <TR>
   <TD ALIGN="right"><% mt('Invoice terms') |h %></TD>
   <TD BGCOLOR="#ffffff">
-    <% $cust_main->invoice_terms || emt('Default').' ('. ( $conf->config('invoice_default_terms') || emt('Payable upon receipt') ). ')' %>
+    <% $cust_main->invoice_terms
+         || emt('Default').' ('. ( $conf->config('invoice_default_terms', $cust_main->agentnum)
+                                     || emt('Payable upon receipt')
+                                 ).
+                           ')'
+    %>
   </TD>
 </TR>
 <TR>