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). <<END;
156 <TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH><TH BGCOLOR="#dcdcdc">Service</TH></TR>
159 #unless ( $cgi->param('clone') ) {
162 #print <<END, $thead;
163 print <<END, itable(), '<TR><TD VALIGN="top">', $thead;
164 <BR><BR>Enter the quantity of each service this package includes.<BR><BR>
171 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
172 foreach my $part_svc ( @part_svc ) {
173 my $svcpart = $part_svc->svcpart;
174 my $pkgpart = $cgi->param('clone') || $part_pkg->pkgpart;
175 my $pkg_svc = $pkgpart && qsearchs( 'pkg_svc', {
176 'pkgpart' => $pkgpart,
177 'svcpart' => $svcpart,
178 } ) || new FS::pkg_svc ( {
179 'pkgpart' => $pkgpart,
180 'svcpart' => $svcpart,
183 #? #next unless $pkg_svc;
185 push @fixups, "pkg_svc$svcpart";
187 #unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
190 print '<TR>'; # if $count == 0 ;
191 print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
192 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
193 qq!"></TD><TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
194 qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
195 # print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
197 foreach ( 1 .. $columns-1 ) {
198 print "</TABLE></TD><TD VALIGN=\"top\">$thead"
199 if $count == int( $_ * scalar(@part_svc) / $columns );
202 print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
203 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
207 #unless ( $cgi->param('clone') ) {
210 print "</TR></TABLE></TD></TR></TABLE>";
211 #print "</TR></TABLE>";
214 foreach my $f ( qw( clone pkgnum ) ) {
215 print qq!<INPUT TYPE="hidden" NAME="$f" VALUE="!. $cgi->param($f). '">';
217 print '<INPUT TYPE="hidden" NAME="pkgpart" VALUE="'. $part_pkg->pkgpart. '">';
219 # prolly should be in database
220 tie my %plans, 'Tie::IxHash',
222 'name' => 'Flat rate (anniversary billing)',
224 'setup_fee' => { 'name' => 'Setup fee for this package',
227 'recur_fee' => { 'name' => 'Recurring fee for this package',
231 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
232 'setup' => 'what.setup_fee.value',
233 'recur' => 'what.recur_fee.value',
237 'name' => 'Free for X days, then flat rate (anniversary billing)',
239 'free_days' => { 'name' => 'Initial free days',
242 'setup_fee' => { 'name' => 'Setup fee for this package',
245 'recur_fee' => { 'name' => 'Recurring fee for this package',
249 'fieldorder' => [ 'free_days', 'setup_fee', 'recur_fee' ],
250 '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',
251 'recur' => 'what.recur_fee.value',
255 'name' => 'First partial month pro-rated, then flat-rate (1st of month billing)',
257 'setup_fee' => { 'name' => 'Setup fee for this package',
260 'recur_fee' => { 'name' => 'Recurring fee for this package',
264 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
265 'setup' => 'what.setup_fee.value',
266 '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) ; \'',
270 'name' => 'First partial month full charge, then flat-rate (1st of month billing)',
272 'setup_fee' => { 'name' => 'Setup fee for this package',
275 'recur_fee' => { 'name' => 'Recurring fee for this package',
279 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
280 'setup' => 'what.setup_fee.value',
281 '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',
284 'flat_comission_cust' => {
285 'name' => 'Flat rate with recurring commission per active customer',
287 'setup_fee' => { 'name' => 'Setup fee for this package',
290 'recur_fee' => { 'name' => 'Recurring fee for this package',
293 'comission_amount' => { 'name' => 'Commission amount per month (per active customer)',
296 'comission_depth' => { 'name' => 'Number of layers',
300 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
301 'setup' => 'what.setup_fee.value',
302 '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 + \';\'',
305 'flat_comission' => {
306 'name' => 'Flat rate with recurring commission per (any) active package',
308 'setup_fee' => { 'name' => 'Setup fee for this package',
311 'recur_fee' => { 'name' => 'Recurring fee for this package',
314 'comission_amount' => { 'name' => 'Commission amount per month (per active package)',
317 'comission_depth' => { 'name' => 'Number of layers',
321 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
322 'setup' => 'what.setup_fee.value',
323 '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 + \';\'',
326 'flat_comission_pkg' => {
327 'name' => 'Flat rate with recurring commission per (selected) active package',
329 'setup_fee' => { 'name' => 'Setup fee for this package',
332 'recur_fee' => { 'name' => 'Recurring fee for this package',
335 'comission_amount' => { 'name' => 'Commission amount per month (per uncancelled package)',
338 'comission_depth' => { 'name' => 'Number of layers',
341 'comission_pkgpart' => { 'name' => 'Applicable packages<BR><FONT SIZE="-1">(hold <b>ctrl</b> to select multiple packages)</FONT>',
342 'type' => 'select_multiple',
343 'select_table' => 'part_pkg',
344 'select_hash' => { 'disabled' => '' } ,
345 'select_key' => 'pkgpart',
346 'select_label' => 'pkg',
349 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount', 'comission_pkgpart' ],
350 'setup' => 'what.setup_fee.value',
351 '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 + \';\'',
357 'name' => 'Base charge plus charge per-hour from the session monitor',
359 'setup_fee' => { 'name' => 'Setup fee for this package',
362 'recur_flat' => { 'name' => 'Base monthly charge for this package',
365 'recur_included_hours' => { 'name' => 'Hours included',
368 'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
372 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
373 'setup' => 'what.setup_fee.value',
374 '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;\'',
378 'name' => 'Base charge plus charge per-minute from the session monitor',
380 'setup_fee' => { 'name' => 'Setup fee for this package',
383 'recur_flat' => { 'name' => 'Base monthly charge for this package',
386 'recur_included_min' => { 'name' => 'Minutes included',
389 'recur_minly_charge' => { 'name' => 'Additional charge per minute',
393 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_min', 'recur_minly_charge' ],
394 'setup' => 'what.setup_fee.value',
395 '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;\'',
399 'sqlradacct_hour' => {
400 'name' => 'Base charge plus charge per-hour (and for data) from an external sqlradius radacct table',
402 'setup_fee' => { 'name' => 'Setup fee for this package',
405 'recur_flat' => { 'name' => 'Base monthly charge for this package',
408 'recur_included_hours' => { 'name' => 'Hours included',
411 'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
414 'recur_included_input' => { 'name' => 'Input megabytes included',
417 'recur_input_charge' => { 'name' =>
418 'Additional charge per input megabyte',
421 'recur_included_output' => { 'name' => 'Output megabytes included',
424 'recur_output_charge' => { 'name' =>
425 'Additional charge per output megabyte',
428 'recur_included_total' => { 'name' =>
429 'Total input+output megabytes included',
432 'recur_total_charge' => { 'name' =>
433 'Additional charge per input+output megabyte',
437 '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 )],
438 'setup' => 'what.setup_fee.value',
439 '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 ;\'',
443 'name' => 'Base charge plus a metered rate from a configurable SQL query',
445 'setup_fee' => { 'name' => 'Setup fee for this package',
448 'recur_flat' => { 'name' => 'Base monthly charge for this package',
451 'recur_included' => { 'name' => 'Units included',
454 'recur_unit_charge' => { 'name' => 'Additional charge per unit',
457 'datasrc' => { 'name' => 'DBI data source',
460 'db_username' => { 'name' => 'Database username',
463 'db_password' => { 'name' => 'Database username',
466 'query' => { 'name' => 'SQL query',
470 'fieldorder' => [qw( setup_fee recur_flat recur_included recur_unit_charge datasrc db_username db_password query )],
471 'setup' => 'what.setup_fee.value',
472 # 'recur' => '\'my $dbh = DBI->connect(\"\' + what.datasrc.value + \'\", \"\' + what.db_username.value + \'\") or die $DBI::errstr; \'',
473 '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 + \';\'',
474 #'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 + \';\'',
479 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
480 split("\n", $part_pkg->plandata );
482 tie my %options, 'Tie::IxHash', map { $_=>$plans{$_}->{'name'} } keys %plans;
484 my @form_select = ();
485 if ( $conf->exists('enable_taxclasses') ) {
486 push @form_select, 'taxclass';
488 push @fixups, 'taxclass'; #hidden
492 my $widget = new HTML::Widgets::SelectLayers(
493 'selected_layer' => $part_pkg->plan,
494 'options' => \%options,
495 'form_name' => 'dummy',
496 'form_action' => 'process/part_pkg.cgi',
497 'form_text' => [ qw(pkg comment freq clone pkgnum pkgpart), @fixups ],
498 'form_checkbox' => [ qw(setuptax recurtax disabled) ],
499 'form_select' => [ @form_select ],
500 'fixup_callback' => sub {
503 for my $p ( keys %plans ) {
504 $html .= "if ( what.plan.value == \"$p\" ) {
505 what.setup.value = $plans{$p}->{setup} ;
506 what.recur.value = $plans{$p}->{recur} ;
511 'layer_callback' => sub {
513 my $html = qq!<INPUT TYPE="hidden" NAME="plan" VALUE="$layer">!.
515 my $href = $plans{$layer}->{'fields'};
516 foreach my $field ( exists($plans{$layer}->{'fieldorder'})
517 ? @{$plans{$layer}->{'fieldorder'}}
521 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
523 if ( ! exists($href->{$field}{'type'}) ) {
524 $html .= qq!<INPUT TYPE="text" NAME="$field" VALUE="!.
525 ( exists($plandata{$field})
527 : $href->{$field}{'default'} ).
528 qq!" onChange="fchanged(this)">!;
529 } elsif ( $href->{$field}{'type'} eq 'select_multiple' ) {
530 $html .= qq!<SELECT MULTIPLE NAME="$field" onChange="fchanged(this)">!;
532 qsearch( $href->{$field}{'select_table'},
533 $href->{$field}{'select_hash'} )
535 my $value = $record->getfield($href->{$field}{'select_key'});
536 $html .= qq!<OPTION VALUE="$value"!.
537 ( $plandata{$field} =~ /(^|, *)$value *(,|$)/
540 '>'. $record->getfield($href->{$field}{'select_label'})
542 $html .= '</SELECT>';
545 $html .= '</TD></TR>';
549 $html .= '<INPUT TYPE="hidden" NAME="plandata" VALUE="'.
550 join(',', keys %{ $href } ). '">'.
553 $html .= '<INPUT TYPE="submit" VALUE="'.
554 ( $hashref->{pkgpart} ? "Apply changes" : "Add package" ).
555 '" onClick="fchanged(this)">';
557 $html .= '<BR><BR>don\'t edit this unless you know what you\'re doing '.
558 '<INPUT TYPE="button" VALUE="refresh expressions" '.
559 'onClick="fchanged(this)">'.
562 '<FONT SIZE="1">Setup expression<BR>'.
563 '<INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="'.
564 encode_entities($hashref->{setup}). '" onLoad="fchanged(this)">'.
566 '<FONT SIZE="1">Recurring espression<BR>'.
567 '<INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="'.
568 encode_entities($hashref->{recur}). '" onLoad="fchanged(this)">'.
581 Price plan <%= $widget->html %>