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;
48 function visualize(what) {
49 if (document.getElementById) {
50 document.getElementById('d<%= $part_pkg->plan %>').style.visibility = "visible";
52 document.l<%= $part_pkg->plan %>.visibility = "visible";
59 print header("$action Package Definition", menubar(
60 'Main Menu' => popurl(2),
61 'View all packages' => popurl(2). 'browse/part_pkg.cgi',
62 ), ' onLoad="visualize()"');
64 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
66 if $cgi->param('error');
68 #print '<FORM ACTION="', popurl(1), 'process/part_pkg.cgi" METHOD=POST>';
69 print '<FORM NAME="dummy">';
71 #if ( $cgi->param('clone') ) {
72 # print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
74 #if ( $cgi->param('pkgnum') ) {
75 # print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
78 #print qq!<INPUT TYPE="hidden" NAME="pkgpart" VALUE="$hashref->{pkgpart}">!,
79 print "Package Part #", $hashref->{pkgpart} ? $hashref->{pkgpart} : "(NEW)";
81 print ntable("#cccccc",2), <<END;
82 <TR><TD ALIGN="right">Package (customer-visable)</TD><TD><INPUT TYPE="text" NAME="pkg" SIZE=32 VALUE="$hashref->{pkg}"></TD></TR>
83 <TR><TD ALIGN="right">Comment (customer-hidden)</TD><TD><INPUT TYPE="text" NAME="comment" SIZE=32 VALUE="$hashref->{comment}"></TD></TR>
84 <TR><TD ALIGN="right">Frequency (months) of recurring fee</TD><TD><INPUT TYPE="text" NAME="freq" VALUE="$hashref->{freq}" SIZE=3></TD></TR>
85 <TR><TD ALIGN="right">Setup fee tax exempt</TD><TD>
88 print '<INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y"';
89 print ' CHECKED' if $hashref->{setuptax} eq "Y";
94 <TR><TD ALIGN="right">Recurring fee tax exempt</TD><TD>
97 print '<INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y"';
98 print ' CHECKED' if $hashref->{recurtax} eq "Y";
103 print '<TR><TD ALIGN="right">Disable new orders</TD><TD>';
104 print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
105 print ' CHECKED' if $hashref->{disabled} eq "Y";
107 print '</TD></TR></TABLE>';
109 my $thead = "\n\n". ntable('#cccccc', 2). <<END;
110 <TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH><TH BGCOLOR="#dcdcdc">Service</TH></TR>
113 #unless ( $cgi->param('clone') ) {
116 #print <<END, $thead;
117 print <<END, itable(), '<TR><TD VALIGN="top">', $thead;
118 <BR><BR>Enter the quantity of each service this package includes.<BR><BR>
125 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
126 foreach my $part_svc ( @part_svc ) {
127 my $svcpart = $part_svc->svcpart;
128 my $pkg_svc = qsearchs( 'pkg_svc', {
129 'pkgpart' => $cgi->param('clone') || $part_pkg->pkgpart,
130 'svcpart' => $svcpart,
131 } ) || new FS::pkg_svc ( {
132 'pkgpart' => $cgi->param('clone') || $part_pkg->pkgpart,
133 'svcpart' => $svcpart,
136 #? #next unless $pkg_svc;
138 push @fixups, "pkg_svc$svcpart";
140 #unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
143 print '<TR>'; # if $count == 0 ;
144 print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
145 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
146 qq!"></TD><TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
147 qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
148 # print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
150 foreach ( 1 .. $columns-1 ) {
151 print "</TABLE></TD><TD VALIGN=\"top\">$thead"
152 if $count == int( $_ * scalar(@part_svc) / $columns );
155 print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
156 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
160 #unless ( $cgi->param('clone') ) {
163 print "</TR></TABLE></TD></TR></TABLE>";
164 #print "</TR></TABLE>";
167 # prolly should be in database
169 tie my %plans, 'Tie::IxHash',
171 'name' => 'Flat rate',
173 'setup_fee' => { 'name' => 'Setup fee for this package',
176 'recur_fee' => { 'name' => 'Recurring fee for this package',
180 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
181 'setup' => 'what.setup_fee.value',
182 'recur' => 'what.recur_fee.value',
186 'name' => 'First month pro-rated, then flat-rate',
188 'setup_fee' => { 'name' => 'Setup fee for this package',
191 'recur_fee' => { 'name' => 'Recurring fee for this package',
195 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
196 'setup' => 'what.setup_fee.value',
197 '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) ; \'',
200 'flat_comission_cust' => {
201 'name' => 'Flat rate with recurring comission per active customer',
203 'setup_fee' => { 'name' => 'Setup fee for this package',
206 'recur_fee' => { 'name' => 'Recurring fee for this package',
209 'comission_amount' => { 'name' => 'Comission amount per month (per active customer)',
212 'comission_depth' => { 'name' => 'Number of layers',
216 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
217 'setup' => 'what.setup_fee.value',
218 '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 + \';\'',
221 'flat_comission' => {
222 'name' => 'Flat rate with recurring comission per (any) active package',
224 'setup_fee' => { 'name' => 'Setup fee for this package',
227 'recur_fee' => { 'name' => 'Recurring fee for this package',
230 'comission_amount' => { 'name' => 'Comission amount per month (per active package)',
233 'comission_depth' => { 'name' => 'Number of layers',
237 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
238 'setup' => 'what.setup_fee.value',
239 '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 + \';\'',
242 'flat_comission_pkg' => {
243 'name' => 'Flat rate with recurring comission per (selected) active package',
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 uncancelled package)',
254 'comission_depth' => { 'name' => 'Number of layers',
257 'comission_pkgpart' => { 'name' => 'Applicable packages<BR><FONT SIZE="-1">(hold <b>ctrl</b> to select multiple packages)</FONT>',
258 'type' => 'select_multiple',
259 'select_table' => 'part_pkg',
260 'select_hash' => { 'disabled' => '' } ,
261 'select_key' => 'pkgpart',
262 'select_label' => 'pkg',
265 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount', 'comission_pkgpart' ],
266 'setup' => 'what.setup_fee.value',
267 '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 + \';\'',
273 'name' => 'Base charge plus charge per-hour from the session monitor',
275 'setup_fee' => { 'name' => 'Setup fee for this package',
278 'recur_flat' => { 'name' => 'Base monthly charge for this package',
281 'recur_included_hours' => { 'name' => 'Hours included',
284 'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
288 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
289 'setup' => 'what.setup_fee.value',
290 '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;\'',
294 'name' => 'Base charge plus charge per-minute from the session monitor',
296 'setup_fee' => { 'name' => 'Setup fee for this package',
299 'recur_flat' => { 'name' => 'Base monthly charge for this package',
302 'recur_included_min' => { 'name' => 'Minutes included',
305 'recur_minly_charge' => { 'name' => 'Additional charge per minute',
309 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_min', 'recur_minly_charge' ],
310 'setup' => 'what.setup_fee.value',
311 '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;\'',
322 function changed(what) {
323 layer = what.options[what.selectedIndex].value;
324 <% foreach my $layer ( keys %plans ) { %>
325 if (layer == "<%= $layer %>" ) {
326 <% foreach my $not ( grep { $_ ne $layer } keys %plans ) { %>
327 if (document.getElementById) {
328 document.getElementById('d<%= $not %>').style.visibility = "hidden";
330 document.l<%= $not %>.visibility = "hidden";
333 if (document.getElementById) {
334 document.getElementById('d<%= $layer %>').style.visibility = "visible";
336 document.l<%= $layer %>.visibility = "visible";
344 Price plan <SELECT NAME="plan" SIZE=1 onChange="changed(this);">
346 <% foreach my $layer (keys %plans ) { %>
347 <OPTION VALUE="<%= $layer %>"<%= ' SELECTED'x($layer eq $part_pkg->plan) %>><%= $plans{$layer}->{'name'} %>
352 function fchanged(what) {
356 function fixup(what) {
357 <% foreach my $f ( qw( pkg comment freq ), @fixups ) { %>
358 what.<%= $f %>.value = document.dummy.<%= $f %>.value;
360 <% foreach my $f ( qw( setuptax recurtax disabled ) ) { %>
361 if (document.dummy.<%= $f %>.checked)
362 what.<%= $f %>.value = 'Y';
364 what.<%= $f %>.value = '';
366 what.plan.value = document.dummy.plan.options[document.dummy.plan.selectedIndex].value;
367 <% foreach my $p ( keys %plans ) { %>
368 if ( what.plan.value == "<%= $p %>" ) {
369 what.setup.value = <%= $plans{$p}->{setup} %>;
370 what.recur.value = <%= $plans{$p}->{recur} %>;
376 <% my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
377 split("\n", $part_pkg->plandata );
378 #foreach my $layer ( 'konq_kludge', keys %plans ) {
379 foreach my $layer ( 'konq_kludge', keys %plans ) {
380 my $visibility = "hidden";
383 if (document.getElementById) {
384 document.write("<DIV ID=\"d<%= $layer %>\" STYLE=\"visibility: <%= $visibility %>; position: absolute\">");
386 <% $visibility="show" if $visibility eq "visible"; %>
387 document.write("<LAYER ID=\"l<%= $layer %>\" VISIBILITY=\"<%= $visibility %>\">");
391 <FORM NAME="<%= $layer %>" ACTION="process/part_pkg.cgi" METHOD=POST onSubmit="fixup(this)">
392 <INPUT TYPE="hidden" NAME="plan" VALUE="<%= $part_pkg->plan %>">
393 <INPUT TYPE="hidden" NAME="pkg" VALUE="<%= $hashref->{pkg} %>">
394 <INPUT TYPE="hidden" NAME="comment" VALUE="$<%= $hashref->{comment} %>">
395 <INPUT TYPE="hidden" NAME="freq" VALUE="<%= $hashref->{freq} %>">
396 <INPUT TYPE="hidden" NAME="setuptax" VALUE="<%= $hashref->{setuptax} %>">
397 <INPUT TYPE="hidden" NAME="recurtax" VALUE="<%= $hashref->{recurtax} %>">
398 <INPUT TYPE="hidden" NAME="disabled" VALUE="<%= $hashref->{disabled} %>">
399 <% foreach my $f ( @fixups ) { %>
400 <INPUT TYPE="hidden" NAME="<%= $f %>" VALUE="">
404 if ( $cgi->param('clone') ) {
405 print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
407 if ( $cgi->param('pkgnum') ) {
408 print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
412 <INPUT TYPE="hidden" NAME="pkgpart" VALUE="<%= $hashref->{pkgpart} %>">
413 <%= ntable("#cccccc",2) %>
415 <% my $href = $plans{$layer}->{'fields'};
416 foreach my $field ( exists($plans{$layer}->{'fieldorder'})
417 ? @{$plans{$layer}->{'fieldorder'}}
421 <TR><TD ALIGN="right"><%= $href->{$field}{'name'} %></TD>
423 <% if ( ! exists($href->{$field}{'type'}) ) { %>
424 <INPUT TYPE="text" NAME="<%= $field %>" VALUE="<%= exists($plandata{$field}) ? $plandata{$field} : $href->{$field}{'default'} %>" onChange="fchanged(this)">
425 <% } elsif ( $href->{$field}{'type'} eq 'select_multiple' ) { %>
426 <SELECT MULTIPLE NAME="<%= $field %>" onChange="fchanged(this)">
427 <% foreach my $record ( qsearch( $href->{$field}{'select_table'}, $href->{$field}{'select_hash'} ) ) {
428 my $value = $record->getfield($href->{$field}{'select_key'}); %>
429 <OPTION VALUE="<%= $value %>"<%= $plandata{$field} =~ /(^|, *)$value *(,|$)/ ? ' SELECTED' : '' %>><%= $record->getfield($href->{$field}{'select_label'}) %>
437 <INPUT TYPE="hidden" NAME="plandata" VALUE="<%= join(',', keys %{ $href } ) %>">
441 print qq!<INPUT TYPE="submit" VALUE="!,
442 $hashref->{pkgpart} ? "Apply changes" : "Add package",
443 qq!" onClick="fchanged(this)">!;
446 <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>
447 <FONT SIZE="1">Setup expression<BR><INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="<%= $hashref->{setup} %>" onLoad="fchanged(this)"></FONT><BR>
448 <FONT SIZE="1">Recurring espression<BR><INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="<%= $hashref->{recur} %>" onLoad="fchanged(this)"></FONT>
455 if (document.getElementById) {
456 document.write("</DIV>");
458 document.write("</LAYER>");
465 if (document.getElementById) {
466 document.getElementById('d<%= $part_pkg->plan %>').style.visibility = 'visible';
468 document.l<%= $part_pkg->plan %>.visibility = 'visible';