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)";
69 #false laziness w/view/cust_main.cgi
71 tie %freq, 'Tie::IxHash',
72 '0' => '(no recurring fee)',
75 '2w' => 'biweekly (every 2 weeks)',
77 '2' => 'bimonthly (every 2 months)',
78 '3' => 'quarterly (every 3 months)',
79 '6' => 'semiannually (every 6 months)',
81 '24' => 'biannually (every 2 years)',
83 if ( $part_pkg->dbdef_table->column('freq')->type =~ /(int)/i ) {
84 delete $freq{$_} foreach grep { ! /^\d+$/ } keys %freq;
88 <%= ntable("#cccccc",2) %>
90 <TD ALIGN="right">Package (customer-visible)</TD>
92 <INPUT TYPE="text" NAME="pkg" SIZE=32 VALUE="<%= $part_pkg->pkg %>">
96 <TD ALIGN="right">Comment (customer-hidden)</TD>
98 <INPUT TYPE="text" NAME="comment" SIZE=32 VALUE="<%=$part_pkg->comment%>">
102 <TD ALIGN="right">Recurring fee frequency </TD>
105 <% foreach my $freq ( keys %freq ) { %>
106 <OPTION VALUE="<%= $freq %>"<%= $freq eq $part_pkg->freq ? ' SELECTED' : '' %>><%= $freq{$freq} %>
112 <TD ALIGN="right">Setup fee tax exempt</TD>
116 print '<INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y"';
117 print ' CHECKED' if $hashref->{setuptax} eq "Y";
122 <TR><TD ALIGN="right">Recurring fee tax exempt</TD><TD>
125 print '<INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y"';
126 print ' CHECKED' if $hashref->{recurtax} eq "Y";
131 my $conf = new FS::Conf;
132 #false laziness w/ view/cust_main.cgi quick order
133 if ( $conf->exists('enable_taxclasses') ) {
134 print '<TR><TD ALIGN="right">Tax class</TD><TD><SELECT NAME="taxclass">';
135 my $sth = dbh->prepare('SELECT DISTINCT taxclass FROM cust_main_county')
137 $sth->execute or die $sth->errstr;
138 foreach my $taxclass ( map $_->[0], @{$sth->fetchall_arrayref} ) {
139 print qq!<OPTION VALUE="$taxclass"!;
140 print ' SELECTED' if $taxclass eq $hashref->{taxclass};
141 print qq!>$taxclass</OPTION>!;
143 print '</SELECT></TD></TR>';
146 '<INPUT TYPE="hidden" NAME="taxclass" VALUE="'. $hashref->{taxclass}. '">';
149 print '<TR><TD ALIGN="right">Disable new orders</TD><TD>';
150 print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
151 print ' CHECKED' if $hashref->{disabled} eq "Y";
153 print '</TD></TR></TABLE>';
155 my $thead = "\n\n". ntable('#cccccc', 2).
156 '<TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH>';
157 $thead .= '<TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Primary</FONT></TH>'
158 if dbdef->table('pkg_svc')->column('primary_svc');
159 $thead .= '<TH BGCOLOR="#dcdcdc">Service</TH></TR>';
161 #unless ( $cgi->param('clone') ) {
164 #print <<END, $thead;
165 print <<END, itable(), '<TR><TD VALIGN="top">', $thead;
166 <BR><BR>Enter the quantity of each service this package includes.<BR><BR>
173 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
174 foreach my $part_svc ( @part_svc ) {
175 my $svcpart = $part_svc->svcpart;
176 my $pkgpart = $cgi->param('clone') || $part_pkg->pkgpart;
177 my $pkg_svc = $pkgpart && qsearchs( 'pkg_svc', {
178 'pkgpart' => $pkgpart,
179 'svcpart' => $svcpart,
180 } ) || new FS::pkg_svc ( {
181 'pkgpart' => $pkgpart,
182 'svcpart' => $svcpart,
186 #? #next unless $pkg_svc;
188 push @fixups, "pkg_svc$svcpart";
190 #unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
193 print '<TR>'; # if $count == 0 ;
194 print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
195 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
197 if ( dbdef->table('pkg_svc')->column('primary_svc') ) {
198 print qq!<TD><INPUT TYPE="radio" NAME="pkg_svc_primary" VALUE="$svcpart"!;
199 print ' CHECKED' if $pkg_svc->primary_svc =~ /^Y/i;
202 print qq!<TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
203 qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
204 # print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
206 foreach ( 1 .. $columns-1 ) {
207 print "</TABLE></TD><TD VALIGN=\"top\">$thead"
208 if $count == int( $_ * scalar(@part_svc) / $columns );
211 print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
212 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
216 #unless ( $cgi->param('clone') ) {
219 print "</TR></TABLE></TD></TR></TABLE>";
220 #print "</TR></TABLE>";
223 foreach my $f ( qw( clone pkgnum ) ) {
224 print qq!<INPUT TYPE="hidden" NAME="$f" VALUE="!. $cgi->param($f). '">';
226 print '<INPUT TYPE="hidden" NAME="pkgpart" VALUE="'. $part_pkg->pkgpart. '">';
228 # prolly should be in database
229 tie my %plans, 'Tie::IxHash',
231 'name' => 'Flat rate (anniversary billing)',
233 'setup_fee' => { 'name' => 'Setup fee for this package',
236 'recur_fee' => { 'name' => 'Recurring fee for this package',
240 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
241 'setup' => 'what.setup_fee.value',
242 'recur' => 'what.recur_fee.value',
246 'name' => 'Free for X days, then flat rate (anniversary billing)',
248 'free_days' => { 'name' => 'Initial free days',
251 'setup_fee' => { 'name' => 'Setup fee for this package',
254 'recur_fee' => { 'name' => 'Recurring fee for this package',
258 'fieldorder' => [ 'free_days', 'setup_fee', 'recur_fee' ],
259 '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',
260 'recur' => 'what.recur_fee.value',
264 'name' => 'First partial month pro-rated, then flat-rate (1st of month billing)',
266 'setup_fee' => { 'name' => 'Setup fee for this package',
269 'recur_fee' => { 'name' => 'Recurring fee for this package',
273 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
274 'setup' => 'what.setup_fee.value',
275 '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) ; \'',
279 'name' => 'First partial month full charge, then flat-rate (1st of month billing)',
281 'setup_fee' => { 'name' => 'Setup fee for this package',
284 'recur_fee' => { 'name' => 'Recurring fee for this package',
288 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
289 'setup' => 'what.setup_fee.value',
290 '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',
293 'flat_comission_cust' => {
294 'name' => 'Flat rate with recurring commission per active customer',
296 'setup_fee' => { 'name' => 'Setup fee for this package',
299 'recur_fee' => { 'name' => 'Recurring fee for this package',
302 'comission_amount' => { 'name' => 'Commission amount per month (per active customer)',
305 'comission_depth' => { 'name' => 'Number of layers',
309 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
310 'setup' => 'what.setup_fee.value',
311 '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 + \';\'',
314 'flat_comission' => {
315 'name' => 'Flat rate with recurring commission per (any) active package',
317 'setup_fee' => { 'name' => 'Setup fee for this package',
320 'recur_fee' => { 'name' => 'Recurring fee for this package',
323 'comission_amount' => { 'name' => 'Commission amount per month (per active package)',
326 'comission_depth' => { 'name' => 'Number of layers',
330 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
331 'setup' => 'what.setup_fee.value',
332 '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 + \';\'',
335 'flat_comission_pkg' => {
336 'name' => 'Flat rate with recurring commission per (selected) active package',
338 'setup_fee' => { 'name' => 'Setup fee for this package',
341 'recur_fee' => { 'name' => 'Recurring fee for this package',
344 'comission_amount' => { 'name' => 'Commission amount per month (per uncancelled package)',
347 'comission_depth' => { 'name' => 'Number of layers',
350 'comission_pkgpart' => { 'name' => 'Applicable packages<BR><FONT SIZE="-1">(hold <b>ctrl</b> to select multiple packages)</FONT>',
351 'type' => 'select_multiple',
352 'select_table' => 'part_pkg',
353 'select_hash' => { 'disabled' => '' } ,
354 'select_key' => 'pkgpart',
355 'select_label' => 'pkg',
358 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount', 'comission_pkgpart' ],
359 'setup' => 'what.setup_fee.value',
360 '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 + \';\'',
366 'name' => 'Base charge plus charge per-hour from the session monitor',
368 'setup_fee' => { 'name' => 'Setup fee for this package',
371 'recur_flat' => { 'name' => 'Base monthly charge for this package',
374 'recur_included_hours' => { 'name' => 'Hours included',
377 'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
381 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
382 'setup' => 'what.setup_fee.value',
383 '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;\'',
387 'name' => 'Base charge plus charge per-minute from the session monitor',
389 'setup_fee' => { 'name' => 'Setup fee for this package',
392 'recur_flat' => { 'name' => 'Base monthly charge for this package',
395 'recur_included_min' => { 'name' => 'Minutes included',
398 'recur_minly_charge' => { 'name' => 'Additional charge per minute',
402 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_min', 'recur_minly_charge' ],
403 'setup' => 'what.setup_fee.value',
404 '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;\'',
408 'sqlradacct_hour' => {
409 'name' => 'Base charge plus charge per-hour (and for data) from an external sqlradius radacct table',
411 'setup_fee' => { 'name' => 'Setup fee for this package',
414 'recur_flat' => { 'name' => 'Base monthly charge for this package',
417 'recur_included_hours' => { 'name' => 'Hours included',
420 'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
423 'recur_included_input' => { 'name' => 'Input megabytes included',
426 'recur_input_charge' => { 'name' =>
427 'Additional charge per input megabyte',
430 'recur_included_output' => { 'name' => 'Output megabytes included',
433 'recur_output_charge' => { 'name' =>
434 'Additional charge per output megabyte',
437 'recur_included_total' => { 'name' =>
438 'Total input+output megabytes included',
441 'recur_total_charge' => { 'name' =>
442 'Additional charge per input+output megabyte',
446 '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 )],
447 'setup' => 'what.setup_fee.value',
448 '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; \' + what.recur_flat.value + \' + \' + what.recur_hourly_charge.value + \' * $hours + \' + what.recur_input_charge.value + \' * $input + \' + what.recur_output_charge.value + \' * $output + \' + what.recur_total_charge.value + \' * $total ;\'',
452 'name' => 'Base charge plus a metered rate from a configurable SQL query',
454 'setup_fee' => { 'name' => 'Setup fee for this package',
457 'recur_flat' => { 'name' => 'Base monthly charge for this package',
460 'recur_included' => { 'name' => 'Units included',
463 'recur_unit_charge' => { 'name' => 'Additional charge per unit',
466 'datasrc' => { 'name' => 'DBI data source',
469 'db_username' => { 'name' => 'Database username',
472 'db_password' => { 'name' => 'Database username',
475 'query' => { 'name' => 'SQL query',
479 'fieldorder' => [qw( setup_fee recur_flat recur_included recur_unit_charge datasrc db_username db_password query )],
480 'setup' => 'what.setup_fee.value',
481 # 'recur' => '\'my $dbh = DBI->connect(\"\' + what.datasrc.value + \'\", \"\' + what.db_username.value + \'\") or die $DBI::errstr; \'',
482 'recur' => '\'my $dbh = DBI->connect(\"\' + what.datasrc.value + \'\", \"\' + what.db_username.value + \'\", \"\' + what.db_password.value + \'\" ) or die $DBI::errstr; my $sth = $dbh->prepare(\"\' + what.query.value + \'\") or die $dbh->errstr; my $units = 0; foreach my $cust_svc ( grep { $_->part_svc->svcdb eq \"svc_domain\" } $cust_pkg->cust_svc ) { my $domain = $cust_svc->svc_x->domain; $sth->execute($domain) or die $sth->errstr; $units += $sth->fetchrow_arrayref->[0]; } $units -= \' + what.recur_included.value + \'; $units = 0 if $units < 0; \' + what.recur_flat.value + \' + $units * \' + what.recur_unit_charge.value + \';\'',
483 #'recur' => '\'my $dbh = DBI->connect("\' + what.datasrc.value + \'", "\' + what.db_username.value + \'", "\' what.db_password.value + \'" ) or die $DBI::errstr; my $sth = $dbh->prepare("\' + what.query.value + \'") or die $dbh->errstr; my $units = 0; foreach my $cust_svc ( grep { $_->part_svc->svcdb eq "svc_domain" } $cust_pkg->cust_svc ) { my $domain = $cust_svc->svc_x->domain; $sth->execute($domain) or die $sth->errstr; $units += $sth->fetchrow_arrayref->[0]; } $units -= \' + what.recur_included.value + \'; $units = 0 if $units < 0; \' + what.recur_flat.value + \' + $units * \' + what.recur_unit_charge + \';\'',
488 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
489 split("\n", $part_pkg->plandata );
491 tie my %options, 'Tie::IxHash', map { $_=>$plans{$_}->{'name'} } keys %plans;
493 my @form_select = ();
494 if ( $conf->exists('enable_taxclasses') ) {
495 push @form_select, 'taxclass';
497 push @fixups, 'taxclass'; #hidden
501 if ( dbdef->table('pkg_svc')->column('primary_svc') ) {
502 push @form_radio, 'pkg_svc_primary';
505 my $widget = new HTML::Widgets::SelectLayers(
506 'selected_layer' => $part_pkg->plan,
507 'options' => \%options,
508 'form_name' => 'dummy',
509 'form_action' => 'process/part_pkg.cgi',
510 'form_text' => [ qw(pkg comment freq clone pkgnum pkgpart), @fixups ],
511 'form_checkbox' => [ qw(setuptax recurtax disabled) ],
512 'form_radio' => \@form_radio,
513 'form_select' => \@form_select,
514 'fixup_callback' => sub {
517 for my $p ( keys %plans ) {
518 $html .= "if ( what.plan.value == \"$p\" ) {
519 what.setup.value = $plans{$p}->{setup} ;
520 what.recur.value = $plans{$p}->{recur} ;
525 'layer_callback' => sub {
527 my $html = qq!<INPUT TYPE="hidden" NAME="plan" VALUE="$layer">!.
529 my $href = $plans{$layer}->{'fields'};
530 foreach my $field ( exists($plans{$layer}->{'fieldorder'})
531 ? @{$plans{$layer}->{'fieldorder'}}
535 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
537 if ( ! exists($href->{$field}{'type'}) ) {
538 $html .= qq!<INPUT TYPE="text" NAME="$field" VALUE="!.
539 ( exists($plandata{$field})
541 : $href->{$field}{'default'} ).
542 qq!" onChange="fchanged(this)">!;
543 } elsif ( $href->{$field}{'type'} eq 'select_multiple' ) {
544 $html .= qq!<SELECT MULTIPLE NAME="$field" onChange="fchanged(this)">!;
546 qsearch( $href->{$field}{'select_table'},
547 $href->{$field}{'select_hash'} )
549 my $value = $record->getfield($href->{$field}{'select_key'});
550 $html .= qq!<OPTION VALUE="$value"!.
551 ( $plandata{$field} =~ /(^|, *)$value *(,|$)/
554 '>'. $record->getfield($href->{$field}{'select_label'})
556 $html .= '</SELECT>';
559 $html .= '</TD></TR>';
563 $html .= '<INPUT TYPE="hidden" NAME="plandata" VALUE="'.
564 join(',', keys %{ $href } ). '">'.
567 $html .= '<INPUT TYPE="submit" VALUE="'.
568 ( $hashref->{pkgpart} ? "Apply changes" : "Add package" ).
569 '" onClick="fchanged(this)">';
571 $html .= '<BR><BR>don\'t edit this unless you know what you\'re doing '.
572 '<INPUT TYPE="button" VALUE="refresh expressions" '.
573 'onClick="fchanged(this)">'.
576 '<FONT SIZE="1">Setup expression<BR>'.
577 '<INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="'.
578 encode_entities($hashref->{setup}). '" onLoad="fchanged(this)">'.
580 '<FONT SIZE="1">Recurring espression<BR>'.
581 '<INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="'.
582 encode_entities($hashref->{recur}). '" onLoad="fchanged(this)">'.
595 Price plan <%= $widget->html %>