- web interface for hourly account charges!
[freeside.git] / httemplate / edit / part_pkg.cgi
1 <!-- $Id: part_pkg.cgi,v 1.9 2002-01-29 16:33:16 ivan Exp $ -->
2
3 <%
4
5 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {
6   $cgi->param('clone', $1);
7 } else {
8   $cgi->param('clone', '');
9 }
10 if ( $cgi->param('pkgnum') && $cgi->param('pkgnum') =~ /^(\d+)$/ ) {
11   $cgi->param('pkgnum', $1);
12 } else {
13   $cgi->param('pkgnum', '');
14 }
15
16 my ($query) = $cgi->keywords;
17 my $action = '';
18 my $part_pkg = '';
19 if ( $cgi->param('error') ) {
20   $part_pkg = new FS::part_pkg ( {
21     map { $_, scalar($cgi->param($_)) } fields('part_pkg')
22   } );
23 }
24 if ( $cgi->param('clone') ) {
25   $action='Custom Pricing';
26   my $old_part_pkg =
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});
32 } else {
33   $part_pkg ||= new FS::part_pkg {};
34   $part_pkg->plan('flat');
35 }
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");
40 }
41 $action ||= $part_pkg->pkgpart ? 'Edit' : 'Add';
42 my $hashref = $part_pkg->hashref;
43
44 %>
45
46 <SCRIPT>
47 function visualize(what) {
48   if (document.getElementById) {
49     document.getElementById('d<%= $part_pkg->plan %>').style.visibility = "visible";
50   } else {
51     document.l<%= $part_pkg->plan %>.visibility = "visible";
52   }
53 }
54 </SCRIPT>
55
56 <% 
57
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()"');
62
63 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
64       "</FONT>"
65   if $cgi->param('error');
66
67 #print '<FORM ACTION="', popurl(1), 'process/part_pkg.cgi" METHOD=POST>';
68 print '<FORM NAME="dummy">';
69
70 #if ( $cgi->param('clone') ) {
71 #  print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
72 #}
73 #if ( $cgi->param('pkgnum') ) {
74 #  print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
75 #}
76 #
77 #print qq!<INPUT TYPE="hidden" NAME="pkgpart" VALUE="$hashref->{pkgpart}">!,
78 print "Package Part #", $hashref->{pkgpart} ? $hashref->{pkgpart} : "(NEW)";
79
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>
85 END
86
87 print '<INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y"';
88 print ' CHECKED' if $hashref->{setuptax} eq "Y";
89 print '>';
90
91 print <<END;
92 </TD></TR>
93 <TR><TD ALIGN="right">Recurring fee tax exempt</TD><TD>
94 END
95
96 print '<INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y"';
97 print ' CHECKED' if $hashref->{recurtax} eq "Y";
98 print '>';
99
100 print '</TD></TR>';
101
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";
105 print '>';
106 print '</TD></TR></TABLE>';
107
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>
110 END
111
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>
116 END
117 }
118
119 my @fixups = ();
120 my $count = 0;
121 my $columns = 3;
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,
131     'quantity' => 0,
132   });
133   #? #next unless $pkg_svc;
134
135   push @fixups, "pkg_svc$svcpart";
136
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);
144     $count+=1;
145     foreach ( 1 .. $columns-1 ) {
146       print "</TABLE></TD><TD VALIGN=\"top\">$thead"
147         if $count == int( $_ * scalar(@part_svc) / $columns );
148     }
149   } else {
150     print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
151           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
152   }
153 }
154
155 unless ( $cgi->param('clone') ) {
156   print "</TR></TABLE></TD></TR></TABLE>";
157   #print "</TR></TABLE>";
158 }
159
160 # prolly should be in database
161 use Tie::IxHash;
162 tie my %plans, 'Tie::IxHash',
163   'flat' => {
164     'name' => 'Flat rate',
165     'fields' => {
166       'setup_fee' => { 'name' => 'Setup fee for this package',
167                        'default' => 0,
168                      },
169       'recur_fee' => { 'name' => 'Recurring fee for this package',
170                        'default' => 0,
171                       },
172     },
173     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
174     'setup' => 'what.setup_fee.value',
175     'recur' => 'what.recur_fee.value',
176   },
177
178   'flat_comission' => {
179     'name' => 'Flat rate with recurring referral comission as credit',
180     'fields' => {
181       'setup_fee' => { 'name' => 'Setup fee for this package',
182                        'default' => 0,
183                      },
184       'recur_fee' => { 'name' => 'Recurring fee for this package',
185                        'default' => 0,
186                      },
187       'comission_amount' => { 'name' => 'Comission amount',
188                               'default' => 0,
189                             },
190       'comission_depth'  => { 'name' => 'Number of layers',
191                               'default' => 1,
192                             },
193     },
194     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
195     'setup' => 'what.setup_fee.value',
196     '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 + \';\'',
197   },
198
199   'sesmon_hour' => {
200     'name' => 'Base charge plus charge per-hour from the session monitor',
201     'fields' => {
202       'setup_fee' => { 'name' => 'Setup fee for this package',
203                        'default' => 0,
204                      },
205       'recur_flat' => { 'name' => 'Base monthly charge for this package',
206                         'default' => 0,
207                       },
208       'recur_included_hours' => { 'name' => 'Hours included',
209                                   'default' => 0,
210                                 },
211       'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
212                                  'default' => 0,
213                                },
214     },
215     'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
216     'setup' => 'what.setup_fee.value',
217     '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;\'',
218   },
219
220   'sesmon_minute' => {
221     'name' => 'Base charge plus charge per-minute from the session monitor',
222     'fields' => {
223       'setup_fee' => { 'name' => 'Setup fee for this package',
224                        'default' => 0,
225                      },
226       'recur_flat' => { 'name' => 'Base monthly charge for this package',
227                         'default' => 0,
228                       },
229       'recur_included_min' => { 'name' => 'Minutes included',
230                                 'default' => 0,
231                                 },
232       'recur_minly_charge' => { 'name' => 'Additional charge per minute',
233                                 'default' => 0,
234                               },
235     },
236     'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_min', 'recur_minly_charge' ],
237     'setup' => 'what.setup_fee.value',
238     '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;\'',
239
240   },
241
242 ;
243
244 %>
245
246 <SCRIPT>
247 var layer = null;
248
249 function changed(what) {
250   layer = what.options[what.selectedIndex].value;
251 <% foreach my $layer ( keys %plans ) { %>
252   if (layer == "<%= $layer %>" ) {
253     <% foreach my $not ( grep { $_ ne $layer } keys %plans ) { %>
254       if (document.getElementById) {
255         document.getElementById('d<%= $not %>').style.visibility = "hidden";
256       } else {
257         document.l<%= $not %>.visibility = "hidden";
258       }
259     <% } %>
260     if (document.getElementById) {
261       document.getElementById('d<%= $layer %>').style.visibility = "visible";
262     } else {
263       document.l<%= $layer %>.visibility = "visible";
264     }
265   }
266 <% } %>
267 }
268
269 </SCRIPT>
270 <BR>
271 Price plan <SELECT NAME="plan" SIZE=1 onChange="changed(this);">
272 <OPTION>
273 <% foreach my $layer (keys %plans ) { %>
274 <OPTION VALUE="<%= $layer %>"<%= ' SELECTED'x($layer eq $part_pkg->plan) %>><%= $plans{$layer}->{'name'} %>
275 <% } %>
276 </SELECT></FORM>
277
278 <SCRIPT>
279 function fchanged(what) {
280   fixup(what.form);
281 }
282
283 function fixup(what) {
284 <% foreach my $f ( qw( pkg comment freq ), @fixups ) { %>
285   what.<%= $f %>.value = document.dummy.<%= $f %>.value;
286 <% } %>
287 <% foreach my $f ( qw( setuptax recurtax disabled ) ) { %>
288   if (document.dummy.<%= $f %>.checked)
289     what.<%= $f %>.value = 'Y';
290   else
291     what.<%= $f %>.value = '';
292 <% } %>
293   what.plan.value = document.dummy.plan.options[document.dummy.plan.selectedIndex].value;
294 <% foreach my $p ( keys %plans ) { %>
295   if ( what.plan.value == "<%= $p %>" ) {
296     what.setup.value = <%= $plans{$p}->{setup} %>;
297     what.recur.value = <%= $plans{$p}->{recur} %>;
298   }
299 <% } %>
300 }
301 </SCRIPT>
302
303 <% my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
304                     split("\n", $part_pkg->plandata );
305    #foreach my $layer ( 'konq_kludge', keys %plans ) { 
306    foreach my $layer ( 'konq_kludge', keys %plans ) {
307      my $visibility = "hidden";
308 %>
309 <SCRIPT>
310 if (document.getElementById) {
311     document.write("<DIV ID=\"d<%= $layer %>\" STYLE=\"visibility: <%= $visibility %>; position: absolute\">");
312 } else {
313 <% $visibility="show" if $visibility eq "visible"; %>
314     document.write("<LAYER ID=\"l<%= $layer %>\" VISIBILITY=\"<%= $visibility %>\">");
315 }
316 </SCRIPT>
317
318 <FORM NAME="<%= $layer %>" ACTION="process/part_pkg.cgi" METHOD=POST onSubmit="fixup(this)">
319 <INPUT TYPE="hidden" NAME="plan" VALUE="<%= $part_pkg->plan %>">
320 <INPUT TYPE="hidden" NAME="pkg" VALUE="<%= $hashref->{pkg} %>">
321 <INPUT TYPE="hidden" NAME="comment" VALUE="$<%= $hashref->{comment} %>">
322 <INPUT TYPE="hidden" NAME="freq" VALUE="<%= $hashref->{freq} %>">
323 <INPUT TYPE="hidden" NAME="setuptax" VALUE="<%= $hashref->{setuptax} %>">
324 <INPUT TYPE="hidden" NAME="recurtax" VALUE="<%= $hashref->{recurtax} %>">
325 <INPUT TYPE="hidden" NAME="disabled" VALUE="<%= $hashref->{disabled} %>">
326 <% foreach my $f ( @fixups ) { %>
327 <INPUT TYPE="hidden" NAME="<%= $f %>" VALUE="">
328 <% } %>
329
330 <%
331 if ( $cgi->param('clone') ) {
332   print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
333 }
334 if ( $cgi->param('pkgnum') ) {
335   print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
336 }
337 %>
338
339 <INPUT TYPE="hidden" NAME="pkgpart" VALUE="<%= $hashref->{pkgpart} %>">
340 <%= ntable("#cccccc",2) %>
341
342 <% my $href = $plans{$layer}->{'fields'};
343    foreach my $field ( exists($plans{$layer}->{'fieldorder'})
344                          ? @{$plans{$layer}->{'fieldorder'}}
345                          : keys %{ $href }
346                      ) {
347 %>
348 <TR><TD ALIGN="right"><%= $href->{$field}{'name'} %></TD>
349 <TD><INPUT TYPE="text" NAME="<%= $field %>" VALUE="<%= exists($plandata{$field}) ? $plandata{$field} : $href->{$field}{'default'} %>" onChange="fchanged(this)"></TD></TR>
350 <% } %>
351 </TABLE>
352 <INPUT TYPE="hidden" NAME="plandata" VALUE="<%= join(',', keys %{ $href } ) %>">
353 <BR><BR>
354
355 <%
356 print qq!<INPUT TYPE="submit" VALUE="!,
357       $hashref->{pkgpart} ? "Apply changes" : "Add package",
358       qq!" onClick="fchanged(this)">!;
359 %>
360
361 <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>
362 <FONT SIZE="1">Setup expression<BR><INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="<%= $hashref->{setup} %>" onLoad="fchanged(this)"></FONT><BR>
363 <FONT SIZE="1">Recurring espression<BR><INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="<%= $hashref->{recur} %>" onLoad="fchanged(this)"></FONT>
364 </TR></TD>
365 </TABLE>
366
367 </FORM>
368
369 <SCRIPT>
370 if (document.getElementById) {
371   document.write("</DIV>");
372 } else {
373   document.write("</LAYER>");
374 }
375 </SCRIPT>
376
377 <% } %>
378
379 <TAG onLoad="
380     if (document.getElementById) {
381       document.getElementById('d<%= $part_pkg->plan %>').style.visibility = 'visible';
382     } else {
383       document.l<%= $part_pkg->plan %>.visibility = 'visible';
384     }
385 ">
386   </BODY>
387 </HTML>