4 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {
5 $cgi->param('clone', $1);
7 $cgi->param('clone', '');
9 if ( $cgi->param('pkgnum') && $cgi->param('pkgnum') =~ /^(\d+)$/ ) {
10 $cgi->param('pkgnum', $1);
12 $cgi->param('pkgnum', '');
15 my ($query) = $cgi->keywords;
18 if ( $cgi->param('error') ) {
19 $part_pkg = new FS::part_pkg ( {
20 map { $_, scalar($cgi->param($_)) } fields('part_pkg')
23 if ( $cgi->param('clone') ) {
24 $action='Custom Pricing';
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});
32 unless ( $part_pkg ) {
33 $part_pkg = new FS::part_pkg {};
34 $part_pkg->plan('flat');
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");
42 $action ||= $part_pkg->pkgpart ? 'Edit' : 'Add';
43 my $hashref = $part_pkg->hashref;
46 print header("$action Package Definition", menubar(
47 'Main Menu' => popurl(2),
48 'View all packages' => popurl(2). 'browse/part_pkg.cgi',
50 #), ' onLoad="visualize()"');
52 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
54 if $cgi->param('error');
56 #print '<FORM ACTION="', popurl(1), 'process/part_pkg.cgi" METHOD=POST>';
57 print '<FORM NAME="dummy">';
59 #if ( $cgi->param('clone') ) {
60 # print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
62 #if ( $cgi->param('pkgnum') ) {
63 # print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
66 #print qq!<INPUT TYPE="hidden" NAME="pkgpart" VALUE="$hashref->{pkgpart}">!,
67 print "Package Part #", $hashref->{pkgpart} ? $hashref->{pkgpart} : "(NEW)";
70 tie %freq, 'Tie::IxHash',
71 '0' => '(no recurring fee)',
74 '2w' => 'biweekly (every 2 weeks)',
76 '2' => 'bimonthly (every 2 months)',
77 '3' => 'quarterly (every 3 months)',
78 '6' => 'semiannually (every 6 months)',
80 '24' => 'biannually (every 2 years)',
82 if ( $part_pkg->dbdef_table->column('freq')->type =~ /(int)/i ) {
83 delete $freq{$_} foreach grep { ! /^\d+$/ } keys %freq;
87 <%= ntable("#cccccc",2) %>
89 <TD ALIGN="right">Package (customer-visible)</TD>
91 <INPUT TYPE="text" NAME="pkg" SIZE=32 VALUE="<%= $part_pkg->pkg %>">
95 <TD ALIGN="right">Comment (customer-hidden)</TD>
97 <INPUT TYPE="text" NAME="comment" SIZE=32 VALUE="<%=$part_pkg->comment%>">
101 <TD ALIGN="right">Recurring fee frequency </TD>
104 <% foreach my $freq ( keys %freq ) { %>
105 <OPTION VALUE="<%= $freq %>"<%= $freq eq $part_pkg->freq ? ' SELECTED' : '' %>><%= $freq{$freq} %>
111 <TD ALIGN="right">Setup fee tax exempt</TD>
115 print '<INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y"';
116 print ' CHECKED' if $hashref->{setuptax} eq "Y";
121 <TR><TD ALIGN="right">Recurring fee tax exempt</TD><TD>
124 print '<INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y"';
125 print ' CHECKED' if $hashref->{recurtax} eq "Y";
130 my $conf = new FS::Conf;
131 #false laziness w/ view/cust_main.cgi quick order
132 if ( $conf->exists('enable_taxclasses') ) {
133 print '<TR><TD ALIGN="right">Tax class</TD><TD><SELECT NAME="taxclass">';
134 my $sth = dbh->prepare('SELECT DISTINCT taxclass FROM cust_main_county')
136 $sth->execute or die $sth->errstr;
137 foreach my $taxclass ( map $_->[0], @{$sth->fetchall_arrayref} ) {
138 print qq!<OPTION VALUE="$taxclass"!;
139 print ' SELECTED' if $taxclass eq $hashref->{taxclass};
140 print qq!>$taxclass</OPTION>!;
142 print '</SELECT></TD></TR>';
145 '<INPUT TYPE="hidden" NAME="taxclass" VALUE="'. $hashref->{taxclass}. '">';
148 print '<TR><TD ALIGN="right">Disable new orders</TD><TD>';
149 print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
150 print ' CHECKED' if $hashref->{disabled} eq "Y";
152 print '</TD></TR></TABLE>';
154 my $thead = "\n\n". ntable('#cccccc', 2). <<END;
155 <TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH><TH BGCOLOR="#dcdcdc">Service</TH></TR>
158 #unless ( $cgi->param('clone') ) {
161 #print <<END, $thead;
162 print <<END, itable(), '<TR><TD VALIGN="top">', $thead;
163 <BR><BR>Enter the quantity of each service this package includes.<BR><BR>
170 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
171 foreach my $part_svc ( @part_svc ) {
172 my $svcpart = $part_svc->svcpart;
173 my $pkgpart = $cgi->param('clone') || $part_pkg->pkgpart;
174 my $pkg_svc = $pkgpart && qsearchs( 'pkg_svc', {
175 'pkgpart' => $pkgpart,
176 'svcpart' => $svcpart,
177 } ) || new FS::pkg_svc ( {
178 'pkgpart' => $pkgpart,
179 'svcpart' => $svcpart,
182 #? #next unless $pkg_svc;
184 push @fixups, "pkg_svc$svcpart";
186 #unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
189 print '<TR>'; # if $count == 0 ;
190 print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
191 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
192 qq!"></TD><TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
193 qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
194 # print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
196 foreach ( 1 .. $columns-1 ) {
197 print "</TABLE></TD><TD VALIGN=\"top\">$thead"
198 if $count == int( $_ * scalar(@part_svc) / $columns );
201 print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
202 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
206 #unless ( $cgi->param('clone') ) {
209 print "</TR></TABLE></TD></TR></TABLE>";
210 #print "</TR></TABLE>";
213 foreach my $f ( qw( clone pkgnum ) ) {
214 print qq!<INPUT TYPE="hidden" NAME="$f" VALUE="!. $cgi->param($f). '">';
216 print '<INPUT TYPE="hidden" NAME="pkgpart" VALUE="'. $part_pkg->pkgpart. '">';
218 # prolly should be in database
219 tie my %plans, 'Tie::IxHash',
221 'name' => 'Flat rate (anniversary billing)',
223 'setup_fee' => { 'name' => 'Setup fee for this package',
226 'recur_fee' => { 'name' => 'Recurring fee for this package',
230 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
231 'setup' => 'what.setup_fee.value',
232 'recur' => 'what.recur_fee.value',
236 'name' => 'Free for X days, then flat rate (anniversary billing)',
238 'free_days' => { 'name' => 'Initial free days',
241 'setup_fee' => { 'name' => 'Setup fee for this package',
244 'recur_fee' => { 'name' => 'Recurring fee for this package',
248 'fieldorder' => [ 'free_days', 'setup_fee', 'recur_fee' ],
249 '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',
250 'recur' => 'what.recur_fee.value',
254 'name' => 'First partial month pro-rated, then flat-rate (1st of month billing)',
256 'setup_fee' => { 'name' => 'Setup fee for this package',
259 'recur_fee' => { 'name' => 'Recurring fee for this package',
263 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
264 'setup' => 'what.setup_fee.value',
265 '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) ; \'',
269 'name' => 'First partial month full charge, then flat-rate (1st of month billing)',
271 'setup_fee' => { 'name' => 'Setup fee for this package',
274 'recur_fee' => { 'name' => 'Recurring fee for this package',
278 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
279 'setup' => 'what.setup_fee.value',
280 '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',
283 'flat_comission_cust' => {
284 'name' => 'Flat rate with recurring commission per active customer',
286 'setup_fee' => { 'name' => 'Setup fee for this package',
289 'recur_fee' => { 'name' => 'Recurring fee for this package',
292 'comission_amount' => { 'name' => 'Commission amount per month (per active customer)',
295 'comission_depth' => { 'name' => 'Number of layers',
299 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
300 'setup' => 'what.setup_fee.value',
301 '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 + \';\'',
304 'flat_comission' => {
305 'name' => 'Flat rate with recurring commission per (any) active package',
307 'setup_fee' => { 'name' => 'Setup fee for this package',
310 'recur_fee' => { 'name' => 'Recurring fee for this package',
313 'comission_amount' => { 'name' => 'Commission amount per month (per active package)',
316 'comission_depth' => { 'name' => 'Number of layers',
320 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
321 'setup' => 'what.setup_fee.value',
322 '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 + \';\'',
325 'flat_comission_pkg' => {
326 'name' => 'Flat rate with recurring commission per (selected) active package',
328 'setup_fee' => { 'name' => 'Setup fee for this package',
331 'recur_fee' => { 'name' => 'Recurring fee for this package',
334 'comission_amount' => { 'name' => 'Commission amount per month (per uncancelled package)',
337 'comission_depth' => { 'name' => 'Number of layers',
340 'comission_pkgpart' => { 'name' => 'Applicable packages<BR><FONT SIZE="-1">(hold <b>ctrl</b> to select multiple packages)</FONT>',
341 'type' => 'select_multiple',
342 'select_table' => 'part_pkg',
343 'select_hash' => { 'disabled' => '' } ,
344 'select_key' => 'pkgpart',
345 'select_label' => 'pkg',
348 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount', 'comission_pkgpart' ],
349 'setup' => 'what.setup_fee.value',
350 '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 + \';\'',
356 'name' => 'Base charge plus charge per-hour from the session monitor',
358 'setup_fee' => { 'name' => 'Setup fee for this package',
361 'recur_flat' => { 'name' => 'Base monthly charge for this package',
364 'recur_included_hours' => { 'name' => 'Hours included',
367 'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
371 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
372 'setup' => 'what.setup_fee.value',
373 '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;\'',
377 'name' => 'Base charge plus charge per-minute from the session monitor',
379 'setup_fee' => { 'name' => 'Setup fee for this package',
382 'recur_flat' => { 'name' => 'Base monthly charge for this package',
385 'recur_included_min' => { 'name' => 'Minutes included',
388 'recur_minly_charge' => { 'name' => 'Additional charge per minute',
392 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_min', 'recur_minly_charge' ],
393 'setup' => 'what.setup_fee.value',
394 '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;\'',
398 'sqlradacct_hour' => {
399 'name' => 'Base charge plus charge per-hour (and for data) from an external sqlradius radacct table',
401 'setup_fee' => { 'name' => 'Setup fee for this package',
404 'recur_flat' => { 'name' => 'Base monthly charge for this package',
407 'recur_included_hours' => { 'name' => 'Hours included',
410 'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
413 'recur_included_input' => { 'name' => 'Input megabytes included',
416 'recur_input_charge' => { 'name' =>
417 'Additional charge per input megabyte',
420 'recur_included_output' => { 'name' => 'Output megabytes included',
423 'recur_output_charge' => { 'name' =>
424 'Additional charge per output megabyte',
427 'recur_included_total' => { 'name' =>
428 'Total input+output megabytes included',
431 'recur_total_charge' => { 'name' =>
432 'Additional charge per input+output megabyte',
436 '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 )],
437 'setup' => 'what.setup_fee.value',
438 '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 ;\'',
443 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
444 split("\n", $part_pkg->plandata );
446 tie my %options, 'Tie::IxHash', map { $_=>$plans{$_}->{'name'} } keys %plans;
448 my @form_select = ();
449 if ( $conf->exists('enable_taxclasses') ) {
450 push @form_select, 'taxclass';
452 push @fixups, 'taxclass'; #hidden
456 my $widget = new HTML::Widgets::SelectLayers(
457 'selected_layer' => $part_pkg->plan,
458 'options' => \%options,
459 'form_name' => 'dummy',
460 'form_action' => 'process/part_pkg.cgi',
461 'form_text' => [ qw(pkg comment freq clone pkgnum pkgpart), @fixups ],
462 'form_checkbox' => [ qw(setuptax recurtax disabled) ],
463 'form_select' => [ @form_select ],
464 'fixup_callback' => sub {
467 for my $p ( keys %plans ) {
468 $html .= "if ( what.plan.value == \"$p\" ) {
469 what.setup.value = $plans{$p}->{setup} ;
470 what.recur.value = $plans{$p}->{recur} ;
475 'layer_callback' => sub {
477 my $html = qq!<INPUT TYPE="hidden" NAME="plan" VALUE="$layer">!.
479 my $href = $plans{$layer}->{'fields'};
480 foreach my $field ( exists($plans{$layer}->{'fieldorder'})
481 ? @{$plans{$layer}->{'fieldorder'}}
485 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
487 if ( ! exists($href->{$field}{'type'}) ) {
488 $html .= qq!<INPUT TYPE="text" NAME="$field" VALUE="!.
489 ( exists($plandata{$field})
491 : $href->{$field}{'default'} ).
492 qq!" onChange="fchanged(this)">!;
493 } elsif ( $href->{$field}{'type'} eq 'select_multiple' ) {
494 $html .= qq!<SELECT MULTIPLE NAME="$field" onChange="fchanged(this)">!;
496 qsearch( $href->{$field}{'select_table'},
497 $href->{$field}{'select_hash'} )
499 my $value = $record->getfield($href->{$field}{'select_key'});
500 $html .= qq!<OPTION VALUE="$value"!.
501 ( $plandata{$field} =~ /(^|, *)$value *(,|$)/
504 '>'. $record->getfield($href->{$field}{'select_label'})
506 $html .= '</SELECT>';
509 $html .= '</TD></TR>';
513 $html .= '<INPUT TYPE="hidden" NAME="plandata" VALUE="'.
514 join(',', keys %{ $href } ). '">'.
517 $html .= '<INPUT TYPE="submit" VALUE="'.
518 ( $hashref->{pkgpart} ? "Apply changes" : "Add package" ).
519 '" onClick="fchanged(this)">';
521 $html .= '<BR><BR>don\'t edit this unless you know what you\'re doing '.
522 '<INPUT TYPE="button" VALUE="refresh expressions" '.
523 'onClick="fchanged(this)">'.
526 '<FONT SIZE="1">Setup expression<BR>'.
527 '<INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="'.
528 encode_entities($hashref->{setup}). '" onLoad="fchanged(this)">'.
530 '<FONT SIZE="1">Recurring espression<BR>'.
531 '<INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="'.
532 encode_entities($hashref->{recur}). '" onLoad="fchanged(this)">'.
545 Price plan <%= $widget->html %>