daily/weekly billing
[freeside.git] / httemplate / edit / part_pkg.cgi
index a63fc23..daa5b74 100755 (executable)
@@ -66,12 +66,51 @@ print '<FORM NAME="dummy">';
 #print qq!<INPUT TYPE="hidden" NAME="pkgpart" VALUE="$hashref->{pkgpart}">!,
 print "Package Part #", $hashref->{pkgpart} ? $hashref->{pkgpart} : "(NEW)";
 
-print ntable("#cccccc",2), <<END;
-<TR><TD ALIGN="right">Package (customer-visable)</TD><TD><INPUT TYPE="text" NAME="pkg" SIZE=32 VALUE="$hashref->{pkg}"></TD></TR>
-<TR><TD ALIGN="right">Comment (customer-hidden)</TD><TD><INPUT TYPE="text" NAME="comment" SIZE=32 VALUE="$hashref->{comment}"></TD></TR>
-<TR><TD ALIGN="right">Frequency (months) of recurring fee</TD><TD><INPUT TYPE="text" NAME="freq" VALUE="$hashref->{freq}" SIZE=3></TD></TR>
-<TR><TD ALIGN="right">Setup fee tax exempt</TD><TD>
-END
+my %freq;
+tie %freq, 'Tie::IxHash', 
+  '0'  => '(no recurring fee)',
+  '1d' => 'daily',
+  '1w' => 'weekly',
+  '2w' => 'biweekly (every 2 weeks)',
+  '1'  => 'monthly',
+  '2'  => 'bimonthly (every 2 months)',
+  '3'  => 'quarterly (every 3 months)',
+  '6'  => 'semiannually (every 6 months)',
+  '12' => 'annually',
+  '24' => 'biannually (every 2 years)',
+;
+if ( $part_pkg->dbdef_table->column('freq')->type =~ /(int)/i ) {
+  delete $freq{$_} foreach grep { ! /^\d+$/ } keys %freq;
+}
+
+%>
+<%= ntable("#cccccc",2) %>
+  <TR>
+    <TD ALIGN="right">Package (customer-visible)</TD>
+    <TD>
+      <INPUT TYPE="text" NAME="pkg" SIZE=32 VALUE="<%= $part_pkg->pkg %>">
+    </TD>
+  </TR>
+  <TR>
+    <TD ALIGN="right">Comment (customer-hidden)</TD>
+    <TD>
+      <INPUT TYPE="text" NAME="comment" SIZE=32 VALUE="<%=$part_pkg->comment%>">
+    </TD>
+  </TR>
+  <TR>
+    <TD ALIGN="right">Recurring fee frequency </TD>
+    <TD>
+      <SELECT NAME="freq">
+        <% foreach my $freq ( keys %freq ) { %>
+          <OPTION VALUE="<%= $freq %>"<%= $freq eq $part_pkg->freq ? ' SELECTED' : '' %>><%= $freq{$freq} %>
+        <% } %>
+      </SELECT>
+    </TD>
+  </TR>
+  <TR>
+    <TD ALIGN="right">Setup fee tax exempt</TD>
+    <TD>
+<%
 
 print '<INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y"';
 print ' CHECKED' if $hashref->{setuptax} eq "Y";
@@ -88,6 +127,24 @@ print '>';
 
 print '</TD></TR>';
 
+my $conf = new FS::Conf;
+#false laziness w/ view/cust_main.cgi quick order
+if ( $conf->exists('enable_taxclasses') ) {
+  print '<TR><TD ALIGN="right">Tax class</TD><TD><SELECT NAME="taxclass">';
+  my $sth = dbh->prepare('SELECT DISTINCT taxclass FROM cust_main_county')
+    or die dbh->errstr;
+  $sth->execute or die $sth->errstr;
+  foreach my $taxclass ( map $_->[0], @{$sth->fetchall_arrayref} ) {
+    print qq!<OPTION VALUE="$taxclass"!;
+    print ' SELECTED' if $taxclass eq $hashref->{taxclass};
+    print qq!>$taxclass</OPTION>!;
+  }
+  print '</SELECT></TD></TR>';
+} else {
+  print
+    '<INPUT TYPE="hidden" NAME="taxclass" VALUE="'. $hashref->{taxclass}. '">';
+}
+
 print '<TR><TD ALIGN="right">Disable new orders</TD><TD>';
 print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
 print ' CHECKED' if $hashref->{disabled} eq "Y";
