[freeside-commits] branch master updated. 63d5cd3d1b5f7f7acf42eb9e0bb9c9c1f5d4dd47

Mark Wells mark at 420.am
Thu Jan 16 13:49:09 PST 2014


The branch, master has been updated
       via  63d5cd3d1b5f7f7acf42eb9e0bb9c9c1f5d4dd47 (commit)
      from  8b0ac664249e4847db8853eac5c4b28d94770294 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 63d5cd3d1b5f7f7acf42eb9e0bb9c9c1f5d4dd47
Author: Mark Wells <mark at freeside.biz>
Date:   Thu Jan 16 13:48:57 2014 -0800

    allow modifying one-time charges before they're billed, #26282

diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm
index fa09640..6d90e52 100644
--- a/FS/FS/cust_pkg.pm
+++ b/FS/FS/cust_pkg.pm
@@ -2228,13 +2228,17 @@ sub set_salesnum {
 
 =item modify_charge OPTIONS
 
-Change the properties of a one-time charge.  Currently the only properties
-that can be changed this way are those that have no impact on billing 
-calculations:
+Change the properties of a one-time charge.  The following properties can
+be changed this way:
 - pkg: the package description
 - classnum: the package class
 - additional: arrayref of additional invoice details to add to this package
 
+and, I<if the charge has not yet been billed>:
+- start_date: the date when it will be billed
+- amount: the setup fee to be charged
+- quantity: the multiplier for the setup fee
+
 If you pass 'adjust_commission' => 1, and the classnum changes, and there are
 commission credits linked to this charge, they will be recalculated.
 
@@ -2268,14 +2272,51 @@ sub modify_charge {
   }
 
   my $old_classnum;
-  if ( exists($opt{'classnum'}) and $part_pkg->classnum ne $opt{'classnum'} ) {
+  if ( exists($opt{'classnum'}) and $part_pkg->classnum ne $opt{'classnum'} )
+  {
     # remember it
     $old_classnum = $part_pkg->classnum;
     $part_pkg->set('classnum', $opt{'classnum'});
   }
 
+  if ( !$self->get('setup') ) {
+    # not yet billed, so allow amount and quantity
+    if ( exists($opt{'quantity'})
+          and $opt{'quantity'} != $self->quantity
+          and $opt{'quantity'} > 0 ) {
+        
+      $self->set('quantity', $opt{'quantity'});
+    }
+    if ( exists($opt{'start_date'})
+          and $opt{'start_date'} != $self->start_date ) {
+
+      $self->set('start_date', $opt{'start_date'});
+    }
+    if ($self->modified) { # for quantity or start_date change
+      my $error = $self->replace;
+      return $error if $error;
+    }
+
+    if ( exists($opt{'amount'}) 
+          and $part_pkg->option('setup_fee') != $opt{'amount'}
+          and $opt{'amount'} > 0 ) {
+
+      $pkg_opt{'setup_fee'} = $opt{'amount'};
+      # standard for one-time charges is to set comment = (formatted) amount
+      # update it to avoid confusion
+      my $conf = FS::Conf->new;
+      $part_pkg->set('comment', 
+        ($conf->config('money_char') || '$') .
+        sprintf('%.2f', $opt{'amount'})
+      );
+    }
+  } # else simply ignore them; the UI shouldn't allow editing the fields
+
   my $error = $part_pkg->replace( options => \%pkg_opt );
-  return $error if $error;
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
 
   if (defined $old_classnum) {
     # fix invoice grouping records
diff --git a/httemplate/edit/process/quick-charge.cgi b/httemplate/edit/process/quick-charge.cgi
index db41fb2..07797d0 100644
--- a/httemplate/edit/process/quick-charge.cgi
+++ b/httemplate/edit/process/quick-charge.cgi
@@ -2,7 +2,7 @@
 %   $cgi->param('error', $error );
 <% $cgi->redirect($p.'quick-charge.html?'. $cgi->query_string) %>
 % } else {
-<% header(emt("One-time charge added")) %>
+<% header(emt($message)) %>
   <SCRIPT TYPE="text/javascript">
     window.top.location.reload();
   </SCRIPT>
@@ -34,7 +34,10 @@ my $cust_main = FS::cust_main->by_key($custnum)
 exists($curuser->agentnums_href->{$cust_main->agentnum})
   or die "access denied";
 
+my $message;
+
 if ( $param->{'pkgnum'} =~ /^(\d+)$/ ) {
+  $message = "One-time charge changed";
   my $pkgnum = $1;
   die "access denied"
     unless $curuser->access_right('Modify one-time charge');
@@ -45,14 +48,31 @@ if ( $param->{'pkgnum'} =~ /^(\d+)$/ ) {
   my $part_pkg = $cust_pkg->part_pkg;
   die "pkgnum $pkgnum is not a one-time charge" unless $part_pkg->freq eq '0';
 
+  my ($amount, $quantity, $start_date);
+  if ( $cgi->param('amount') =~ /^\s*(\d*(\.\d{1,2})*)\s*$/ ) {
+    $amount = sprintf('%.2f', $1);
+  }
+  if ( $cgi->param('quantity') =~ /^\s*(\d*)\s*$/ ) {
+    $quantity = $1 || 1;
+  }
+  if ( $cgi->param('start_date') ) {
+    $start_date = parse_datetime($cgi->param('start_date'));
+  } else {
+    $start_date = time;
+  }
+
   $error = $cust_pkg->modify_charge(
       'pkg'               => scalar($cgi->param('pkg')),
       'classnum'          => scalar($cgi->param('classnum')),
       'additional'        => \@description,
       'adjust_commission' => ($cgi->param('adjust_commission') ? 1 : 0),
+      'amount'            => $amount,
+      'quantity'          => $quantity,
+      'start_date'        => $start_date,
   );
 
 } else {
+  $message = "One-time charge added";
   # the usual case: new one-time charge
   $param->{"amount"} =~ /^\s*(\d*(?:\.?\d{1,2}))\s*$/
     or $error .= "Illegal amount " . $param->{"amount"} . "  ";
diff --git a/httemplate/edit/quick-charge.html b/httemplate/edit/quick-charge.html
index 666ba82..7b88bcc 100644
--- a/httemplate/edit/quick-charge.html
+++ b/httemplate/edit/quick-charge.html
@@ -107,14 +107,19 @@ function bill_now_changed (what) {
 % if ( $cust_pkg ) {
 
 <INPUT TYPE="hidden" NAME="pkgnum" VALUE="<% $cust_pkg->pkgnum %>">
-<& /elements/tr-fixed.html,
+% my $field = '/elements/tr-input-text.html';
+% # don't allow changing these after the fact
+% $field = '/elements/tr-fixed.html' if $billed;
+<& $field,
   label => 'Amount',
   field => 'amount',
-  value => $money_char . sprintf('%.2f',$part_pkg->option('setup_fee')),
-&>
+  value => sprintf('%.2f',$part_pkg->option('setup_fee')),
+  size  => 8,
+  prefix  => $money_char,
+&> 
 
 %   if ( $conf->exists('invoice-unitprice') ) {
-<& /elements/tr-fixed.html,
+<& $field,
   label => 'Quantity',
   field => 'quantity',
   value => $cust_pkg->quantity
@@ -137,14 +142,22 @@ function bill_now_changed (what) {
 % #display the future or past charge date, but don't allow changes
 % # XXX we probably _could_ let as-yet unbilled charges be rescheduled, but
 % # there's no compelling need yet
-%   if ( $cust_pkg->setup or $cust_pkg->start_date ) {
-%     my $label = $cust_pkg->setup ? emt('Billed on') : emt('Will be billed');
-%     my $field = $cust_pkg->setup ? 'setup' : 'start_date';
+%   if ( $billed ) {
       <& /elements/tr-fixed-date.html,
-        label => $label,
-        value => $cust_pkg->get($field)
+        label => emt('Billed on'),
+        value => $cust_pkg->get('setup')
+      &>
+%   } else {
+      <& /elements/tr-input-date-field.html,
+        {
+          name    => 'start_date',
+          label   => emt('Will be billed'),
+          value   => $cust_pkg->get('start_date'),
+          format  => $date_format,
+          noinit  => 1,
+        }
       &>
-%   } # else we don't show anything here
+%   }
 % } else { # new one-time charge
 
 <TR>
@@ -446,4 +459,6 @@ if ( $cust_pkg ) { # set defaults
   }
 }
 
+my $billed = $cust_pkg->get('setup') ? 1 : 0;
+
 </%init>
diff --git a/httemplate/elements/tr-fixed.html b/httemplate/elements/tr-fixed.html
index dd07d90..6904e3b 100644
--- a/httemplate/elements/tr-fixed.html
+++ b/httemplate/elements/tr-fixed.html
@@ -13,6 +13,7 @@ my %opt = @_;
 my $style = $opt{'cell_style'} ? 'STYLE="'. $opt{'cell_style'}. '"' : '';
 
 my $value = $opt{'formatted_value'} || $opt{'curr_value'} || $opt{'value'};
+$value = $opt{'prefix'} . $value if defined($opt{'prefix'});
 
 unless ( $opt{'noescape'} ) {
   #compatibility with select-table and friends

-----------------------------------------------------------------------

Summary of changes:
 FS/FS/cust_pkg.pm                        |   51 +++++++++++++++++++++++++++---
 httemplate/edit/process/quick-charge.cgi |   22 ++++++++++++-
 httemplate/edit/quick-charge.html        |   35 ++++++++++++++------
 httemplate/elements/tr-fixed.html        |    1 +
 4 files changed, 93 insertions(+), 16 deletions(-)




More information about the freeside-commits mailing list