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-visable)</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></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 if ( $conf->exists('enable_taxclasses') ) {
93 print '<TR><TD ALIGN="right">Tax class</TD><TD><SELECT NAME="taxclass">';
94 my $sth = dbh->prepare('SELECT DISTINCT taxclass FROM cust_main_county')
96 $sth->execute or die $sth->errstr;
97 foreach my $taxclass ( map $_->[0], @{$sth->fetchall_arrayref} ) {
98 print qq!<OPTION VALUE="$taxclass"!;
99 print ' SELECTED' if $taxclass eq $hashref->{taxclass};
100 print qq!>$taxclass</OPTION>!;
102 print '</SELECT></TD></TR>';
105 '<INPUT TYPE="hidden" NAME="taxclass" VALUE="'. $hashref->{taxclass}. '">';
108 print '<TR><TD ALIGN="right">Disable new orders</TD><TD>';
109 print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
110 print ' CHECKED' if $hashref->{disabled} eq "Y";
112 print '</TD></TR></TABLE>';
114 my $thead = "\n\n". ntable('#cccccc', 2). <<END;
115 <TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH><TH BGCOLOR="#dcdcdc">Service</TH></TR>
118 #unless ( $cgi->param('clone') ) {
121 #print <<END, $thead;
122 print <<END, itable(), '<TR><TD VALIGN="top">', $thead;
123 <BR><BR>Enter the quantity of each service this package includes.<BR><BR>
130 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
131 foreach my $part_svc ( @part_svc ) {
132 my $svcpart = $part_svc->svcpart;
133 my $pkg_svc = qsearchs( 'pkg_svc', {
134 'pkgpart' => $cgi->param('clone') || $part_pkg->pkgpart,
135 'svcpart' => $svcpart,
136 } ) || new FS::pkg_svc ( {
137 'pkgpart' => $cgi->param('clone') || $part_pkg->pkgpart,
138 'svcpart' => $svcpart,
141 #? #next unless $pkg_svc;
143 push @fixups, "pkg_svc$svcpart";
145 #unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
148 print '<TR>'; # if $count == 0 ;
149 print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
150 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
151 qq!"></TD><TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
152 qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
153 # print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
155 foreach ( 1 .. $columns-1 ) {
156 print "</TABLE></TD><TD VALIGN=\"top\">$thead"
157 if $count == int( $_ * scalar(@part_svc) / $columns );
160 print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
161 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
165 #unless ( $cgi->param('clone') ) {
168 print "</TR></TABLE></TD></TR></TABLE>";
169 #print "</TR></TABLE>";
172 foreach my $f ( qw( clone pkgnum ) ) {
173 print qq!<INPUT TYPE="hidden" NAME="$f" VALUE="!. $cgi->param($f). '">';
175 print '<INPUT TYPE="hidden" NAME="pkgpart" VALUE="'. $part_pkg->pkgpart. '">';
177 # prolly should be in database
178 tie my %plans, 'Tie::IxHash',
180 'name' => 'Flat rate (anniversary billing)',
182 'setup_fee' => { 'name' => 'Setup fee for this package',
185 'recur_fee' => { 'name' => 'Recurring fee for this package',
189 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
190 'setup' => 'what.setup_fee.value',
191 'recur' => 'what.recur_fee.value',
195 'name' => 'Free for X days, then flat rate (anniversary billing)',
197 'free_days' => { 'name' => 'Initial free days',
200 'setup_fee' => { 'name' => 'Setup fee for this package',
203 'recur_fee' => { 'name' => 'Recurring fee for this package',
207 'fieldorder' => [ 'free_days', 'setup_fee', 'recur_fee' ],
208 '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',
209 'recur' => 'what.recur_fee.value',
213 'name' => 'First partial month pro-rated, then flat-rate (1st of month billing)',
215 'setup_fee' => { 'name' => 'Setup fee for this package',
218 'recur_fee' => { 'name' => 'Recurring fee for this package',
222 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
223 'setup' => 'what.setup_fee.value',
224 '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) ; \'',
228 'name' => 'First partial month full charge, then flat-rate (1st of month billing)',
230 'setup_fee' => { 'name' => 'Setup fee for this package',
233 'recur_fee' => { 'name' => 'Recurring fee for this package',
237 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
238 'setup' => 'what.setup_fee.value',
239 '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',
242 'flat_comission_cust' => {
243 'name' => 'Flat rate with recurring comission per active customer',
245 'setup_fee' => { 'name' => 'Setup fee for this package',
248 'recur_fee' => { 'name' => 'Recurring fee for this package',
251 'comission_amount' => { 'name' => 'Comission amount per month (per active customer)',
254 'comission_depth' => { 'name' => 'Number of layers',
258 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
259 'setup' => 'what.setup_fee.value',
260 '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 + \';\'',
263 'flat_comission' => {
264 'name' => 'Flat rate with recurring comission per (any) active package',
266 'setup_fee' => { 'name' => 'Setup fee for this package',
269 'recur_fee' => { 'name' => 'Recurring fee for this package',
272 'comission_amount' => { 'name' => 'Comission amount per month (per active package)',
275 'comission_depth' => { 'name' => 'Number of layers',
279 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
280 'setup' => 'what.setup_fee.value',
281 '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 + \';\'',
284 'flat_comission_pkg' => {
285 'name' => 'Flat rate with recurring comission per (selected) active package',
287 'setup_fee' => { 'name' => 'Setup fee for this package',
290 'recur_fee' => { 'name' => 'Recurring fee for this package',
293 'comission_amount' => { 'name' => 'Comission amount per month (per uncancelled package)',
296 'comission_depth' => { 'name' => 'Number of layers',
299 'comission_pkgpart' => { 'name' => 'Applicable packages<BR><FONT SIZE="-1">(hold <b>ctrl</b> to select multiple packages)</FONT>',
300 'type' => 'select_multiple',
301 'select_table' => 'part_pkg',
302 'select_hash' => { 'disabled' => '' } ,
303 'select_key' => 'pkgpart',
304 'select_label' => 'pkg',
307 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount', 'comission_pkgpart' ],
308 'setup' => 'what.setup_fee.value',
309 '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 + \';\'',
315 'name' => 'Base charge plus charge per-hour from the session monitor',
317 'setup_fee' => { 'name' => 'Setup fee for this package',
320 'recur_flat' => { 'name' => 'Base monthly charge for this package',
323 'recur_included_hours' => { 'name' => 'Hours included',
326 'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
330 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
331 'setup' => 'what.setup_fee.value',
332 '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;\'',
336 'name' => 'Base charge plus charge per-minute from the session monitor',
338 'setup_fee' => { 'name' => 'Setup fee for this package',
341 'recur_flat' => { 'name' => 'Base monthly charge for this package',
344 'recur_included_min' => { 'name' => 'Minutes included',
347 'recur_minly_charge' => { 'name' => 'Additional charge per minute',
351 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_min', 'recur_minly_charge' ],
352 'setup' => 'what.setup_fee.value',
353 '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;\'',
359 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
360 split("\n", $part_pkg->plandata );
362 tie my %options, 'Tie::IxHash', map { $_=>$plans{$_}->{'name'} } keys %plans;
364 my @form_select = ();
365 if ( $conf->exists('enable_taxclasses') ) {
366 push @form_select, 'taxclass';
368 push @fixups, 'taxclass'; #hidden
372 my $widget = new HTML::Widgets::SelectLayers(
373 'selected_layer' => $part_pkg->plan,
374 'options' => \%options,
375 'form_name' => 'dummy',
376 'form_action' => 'process/part_pkg.cgi',
377 'form_text' => [ qw(pkg comment freq clone pkgnum pkgpart), @fixups ],
378 'form_checkbox' => [ qw(setuptax recurtax disabled) ],
379 'form_select' => [ @form_select ],
380 'fixup_callback' => sub {
383 for my $p ( keys %plans ) {
384 $html .= "if ( what.plan.value == \"$p\" ) {
385 what.setup.value = $plans{$p}->{setup} ;
386 what.recur.value = $plans{$p}->{recur} ;
391 'layer_callback' => sub {
393 my $html = qq!<INPUT TYPE="hidden" NAME="plan" VALUE="$layer">!.
395 my $href = $plans{$layer}->{'fields'};
396 foreach my $field ( exists($plans{$layer}->{'fieldorder'})
397 ? @{$plans{$layer}->{'fieldorder'}}
401 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
403 if ( ! exists($href->{$field}{'type'}) ) {
404 $html .= qq!<INPUT TYPE="text" NAME="$field" VALUE="!.
405 ( exists($plandata{$field})
407 : $href->{$field}{'default'} ).
408 qq!" onChange="fchanged(this)">!;
409 } elsif ( $href->{$field}{'type'} eq 'select_multiple' ) {
410 $html .= qq!<SELECT MULTIPLE NAME="$field" onChange="fchanged(this)">!;
412 qsearch( $href->{$field}{'select_table'},
413 $href->{$field}{'select_hash'} )
415 my $value = $record->getfield($href->{$field}{'select_key'});
416 $html .= qq!<OPTION VALUE="$value"!.
417 ( $plandata{$field} =~ /(^|, *)$value *(,|$)/
420 '>'. $record->getfield($href->{$field}{'select_label'})
422 $html .= '</SELECT>';
425 $html .= '</TD></TR>';
429 $html .= '<INPUT TYPE="hidden" NAME="plandata" VALUE="'.
430 join(',', keys %{ $href } ). '">'.
433 $html .= '<INPUT TYPE="submit" VALUE="'.
434 ( $hashref->{pkgpart} ? "Apply changes" : "Add package" ).
435 '" onClick="fchanged(this)">';
437 $html .= '<BR><BR>don\'t edit this unless you know what you\'re doing '.
438 '<INPUT TYPE="button" VALUE="refresh expressions" '.
439 'onClick="fchanged(this)">'.
442 '<FONT SIZE="1">Setup expression<BR>'.
443 '<INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="'.
444 $hashref->{setup}. '" onLoad="fchanged(this)">'.
446 '<FONT SIZE="1">Recurring espression<BR>'.
447 '<INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="'.
448 $hashref->{recur}. '" onLoad="fchanged(this)">'.
461 Price plan <%= $widget->html %>