@@ -113,11 +170,12 @@ my $columns = 3;
 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
 foreach my $part_svc ( @part_svc ) {
   my $svcpart = $part_svc->svcpart;
-  my $pkg_svc = qsearchs( 'pkg_svc', {
-    'pkgpart'  => $cgi->param('clone') || $part_pkg->pkgpart,
+  my $pkgpart = $cgi->param('clone') || $part_pkg->pkgpart;
+  my $pkg_svc = $pkgpart && qsearchs( 'pkg_svc', {
+    'pkgpart'  => $pkgpart,
     'svcpart'  => $svcpart,
   } ) || new FS::pkg_svc ( {
-    'pkgpart'  => $cgi->param('clone') || $part_pkg->pkgpart,
+    'pkgpart'  => $pkgpart,
     'svcpart'  => $svcpart,
     'quantity' => 0,
   });
@@ -223,7 +281,7 @@ tie my %plans, 'Tie::IxHash',
   },
 
   'flat_comission_cust' => {
-    'name' => 'Flat rate with recurring comission per active customer',
+    'name' => 'Flat rate with recurring commission per active customer',
     'fields' => {
       'setup_fee' => { 'name' => 'Setup fee for this package',
                        'default' => 0,
@@ -231,7 +289,7 @@ tie my %plans, 'Tie::IxHash',
       'recur_fee' => { 'name' => 'Recurring fee for this package',
                        'default' => 0,
                      },
-      'comission_amount' => { 'name' => 'Comission amount per month (per active customer)',
+      'comission_amount' => { 'name' => 'Commission amount per month (per active customer)',
                               'default' => 0,
                             },
       'comission_depth'  => { 'name' => 'Number of layers',
@@ -244,7 +302,7 @@ tie my %plans, 'Tie::IxHash',
   },
 
   'flat_comission' => {
-    'name' => 'Flat rate with recurring comission per (any) active package',
+    'name' => 'Flat rate with recurring commission per (any) active package',
     'fields' => {
       'setup_fee' => { 'name' => 'Setup fee for this package',
                        'default' => 0,
@@ -252,7 +310,7 @@ tie my %plans, 'Tie::IxHash',
       'recur_fee' => { 'name' => 'Recurring fee for this package',
                        'default' => 0,
                      },
-      'comission_amount' => { 'name' => 'Comission amount per month (per active package)',
+      'comission_amount' => { 'name' => 'Commission amount per month (per active package)',
                               'default' => 0,
                             },
       'comission_depth'  => { 'name' => 'Number of layers',
@@ -265,7 +323,7 @@ tie my %plans, 'Tie::IxHash',
   },
 
   'flat_comission_pkg' => {
-    'name' => 'Flat rate with recurring comission per (selected) active package',
+    'name' => 'Flat rate with recurring commission per (selected) active package',
     'fields' => {
       'setup_fee' => { 'name' => 'Setup fee for this package',
                        'default' => 0,
@@ -273,7 +331,7 @@ tie my %plans, 'Tie::IxHash',
       'recur_fee' => { 'name' => 'Recurring fee for this package',
                        'default' => 0,
                      },
-      'comission_amount' => { 'name' => 'Comission amount per month (per uncancelled package)',
+      'comission_amount' => { 'name' => 'Commission amount per month (per uncancelled package)',
                               'default' => 0,
                             },
       'comission_depth'  => { 'name' => 'Number of layers',
@@ -337,6 +395,49 @@ tie my %plans, 'Tie::IxHash',
 
   },
 
+  'sqlradacct_hour' => {
+    'name' => 'Base charge plus charge per-hour (and for data) from an external sqlradius radacct table',
+    'fields' => {
+      'setup_fee' => { 'name' => 'Setup fee for this package',
+                       'default' => 0,
+                     },
+      'recur_flat' => { 'name' => 'Base monthly charge for this package',
+                        'default' => 0,
+                      },
+      'recur_included_hours' => { 'name' => 'Hours included',
+                                  'default' => 0,
+                                },
+      'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
+                                 'default' => 0,
+                               },
+      'recur_included_input' => { 'name' => 'Input megabytes included',
+                                  'default' => 0,
+                                },
+      'recur_input_charge' => { 'name' =>
+                                        'Additional charge per input megabyte',
+                                'default' => 0,
+                              },
+      'recur_included_output' => { 'name' => 'Output megabytes included',
+                                   'default' => 0,
+                                },
+      'recur_output_charge' => { 'name' =>
+                                       'Additional charge per output megabyte',
+                                'default' => 0,
+                              },
+      'recur_included_total' => { 'name' =>
+                                       'Total input+output megabytes included',
+                                  'default' => 0,
+                                },
+      'recur_total_charge' => { 'name' =>
+                                 'Additional charge per input+output megabyte',
+                                'default' => 0,
+                              },
+    },
+    'fieldorder' => [qw( setup_fee recur_flat recur_included_hours recur_hourly_charge recur_included_input recur_input_charge recur_included_output recur_output_charge recur_included_total recur_total_charge )],
+    'setup' => 'what.setup_fee.value',
+    'recur' => '\'my $last_bill = $cust_pkg->last_bill; my $hours = $cust_pkg->seconds_since_sqlradacct($last_bill, $sdate ) / 3600 - \' + what.recur_included_hours.value + \'; $hours = 0 if $hours < 0; my $input = $cust_pkg->attribute_since_sqlradacct($last_bill, $sdate, \"AcctInputOctets\" ) / 1048576; my $output = $cust_pkg->attribute_since_sqlradacct($last_bill, $sdate, \"AcctOutputOctets\" ) / 1048576; my $total = $input + $output - \' + what.recur_included_total.value + \'; $total = 0 if $total < 0; my $input = $input - \' + what.recur_included_input.value + \'; $input = 0 if $input < 0; my $output = $output - \' + what.recur_included_output.value + \'; $output = 0 if $output < 0; my $totalcharge = sprintf(\"%.2f\", \' + what.recur_total_charge.value + \' * $total); my $hourscharge = sprintf(\"%.2f\", \' + what.recur_hourly_charge.value + \' * $hours); push @details, \"Last month\\\'s excess data \". sprintf(\"%.1f\", $total). \" megs: \\\$$totalcharge\", \"Last month\\\'s excess time \". sprintf(\"%.1f\", $hours). \" hours: \\\$$hourscharge\"; \' + what.recur_flat.value + \' + $hourscharge + \' + what.recur_input_charge.value + \' * $input + \' + what.recur_output_charge.value + \' * $output + $totalcharge ;\'',
+  },
+
 ;
 
 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
@@ -344,6 +445,14 @@ my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
 
 tie my %options, 'Tie::IxHash', map { $_=>$plans{$_}->{'name'} } keys %plans;
 
+my @form_select = ();
+if ( $conf->exists('enable_taxclasses') ) {
+  push @form_select, 'taxclass';
+} else {
+  push @fixups, 'taxclass'; #hidden
+}
+
+
 my $widget = new HTML::Widgets::SelectLayers(
   'selected_layer' => $part_pkg->plan,
   'options'        => \%options,
@@ -351,6 +460,7 @@ my $widget = new HTML::Widgets::SelectLayers(
   'form_action'    => 'process/part_pkg.cgi',
   'form_text'      => [ qw(pkg comment freq clone pkgnum pkgpart), @fixups ],
   'form_checkbox'  => [ qw(setuptax recurtax disabled) ],
+  'form_select'    => [ @form_select ],
   'fixup_callback' => sub {
                         #my $ = @_;
                         my $html = '';
@@ -415,11 +525,11 @@ my $widget = new HTML::Widgets::SelectLayers(
              '<TR><TD>'.
              '<FONT SIZE="1">Setup expression<BR>'.
              '<INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="'.
-               $hashref->{setup}. '" onLoad="fchanged(this)">'.
+               encode_entities($hashref->{setup}). '" onLoad="fchanged(this)">'.
              '</FONT><BR>'.
              '<FONT SIZE="1">Recurring espression<BR>'.
              '<INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="'.
-               $hashref->{recur}. '" onLoad="fchanged(this)">'.
+               encode_entities($hashref->{recur}). '" onLoad="fchanged(this)">'.
              '</FONT>'.
              '</TR></TD>'.
              '</TABLE>';