time/data/etc. unit pricing add-ons, RT#24392
authorIvan Kohler <ivan@freeside.biz>
Wed, 8 Jan 2014 06:55:58 +0000 (22:55 -0800)
committerIvan Kohler <ivan@freeside.biz>
Wed, 8 Jan 2014 06:55:58 +0000 (22:55 -0800)
FS/FS/part_pkg_usageprice.pm
httemplate/browse/part_pkg.cgi
httemplate/edit/part_pkg.cgi
httemplate/edit/process/part_pkg.cgi
httemplate/elements/part_pkg_usageprice.html

index 0fa6bca..88e3870 100644 (file)
@@ -2,6 +2,7 @@ package FS::part_pkg_usageprice;
 use base qw( FS::Record );
 
 use strict;
 use base qw( FS::Record );
 
 use strict;
+use Tie::IxHash;
 #use FS::Record qw( qsearch qsearchs );
 
 =head1 NAME
 #use FS::Record qw( qsearch qsearchs );
 
 =head1 NAME
@@ -120,6 +121,38 @@ sub check {
   $self->SUPER::check;
 }
 
   $self->SUPER::check;
 }
 
+=item targets
+
+Returns a hash reference.  Keys are possible values for the "target" field.
+Values are hash references with "label" and "multiplier" keys.
+
+=cut
+
+sub targets {
+
+  tie my %targets, 'Tie::IxHash', # once?
+    #'svc_acct.totalbytes' => { label      => 'Megabytes',
+    #                           multiplier => 1048576,
+    #                         },
+    'svc_acct.totalbytes' => { label      => 'Gigabytes',
+                               multiplier => 1073741824,
+                             },
+    'svc_acct.seconds' => { label      => 'Hours',
+                            multiplier => 3600,
+                          },
+    'svc_conferencing.participants' => { label     => 'Conference Participants',
+                                         multiplier=> 1,
+                                       },
+  #this will take more work: set action, not increment..
+  #  and then value comes from a select, not a text field
+  #  'svc_conferencing.confqualitynum' => { label => 'Conference Quality',
+  #                                        },
+  ;
+
+  \%targets;
+
+}
+
 =back
 
 =head1 BUGS
 =back
 
 =head1 BUGS
index 8b50a50..b073866 100755 (executable)
@@ -277,6 +277,21 @@ push @fields, sub {
         : ()
       ),
     ],
         : ()
       ),
     ],
