1 <!-- $Id: part_pkg.cgi,v 1.10 2002-02-10 02:16:47 ivan Exp $ -->
5 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {
6 $cgi->param('clone', $1);
8 $cgi->param('clone', '');
10 if ( $cgi->param('pkgnum') && $cgi->param('pkgnum') =~ /^(\d+)$/ ) {
11 $cgi->param('pkgnum', $1);
13 $cgi->param('pkgnum', '');
16 my ($query) = $cgi->keywords;
19 if ( $cgi->param('error') ) {
20 $part_pkg = new FS::part_pkg ( {
21 map { $_, scalar($cgi->param($_)) } fields('part_pkg')
24 if ( $cgi->param('clone') ) {
25 $action='Custom Pricing';
27 qsearchs('part_pkg', { 'pkgpart' => $cgi->param('clone') } );
28 $part_pkg ||= $old_part_pkg->clone;
29 $part_pkg->disabled('Y');
30 } elsif ( $query && $query =~ /^(\d+)$/ ) {
31 $part_pkg ||= qsearchs('part_pkg',{'pkgpart'=>$1});
33 $part_pkg ||= new FS::part_pkg {};
34 $part_pkg->plan('flat');
36 unless ( $part_pkg->plan ) { #backwards-compat
37 $part_pkg->plan('flat');
38 $part_pkg->plandata("setup_fee=". $part_pkg->setup. "\n".
39 "recur_fee=". $part_pkg->recur. "\n");
41 $action ||= $part_pkg->pkgpart ? 'Edit' : 'Add';
42 my $hashref = $part_pkg->hashref;
47 function visualize(what) {
48 if (document.getElementById) {
49 document.getElementById('d<%= $part_pkg->plan %>').style.visibility = "visible";
51 document.l<%= $part_pkg->plan %>.visibility = "visible";
58 print header("$action Package Definition", menubar(
59 'Main Menu' => popurl(2),
60 'View all packages' => popurl(2). 'browse/part_pkg.cgi',
61 ), ' onLoad="visualize()"');
63 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
65 if $cgi->param('error');
67 #print '<FORM ACTION="', popurl(1), 'process/part_pkg.cgi" METHOD=POST>';
68 print '<FORM NAME="dummy">';
70 #if ( $cgi->param('clone') ) {
71 # print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
73 #if ( $cgi->param('pkgnum') ) {
74 # print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
77 #print qq!<INPUT TYPE="hidden" NAME="pkgpart" VALUE="$hashref->{pkgpart}">!,
78 print "Package Part #", $hashref->{pkgpart} ? $hashref->{pkgpart} : "(NEW)";
80 print ntable("#cccccc",2), <<END;
81 <TR><TD ALIGN="right">Package (customer-visable)</TD><TD><INPUT TYPE="text" NAME="pkg" SIZE=32 VALUE="$hashref->{pkg}"></TD></TR>
82 <TR><TD ALIGN="right">Comment (customer-hidden)</TD><TD><INPUT TYPE="text" NAME="comment" SIZE=32 VALUE="$hashref->{comment}"></TD></TR>
83 <TR><TD ALIGN="right">Frequency (months) of recurring fee</TD><TD><INPUT TYPE="text" NAME="freq" VALUE="$hashref->{freq}" SIZE=3></TD></TR>
84 <TR><TD ALIGN="right">Setup fee tax exempt</TD><TD>
87 print '<INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y"';
88 print ' CHECKED' if $hashref->{setuptax} eq "Y";
93 <TR><TD ALIGN="right">Recurring fee tax exempt</TD><TD>
96 print '<INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y"';
97 print ' CHECKED' if $hashref->{recurtax} eq "Y";
102 print '<TR><TD ALIGN="right">Disable new orders</TD><TD>';
103 print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
104 print ' CHECKED' if $hashref->{disabled} eq "Y";
106 print '</TD></TR></TABLE>';
108 my $thead = "\n\n". ntable('#cccccc', 2). <<END;
109 <TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH><TH BGCOLOR="#dcdcdc">Service</TH></TR>
112 unless ( $cgi->param('clone') ) {
113 #print <<END, $thead;
114 print <<END, itable(), '<TR><TD VALIGN="top">', $thead;
115 <BR><BR>Enter the quantity of each service this package includes.<BR><BR>
122 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
123 foreach my $part_svc ( @part_svc ) {
124 my $svcpart = $part_svc->svcpart;
125 my $pkg_svc = qsearchs( 'pkg_svc', {
126 'pkgpart' => $cgi->param('clone') || $part_pkg->pkgpart,
127 'svcpart' => $svcpart,
128 } ) || new FS::pkg_svc ( {
129 'pkgpart' => $cgi->param('clone') || $part_pkg->pkgpart,
130 'svcpart' => $svcpart,
133 #? #next unless $pkg_svc;
135 push @fixups, "pkg_svc$svcpart";
137 unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
138 print '<TR>'; # if $count == 0 ;
139 print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
140 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
141 qq!"></TD><TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
142 qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
143 # print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
145 foreach ( 1 .. $columns-1 ) {
146 print "</TABLE></TD><TD VALIGN=\"top\">$thead"
147 if $count == int( $_ * scalar(@part_svc) / $columns );
150 print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
151 $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
155 unless ( $cgi->param('clone') ) {
156 print "</TR></TABLE></TD></TR></TABLE>";
157 #print "</TR></TABLE>";
160 # prolly should be in database
162 tie my %plans, 'Tie::IxHash',
164 'name' => 'Flat rate',
166 'setup_fee' => { 'name' => 'Setup fee for this package',
169 'recur_fee' => { 'name' => 'Recurring fee for this package',
173 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
174 'setup' => 'what.setup_fee.value',
175 'recur' => 'what.recur_fee.value',
179 'name' => 'First month pro-rated, then flat-rate',
181 'setup_fee' => { 'name' => 'Setup fee for this package',
184 'recur_fee' => { 'name' => 'Recurring fee for this package',
188 'fieldorder' => [ 'setup_fee', 'recur_fee' ],
189 'setup' => 'what.setup_fee.value',
190 '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) ; \'',
193 'flat_comission_cust' => {
194 'name' => 'Flat rate with recurring comission per active customer',
196 'setup_fee' => { 'name' => 'Setup fee for this package',
199 'recur_fee' => { 'name' => 'Recurring fee for this package',
202 'comission_amount' => { 'name' => 'Comission amount per month (per active customer)',
205 'comission_depth' => { 'name' => 'Number of layers',
209 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
210 'setup' => 'what.setup_fee.value',
211 '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 + \';\'',
214 'flat_comission' => {
215 'name' => 'Flat rate with recurring comission per active package',
217 'setup_fee' => { 'name' => 'Setup fee for this package',
220 'recur_fee' => { 'name' => 'Recurring fee for this package',
223 'comission_amount' => { 'name' => 'Comission amount per month (per active package)',
226 'comission_depth' => { 'name' => 'Number of layers',
230 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
231 'setup' => 'what.setup_fee.value',
232 '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 + \';\'',
235 'flat_comission_pkg' => {
236 'name' => 'Flat rate with recurring comission per active (selected) package',
238 'setup_fee' => { 'name' => 'Setup fee for this package',
241 'recur_fee' => { 'name' => 'Recurring fee for this package',
244 'comission_amount' => { 'name' => 'Comission amount per month (per uncancelled package)',
247 'comission_depth' => { 'name' => 'Number of layers',
250 'comission_pkgpart' => { 'name' => 'Applicable packages<BR><FONT SIZE="-1">(hold <b>ctrl</b> to select multiple packages)</FONT>',
251 'type' => 'select_multiple',
252 'select_table' => 'part_pkg',
253 'select_hash' => { 'disabled' => '' } ,
254 'select_key' => 'pkgpart',
255 'select_label' => 'pkg',
258 'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount', 'comission_pkgpart' ],
259 'setup' => 'what.setup_fee.value',
260 '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 + \';\'',
266 'name' => 'Base charge plus charge per-hour from the session monitor',
268 'setup_fee' => { 'name' => 'Setup fee for this package',
271 'recur_flat' => { 'name' => 'Base monthly charge for this package',
274 'recur_included_hours' => { 'name' => 'Hours included',
277 'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
281 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
282 'setup' => 'what.setup_fee.value',
283 '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;\'',
287 'name' => 'Base charge plus charge per-minute from the session monitor',
289 'setup_fee' => { 'name' => 'Setup fee for this package',
292 'recur_flat' => { 'name' => 'Base monthly charge for this package',
295 'recur_included_min' => { 'name' => 'Minutes included',
298 'recur_minly_charge' => { 'name' => 'Additional charge per minute',
302 'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_min', 'recur_minly_charge' ],
303 'setup' => 'what.setup_fee.value',
304 '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;\'',
315 function changed(what) {
316 layer = what.options[what.selectedIndex].value;
317 <% foreach my $layer ( keys %plans ) { %>
318 if (layer == "<%= $layer %>" ) {
319 <% foreach my $not ( grep { $_ ne $layer } keys %plans ) { %>
320 if (document.getElementById) {
321 document.getElementById('d<%= $not %>').style.visibility = "hidden";
323 document.l<%= $not %>.visibility = "hidden";
326 if (document.getElementById) {
327 document.getElementById('d<%= $layer %>').style.visibility = "visible";
329 document.l<%= $layer %>.visibility = "visible";
337 Price plan <SELECT NAME="plan" SIZE=1 onChange="changed(this);">
339 <% foreach my $layer (keys %plans ) { %>
340 <OPTION VALUE="<%= $layer %>"<%= ' SELECTED'x($layer eq $part_pkg->plan) %>><%= $plans{$layer}->{'name'} %>
345 function fchanged(what) {
349 function fixup(what) {
350 <% foreach my $f ( qw( pkg comment freq ), @fixups ) { %>
351 what.<%= $f %>.value = document.dummy.<%= $f %>.value;
353 <% foreach my $f ( qw( setuptax recurtax disabled ) ) { %>
354 if (document.dummy.<%= $f %>.checked)
355 what.<%= $f %>.value = 'Y';
357 what.<%= $f %>.value = '';
359 what.plan.value = document.dummy.plan.options[document.dummy.plan.selectedIndex].value;
360 <% foreach my $p ( keys %plans ) { %>
361 if ( what.plan.value == "<%= $p %>" ) {
362 what.setup.value = <%= $plans{$p}->{setup} %>;
363 what.recur.value = <%= $plans{$p}->{recur} %>;
369 <% my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
370 split("\n", $part_pkg->plandata );
371 #foreach my $layer ( 'konq_kludge', keys %plans ) {
372 foreach my $layer ( 'konq_kludge', keys %plans ) {
373 my $visibility = "hidden";
376 if (document.getElementById) {
377 document.write("<DIV ID=\"d<%= $layer %>\" STYLE=\"visibility: <%= $visibility %>; position: absolute\">");
379 <% $visibility="show" if $visibility eq "visible"; %>
380 document.write("<LAYER ID=\"l<%= $layer %>\" VISIBILITY=\"<%= $visibility %>\">");
384 <FORM NAME="<%= $layer %>" ACTION="process/part_pkg.cgi" METHOD=POST onSubmit="fixup(this)">
385 <INPUT TYPE="hidden" NAME="plan" VALUE="<%= $part_pkg->plan %>">
386 <INPUT TYPE="hidden" NAME="pkg" VALUE="<%= $hashref->{pkg} %>">
387 <INPUT TYPE="hidden" NAME="comment" VALUE="$<%= $hashref->{comment} %>">
388 <INPUT TYPE="hidden" NAME="freq" VALUE="<%= $hashref->{freq} %>">
389 <INPUT TYPE="hidden" NAME="setuptax" VALUE="<%= $hashref->{setuptax} %>">
390 <INPUT TYPE="hidden" NAME="recurtax" VALUE="<%= $hashref->{recurtax} %>">
391 <INPUT TYPE="hidden" NAME="disabled" VALUE="<%= $hashref->{disabled} %>">
392 <% foreach my $f ( @fixups ) { %>
393 <INPUT TYPE="hidden" NAME="<%= $f %>" VALUE="">
397 if ( $cgi->param('clone') ) {
398 print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
400 if ( $cgi->param('pkgnum') ) {
401 print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
405 <INPUT TYPE="hidden" NAME="pkgpart" VALUE="<%= $hashref->{pkgpart} %>">
406 <%= ntable("#cccccc",2) %>
408 <% my $href = $plans{$layer}->{'fields'};
409 foreach my $field ( exists($plans{$layer}->{'fieldorder'})
410 ? @{$plans{$layer}->{'fieldorder'}}
414 <TR><TD ALIGN="right"><%= $href->{$field}{'name'} %></TD>
416 <% if ( ! exists($href->{$field}{'type'}) ) { %>
417 <INPUT TYPE="text" NAME="<%= $field %>" VALUE="<%= exists($plandata{$field}) ? $plandata{$field} : $href->{$field}{'default'} %>" onChange="fchanged(this)">
418 <% } elsif ( $href->{$field}{'type'} eq 'select_multiple' ) { %>
419 <SELECT MULTIPLE NAME="<%= $field %>" onChange="fchanged(this)">
420 <% foreach $record ( qsearch( $href->{$field}{'select_table'}, $href->{$field}{'select_hash'} ) ) {
421 my $value = $record->getfield($href->{$field}{'select_key'}); %>
422 <OPTION VALUE="<%= $value %>"<%= $plandata{$field} =~ /(^|, *)$value *(,|$)/ ? ' SELECTED' : '' %>><%= $record->getfield($href->{$field}{'select_label'}) %>
430 <INPUT TYPE="hidden" NAME="plandata" VALUE="<%= join(',', keys %{ $href } ) %>">
434 print qq!<INPUT TYPE="submit" VALUE="!,
435 $hashref->{pkgpart} ? "Apply changes" : "Add package",
436 qq!" onClick="fchanged(this)">!;
439 <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>
440 <FONT SIZE="1">Setup expression<BR><INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="<%= $hashref->{setup} %>" onLoad="fchanged(this)"></FONT><BR>
441 <FONT SIZE="1">Recurring espression<BR><INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="<%= $hashref->{recur} %>" onLoad="fchanged(this)"></FONT>
448 if (document.getElementById) {
449 document.write("</DIV>");
451 document.write("</LAYER>");
458 if (document.getElementById) {
459 document.getElementById('d<%= $part_pkg->plan %>').style.visibility = 'visible';
461 document.l<%= $part_pkg->plan %>.visibility = 'visible';