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 $part_pkg ||= new FS::part_pkg {};
33 $part_pkg->plan('flat');
35 unless ( $part_pkg->plan ) { #backwards-compat
36 $part_pkg->plan('flat');
37 $part_pkg->plandata("setup_fee=". $part_pkg->setup. "\n".
38 "recur_fee=". $part_pkg->recur. "\n");
40 $action ||= $part_pkg->pkgpart ? 'Edit' : 'Add';
41 my $hashref = $part_pkg->hashref;
46 function visualize(what) {
47 if (document.getElementById) {
48 document.getElementById('d<%= $part_pkg->plan %>').style.visibility = "visible";
50 document.l<%= $part_pkg->plan %>.visibility = "visible";
57 print header("$action Package Definition", menubar(
58 'Main Menu' => popurl(2),
59 'View all packages' => popurl(2). 'browse/part_pkg.cgi',
60 ), ' onLoad="visualize()"');
62 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
64 if $cgi->param('error');
66 #print '<FORM ACTION="', popurl(1), 'process/part_pkg.cgi" METHOD=POST>';
67 print '<FORM NAME="dummy">';
69 #if ( $cgi->param('clone') ) {
70 # print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
72 #if ( $cgi->param('pkgnum') ) {
73 # print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
76 #print qq!<INPUT TYPE="hidden" NAME="pkgpart" VALUE="$hashref->{pkgpart}">!,
77 print "Package Part #", $hashref->{pkgpart} ? $hashref->{pkgpart} : "(NEW)";
79 print ntable("#cccccc",2), <<END;
80 <TR><TD ALIGN="right">Package (customer-visable)</TD><TD><INPUT TYPE="text" NAME="pkg" SIZE=32 VALUE="$hashref->{pkg}"></TD></TR>
81 <TR><TD ALIGN="right">Comment (customer-hidden)</TD><TD><INPUT TYPE="text" NAME="comment" SIZE=32 VALUE="$hashref->{comment}"></TD></TR>
82 <TR><TD ALIGN="right">Frequency (months) of recurring fee</TD><TD><INPUT TYPE="text" NAME="freq" VALUE="$hashref->{freq}" SIZE=3></TD></TR>
83 <TR><TD ALIGN="right">Setup fee tax exempt</TD><TD>
86 print '<INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y"';
87 print ' CHECKED' if $hashref->{setuptax} eq "Y";
92 <TR><TD ALIGN="right">Recurring fee tax exempt</TD><TD>
95 print '<INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y"';
96 print ' CHECKED' if $hashref->{recurtax} eq "Y";
101 print '<TR><TD ALIGN="right">Disable new orders</TD><TD>';
102 print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
103 print ' CHECKED' if $hashref->{disabled} eq "Y";
105 print '</TD></TR></TABLE>';
107 my $thead = "\n\n". ntable('#cccccc', 2). <<END;
108 <TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH><TH BGCOLOR="#dcdcdc">Service</TH></TR>
111 unless ( $cgi->param('clone') ) {
112 #print <<END, $thead;
113 print <<END, itable(), '<TR><TD VALIGN="top">', $thead;
114 <BR><BR>Enter the quantity of each service this package includes.<BR><BR>
121 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
122 foreach my $part_svc ( @part_svc ) {
123 my $svcpart = $part_svc->svcpart;
124 my $pkg_svc = qsearchs( 'pkg_svc', {
125 'pkgpart' => $cgi->param('clone') || $part_pkg->pkgpart,
126 'svcpart' => $svcpart,
127 } ) || new FS::pkg_svc ( {
128 'pkgpart' => $cgi->param('clone') || $part_pkg->pkgpart,
129 'svcpart' => $svcpart,
132 #? #next unless $pkg_svc;
134 push @fixups, "pkg_svc$svcpart";
136 unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
137 print '<TR>'; # if $count == 0 ;
138 print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
139 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
140 qq!"></TD><TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
141 qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
142 # print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
144 foreach ( 1 .. $columns-1 ) {
145 print "</TABLE></TD><TD VALIGN=\"top\">$thead"
146 if $count == int( $_ * scalar(@part_svc) / $columns );
149 print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
150 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
154 unless ( $cgi->param('clone') ) {
155 print "</TR></TABLE></TD></TR></TABLE>";
156 #print "</TR></TABLE>";
159 # prolly should be in database
161 tie my %plans, 'Tie::IxHash',
163 'name' => 'Flat rate',
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' => 'First month pro-rated, then flat-rate',
180 'setup_fee' => { 'name' => 'Setup fee for this package',
183 'recur_fee' => { 'name' => 'Recurring fee for this package',
187 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
188 'setup' => 'what.setup_fee.value',
189 '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) ; \'',
192 'flat_comission_cust' => {
193 'name' => 'Flat rate with recurring comission per active customer',
195 'setup_fee' => { 'name' => 'Setup fee for this package',
198 'recur_fee' => { 'name' => 'Recurring fee for this package',
201 'comission_amount' => { 'name' => 'Comission amount per month (per active customer)',
204 'comission_depth' => { 'name' => 'Number of layers',
208 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
209 'setup' => 'what.setup_fee.value',
210 '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 + \';\'',
213 'flat_comission' => {
214 'name' => 'Flat rate with recurring comission per (any) active package',
216 'setup_fee' => { 'name' => 'Setup fee for this package',
219 'recur_fee' => { 'name' => 'Recurring fee for this package',
222 'comission_amount' => { 'name' => 'Comission amount per month (per active package)',
225 'comission_depth' => { 'name' => 'Number of layers',
229 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
230 'setup' => 'what.setup_fee.value',
231 '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 + \';\'',
234 'flat_comission_pkg' => {
235 'name' => 'Flat rate with recurring comission per (selected) active package',
237 'setup_fee' => { 'name' => 'Setup fee for this package',
240 'recur_fee' => { 'name' => 'Recurring fee for this package',
243 'comission_amount' => { 'name' => 'Comission amount per month (per uncancelled package)',
246 'comission_depth' => { 'name' => 'Number of layers',
249 'comission_pkgpart' => { 'name' => 'Applicable packages<BR><FONT SIZE="-1">(hold <b>ctrl</b> to select multiple packages)</FONT>',
250 'type' => 'select_multiple',
251 'select_table' => 'part_pkg',
252 'select_hash' => { 'disabled' => '' } ,
253 'select_key' => 'pkgpart',
254 'select_label' => 'pkg',
257 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount', 'comission_pkgpart' ],
258 'setup' => 'what.setup_fee.value',
259 '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 + \';\'',
265 'name' => 'Base charge plus charge per-hour from the session monitor',
267 'setup_fee' => { 'name' => 'Setup fee for this package',
270 'recur_flat' => { 'name' => 'Base monthly charge for this package',
273 'recur_included_hours' => { 'name' => 'Hours included',
276 'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
280 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
281 'setup' => 'what.setup_fee.value',
282 '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;\'',
286 'name' => 'Base charge plus charge per-minute from the session monitor',
288 'setup_fee' => { 'name' => 'Setup fee for this package',
291 'recur_flat' => { 'name' => 'Base monthly charge for this package',
294 'recur_included_min' => { 'name' => 'Minutes included',
297 'recur_minly_charge' => { 'name' => 'Additional charge per minute',
301 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_min', 'recur_minly_charge' ],
302 'setup' => 'what.setup_fee.value',
303 '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;\'',
314 function changed(what) {
315 layer = what.options[what.selectedIndex].value;
316 <% foreach my $layer ( keys %plans ) { %>
317 if (layer == "<%= $layer %>" ) {
318 <% foreach my $not ( grep { $_ ne $layer } keys %plans ) { %>
319 if (document.getElementById) {
320 document.getElementById('d<%= $not %>').style.visibility = "hidden";
322 document.l<%= $not %>.visibility = "hidden";
325 if (document.getElementById) {
326 document.getElementById('d<%= $layer %>').style.visibility = "visible";
328 document.l<%= $layer %>.visibility = "visible";
336 Price plan <SELECT NAME="plan" SIZE=1 onChange="changed(this);">
338 <% foreach my $layer (keys %plans ) { %>
339 <OPTION VALUE="<%= $layer %>"<%= ' SELECTED'x($layer eq $part_pkg->plan) %>><%= $plans{$layer}->{'name'} %>
344 function fchanged(what) {
348 function fixup(what) {
349 <% foreach my $f ( qw( pkg comment freq ), @fixups ) { %>
350 what.<%= $f %>.value = document.dummy.<%= $f %>.value;
352 <% foreach my $f ( qw( setuptax recurtax disabled ) ) { %>
353 if (document.dummy.<%= $f %>.checked)
354 what.<%= $f %>.value = 'Y';
356 what.<%= $f %>.value = '';
358 what.plan.value = document.dummy.plan.options[document.dummy.plan.selectedIndex].value;
359 <% foreach my $p ( keys %plans ) { %>
360 if ( what.plan.value == "<%= $p %>" ) {
361 what.setup.value = <%= $plans{$p}->{setup} %>;
362 what.recur.value = <%= $plans{$p}->{recur} %>;
368 <% my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
369 split("\n", $part_pkg->plandata );
370 #foreach my $layer ( 'konq_kludge', keys %plans ) {
371 foreach my $layer ( 'konq_kludge', keys %plans ) {
372 my $visibility = "hidden";
375 if (document.getElementById) {
376 document.write("<DIV ID=\"d<%= $layer %>\" STYLE=\"visibility: <%= $visibility %>; position: absolute\">");
378 <% $visibility="show" if $visibility eq "visible"; %>
379 document.write("<LAYER ID=\"l<%= $layer %>\" VISIBILITY=\"<%= $visibility %>\">");
383 <FORM NAME="<%= $layer %>" ACTION="process/part_pkg.cgi" METHOD=POST onSubmit="fixup(this)">
384 <INPUT TYPE="hidden" NAME="plan" VALUE="<%= $part_pkg->plan %>">
385 <INPUT TYPE="hidden" NAME="pkg" VALUE="<%= $hashref->{pkg} %>">
386 <INPUT TYPE="hidden" NAME="comment" VALUE="$<%= $hashref->{comment} %>">
387 <INPUT TYPE="hidden" NAME="freq" VALUE="<%= $hashref->{freq} %>">
388 <INPUT TYPE="hidden" NAME="setuptax" VALUE="<%= $hashref->{setuptax} %>">
389 <INPUT TYPE="hidden" NAME="recurtax" VALUE="<%= $hashref->{recurtax} %>">
390 <INPUT TYPE="hidden" NAME="disabled" VALUE="<%= $hashref->{disabled} %>">
391 <% foreach my $f ( @fixups ) { %>
392 <INPUT TYPE="hidden" NAME="<%= $f %>" VALUE="">
396 if ( $cgi->param('clone') ) {
397 print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
399 if ( $cgi->param('pkgnum') ) {
400 print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
404 <INPUT TYPE="hidden" NAME="pkgpart" VALUE="<%= $hashref->{pkgpart} %>">
405 <%= ntable("#cccccc",2) %>
407 <% my $href = $plans{$layer}->{'fields'};
408 foreach my $field ( exists($plans{$layer}->{'fieldorder'})
409 ? @{$plans{$layer}->{'fieldorder'}}
413 <TR><TD ALIGN="right"><%= $href->{$field}{'name'} %></TD>
415 <% if ( ! exists($href->{$field}{'type'}) ) { %>
416 <INPUT TYPE="text" NAME="<%= $field %>" VALUE="<%= exists($plandata{$field}) ? $plandata{$field} : $href->{$field}{'default'} %>" onChange="fchanged(this)">
417 <% } elsif ( $href->{$field}{'type'} eq 'select_multiple' ) { %>
418 <SELECT MULTIPLE NAME="<%= $field %>" onChange="fchanged(this)">
419 <% foreach my $record ( qsearch( $href->{$field}{'select_table'}, $href->{$field}{'select_hash'} ) ) {
420 my $value = $record->getfield($href->{$field}{'select_key'}); %>
421 <OPTION VALUE="<%= $value %>"<%= $plandata{$field} =~ /(^|, *)$value *(,|$)/ ? ' SELECTED' : '' %>><%= $record->getfield($href->{$field}{'select_label'}) %>
429 <INPUT TYPE="hidden" NAME="plandata" VALUE="<%= join(',', keys %{ $href } ) %>">
433 print qq!<INPUT TYPE="submit" VALUE="!,
434 $hashref->{pkgpart} ? "Apply changes" : "Add package",
435 qq!" onClick="fchanged(this)">!;
438 <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>
439 <FONT SIZE="1">Setup expression<BR><INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="<%= $hashref->{setup} %>" onLoad="fchanged(this)"></FONT><BR>
440 <FONT SIZE="1">Recurring espression<BR><INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="<%= $hashref->{recur} %>" onLoad="fchanged(this)"></FONT>
447 if (document.getElementById) {
448 document.write("</DIV>");
450 document.write("</LAYER>");
457 if (document.getElementById) {
458 document.getElementById('d<%= $part_pkg->plan %>').style.visibility = 'visible';
460 document.l<%= $part_pkg->plan %>.visibility = 'visible';