slighly different wording
[freeside.git] / httemplate / edit / part_pkg.cgi
1 <!-- mason kludge -->
2 <%
3
4 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {
5   $cgi->param('clone', $1);
6 } else {
7   $cgi->param('clone', '');
8 }
9 if ( $cgi->param('pkgnum') && $cgi->param('pkgnum') =~ /^(\d+)$/ ) {
10   $cgi->param('pkgnum', $1);
11 } else {
12   $cgi->param('pkgnum', '');
13 }
14
15 my ($query) = $cgi->keywords;
16 my $action = '';
17 my $part_pkg = '';
18 if ( $cgi->param('error') ) {
19   $part_pkg = new FS::part_pkg ( {
20     map { $_, scalar($cgi->param($_)) } fields('part_pkg')
21   } );
22 }
23 if ( $cgi->param('clone') ) {
24   $action='Custom Pricing';
25   my $old_part_pkg =
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});
31 } else {
32   $part_pkg ||= new FS::part_pkg {};
33   $part_pkg->plan('flat');
34 }
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");
39 }
40 $action ||= $part_pkg->pkgpart ? 'Edit' : 'Add';
41 my $hashref = $part_pkg->hashref;
42
43 %>
44
45 <SCRIPT>
46 function visualize(what) {
47   if (document.getElementById) {
48     document.getElementById('d<%= $part_pkg->plan %>').style.visibility = "visible";
49   } else {
50     document.l<%= $part_pkg->plan %>.visibility = "visible";
51   }
52 }
53 </SCRIPT>
54
55 <% 
56
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()"');
61
62 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
63       "</FONT>"
64   if $cgi->param('error');
65
66 #print '<FORM ACTION="', popurl(1), 'process/part_pkg.cgi" METHOD=POST>';
67 print '<FORM NAME="dummy">';
68
69 #if ( $cgi->param('clone') ) {
70 #  print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
71 #}
72 #if ( $cgi->param('pkgnum') ) {
73 #  print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
74 #}
75 #
76 #print qq!<INPUT TYPE="hidden" NAME="pkgpart" VALUE="$hashref->{pkgpart}">!,
77 print "Package Part #", $hashref->{pkgpart} ? $hashref->{pkgpart} : "(NEW)";
78
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>
84 END
85
86 print '<INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y"';
87 print ' CHECKED' if $hashref->{setuptax} eq "Y";
88 print '>';
89
90 print <<END;
91 </TD></TR>
92 <TR><TD ALIGN="right">Recurring fee tax exempt</TD><TD>
93 END
94
95 print '<INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y"';
96 print ' CHECKED' if $hashref->{recurtax} eq "Y";
97 print '>';
98
99 print '</TD></TR>';
100
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";
104 print '>';
105 print '</TD></TR></TABLE>';
106
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>
109 END
110
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>
115 END
116 }
117
118 my @fixups = ();
119 my $count = 0;
120 my $columns = 3;
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,
130     'quantity' => 0,
131   });
132   #? #next unless $pkg_svc;
133
134   push @fixups, "pkg_svc$svcpart";
135
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);
143     $count+=1;
144     foreach ( 1 .. $columns-1 ) {
145       print "</TABLE></TD><TD VALIGN=\"top\">$thead"
146         if $count == int( $_ * scalar(@part_svc) / $columns );
147     }
148   } else {
149     print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
150           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
151   }
152 }
153
154 unless ( $cgi->param('clone') ) {
155   print "</TR></TABLE></TD></TR></TABLE>";
156   #print "</TR></TABLE>";
157 }
158
159 # prolly should be in database
160 use Tie::IxHash;
161 tie my %plans, 'Tie::IxHash',
162   'flat' => {
163     'name' => 'Flat rate',
164     'fields' => {
165       'setup_fee' => { 'name' => 'Setup fee for this package',
166                        'default' => 0,
167                      },
168       'recur_fee' => { 'name' => 'Recurring fee for this package',
169                        'default' => 0,
170                       },
171     },
172     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
173     'setup' => 'what.setup_fee.value',
174     'recur' => 'what.recur_fee.value',
175   },
176
177   'prorate' => {
178     'name' => 'First month pro-rated, then flat-rate',
179     'fields' =>  {
180       'setup_fee' => { 'name' => 'Setup fee for this package',
181                        'default' => 0,
182                      },
183       'recur_fee' => { 'name' => 'Recurring fee for this package',
184                        'default' => 0,
185                       },
186     },
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) ; \'',
190   },
191
192   'flat_comission_cust' => {
193     'name' => 'Flat rate with recurring comission per active customer',
194     'fields' => {
195       'setup_fee' => { 'name' => 'Setup fee for this package',
196                        'default' => 0,
197                      },
198       'recur_fee' => { 'name' => 'Recurring fee for this package',
199                        'default' => 0,
200                      },
201       'comission_amount' => { 'name' => 'Comission amount per month (per active customer)',
202                               'default' => 0,
203                             },
204       'comission_depth'  => { 'name' => 'Number of layers',
205                               'default' => 1,
206                             },
207     },
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 + \';\'',
211   },
212
213   'flat_comission' => {
214     'name' => 'Flat rate with recurring comission per (any) active package',
215     'fields' => {
216       'setup_fee' => { 'name' => 'Setup fee for this package',
217                        'default' => 0,
218                      },
219       'recur_fee' => { 'name' => 'Recurring fee for this package',
220                        'default' => 0,
221                      },
222       'comission_amount' => { 'name' => 'Comission amount per month (per active package)',
223                               'default' => 0,
224                             },
225       'comission_depth'  => { 'name' => 'Number of layers',
226                               'default' => 1,
227                             },
228     },
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 + \';\'',
232   },
233
234   'flat_comission_pkg' => {
235     'name' => 'Flat rate with recurring comission per (selected) active package',
236     'fields' => {
237       'setup_fee' => { 'name' => 'Setup fee for this package',
238                        'default' => 0,
239                      },
240       'recur_fee' => { 'name' => 'Recurring fee for this package',
241                        'default' => 0,
242                      },
243       'comission_amount' => { 'name' => 'Comission amount per month (per uncancelled package)',
244                               'default' => 0,
245                             },
246       'comission_depth'  => { 'name' => 'Number of layers',
247                               'default' => 1,
248                             },
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',
255                              },
256     },
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 + \';\'',
260   },
261
262
263
264   'sesmon_hour' => {
265     'name' => 'Base charge plus charge per-hour from the session monitor',
266     'fields' => {
267       'setup_fee' => { 'name' => 'Setup fee for this package',
268                        'default' => 0,
269                      },
270       'recur_flat' => { 'name' => 'Base monthly charge for this package',
271                         'default' => 0,
272                       },
273       'recur_included_hours' => { 'name' => 'Hours included',
274                                   'default' => 0,
275                                 },
276       'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
277                                  'default' => 0,
278                                },
279     },
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;\'',
283   },
284
285   'sesmon_minute' => {
286     'name' => 'Base charge plus charge per-minute from the session monitor',
287     'fields' => {
288       'setup_fee' => { 'name' => 'Setup fee for this package',
289                        'default' => 0,
290                      },
291       'recur_flat' => { 'name' => 'Base monthly charge for this package',
292                         'default' => 0,
293                       },
294       'recur_included_min' => { 'name' => 'Minutes included',
295                                 'default' => 0,
296                                 },
297       'recur_minly_charge' => { 'name' => 'Additional charge per minute',
298                                 'default' => 0,
299                               },
300     },
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;\'',
304
305   },
306
307 ;
308
309 %>
310
311 <SCRIPT>
312 var layer = null;
313
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";
321       } else {
322         document.l<%= $not %>.visibility = "hidden";
323       }
324     <% } %>
325     if (document.getElementById) {
326       document.getElementById('d<%= $layer %>').style.visibility = "visible";
327     } else {
328       document.l<%= $layer %>.visibility = "visible";
329     }
330   }
331 <% } %>
332 }
333
334 </SCRIPT>
335 <BR>
336 Price plan <SELECT NAME="plan" SIZE=1 onChange="changed(this);">
337 <OPTION>
338 <% foreach my $layer (keys %plans ) { %>
339 <OPTION VALUE="<%= $layer %>"<%= ' SELECTED'x($layer eq $part_pkg->plan) %>><%= $plans{$layer}->{'name'} %>
340 <% } %>
341 </SELECT></FORM>
342
343 <SCRIPT>
344 function fchanged(what) {
345   fixup(what.form);
346 }
347
348 function fixup(what) {
349 <% foreach my $f ( qw( pkg comment freq ), @fixups ) { %>
350   what.<%= $f %>.value = document.dummy.<%= $f %>.value;
351 <% } %>
352 <% foreach my $f ( qw( setuptax recurtax disabled ) ) { %>
353   if (document.dummy.<%= $f %>.checked)
354     what.<%= $f %>.value = 'Y';
355   else
356     what.<%= $f %>.value = '';
357 <% } %>
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} %>;
363   }
364 <% } %>
365 }
366 </SCRIPT>
367
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";
373 %>
374 <SCRIPT>
375 if (document.getElementById) {
376     document.write("<DIV ID=\"d<%= $layer %>\" STYLE=\"visibility: <%= $visibility %>; position: absolute\">");
377 } else {
378 <% $visibility="show" if $visibility eq "visible"; %>
379     document.write("<LAYER ID=\"l<%= $layer %>\" VISIBILITY=\"<%= $visibility %>\">");
380 }
381 </SCRIPT>
382
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="">
393 <% } %>
394
395 <%
396 if ( $cgi->param('clone') ) {
397   print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
398 }
399 if ( $cgi->param('pkgnum') ) {
400   print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
401 }
402 %>
403
404 <INPUT TYPE="hidden" NAME="pkgpart" VALUE="<%= $hashref->{pkgpart} %>">
405 <%= ntable("#cccccc",2) %>
406
407 <% my $href = $plans{$layer}->{'fields'};
408    foreach my $field ( exists($plans{$layer}->{'fieldorder'})
409                          ? @{$plans{$layer}->{'fieldorder'}}
410                          : keys %{ $href }
411                      ) {
412 %>
413   <TR><TD ALIGN="right"><%= $href->{$field}{'name'} %></TD>
414   <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'}) %>
422        <% } %>
423        </SELECT>
424   <% } %>
425   </TD></TR>
426 <% } %>
427
428 </TABLE>
429 <INPUT TYPE="hidden" NAME="plandata" VALUE="<%= join(',', keys %{ $href } ) %>">
430 <BR><BR>
431
432 <%
433 print qq!<INPUT TYPE="submit" VALUE="!,
434       $hashref->{pkgpart} ? "Apply changes" : "Add package",
435       qq!" onClick="fchanged(this)">!;
436 %>
437
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>
441 </TR></TD>
442 </TABLE>
443
444 </FORM>
445
446 <SCRIPT>
447 if (document.getElementById) {
448   document.write("</DIV>");
449 } else {
450   document.write("</LAYER>");
451 }
452 </SCRIPT>
453
454 <% } %>
455
456 <TAG onLoad="
457     if (document.getElementById) {
458       document.getElementById('d<%= $part_pkg->plan %>').style.visibility = 'visible';
459     } else {
460       document.l<%= $part_pkg->plan %>.visibility = 'visible';
461     }
462 ">
463   </BODY>
464 </HTML>