sqlradacct_hour price plan to charge per-hour against an external radacct table
[freeside.git] / httemplate / edit / part_pkg.cgi
1 <!-- mason kludge -->
2 <%
3
4 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {
5   $cgi->param('clone', $1);
6 } else {
7   $cgi->param('clone', '');
8 }
9 if ( $cgi->param('pkgnum') && $cgi->param('pkgnum') =~ /^(\d+)$/ ) {
10   $cgi->param('pkgnum', $1);
11 } else {
12   $cgi->param('pkgnum', '');
13 }
14
15 my ($query) = $cgi->keywords;
16 my $action = '';
17 my $part_pkg = '';
18 if ( $cgi->param('error') ) {
19   $part_pkg = new FS::part_pkg ( {
20     map { $_, scalar($cgi->param($_)) } fields('part_pkg')
21   } );
22 }
23 if ( $cgi->param('clone') ) {
24   $action='Custom Pricing';
25   my $old_part_pkg =
26     qsearchs('part_pkg', { 'pkgpart' => $cgi->param('clone') } );
27   $part_pkg ||= $old_part_pkg->clone;
28   $part_pkg->disabled('Y');
29 } elsif ( $query && $query =~ /^(\d+)$/ ) {
30   $part_pkg ||= qsearchs('part_pkg',{'pkgpart'=>$1});
31 } else {
32   unless ( $part_pkg ) {
33     $part_pkg = new FS::part_pkg {};
34     $part_pkg->plan('flat');
35   }
36 }
37 unless ( $part_pkg->plan ) { #backwards-compat
38   $part_pkg->plan('flat');
39   $part_pkg->plandata("setup_fee=". $part_pkg->setup. "\n".
40                       "recur_fee=". $part_pkg->recur. "\n");
41 }
42 $action ||= $part_pkg->pkgpart ? 'Edit' : 'Add';
43 my $hashref = $part_pkg->hashref;
44
45
46 print header("$action Package Definition", menubar(
47   'Main Menu' => popurl(2),
48   'View all packages' => popurl(2). 'browse/part_pkg.cgi',
49 ));
50 #), ' onLoad="visualize()"');
51
52 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
53       "</FONT>"
54   if $cgi->param('error');
55
56 #print '<FORM ACTION="', popurl(1), 'process/part_pkg.cgi" METHOD=POST>';
57 print '<FORM NAME="dummy">';
58
59 #if ( $cgi->param('clone') ) {
60 #  print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
61 #}
62 #if ( $cgi->param('pkgnum') ) {
63 #  print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
64 #}
65 #
66 #print qq!<INPUT TYPE="hidden" NAME="pkgpart" VALUE="$hashref->{pkgpart}">!,
67 print "Package Part #", $hashref->{pkgpart} ? $hashref->{pkgpart} : "(NEW)";
68
69 print ntable("#cccccc",2), <<END;
70 <TR><TD ALIGN="right">Package (customer-visible)</TD><TD><INPUT TYPE="text" NAME="pkg" SIZE=32 VALUE="$hashref->{pkg}"></TD></TR>
71 <TR><TD ALIGN="right">Comment (customer-hidden)</TD><TD><INPUT TYPE="text" NAME="comment" SIZE=32 VALUE="$hashref->{comment}"></TD></TR>
72 <TR><TD ALIGN="right">Frequency (months) of recurring fee</TD><TD><INPUT TYPE="text" NAME="freq" VALUE="$hashref->{freq}" SIZE=3>&nbsp;&nbsp;<I>0=no recurring fee, 1=monthly, 3=quarterly, 12=yearly</TD></TR>
73 <TR><TD ALIGN="right">Setup fee tax exempt</TD><TD>
74 END
75
76 print '<INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y"';
77 print ' CHECKED' if $hashref->{setuptax} eq "Y";
78 print '>';
79
80 print <<END;
81 </TD></TR>
82 <TR><TD ALIGN="right">Recurring fee tax exempt</TD><TD>
83 END
84
85 print '<INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y"';
86 print ' CHECKED' if $hashref->{recurtax} eq "Y";
87 print '>';
88
89 print '</TD></TR>';
90
91 my $conf = new FS::Conf;
92 #false laziness w/ view/cust_main.cgi quick order
93 if ( $conf->exists('enable_taxclasses') ) {
94   print '<TR><TD ALIGN="right">Tax class</TD><TD><SELECT NAME="taxclass">';
95   my $sth = dbh->prepare('SELECT DISTINCT taxclass FROM cust_main_county')
96     or die dbh->errstr;
97   $sth->execute or die $sth->errstr;
98   foreach my $taxclass ( map $_->[0], @{$sth->fetchall_arrayref} ) {
99     print qq!<OPTION VALUE="$taxclass"!;
100     print ' SELECTED' if $taxclass eq $hashref->{taxclass};
101     print qq!>$taxclass</OPTION>!;
102   }
103   print '</SELECT></TD></TR>';
104 } else {
105   print
106     '<INPUT TYPE="hidden" NAME="taxclass" VALUE="'. $hashref->{taxclass}. '">';
107 }
108
109 print '<TR><TD ALIGN="right">Disable new orders</TD><TD>';
110 print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
111 print ' CHECKED' if $hashref->{disabled} eq "Y";
112 print '>';
113 print '</TD></TR></TABLE>';
114
115 my $thead =  "\n\n". ntable('#cccccc', 2). <<END;
116 <TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH><TH BGCOLOR="#dcdcdc">Service</TH></TR>
117 END
118
119 #unless ( $cgi->param('clone') ) {
120 #dunno why...
121 unless ( 0 ) {
122   #print <<END, $thead;
123   print <<END, itable(), '<TR><TD VALIGN="top">', $thead;
124 <BR><BR>Enter the quantity of each service this package includes.<BR><BR>
125 END
126 }
127
128 my @fixups = ();
129 my $count = 0;
130 my $columns = 3;
131 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
132 foreach my $part_svc ( @part_svc ) {
133   my $svcpart = $part_svc->svcpart;
134   my $pkg_svc = qsearchs( 'pkg_svc', {
135     'pkgpart'  => $cgi->param('clone') || $part_pkg->pkgpart,
136     'svcpart'  => $svcpart,
137   } ) || new FS::pkg_svc ( {
138     'pkgpart'  => $cgi->param('clone') || $part_pkg->pkgpart,
139     'svcpart'  => $svcpart,
140     'quantity' => 0,
141   });
142   #? #next unless $pkg_svc;
143
144   push @fixups, "pkg_svc$svcpart";
145
146   #unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
147   #dunno why...
148   unless ( 0 ) {
149     print '<TR>'; # if $count == 0 ;
150     print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
151           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
152           qq!"></TD><TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
153           qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
154 #    print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
155     $count+=1;
156     foreach ( 1 .. $columns-1 ) {
157       print "</TABLE></TD><TD VALIGN=\"top\">$thead"
158         if $count == int( $_ * scalar(@part_svc) / $columns );
159     }
160   } else {
161     print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
162           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
163   }
164 }
165
166 #unless ( $cgi->param('clone') ) {
167 #dunno why...
168 unless ( 0 ) {
169   print "</TR></TABLE></TD></TR></TABLE>";
170   #print "</TR></TABLE>";
171 }
172
173 foreach my $f ( qw( clone pkgnum ) ) {
174   print qq!<INPUT TYPE="hidden" NAME="$f" VALUE="!. $cgi->param($f). '">';
175 }
176 print '<INPUT TYPE="hidden" NAME="pkgpart" VALUE="'. $part_pkg->pkgpart. '">';
177
178 # prolly should be in database
179 tie my %plans, 'Tie::IxHash',
180   'flat' => {
181     'name' => 'Flat rate (anniversary billing)',
182     'fields' => {
183       'setup_fee' => { 'name' => 'Setup fee for this package',
184                        'default' => 0,
185                      },
186       'recur_fee' => { 'name' => 'Recurring fee for this package',
187                        'default' => 0,
188                       },
189     },
190     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
191     'setup' => 'what.setup_fee.value',
192     'recur' => 'what.recur_fee.value',
193   },
194
195   'flat_delayed' => {
196     'name' => 'Free for X days, then flat rate (anniversary billing)',
197     'fields' =>  {
198       'free_days' => { 'name' => 'Initial free days',
199                        'default' => 0,
200                      },
201       'setup_fee' => { 'name' => 'Setup fee for this package',
202                        'default' => 0,
203                      },
204       'recur_fee' => { 'name' => 'Recurring fee for this package',
205                        'default' => 0,
206                       },
207     },
208     'fieldorder' => [ 'free_days', 'setup_fee', 'recur_fee' ],
209     'setup' => '\'my $d = $cust_pkg->bill || $time; $d += 86400 * \' + what.free_days.value + \'; $cust_pkg->bill($d); $cust_pkg_mod_flag=1; \' + what.setup_fee.value',
210     'recur' => 'what.recur_fee.value',
211   },
212
213   'prorate' => {
214     'name' => 'First partial month pro-rated, then flat-rate (1st of month billing)',
215     'fields' =>  {
216       'setup_fee' => { 'name' => 'Setup fee for this package',
217                        'default' => 0,
218                      },
219       'recur_fee' => { 'name' => 'Recurring fee for this package',
220                        'default' => 0,
221                       },
222     },
223     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
224     'setup' => 'what.setup_fee.value',
225     'recur' => '\'my $mnow = $sdate; my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($sdate) )[0,1,2,3,4,5]; my $mstart = timelocal(0,0,0,1,$mon,$year); my $mend = timelocal(0,0,0,1, $mon == 11 ? 0 : $mon+1, $year+($mon==11)); $sdate = $mstart; ( $part_pkg->freq - 1 ) * \' + what.recur_fee.value + \' / $part_pkg->freq + \' + what.recur_fee.value + \' / $part_pkg->freq * ($mend-$mnow) / ($mend-$mstart) ; \'',
226   },
227
228   'subscription' => {
229     'name' => 'First partial month full charge, then flat-rate (1st of month billing)',
230     'fields' => {
231       'setup_fee' => { 'name' => 'Setup fee for this package',
232                        'default' => 0,
233                      },
234       'recur_fee' => { 'name' => 'Recurring fee for this package',
235                        'default' => 0,
236                       },
237     },
238     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
239     'setup' => 'what.setup_fee.value',
240     'recur' => '\'my $mnow = $sdate; my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($sdate) )[0,1,2,3,4,5]; $sdate = timelocal(0,0,0,1,$mon,$year); \' + what.recur_fee.value',
241   },
242
243   'flat_comission_cust' => {
244     'name' => 'Flat rate with recurring commission per active customer',
245     'fields' => {
246       'setup_fee' => { 'name' => 'Setup fee for this package',
247                        'default' => 0,
248                      },
249       'recur_fee' => { 'name' => 'Recurring fee for this package',
250                        'default' => 0,
251                      },
252       'comission_amount' => { 'name' => 'Commission amount per month (per active customer)',
253                               'default' => 0,
254                             },
255       'comission_depth'  => { 'name' => 'Number of layers',
256                               'default' => 1,
257                             },
258     },
259     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
260     'setup' => 'what.setup_fee.value',
261     'recur' => '\'my $error = $cust_pkg->cust_main->credit( \' + what.comission_amount.value + \' * scalar($cust_pkg->cust_main->referral_cust_main_ncancelled(\' + what.comission_depth.value+ \')), "commission" ); die $error if $error; \' + what.recur_fee.value + \';\'',
262   },
263
264   'flat_comission' => {
265     'name' => 'Flat rate with recurring commission per (any) active package',
266     'fields' => {
267       'setup_fee' => { 'name' => 'Setup fee for this package',
268                        'default' => 0,
269                      },
270       'recur_fee' => { 'name' => 'Recurring fee for this package',
271                        'default' => 0,
272                      },
273       'comission_amount' => { 'name' => 'Commission amount per month (per active package)',
274                               'default' => 0,
275                             },
276       'comission_depth'  => { 'name' => 'Number of layers',
277                               'default' => 1,
278                             },
279     },
280     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
281     'setup' => 'what.setup_fee.value',
282     'recur' => '\'my $error = $cust_pkg->cust_main->credit( \' + what.comission_amount.value + \' * scalar($cust_pkg->cust_main->referral_cust_pkg(\' + what.comission_depth.value+ \')), "commission" ); die $error if $error; \' + what.recur_fee.value + \';\'',
283   },
284
285   'flat_comission_pkg' => {
286     'name' => 'Flat rate with recurring commission per (selected) active package',
287     'fields' => {
288       'setup_fee' => { 'name' => 'Setup fee for this package',
289                        'default' => 0,
290                      },
291       'recur_fee' => { 'name' => 'Recurring fee for this package',
292                        'default' => 0,
293                      },
294       'comission_amount' => { 'name' => 'Commission amount per month (per uncancelled package)',
295                               'default' => 0,
296                             },
297       'comission_depth'  => { 'name' => 'Number of layers',
298                               'default' => 1,
299                             },
300       'comission_pkgpart' => { 'name' => 'Applicable packages<BR><FONT SIZE="-1">(hold <b>ctrl</b> to select multiple packages)</FONT>',
301                                'type' => 'select_multiple',
302                                'select_table' => 'part_pkg',
303                                'select_hash'  => { 'disabled' => '' } ,
304                                'select_key'   => 'pkgpart',
305                                'select_label' => 'pkg',
306                              },
307     },
308     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount', 'comission_pkgpart' ],
309     'setup' => 'what.setup_fee.value',
310     'recur' => '""; var pkgparts = ""; for ( var c=0; c < document.flat_comission_pkg.comission_pkgpart.options.length; c++ ) { if (document.flat_comission_pkg.comission_pkgpart.options[c].selected) { pkgparts = pkgparts + document.flat_comission_pkg.comission_pkgpart.options[c].value + \', \'; } } what.recur.value = \'my $error = $cust_pkg->cust_main->credit( \' + what.comission_amount.value + \' * scalar( grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } ( \' + pkgparts + \'  ) } $cust_pkg->cust_main->referral_cust_pkg(\' + what.comission_depth.value+ \')), "commission" ); die $error if $error; \' + what.recur_fee.value + \';\'',
311   },
312
313
314
315   'sesmon_hour' => {
316     'name' => 'Base charge plus charge per-hour from the session monitor',
317     'fields' => {
318       'setup_fee' => { 'name' => 'Setup fee for this package',
319                        'default' => 0,
320                      },
321       'recur_flat' => { 'name' => 'Base monthly charge for this package',
322                         'default' => 0,
323                       },
324       'recur_included_hours' => { 'name' => 'Hours included',
325                                   'default' => 0,
326                                 },
327       'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
328                                  'default' => 0,
329                                },
330     },
331     'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
332     'setup' => 'what.setup_fee.value',
333     'recur' => '\'my $hours = $cust_pkg->seconds_since($cust_pkg->bill || 0) / 3600 - \' + what.recur_included_hours.value + \'; $hours = 0 if $hours < 0; \' + what.recur_flat.value + \' + \' + what.recur_hourly_charge.value + \' * $hours;\'',
334   },
335
336   'sesmon_minute' => {
337     'name' => 'Base charge plus charge per-minute from the session monitor',
338     'fields' => {
339       'setup_fee' => { 'name' => 'Setup fee for this package',
340                        'default' => 0,
341                      },
342       'recur_flat' => { 'name' => 'Base monthly charge for this package',
343                         'default' => 0,
344                       },
345       'recur_included_min' => { 'name' => 'Minutes included',
346                                 'default' => 0,
347                                 },
348       'recur_minly_charge' => { 'name' => 'Additional charge per minute',
349                                 'default' => 0,
350                               },
351     },
352     'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_min', 'recur_minly_charge' ],
353     'setup' => 'what.setup_fee.value',
354     'recur' => '\'my $min = $cust_pkg->seconds_since($cust_pkg->bill || 0) / 60 - \' + what.recur_included_min.value + \'; $min = 0 if $min < 0; \' + what.recur_flat.value + \' + \' + what.recur_minly_charge.value + \' * $min;\'',
355
356   },
357
358   'sqlradacct_hour' => {
359     'name' => 'Base charge plus charge per-hour from an external SQL radacct table',
360     'fields' => {
361       'setup_fee' => { 'name' => 'Setup fee for this package',
362                        'default' => 0,
363                      },
364       'recur_flat' => { 'name' => 'Base monthly charge for this package',
365                         'default' => 0,
366                       },
367       'sql_datasrc' => { 'name' => 'DBI data source',
368                          'default' => 'DBI:mysql:host=radius.server.name;dbname=radius',
369                        },
370       'sql_username' => { 'name' => 'Database username',
371                           'default' => 'radius',
372                         },
373       'sql_password' => { 'name' => 'Database password',
374                           'default' => '',
375                         },
376       'recur_included_hours' => { 'name' => 'Hours included',
377                                   'default' => 0,
378                                 },
379       'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
380                                  'default' => 0,
381                                },
382     },
383     'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
384     'setup' => 'what.setup_fee.value',
385     'recur' => '\'my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($sdate))[0,1,2,3,4,5]; $mon+=$part_pkg->freq; until ($mon<12) { $mon-=12; $year++ }; $edate = timelocal($sec,$min,$hour,$mday,$mon,$year); my $hours = $cust_pkg->seconds_since_sqlradacct($sdate, $edate, \' + what.sql_datasrc + \', \' + what.sql_username + \', \' + what.sql_password + \' ) / 3600 - \' + what.recur_included_hours.value + \'; $hours = 0 if $hours < 0; \' + what.recur_flat.value + \' + \' + what.recur_hourly_charge.value + \' * $hours;\'',
386   },
387
388
389 ;
390
391 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
392                     split("\n", $part_pkg->plandata );
393
394 tie my %options, 'Tie::IxHash', map { $_=>$plans{$_}->{'name'} } keys %plans;
395
396 my @form_select = ();
397 if ( $conf->exists('enable_taxclasses') ) {
398   push @form_select, 'taxclass';
399 } else {
400   push @fixups, 'taxclass'; #hidden
401 }
402
403
404 my $widget = new HTML::Widgets::SelectLayers(
405   'selected_layer' => $part_pkg->plan,
406   'options'        => \%options,
407   'form_name'      => 'dummy',
408   'form_action'    => 'process/part_pkg.cgi',
409   'form_text'      => [ qw(pkg comment freq clone pkgnum pkgpart), @fixups ],
410   'form_checkbox'  => [ qw(setuptax recurtax disabled) ],
411   'form_select'    => [ @form_select ],
412   'fixup_callback' => sub {
413                         #my $ = @_;
414                         my $html = '';
415                         for my $p ( keys %plans ) {
416                           $html .= "if ( what.plan.value == \"$p\" ) {
417                                       what.setup.value = $plans{$p}->{setup} ;
418                                       what.recur.value = $plans{$p}->{recur} ;
419                                     }\n";
420                         }
421                         $html;
422                       },
423   'layer_callback' => sub {
424     my $layer = shift;
425     my $html = qq!<INPUT TYPE="hidden" NAME="plan" VALUE="$layer">!.
426                ntable("#cccccc",2);
427     my $href = $plans{$layer}->{'fields'};
428     foreach my $field ( exists($plans{$layer}->{'fieldorder'})
429                           ? @{$plans{$layer}->{'fieldorder'}}
430                           : keys %{ $href }
431                       ) {
432
433       $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
434
435       if ( ! exists($href->{$field}{'type'}) ) {
436         $html .= qq!<INPUT TYPE="text" NAME="$field" VALUE="!.
437                  ( exists($plandata{$field})
438                      ? $plandata{$field}
439                      : $href->{$field}{'default'} ).
440                  qq!" onChange="fchanged(this)">!;
441       } elsif ( $href->{$field}{'type'} eq 'select_multiple' ) {
442         $html .= qq!<SELECT MULTIPLE NAME="$field" onChange="fchanged(this)">!;
443         foreach my $record (
444           qsearch( $href->{$field}{'select_table'},
445                    $href->{$field}{'select_hash'}   )
446         ) {
447           my $value = $record->getfield($href->{$field}{'select_key'});
448           $html .= qq!<OPTION VALUE="$value"!.
449                    (  $plandata{$field} =~ /(^|, *)$value *(,|$)/
450                         ? ' SELECTED'
451                         : ''          ).
452                    '>'. $record->getfield($href->{$field}{'select_label'})
453         }
454         $html .= '</SELECT>';
455       }
456
457       $html .= '</TD></TR>';
458     }
459     $html .= '</TABLE>';
460
461     $html .= '<INPUT TYPE="hidden" NAME="plandata" VALUE="'.
462              join(',', keys %{ $href } ). '">'.
463              '<BR><BR>';
464              
465     $html .= '<INPUT TYPE="submit" VALUE="'.
466              ( $hashref->{pkgpart} ? "Apply changes" : "Add package" ).
467              '" onClick="fchanged(this)">';
468
469     $html .= '<BR><BR>don\'t edit this unless you know what you\'re doing '.
470              '<INPUT TYPE="button" VALUE="refresh expressions" '.
471                'onClick="fchanged(this)">'.
472              ntable("#cccccc",2).
473              '<TR><TD>'.
474              '<FONT SIZE="1">Setup expression<BR>'.
475              '<INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="'.
476                $hashref->{setup}. '" onLoad="fchanged(this)">'.
477              '</FONT><BR>'.
478              '<FONT SIZE="1">Recurring espression<BR>'.
479              '<INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="'.
480                $hashref->{recur}. '" onLoad="fchanged(this)">'.
481              '</FONT>'.
482              '</TR></TD>'.
483              '</TABLE>';
484
485     $html;
486
487   },
488 );
489
490 %>
491
492 <BR>
493 Price plan <%= $widget->html %>
494   </BODY>
495 </HTML>