bead1410ab21f941798bb66a589c8e4e9e3479fb
[freeside.git] / httemplate / edit / part_pkg.cgi
1 <!-- $Id: part_pkg.cgi,v 1.10 2002-02-10 02:16:47 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   'prorate' => {
179     'name' => 'First month pro-rated, then flat-rate',
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     },
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) ; \'',
191   },
192
193   'flat_comission_cust' => {
194     'name' => 'Flat rate with recurring comission per active customer',
195     'fields' => {
196       'setup_fee' => { 'name' => 'Setup fee for this package',
197                        'default' => 0,
198                      },
199       'recur_fee' => { 'name' => 'Recurring fee for this package',
200                        'default' => 0,
201                      },
202       'comission_amount' => { 'name' => 'Comission amount per month (per active customer)',
203                               'default' => 0,
204                             },
205       'comission_depth'  => { 'name' => 'Number of layers',
206                               'default' => 1,
207                             },
208     },
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 + \';\'',
212   },
213
214   'flat_comission' => {
215     'name' => 'Flat rate with recurring comission per active package',
216     'fields' => {
217       'setup_fee' => { 'name' => 'Setup fee for this package',
218                        'default' => 0,
219                      },
220       'recur_fee' => { 'name' => 'Recurring fee for this package',
221                        'default' => 0,
222                      },
223       'comission_amount' => { 'name' => 'Comission amount per month (per active package)',
224                               'default' => 0,
225                             },
226       'comission_depth'  => { 'name' => 'Number of layers',
227                               'default' => 1,
228                             },
229     },
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 + \';\'',
233   },
234
235   'flat_comission_pkg' => {
236     'name' => 'Flat rate with recurring comission per active (selected) package',
237     'fields' => {
238       'setup_fee' => { 'name' => 'Setup fee for this package',
239                        'default' => 0,
240                      },
241       'recur_fee' => { 'name' => 'Recurring fee for this package',
242                        'default' => 0,
243                      },
244       'comission_amount' => { 'name' => 'Comission amount per month (per uncancelled package)',
245                               'default' => 0,
246                             },
247       'comission_depth'  => { 'name' => 'Number of layers',
248                               'default' => 1,
249                             },
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',
256                              },
257     },
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 + \';\'',
261   },
262
263
264
265   'sesmon_hour' => {
266     'name' => 'Base charge plus charge per-hour from the session monitor',
267     'fields' => {
268       'setup_fee' => { 'name' => 'Setup fee for this package',
269                        'default' => 0,
270                      },
271       'recur_flat' => { 'name' => 'Base monthly charge for this package',
272                         'default' => 0,
273                       },
274       'recur_included_hours' => { 'name' => 'Hours included',
275                                   'default' => 0,
276                                 },
277       'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
278                                  'default' => 0,
279                                },
280     },
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;\'',
284   },
285
286   'sesmon_minute' => {
287     'name' => 'Base charge plus charge per-minute from the session monitor',
288     'fields' => {
289       'setup_fee' => { 'name' => 'Setup fee for this package',
290                        'default' => 0,
291                      },
292       'recur_flat' => { 'name' => 'Base monthly charge for this package',
293                         'default' => 0,
294                       },
295       'recur_included_min' => { 'name' => 'Minutes included',
296                                 'default' => 0,
297                                 },
298       'recur_minly_charge' => { 'name' => 'Additional charge per minute',
299                                 'default' => 0,
300                               },
301     },
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;\'',
305
306   },
307
308 ;
309
310 %>
311
312 <SCRIPT>
313 var layer = null;
314
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";
322       } else {
323         document.l<%= $not %>.visibility = "hidden";
324       }
325     <% } %>
326     if (document.getElementById) {
327       document.getElementById('d<%= $layer %>').style.visibility = "visible";
328     } else {
329       document.l<%= $layer %>.visibility = "visible";
330     }
331   }
332 <% } %>
333 }
334
335 </SCRIPT>
336 <BR>
337 Price plan <SELECT NAME="plan" SIZE=1 onChange="changed(this);">
338 <OPTION>
339 <% foreach my $layer (keys %plans ) { %>
340 <OPTION VALUE="<%= $layer %>"<%= ' SELECTED'x($layer eq $part_pkg->plan) %>><%= $plans{$layer}->{'name'} %>
341 <% } %>
342 </SELECT></FORM>
343
344 <SCRIPT>
345 function fchanged(what) {
346   fixup(what.form);
347 }
348
349 function fixup(what) {
350 <% foreach my $f ( qw( pkg comment freq ), @fixups ) { %>
351   what.<%= $f %>.value = document.dummy.<%= $f %>.value;
352 <% } %>
353 <% foreach my $f ( qw( setuptax recurtax disabled ) ) { %>
354   if (document.dummy.<%= $f %>.checked)
355     what.<%= $f %>.value = 'Y';
356   else
357     what.<%= $f %>.value = '';
358 <% } %>
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} %>;
364   }
365 <% } %>
366 }
367 </SCRIPT>
368
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";
374 %>
375 <SCRIPT>
376 if (document.getElementById) {
377     document.write("<DIV ID=\"d<%= $layer %>\" STYLE=\"visibility: <%= $visibility %>; position: absolute\">");
378 } else {
379 <% $visibility="show" if $visibility eq "visible"; %>
380     document.write("<LAYER ID=\"l<%= $layer %>\" VISIBILITY=\"<%= $visibility %>\">");
381 }
382 </SCRIPT>
383
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="">
394 <% } %>
395
396 <%
397 if ( $cgi->param('clone') ) {
398   print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
399 }
400 if ( $cgi->param('pkgnum') ) {
401   print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
402 }
403 %>
404
405 <INPUT TYPE="hidden" NAME="pkgpart" VALUE="<%= $hashref->{pkgpart} %>">
406 <%= ntable("#cccccc",2) %>
407
408 <% my $href = $plans{$layer}->{'fields'};
409    foreach my $field ( exists($plans{$layer}->{'fieldorder'})
410                          ? @{$plans{$layer}->{'fieldorder'}}
411                          : keys %{ $href }
412                      ) {
413 %>
414   <TR><TD ALIGN="right"><%= $href->{$field}{'name'} %></TD>
415   <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'}) %>
423        <% } %>
424        </SELECT>
425   <% } %>
426   </TD></TR>
427 <% } %>
428
429 </TABLE>
430 <INPUT TYPE="hidden" NAME="plandata" VALUE="<%= join(',', keys %{ $href } ) %>">
431 <BR><BR>
432
433 <%
434 print qq!<INPUT TYPE="submit" VALUE="!,
435       $hashref->{pkgpart} ? "Apply changes" : "Add package",
436       qq!" onClick="fchanged(this)">!;
437 %>
438
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>
442 </TR></TD>
443 </TABLE>
444
445 </FORM>
446
447 <SCRIPT>
448 if (document.getElementById) {
449   document.write("</DIV>");
450 } else {
451   document.write("</LAYER>");
452 }
453 </SCRIPT>
454
455 <% } %>
456
457 <TAG onLoad="
458     if (document.getElementById) {
459       document.getElementById('d<%= $part_pkg->plan %>').style.visibility = 'visible';
460     } else {
461       document.l<%= $part_pkg->plan %>.visibility = 'visible';
462     }
463 ">
464   </BODY>
465 </HTML>