+    (
+      map { my $amount = $_->amount
+              / (FS::part_pkg_usageprice->targets->{$_->target}{multiplier}||1);
+            my $label = FS::part_pkg_usageprice->targets->{$_->target}{label};
+            [
+              { data    => "Plus&nbsp;$money_char". $_->price. '&nbsp;'.
+                           ( $_->action eq 'increment' ? 'per' : 'for' ).
+                           "&nbsp;$amount&nbsp;$label",
+                align   => 'center', #left?
+                colspan => 2,
+              },
+            ];
+          }
+        $part_pkg->part_pkg_usageprice
+    ),
     ( map { my $dst_pkg = $_->dst_pkg;
             [
               { data => 'Supplemental: &nbsp;'.
     ( map { my $dst_pkg = $_->dst_pkg;
             [
               { data => 'Supplemental: &nbsp;'.
index 23de7c1..3fee36b 100755 (executable)
@@ -806,13 +806,15 @@ my $javascript = <<'END';
 
       var plan = what.options[what.selectedIndex].value;
 
 
       var plan = what.options[what.selectedIndex].value;
 
-      var term_table = document.getElementById('TableNumber7') // XXX NOT ROBUST
+      var term_table = document.getElementById('TableNumber8') // XXX NOT ROBUST
       if ( plan == 'flat' || plan == 'prorate' || plan == 'subscription' ) {
         //term_table.disabled = false;
       if ( plan == 'flat' || plan == 'prorate' || plan == 'subscription' ) {
         //term_table.disabled = false;
-        term_table.style.visibility = '';
+        //term_table.style.visibility = '';
+        term_table.style.display = '';
       } else {
         //term_table.disabled = true;
       } else {
         //term_table.disabled = true;
-        term_table.style.visibility = 'hidden';
+        //term_table.style.visibility = 'hidden';
+        term_table.style.display = 'none';
       }
 
       var currency_regex = /^(setup|recur)_fee_[A-Z]{3}$/;
       }
 
       var currency_regex = /^(setup|recur)_fee_[A-Z]{3}$/;
index 82c4e1e..69138bc 100755 (executable)
@@ -264,6 +264,21 @@ if ( $cgi->param('pkgpart') || ! $conf->exists('agent_defaultpkg') ) {
   };
 }
 
   };
 }
 
+my $targets = FS::part_pkg_usageprice->targets;
+foreach my $amount_param ( grep /^usagepricepart(\d+)_amount$/, $cgi->param ) {
+  $amount_param =~ /^usagepricepart(\d+)_amount$/ or die 'unpossible';
+  my $num = $1;
+  my $amount = $cgi->param($amount_param);
+  if ( ! $amount && ! $cgi->param("usagepricepart${num}_price") ) {
+    #don't add empty rows just because the dropdowns have a value
+    $cgi->param("usagepricepart${num}_$_", '') for qw( currency action target );
+    next;
+  } 
+  my $target = $cgi->param("usagepricepart${num}_target");
+  $amount *= $targets->{$target}{multiplier} if $targets->{$target}{multiplier};
+  $cgi->param($amount_param, $amount);
+}
+
 my @process_o2m = (
   {
     'table'  => 'part_pkg_msgcat',
 my @process_o2m = (
   {
     'table'  => 'part_pkg_msgcat',
@@ -272,6 +287,7 @@ my @process_o2m = (
   {
     'table'  => 'part_pkg_usageprice',
     'fields' => [qw( price currency action target amount )],
   {
     'table'  => 'part_pkg_usageprice',
     'fields' => [qw( price currency action target amount )],
+
   }
 );
 
   }
 );
 
index 5663469..5482137 100644 (file)
                 ID   = "<%$id%>_target"
                 <% $onchange %>
         >
                 ID   = "<%$id%>_target"
                 <% $onchange %>
         >
-%       foreach my $target (keys %targets) {
+%       foreach my $target (keys %$targets) {
           <OPTION VALUE="<% $target %>"
                   <% ($cgi->param($name.'_target') || $part_pkg_usageprice->target) eq $target ? 'SELECTED' : '' %>
           <OPTION VALUE="<% $target %>"
                   <% ($cgi->param($name.'_target') || $part_pkg_usageprice->target) eq $target ? 'SELECTED' : '' %>
-          ><% $targets{$target}->{label} %>
+          ><% $targets->{$target}->{label} %>
 %       }
       </TD>
 
 %     ###
 %     # amount
 %     ###
 %       }
       </TD>
 
 %     ###
 %     # amount
 %     ###
+%     my $amount =
+%       scalar($cgi->param($name.'_amount'))
+%         || ( $part_pkg_usageprice->amount
+%               / ($targets->{$part_pkg_usageprice->target}{multiplier} || 1) );
+%     $amount = '' unless $amount > 0;
       <TD>
         <INPUT TYPE = "text"
                NAME = "<%$name%>_amount"
                ID   = "<%$id%>_amount"
                SIZE = 5
       <TD>
         <INPUT TYPE = "text"
                NAME = "<%$name%>_amount"
                ID   = "<%$id%>_amount"
                SIZE = 5
-               VALUE = "<% scalar($cgi->param($name.'_amount'))
-                             || $part_pkg_usageprice->amount
-                        %>"
+               VALUE = "<% $amount %>"
                <% $onchange %>
         >
       </TD>
                <% $onchange %>
         >
       </TD>
 % }
 <%init>
 
 % }
 <%init>
 
-tie my %targets, 'Tie::IxHash', # once?
-  #'svc_acct.totalbytes' => { label => 'Megabytes',
-  #                           mult  => 1048576,
-  #                         },
-  'svc_acct.totalbytes' => { label => 'Gigabytes',
-                             mult  => 1073741824,
-                           },
-  'svc_acct.seconds' => { label => 'Hours',
-                          mult  => 3600,
-                        },
-  'svc_conferencing.participants' => { label => 'Conference Participants',
-                                       mult  => 1,
-                                     },
-#this will take more work: set action, not increment..
-#  and then value comes from a select, not a text field
-#  'svc_conferencing.confqualitynum' => { label => 'Conference Quality',
-#                                        },
-;
+my $targets = FS::part_pkg_usageprice->targets;
 
 my( %opt ) = @_;
 
 
 my( %opt ) = @_;