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 print '<TR><TD ALIGN="right">Disable new orders</TD><TD>';
92 print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
93 print ' CHECKED' if $hashref->{disabled} eq "Y";
95 print '</TD></TR></TABLE>';
97 my $thead = "\n\n". ntable('#cccccc', 2). <<END;
98 <TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH><TH BGCOLOR="#dcdcdc">Service</TH></TR>
101 #unless ( $cgi->param('clone') ) {
104 #print <<END, $thead;
105 print <<END, itable(), '<TR><TD VALIGN="top">', $thead;
106 <BR><BR>Enter the quantity of each service this package includes.<BR><BR>
113 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
114 foreach my $part_svc ( @part_svc ) {
115 my $svcpart = $part_svc->svcpart;
116 my $pkg_svc = qsearchs( 'pkg_svc', {
117 'pkgpart' => $cgi->param('clone') || $part_pkg->pkgpart,
118 'svcpart' => $svcpart,
119 } ) || new FS::pkg_svc ( {
120 'pkgpart' => $cgi->param('clone') || $part_pkg->pkgpart,
121 'svcpart' => $svcpart,
124 #? #next unless $pkg_svc;
126 push @fixups, "pkg_svc$svcpart";
128 #unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
131 print '<TR>'; # if $count == 0 ;
132 print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
133 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
134 qq!"></TD><TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
135 qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
136 # print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
138 foreach ( 1 .. $columns-1 ) {
139 print "</TABLE></TD><TD VALIGN=\"top\">$thead"
140 if $count == int( $_ * scalar(@part_svc) / $columns );
143 print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
144 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
148 #unless ( $cgi->param('clone') ) {
151 print "</TR></TABLE></TD></TR></TABLE>";
152 #print "</TR></TABLE>";
155 foreach my $f ( qw( clone pkgnum ) ) {
156 print qq!<INPUT TYPE="hidden" NAME="$f" VALUE="!. $cgi->param($f). '">';
158 print '<INPUT TYPE="hidden" NAME="pkgpart" VALUE="'. $part_pkg->pkgpart. '">';
160 # prolly should be in database
161 tie my %plans, 'Tie::IxHash',
163 'name' => 'Flat rate (anniversary billing)',
165 'setup_fee' => { 'name' => 'Setup fee for this package',
168 'recur_fee' => { 'name' => 'Recurring fee for this package',
172 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
173 'setup' => 'what.setup_fee.value',
174 'recur' => 'what.recur_fee.value',
178 'name' => 'Free for X days, then flat rate (anniversary billing)',
180 'free_days' => { 'name' => 'Initial free days',
183 'setup_fee' => { 'name' => 'Setup fee for this package',
186 'recur_fee' => { 'name' => 'Recurring fee for this package',
190 'fieldorder' => [ 'free_days', 'setup_fee', 'recur_fee' ],
191 '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',
192 'recur' => 'what.recur_fee.value',
196 'name' => 'First partial month pro-rated, then flat-rate (1st of month billing)',
198 'setup_fee' => { 'name' => 'Setup fee for this package',
201 'recur_fee' => { 'name' => 'Recurring fee for this package',
205 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
206 'setup' => 'what.setup_fee.value',
207 '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) ; \'',
211 'name' => 'First partial month full charge, then flat-rate (1st of month billing)',
213 'setup_fee' => { 'name' => 'Setup fee for this package',
216 'recur_fee' => { 'name' => 'Recurring fee for this package',
220 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
221 'setup' => 'what.setup_fee.value',
222 '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',
225 'flat_comission_cust' => {
226 'name' => 'Flat rate with recurring comission per active customer',
228 'setup_fee' => { 'name' => 'Setup fee for this package',
231 'recur_fee' => { 'name' => 'Recurring fee for this package',
234 'comission_amount' => { 'name' => 'Comission amount per month (per active customer)',
237 'comission_depth' => { 'name' => 'Number of layers',
241 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
242 'setup' => 'what.setup_fee.value',
243 '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 + \';\'',
246 'flat_comission' => {
247 'name' => 'Flat rate with recurring comission per (any) active package',
249 'setup_fee' => { 'name' => 'Setup fee for this package',
252 'recur_fee' => { 'name' => 'Recurring fee for this package',
255 'comission_amount' => { 'name' => 'Comission amount per month (per active package)',
258 'comission_depth' => { 'name' => 'Number of layers',
262 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
263 'setup' => 'what.setup_fee.value',
264 '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 + \';\'',
267 'flat_comission_pkg' => {
268 'name' => 'Flat rate with recurring comission per (selected) active package',
270 'setup_fee' => { 'name' => 'Setup fee for this package',
273 'recur_fee' => { 'name' => 'Recurring fee for this package',
276 'comission_amount' => { 'name' => 'Comission amount per month (per uncancelled package)',
279 'comission_depth' => { 'name' => 'Number of layers',
282 'comission_pkgpart' => { 'name' => 'Applicable packages<BR><FONT SIZE="-1">(hold <b>ctrl</b> to select multiple packages)</FONT>',
283 'type' => 'select_multiple',
284 'select_table' => 'part_pkg',
285 'select_hash' => { 'disabled' => '' } ,
286 'select_key' => 'pkgpart',
287 'select_label' => 'pkg',
290 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount', 'comission_pkgpart' ],
291 'setup' => 'what.setup_fee.value',
292 '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 + \';\'',
298 'name' => 'Base charge plus charge per-hour from the session monitor',
300 'setup_fee' => { 'name' => 'Setup fee for this package',
303 'recur_flat' => { 'name' => 'Base monthly charge for this package',
306 'recur_included_hours' => { 'name' => 'Hours included',
309 'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
313 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
314 'setup' => 'what.setup_fee.value',
315 '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;\'',
319 'name' => 'Base charge plus charge per-minute from the session monitor',
321 'setup_fee' => { 'name' => 'Setup fee for this package',
324 'recur_flat' => { 'name' => 'Base monthly charge for this package',
327 'recur_included_min' => { 'name' => 'Minutes included',
330 'recur_minly_charge' => { 'name' => 'Additional charge per minute',
334 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_min', 'recur_minly_charge' ],
335 'setup' => 'what.setup_fee.value',
336 '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;\'',
342 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
343 split("\n", $part_pkg->plandata );
345 tie my %options, 'Tie::IxHash', map { $_=>$plans{$_}->{'name'} } keys %plans;
347 my $widget = new HTML::Widgets::SelectLayers(
348 'selected_layer' => $part_pkg->plan,
349 'options' => \%options,
350 'form_name' => 'dummy',
351 'form_action' => 'process/part_pkg.cgi',
352 'form_text' => [ qw(pkg comment freq clone pkgnum pkgpart), @fixups ],
353 'form_checkbox' => [ qw(setuptax recurtax disabled) ],
354 'fixup_callback' => sub {
357 for my $p ( keys %plans ) {
358 $html .= "if ( what.plan.value == \"$p\" ) {
359 what.setup.value = $plans{$p}->{setup} ;
360 what.recur.value = $plans{$p}->{recur} ;
365 'layer_callback' => sub {
367 my $html = qq!<INPUT TYPE="hidden" NAME="plan" VALUE="$layer">!.
369 my $href = $plans{$layer}->{'fields'};
370 foreach my $field ( exists($plans{$layer}->{'fieldorder'})
371 ? @{$plans{$layer}->{'fieldorder'}}
375 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
377 if ( ! exists($href->{$field}{'type'}) ) {
378 $html .= qq!<INPUT TYPE="text" NAME="$field" VALUE="!.
379 ( exists($plandata{$field})
381 : $href->{$field}{'default'} ).
382 qq!" onChange="fchanged(this)">!;
383 } elsif ( $href->{$field}{'type'} eq 'select_multiple' ) {
384 $html .= qq!<SELECT MULTIPLE NAME="$field" onChange="fchanged(this)">!;
386 qsearch( $href->{$field}{'select_table'},
387 $href->{$field}{'select_hash'} )
389 my $value = $record->getfield($href->{$field}{'select_key'});
390 $html .= qq!<OPTION VALUE="$value"!.
391 ( $plandata{$field} =~ /(^|, *)$value *(,|$)/
394 '>'. $record->getfield($href->{$field}{'select_label'})
396 $html .= '</SELECT>';
399 $html .= '</TD></TR>';
403 $html .= '<INPUT TYPE="hidden" NAME="plandata" VALUE="'.
404 join(',', keys %{ $href } ). '">'.
407 $html .= '<INPUT TYPE="submit" VALUE="'.
408 ( $hashref->{pkgpart} ? "Apply changes" : "Add package" ).
409 '" onClick="fchanged(this)">';
411 $html .= '<BR><BR>don\'t edit this unless you know what you\'re doing '.
412 '<INPUT TYPE="button" VALUE="refresh expressions" '.
413 'onClick="fchanged(this)">'.
416 '<FONT SIZE="1">Setup expression<BR>'.
417 '<INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="'.
418 $hashref->{setup}. '" onLoad="fchanged(this)">'.
420 '<FONT SIZE="1">Recurring espression<BR>'.
421 '<INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="'.
422 $hashref->{recur}. '" onLoad="fchanged(this)">'.
435 Price plan <%= $widget->html %>