3 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {
4 $cgi->param('clone', $1);
6 $cgi->param('clone', '');
8 if ( $cgi->param('pkgnum') && $cgi->param('pkgnum') =~ /^(\d+)$/ ) {
9 $cgi->param('pkgnum', $1);
11 $cgi->param('pkgnum', '');
14 my ($query) = $cgi->keywords;
17 if ( $cgi->param('error') ) {
18 $part_pkg = new FS::part_pkg ( {
19 map { $_, scalar($cgi->param($_)) } fields('part_pkg')
22 if ( $cgi->param('clone') ) {
23 $action='Custom Pricing';
25 qsearchs('part_pkg', { 'pkgpart' => $cgi->param('clone') } );
26 $part_pkg ||= $old_part_pkg->clone;
27 $part_pkg->disabled('Y');
28 } elsif ( $query && $query =~ /^(\d+)$/ ) {
29 $part_pkg ||= qsearchs('part_pkg',{'pkgpart'=>$1});
31 $part_pkg ||= new FS::part_pkg {};
32 $part_pkg->plan('flat');
34 unless ( $part_pkg->plan ) { #backwards-compat
35 $part_pkg->plan('flat');
36 $part_pkg->plandata("setup_fee=". $part_pkg->setup. "\n".
37 "recur_fee=". $part_pkg->recur. "\n");
39 $action ||= $part_pkg->pkgpart ? 'Edit' : 'Add';
40 my $hashref = $part_pkg->hashref;
45 function visualize(what) {
46 if (document.getElementById) {
47 document.getElementById('d<%= $part_pkg->plan %>').style.visibility = "visible";
49 document.l<%= $part_pkg->plan %>.visibility = "visible";
56 print header("$action Package Definition", menubar(
57 'Main Menu' => popurl(2),
58 'View all packages' => popurl(2). 'browse/part_pkg.cgi',
59 ), ' onLoad="visualize()"');
61 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
63 if $cgi->param('error');
65 #print '<FORM ACTION="', popurl(1), 'process/part_pkg.cgi" METHOD=POST>';
66 print '<FORM NAME="dummy">';
68 #if ( $cgi->param('clone') ) {
69 # print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
71 #if ( $cgi->param('pkgnum') ) {
72 # print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
75 #print qq!<INPUT TYPE="hidden" NAME="pkgpart" VALUE="$hashref->{pkgpart}">!,
76 print "Package Part #", $hashref->{pkgpart} ? $hashref->{pkgpart} : "(NEW)";
78 print ntable("#cccccc",2), <<END;
79 <TR><TD ALIGN="right">Package (customer-visable)</TD><TD><INPUT TYPE="text" NAME="pkg" SIZE=32 VALUE="$hashref->{pkg}"></TD></TR>
80 <TR><TD ALIGN="right">Comment (customer-hidden)</TD><TD><INPUT TYPE="text" NAME="comment" SIZE=32 VALUE="$hashref->{comment}"></TD></TR>
81 <TR><TD ALIGN="right">Frequency (months) of recurring fee</TD><TD><INPUT TYPE="text" NAME="freq" VALUE="$hashref->{freq}" SIZE=3></TD></TR>
82 <TR><TD ALIGN="right">Setup fee tax exempt</TD><TD>
85 print '<INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y"';
86 print ' CHECKED' if $hashref->{setuptax} eq "Y";
91 <TR><TD ALIGN="right">Recurring fee tax exempt</TD><TD>
94 print '<INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y"';
95 print ' CHECKED' if $hashref->{recurtax} eq "Y";
100 print '<TR><TD ALIGN="right">Disable new orders</TD><TD>';
101 print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
102 print ' CHECKED' if $hashref->{disabled} eq "Y";
104 print '</TD></TR></TABLE>';
106 my $thead = "\n\n". ntable('#cccccc', 2). <<END;
107 <TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH><TH BGCOLOR="#dcdcdc">Service</TH></TR>
110 unless ( $cgi->param('clone') ) {
111 #print <<END, $thead;
112 print <<END, itable(), '<TR><TD VALIGN="top">', $thead;
113 <BR><BR>Enter the quantity of each service this package includes.<BR><BR>
120 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
121 foreach my $part_svc ( @part_svc ) {
122 my $svcpart = $part_svc->svcpart;
123 my $pkg_svc = qsearchs( 'pkg_svc', {
124 'pkgpart' => $cgi->param('clone') || $part_pkg->pkgpart,
125 'svcpart' => $svcpart,
126 } ) || new FS::pkg_svc ( {
127 'pkgpart' => $cgi->param('clone') || $part_pkg->pkgpart,
128 'svcpart' => $svcpart,
131 #? #next unless $pkg_svc;
133 push @fixups, "pkg_svc$svcpart";
135 unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
136 print '<TR>'; # if $count == 0 ;
137 print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
138 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
139 qq!"></TD><TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
140 qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
141 # print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
143 foreach ( 1 .. $columns-1 ) {
144 print "</TABLE></TD><TD VALIGN=\"top\">$thead"
145 if $count == int( $_ * scalar(@part_svc) / $columns );
148 print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
149 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
153 unless ( $cgi->param('clone') ) {
154 print "</TR></TABLE></TD></TR></TABLE>";
155 #print "</TR></TABLE>";
158 # prolly should be in database
160 tie my %plans, 'Tie::IxHash',
162 'name' => 'Flat rate',
164 'setup_fee' => { 'name' => 'Setup fee for this package',
167 'recur_fee' => { 'name' => 'Recurring fee for this package',
171 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
172 'setup' => 'what.setup_fee.value',
173 'recur' => 'what.recur_fee.value',
177 'name' => 'First month pro-rated, then flat-rate',
179 'setup_fee' => { 'name' => 'Setup fee for this package',
182 'recur_fee' => { 'name' => 'Recurring fee for this package',
186 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
187 'setup' => 'what.setup_fee.value',
188 '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) ; \'',
191 'flat_comission_cust' => {
192 'name' => 'Flat rate with recurring comission per active customer',
194 'setup_fee' => { 'name' => 'Setup fee for this package',
197 'recur_fee' => { 'name' => 'Recurring fee for this package',
200 'comission_amount' => { 'name' => 'Comission amount per month (per active customer)',
203 'comission_depth' => { 'name' => 'Number of layers',
207 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
208 'setup' => 'what.setup_fee.value',
209 '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 + \';\'',
212 'flat_comission' => {
213 'name' => 'Flat rate with recurring comission per active package',
215 'setup_fee' => { 'name' => 'Setup fee for this package',
218 'recur_fee' => { 'name' => 'Recurring fee for this package',
221 'comission_amount' => { 'name' => 'Comission amount per month (per active package)',
224 'comission_depth' => { 'name' => 'Number of layers',
228 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
229 'setup' => 'what.setup_fee.value',
230 '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 + \';\'',
233 'flat_comission_pkg' => {
234 'name' => 'Flat rate with recurring comission per active (selected) package',
236 'setup_fee' => { 'name' => 'Setup fee for this package',
239 'recur_fee' => { 'name' => 'Recurring fee for this package',
242 'comission_amount' => { 'name' => 'Comission amount per month (per uncancelled package)',
245 'comission_depth' => { 'name' => 'Number of layers',
248 'comission_pkgpart' => { 'name' => 'Applicable packages<BR><FONT SIZE="-1">(hold <b>ctrl</b> to select multiple packages)</FONT>',
249 'type' => 'select_multiple',
250 'select_table' => 'part_pkg',
251 'select_hash' => { 'disabled' => '' } ,
252 'select_key' => 'pkgpart',
253 'select_label' => 'pkg',
256 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount', 'comission_pkgpart' ],
257 'setup' => 'what.setup_fee.value',
258 '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 + \';\'',
264 'name' => 'Base charge plus charge per-hour from the session monitor',
266 'setup_fee' => { 'name' => 'Setup fee for this package',
269 'recur_flat' => { 'name' => 'Base monthly charge for this package',
272 'recur_included_hours' => { 'name' => 'Hours included',
275 'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
279 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
280 'setup' => 'what.setup_fee.value',
281 'recur' => '\'my $hours = $cust_pkg->seconds_since($cust_bkg->bill || 0) / 3600 - \' + what.recur_included_hours.value + \'; $hours = 0 if $hours < 0; \' + what.recur_flat.value + \' + \' + what.recur_hourly_charge.value + \' * $hours;\'',
285 'name' => 'Base charge plus charge per-minute from the session monitor',
287 'setup_fee' => { 'name' => 'Setup fee for this package',
290 'recur_flat' => { 'name' => 'Base monthly charge for this package',
293 'recur_included_min' => { 'name' => 'Minutes included',
296 'recur_minly_charge' => { 'name' => 'Additional charge per minute',
300 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_min', 'recur_minly_charge' ],
301 'setup' => 'what.setup_fee.value',
302 'recur' => '\'my $min = $cust_pkg->seconds_since($cust_bkg->bill || 0) / 60 - \' + what.recur_included_min.value + \'; $min = 0 if $min < 0; \' + what.recur_flat.value + \' + \' + what.recur_minly_charge.value + \' * $min;\'',
313 function changed(what) {
314 layer = what.options[what.selectedIndex].value;
315 <% foreach my $layer ( keys %plans ) { %>
316 if (layer == "<%= $layer %>" ) {
317 <% foreach my $not ( grep { $_ ne $layer } keys %plans ) { %>
318 if (document.getElementById) {
319 document.getElementById('d<%= $not %>').style.visibility = "hidden";
321 document.l<%= $not %>.visibility = "hidden";
324 if (document.getElementById) {
325 document.getElementById('d<%= $layer %>').style.visibility = "visible";
327 document.l<%= $layer %>.visibility = "visible";
335 Price plan <SELECT NAME="plan" SIZE=1 onChange="changed(this);">
337 <% foreach my $layer (keys %plans ) { %>
338 <OPTION VALUE="<%= $layer %>"<%= ' SELECTED'x($layer eq $part_pkg->plan) %>><%= $plans{$layer}->{'name'} %>
343 function fchanged(what) {
347 function fixup(what) {
348 <% foreach my $f ( qw( pkg comment freq ), @fixups ) { %>
349 what.<%= $f %>.value = document.dummy.<%= $f %>.value;
351 <% foreach my $f ( qw( setuptax recurtax disabled ) ) { %>
352 if (document.dummy.<%= $f %>.checked)
353 what.<%= $f %>.value = 'Y';
355 what.<%= $f %>.value = '';
357 what.plan.value = document.dummy.plan.options[document.dummy.plan.selectedIndex].value;
358 <% foreach my $p ( keys %plans ) { %>
359 if ( what.plan.value == "<%= $p %>" ) {
360 what.setup.value = <%= $plans{$p}->{setup} %>;
361 what.recur.value = <%= $plans{$p}->{recur} %>;
367 <% my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
368 split("\n", $part_pkg->plandata );
369 #foreach my $layer ( 'konq_kludge', keys %plans ) {
370 foreach my $layer ( 'konq_kludge', keys %plans ) {
371 my $visibility = "hidden";
374 if (document.getElementById) {
375 document.write("<DIV ID=\"d<%= $layer %>\" STYLE=\"visibility: <%= $visibility %>; position: absolute\">");
377 <% $visibility="show" if $visibility eq "visible"; %>
378 document.write("<LAYER ID=\"l<%= $layer %>\" VISIBILITY=\"<%= $visibility %>\">");
382 <FORM NAME="<%= $layer %>" ACTION="process/part_pkg.cgi" METHOD=POST onSubmit="fixup(this)">
383 <INPUT TYPE="hidden" NAME="plan" VALUE="<%= $part_pkg->plan %>">
384 <INPUT TYPE="hidden" NAME="pkg" VALUE="<%= $hashref->{pkg} %>">
385 <INPUT TYPE="hidden" NAME="comment" VALUE="$<%= $hashref->{comment} %>">
386 <INPUT TYPE="hidden" NAME="freq" VALUE="<%= $hashref->{freq} %>">
387 <INPUT TYPE="hidden" NAME="setuptax" VALUE="<%= $hashref->{setuptax} %>">
388 <INPUT TYPE="hidden" NAME="recurtax" VALUE="<%= $hashref->{recurtax} %>">
389 <INPUT TYPE="hidden" NAME="disabled" VALUE="<%= $hashref->{disabled} %>">
390 <% foreach my $f ( @fixups ) { %>
391 <INPUT TYPE="hidden" NAME="<%= $f %>" VALUE="">
395 if ( $cgi->param('clone') ) {
396 print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
398 if ( $cgi->param('pkgnum') ) {
399 print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
403 <INPUT TYPE="hidden" NAME="pkgpart" VALUE="<%= $hashref->{pkgpart} %>">
404 <%= ntable("#cccccc",2) %>
406 <% my $href = $plans{$layer}->{'fields'};
407 foreach my $field ( exists($plans{$layer}->{'fieldorder'})
408 ? @{$plans{$layer}->{'fieldorder'}}
412 <TR><TD ALIGN="right"><%= $href->{$field}{'name'} %></TD>
414 <% if ( ! exists($href->{$field}{'type'}) ) { %>
415 <INPUT TYPE="text" NAME="<%= $field %>" VALUE="<%= exists($plandata{$field}) ? $plandata{$field} : $href->{$field}{'default'} %>" onChange="fchanged(this)">
416 <% } elsif ( $href->{$field}{'type'} eq 'select_multiple' ) { %>
417 <SELECT MULTIPLE NAME="<%= $field %>" onChange="fchanged(this)">
418 <% foreach my $record ( qsearch( $href->{$field}{'select_table'}, $href->{$field}{'select_hash'} ) ) {
419 my $value = $record->getfield($href->{$field}{'select_key'}); %>
420 <OPTION VALUE="<%= $value %>"<%= $plandata{$field} =~ /(^|, *)$value *(,|$)/ ? ' SELECTED' : '' %>><%= $record->getfield($href->{$field}{'select_label'}) %>
428 <INPUT TYPE="hidden" NAME="plandata" VALUE="<%= join(',', keys %{ $href } ) %>">
432 print qq!<INPUT TYPE="submit" VALUE="!,
433 $hashref->{pkgpart} ? "Apply changes" : "Add package",
434 qq!" onClick="fchanged(this)">!;
437 <BR><BR>don't edit this unless you know what you're doing <INPUT TYPE="button" VALUE="refresh expressions" onClick="fchanged(this)"><%= ntable("#cccccc",2) %><TR><TD>
438 <FONT SIZE="1">Setup expression<BR><INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="<%= $hashref->{setup} %>" onLoad="fchanged(this)"></FONT><BR>
439 <FONT SIZE="1">Recurring espression<BR><INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="<%= $hashref->{recur} %>" onLoad="fchanged(this)"></FONT>
446 if (document.getElementById) {
447 document.write("</DIV>");
449 document.write("</LAYER>");
456 if (document.getElementById) {
457 document.getElementById('d<%= $part_pkg->plan %>').style.visibility = 'visible';
459 document.l<%= $part_pkg->plan %>.visibility = 'visible';