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 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> <I>0=no recurring fee, 1=monthly, 3=quarterly, 12=yearly</TD></TR>
73 <TR><TD ALIGN="right">Setup fee tax exempt</TD><TD>
76 print '<INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y"';
77 print ' CHECKED' if $hashref->{setuptax} eq "Y";
82 <TR><TD ALIGN="right">Recurring fee tax exempt</TD><TD>
85 print '<INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y"';
86 print ' CHECKED' if $hashref->{recurtax} eq "Y";
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')
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>!;
103 print '</SELECT></TD></TR>';
106 '<INPUT TYPE="hidden" NAME="taxclass" VALUE="'. $hashref->{taxclass}. '">';
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";
113 print '</TD></TR></TABLE>';
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>
119 #unless ( $cgi->param('clone') ) {
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>
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,
142 #? #next unless $pkg_svc;
144 push @fixups, "pkg_svc$svcpart";
146 #unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
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);
156 foreach ( 1 .. $columns-1 ) {
157 print "</TABLE></TD><TD VALIGN=\"top\">$thead"
158 if $count == int( $_ * scalar(@part_svc) / $columns );
161 print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
162 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
166 #unless ( $cgi->param('clone') ) {
169 print "</TR></TABLE></TD></TR></TABLE>";
170 #print "</TR></TABLE>";
173 foreach my $f ( qw( clone pkgnum ) ) {
174 print qq!<INPUT TYPE="hidden" NAME="$f" VALUE="!. $cgi->param($f). '">';
176 print '<INPUT TYPE="hidden" NAME="pkgpart" VALUE="'. $part_pkg->pkgpart. '">';
178 # prolly should be in database
179 tie my %plans, 'Tie::IxHash',
181 'name' => 'Flat rate (anniversary billing)',
183 'setup_fee' => { 'name' => 'Setup fee for this package',
186 'recur_fee' => { 'name' => 'Recurring fee for this package',
190 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
191 'setup' => 'what.setup_fee.value',
192 'recur' => 'what.recur_fee.value',
196 'name' => 'Free for X days, then flat rate (anniversary billing)',
198 'free_days' => { 'name' => 'Initial free days',
201 'setup_fee' => { 'name' => 'Setup fee for this package',
204 'recur_fee' => { 'name' => 'Recurring fee for this package',
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',
214 'name' => 'First partial month pro-rated, then flat-rate (1st of month billing)',
216 'setup_fee' => { 'name' => 'Setup fee for this package',
219 'recur_fee' => { 'name' => 'Recurring fee for this package',
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) ; \'',
229 'name' => 'First partial month full charge, then flat-rate (1st of month billing)',
231 'setup_fee' => { 'name' => 'Setup fee for this package',
234 'recur_fee' => { 'name' => 'Recurring fee for this package',
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',
243 'flat_comission_cust' => {
244 'name' => 'Flat rate with recurring commission per active customer',
246 'setup_fee' => { 'name' => 'Setup fee for this package',
249 'recur_fee' => { 'name' => 'Recurring fee for this package',
252 'comission_amount' => { 'name' => 'Commission amount per month (per active customer)',
255 'comission_depth' => { 'name' => 'Number of layers',
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 + \';\'',
264 'flat_comission' => {
265 'name' => 'Flat rate with recurring commission per (any) active package',
267 'setup_fee' => { 'name' => 'Setup fee for this package',
270 'recur_fee' => { 'name' => 'Recurring fee for this package',
273 'comission_amount' => { 'name' => 'Commission amount per month (per active package)',
276 'comission_depth' => { 'name' => 'Number of layers',
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 + \';\'',
285 'flat_comission_pkg' => {
286 'name' => 'Flat rate with recurring commission per (selected) active package',
288 'setup_fee' => { 'name' => 'Setup fee for this package',
291 'recur_fee' => { 'name' => 'Recurring fee for this package',
294 'comission_amount' => { 'name' => 'Commission amount per month (per uncancelled package)',
297 'comission_depth' => { 'name' => 'Number of layers',
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',
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 + \';\'',
316 'name' => 'Base charge plus charge per-hour from the session monitor',
318 'setup_fee' => { 'name' => 'Setup fee for this package',
321 'recur_flat' => { 'name' => 'Base monthly charge for this package',
324 'recur_included_hours' => { 'name' => 'Hours included',
327 'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
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;\'',
337 'name' => 'Base charge plus charge per-minute from the session monitor',
339 'setup_fee' => { 'name' => 'Setup fee for this package',
342 'recur_flat' => { 'name' => 'Base monthly charge for this package',
345 'recur_included_min' => { 'name' => 'Minutes included',
348 'recur_minly_charge' => { 'name' => 'Additional charge per minute',
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;\'',
360 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
361 split("\n", $part_pkg->plandata );
363 tie my %options, 'Tie::IxHash', map { $_=>$plans{$_}->{'name'} } keys %plans;
365 my @form_select = ();
366 if ( $conf->exists('enable_taxclasses') ) {
367 push @form_select, 'taxclass';
369 push @fixups, 'taxclass'; #hidden
373 my $widget = new HTML::Widgets::SelectLayers(
374 'selected_layer' => $part_pkg->plan,
375 'options' => \%options,
376 'form_name' => 'dummy',
377 'form_action' => 'process/part_pkg.cgi',
378 'form_text' => [ qw(pkg comment freq clone pkgnum pkgpart), @fixups ],
379 'form_checkbox' => [ qw(setuptax recurtax disabled) ],
380 'form_select' => [ @form_select ],
381 'fixup_callback' => sub {
384 for my $p ( keys %plans ) {
385 $html .= "if ( what.plan.value == \"$p\" ) {
386 what.setup.value = $plans{$p}->{setup} ;
387 what.recur.value = $plans{$p}->{recur} ;
392 'layer_callback' => sub {
394 my $html = qq!<INPUT TYPE="hidden" NAME="plan" VALUE="$layer">!.
396 my $href = $plans{$layer}->{'fields'};
397 foreach my $field ( exists($plans{$layer}->{'fieldorder'})
398 ? @{$plans{$layer}->{'fieldorder'}}
402 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
404 if ( ! exists($href->{$field}{'type'}) ) {
405 $html .= qq!<INPUT TYPE="text" NAME="$field" VALUE="!.
406 ( exists($plandata{$field})
408 : $href->{$field}{'default'} ).
409 qq!" onChange="fchanged(this)">!;
410 } elsif ( $href->{$field}{'type'} eq 'select_multiple' ) {
411 $html .= qq!<SELECT MULTIPLE NAME="$field" onChange="fchanged(this)">!;
413 qsearch( $href->{$field}{'select_table'},
414 $href->{$field}{'select_hash'} )
416 my $value = $record->getfield($href->{$field}{'select_key'});
417 $html .= qq!<OPTION VALUE="$value"!.
418 ( $plandata{$field} =~ /(^|, *)$value *(,|$)/
421 '>'. $record->getfield($href->{$field}{'select_label'})
423 $html .= '</SELECT>';
426 $html .= '</TD></TR>';
430 $html .= '<INPUT TYPE="hidden" NAME="plandata" VALUE="'.
431 join(',', keys %{ $href } ). '">'.
434 $html .= '<INPUT TYPE="submit" VALUE="'.
435 ( $hashref->{pkgpart} ? "Apply changes" : "Add package" ).
436 '" onClick="fchanged(this)">';
438 $html .= '<BR><BR>don\'t edit this unless you know what you\'re doing '.
439 '<INPUT TYPE="button" VALUE="refresh expressions" '.
440 'onClick="fchanged(this)">'.
443 '<FONT SIZE="1">Setup expression<BR>'.
444 '<INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="'.
445 $hashref->{setup}. '" onLoad="fchanged(this)">'.
447 '<FONT SIZE="1">Recurring espression<BR>'.
448 '<INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="'.
449 $hashref->{recur}. '" onLoad="fchanged(this)">'.
462 Price plan <%= $widget->html %>