removed <!-- $Id$ --> from all files to fix any redirects, whew
[freeside.git] / httemplate / edit / part_pkg.cgi
1 <%
2
3 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {
4   $cgi->param('clone', $1);
5 } else {
6   $cgi->param('clone', '');
7 }
8 if ( $cgi->param('pkgnum') && $cgi->param('pkgnum') =~ /^(\d+)$/ ) {
9   $cgi->param('pkgnum', $1);
10 } else {
11   $cgi->param('pkgnum', '');
12 }
13
14 my ($query) = $cgi->keywords;
15 my $action = '';
16 my $part_pkg = '';
17 if ( $cgi->param('error') ) {
18   $part_pkg = new FS::part_pkg ( {
19     map { $_, scalar($cgi->param($_)) } fields('part_pkg')
20   } );
21 }
22 if ( $cgi->param('clone') ) {
23   $action='Custom Pricing';
24   my $old_part_pkg =
25     qsearchs('part_pkg', { 'pkgpart' => $cgi->param('clone') } );
26   $part_pkg ||= $old_part_pkg->clone;
27   $part_pkg->disabled('Y');
28 } elsif ( $query && $query =~ /^(\d+)$/ ) {
29   $part_pkg ||= qsearchs('part_pkg',{'pkgpart'=>$1});
30 } else {
31   $part_pkg ||= new FS::part_pkg {};
32   $part_pkg->plan('flat');
33 }
34 unless ( $part_pkg->plan ) { #backwards-compat
35   $part_pkg->plan('flat');
36   $part_pkg->plandata("setup_fee=". $part_pkg->setup. "\n".
37                       "recur_fee=". $part_pkg->recur. "\n");
38 }
39 $action ||= $part_pkg->pkgpart ? 'Edit' : 'Add';
40 my $hashref = $part_pkg->hashref;
41
42 %>
43
44 <SCRIPT>
45 function visualize(what) {
46   if (document.getElementById) {
47     document.getElementById('d<%= $part_pkg->plan %>').style.visibility = "visible";
48   } else {
49     document.l<%= $part_pkg->plan %>.visibility = "visible";
50   }
51 }
52 </SCRIPT>
53
54 <% 
55
56 print header("$action Package Definition", menubar(
57   'Main Menu' => popurl(2),
58   'View all packages' => popurl(2). 'browse/part_pkg.cgi',
59 ), ' onLoad="visualize()"');
60
61 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
62       "</FONT>"
63   if $cgi->param('error');
64
65 #print '<FORM ACTION="', popurl(1), 'process/part_pkg.cgi" METHOD=POST>';
66 print '<FORM NAME="dummy">';
67
68 #if ( $cgi->param('clone') ) {
69 #  print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
70 #}
71 #if ( $cgi->param('pkgnum') ) {
72 #  print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
73 #}
74 #
75 #print qq!<INPUT TYPE="hidden" NAME="pkgpart" VALUE="$hashref->{pkgpart}">!,
76 print "Package Part #", $hashref->{pkgpart} ? $hashref->{pkgpart} : "(NEW)";
77
78 print ntable("#cccccc",2), <<END;
79 <TR><TD ALIGN="right">Package (customer-visable)</TD><TD><INPUT TYPE="text" NAME="pkg" SIZE=32 VALUE="$hashref->{pkg}"></TD></TR>
80 <TR><TD ALIGN="right">Comment (customer-hidden)</TD><TD><INPUT TYPE="text" NAME="comment" SIZE=32 VALUE="$hashref->{comment}"></TD></TR>
81 <TR><TD ALIGN="right">Frequency (months) of recurring fee</TD><TD><INPUT TYPE="text" NAME="freq" VALUE="$hashref->{freq}" SIZE=3></TD></TR>
82 <TR><TD ALIGN="right">Setup fee tax exempt</TD><TD>
83 END
84
85 print '<INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y"';
86 print ' CHECKED' if $hashref->{setuptax} eq "Y";
87 print '>';
88
89 print <<END;
90 </TD></TR>
91 <TR><TD ALIGN="right">Recurring fee tax exempt</TD><TD>
92 END
93
94 print '<INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y"';
95 print ' CHECKED' if $hashref->{recurtax} eq "Y";
96 print '>';
97
98 print '</TD></TR>';
99
100 print '<TR><TD ALIGN="right">Disable new orders</TD><TD>';
101 print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
102 print ' CHECKED' if $hashref->{disabled} eq "Y";
103 print '>';
104 print '</TD></TR></TABLE>';
105
106 my $thead =  "\n\n". ntable('#cccccc', 2). <<END;
107 <TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH><TH BGCOLOR="#dcdcdc">Service</TH></TR>
108 END
109
110 unless ( $cgi->param('clone') ) {
111   #print <<END, $thead;
112   print <<END, itable(), '<TR><TD VALIGN="top">', $thead;
113 <BR><BR>Enter the quantity of each service this package includes.<BR><BR>
114 END
115 }
116
117 my @fixups = ();
118 my $count = 0;
119 my $columns = 3;
120 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
121 foreach my $part_svc ( @part_svc ) {
122   my $svcpart = $part_svc->svcpart;
123   my $pkg_svc = qsearchs( 'pkg_svc', {
124     'pkgpart'  => $cgi->param('clone') || $part_pkg->pkgpart,
125     'svcpart'  => $svcpart,
126   } ) || new FS::pkg_svc ( {
127     'pkgpart'  => $cgi->param('clone') || $part_pkg->pkgpart,
128     'svcpart'  => $svcpart,
129     'quantity' => 0,
130   });
131   #? #next unless $pkg_svc;
132
133   push @fixups, "pkg_svc$svcpart";
134
135   unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
136     print '<TR>'; # if $count == 0 ;
137     print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
138           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
139           qq!"></TD><TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
140           qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
141 #    print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
142     $count+=1;
143     foreach ( 1 .. $columns-1 ) {
144       print "</TABLE></TD><TD VALIGN=\"top\">$thead"
145         if $count == int( $_ * scalar(@part_svc) / $columns );
146     }
147   } else {
148     print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
149           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
150   }
151 }
152
153 unless ( $cgi->param('clone') ) {
154   print "</TR></TABLE></TD></TR></TABLE>";
155   #print "</TR></TABLE>";
156 }
157
158 # prolly should be in database
159 use Tie::IxHash;
160 tie my %plans, 'Tie::IxHash',
161   'flat' => {
162     'name' => 'Flat rate',
163     'fields' => {
164       'setup_fee' => { 'name' => 'Setup fee for this package',
165                        'default' => 0,
166                      },
167       'recur_fee' => { 'name' => 'Recurring fee for this package',
168                        'default' => 0,
169                       },
170     },
171     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
172     'setup' => 'what.setup_fee.value',
173     'recur' => 'what.recur_fee.value',
174   },
175
176   'prorate' => {
177     'name' => 'First month pro-rated, then flat-rate',
178     'fields' =>  {
179       'setup_fee' => { 'name' => 'Setup fee for this package',
180                        'default' => 0,
181                      },
182       'recur_fee' => { 'name' => 'Recurring fee for this package',
183                        'default' => 0,
184                       },
185     },
186     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
187     'setup' => 'what.setup_fee.value',
188     '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) ; \'',
189   },
190
191   'flat_comission_cust' => {
192     'name' => 'Flat rate with recurring comission per active customer',
193     'fields' => {
194       'setup_fee' => { 'name' => 'Setup fee for this package',
195                        'default' => 0,
196                      },
197       'recur_fee' => { 'name' => 'Recurring fee for this package',
198                        'default' => 0,
199                      },
200       'comission_amount' => { 'name' => 'Comission amount per month (per active customer)',
201                               'default' => 0,
202                             },
203       'comission_depth'  => { 'name' => 'Number of layers',
204                               'default' => 1,
205                             },
206     },
207     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
208     'setup' => 'what.setup_fee.value',
209     '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 + \';\'',
210   },
211
212   'flat_comission' => {
213     'name' => 'Flat rate with recurring comission per active package',
214     'fields' => {
215       'setup_fee' => { 'name' => 'Setup fee for this package',
216                        'default' => 0,
217                      },
218       'recur_fee' => { 'name' => 'Recurring fee for this package',
219                        'default' => 0,
220                      },
221       'comission_amount' => { 'name' => 'Comission amount per month (per active package)',
222                               'default' => 0,
223                             },
224       'comission_depth'  => { 'name' => 'Number of layers',
225                               'default' => 1,
226                             },
227     },
228     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
229     'setup' => 'what.setup_fee.value',
230     '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 + \';\'',
231   },
232
233   'flat_comission_pkg' => {
234     'name' => 'Flat rate with recurring comission per active (selected) package',
235     'fields' => {
236       'setup_fee' => { 'name' => 'Setup fee for this package',
237                        'default' => 0,
238                      },
239       'recur_fee' => { 'name' => 'Recurring fee for this package',
240                        'default' => 0,
241                      },
242       'comission_amount' => { 'name' => 'Comission amount per month (per uncancelled package)',
243                               'default' => 0,
244                             },
245       'comission_depth'  => { 'name' => 'Number of layers',
246                               'default' => 1,
247                             },
248       'comission_pkgpart' => { 'name' => 'Applicable packages<BR><FONT SIZE="-1">(hold <b>ctrl</b> to select multiple packages)</FONT>',
249                                'type' => 'select_multiple',
250                                'select_table' => 'part_pkg',
251                                'select_hash'  => { 'disabled' => '' } ,
252                                'select_key'   => 'pkgpart',
253                                'select_label' => 'pkg',
254                              },
255     },
256     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount', 'comission_pkgpart' ],
257     'setup' => 'what.setup_fee.value',
258     '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 + \';\'',
259   },
260
261
262
263   'sesmon_hour' => {
264     'name' => 'Base charge plus charge per-hour from the session monitor',
265     'fields' => {
266       'setup_fee' => { 'name' => 'Setup fee for this package',
267                        'default' => 0,
268                      },
269       'recur_flat' => { 'name' => 'Base monthly charge for this package',
270                         'default' => 0,
271                       },
272       'recur_included_hours' => { 'name' => 'Hours included',
273                                   'default' => 0,
274                                 },
275       'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
276                                  'default' => 0,
277                                },
278     },
279     'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
280     'setup' => 'what.setup_fee.value',
281     '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;\'',
282   },
283
284   'sesmon_minute' => {
285     'name' => 'Base charge plus charge per-minute from the session monitor',
286     'fields' => {
287       'setup_fee' => { 'name' => 'Setup fee for this package',
288                        'default' => 0,
289                      },
290       'recur_flat' => { 'name' => 'Base monthly charge for this package',
291                         'default' => 0,
292                       },
293       'recur_included_min' => { 'name' => 'Minutes included',
294                                 'default' => 0,
295                                 },
296       'recur_minly_charge' => { 'name' => 'Additional charge per minute',
297                                 'default' => 0,
298                               },
299     },
300     'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_min', 'recur_minly_charge' ],
301     'setup' => 'what.setup_fee.value',
302     '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;\'',
303
304   },
305
306 ;
307
308 %>
309
310 <SCRIPT>
311 var layer = null;
312
313 function changed(what) {
314   layer = what.options[what.selectedIndex].value;
315 <% foreach my $layer ( keys %plans ) { %>
316   if (layer == "<%= $layer %>" ) {
317     <% foreach my $not ( grep { $_ ne $layer } keys %plans ) { %>
318       if (document.getElementById) {
319         document.getElementById('d<%= $not %>').style.visibility = "hidden";
320       } else {
321         document.l<%= $not %>.visibility = "hidden";
322       }
323     <% } %>
324     if (document.getElementById) {
325       document.getElementById('d<%= $layer %>').style.visibility = "visible";
326     } else {
327       document.l<%= $layer %>.visibility = "visible";
328     }
329   }
330 <% } %>
331 }
332
333 </SCRIPT>
334 <BR>
335 Price plan <SELECT NAME="plan" SIZE=1 onChange="changed(this);">
336 <OPTION>
337 <% foreach my $layer (keys %plans ) { %>
338 <OPTION VALUE="<%= $layer %>"<%= ' SELECTED'x($layer eq $part_pkg->plan) %>><%= $plans{$layer}->{'name'} %>
339 <% } %>
340 </SELECT></FORM>
341
342 <SCRIPT>
343 function fchanged(what) {
344   fixup(what.form);
345 }
346
347 function fixup(what) {
348 <% foreach my $f ( qw( pkg comment freq ), @fixups ) { %>
349   what.<%= $f %>.value = document.dummy.<%= $f %>.value;
350 <% } %>
351 <% foreach my $f ( qw( setuptax recurtax disabled ) ) { %>
352   if (document.dummy.<%= $f %>.checked)
353     what.<%= $f %>.value = 'Y';
354   else
355     what.<%= $f %>.value = '';
356 <% } %>
357   what.plan.value = document.dummy.plan.options[document.dummy.plan.selectedIndex].value;
358 <% foreach my $p ( keys %plans ) { %>
359   if ( what.plan.value == "<%= $p %>" ) {
360     what.setup.value = <%= $plans{$p}->{setup} %>;
361     what.recur.value = <%= $plans{$p}->{recur} %>;
362   }
363 <% } %>
364 }
365 </SCRIPT>
366
367 <% my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
368                     split("\n", $part_pkg->plandata );
369    #foreach my $layer ( 'konq_kludge', keys %plans ) { 
370    foreach my $layer ( 'konq_kludge', keys %plans ) {
371      my $visibility = "hidden";
372 %>
373 <SCRIPT>
374 if (document.getElementById) {
375     document.write("<DIV ID=\"d<%= $layer %>\" STYLE=\"visibility: <%= $visibility %>; position: absolute\">");
376 } else {
377 <% $visibility="show" if $visibility eq "visible"; %>
378     document.write("<LAYER ID=\"l<%= $layer %>\" VISIBILITY=\"<%= $visibility %>\">");
379 }
380 </SCRIPT>
381
382 <FORM NAME="<%= $layer %>" ACTION="process/part_pkg.cgi" METHOD=POST onSubmit="fixup(this)">
383 <INPUT TYPE="hidden" NAME="plan" VALUE="<%= $part_pkg->plan %>">
384 <INPUT TYPE="hidden" NAME="pkg" VALUE="<%= $hashref->{pkg} %>">
385 <INPUT TYPE="hidden" NAME="comment" VALUE="$<%= $hashref->{comment} %>">
386 <INPUT TYPE="hidden" NAME="freq" VALUE="<%= $hashref->{freq} %>">
387 <INPUT TYPE="hidden" NAME="setuptax" VALUE="<%= $hashref->{setuptax} %>">
388 <INPUT TYPE="hidden" NAME="recurtax" VALUE="<%= $hashref->{recurtax} %>">
389 <INPUT TYPE="hidden" NAME="disabled" VALUE="<%= $hashref->{disabled} %>">
390 <% foreach my $f ( @fixups ) { %>
391 <INPUT TYPE="hidden" NAME="<%= $f %>" VALUE="">
392 <% } %>
393
394 <%
395 if ( $cgi->param('clone') ) {
396   print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
397 }
398 if ( $cgi->param('pkgnum') ) {
399   print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
400 }
401 %>
402
403 <INPUT TYPE="hidden" NAME="pkgpart" VALUE="<%= $hashref->{pkgpart} %>">
404 <%= ntable("#cccccc",2) %>
405
406 <% my $href = $plans{$layer}->{'fields'};
407    foreach my $field ( exists($plans{$layer}->{'fieldorder'})
408                          ? @{$plans{$layer}->{'fieldorder'}}
409                          : keys %{ $href }
410                      ) {
411 %>
412   <TR><TD ALIGN="right"><%= $href->{$field}{'name'} %></TD>
413   <TD>
414   <% if ( ! exists($href->{$field}{'type'}) ) { %>
415        <INPUT TYPE="text" NAME="<%= $field %>" VALUE="<%= exists($plandata{$field}) ? $plandata{$field} : $href->{$field}{'default'} %>" onChange="fchanged(this)">
416   <% } elsif ( $href->{$field}{'type'} eq 'select_multiple' ) { %>
417        <SELECT MULTIPLE NAME="<%= $field %>" onChange="fchanged(this)">
418        <% foreach my $record ( qsearch( $href->{$field}{'select_table'}, $href->{$field}{'select_hash'} ) ) {
419           my $value = $record->getfield($href->{$field}{'select_key'}); %>
420          <OPTION VALUE="<%= $value %>"<%= $plandata{$field} =~ /(^|, *)$value *(,|$)/ ? ' SELECTED' : '' %>><%= $record->getfield($href->{$field}{'select_label'}) %>
421        <% } %>
422        </SELECT>
423   <% } %>
424   </TD></TR>
425 <% } %>
426
427 </TABLE>
428 <INPUT TYPE="hidden" NAME="plandata" VALUE="<%= join(',', keys %{ $href } ) %>">
429 <BR><BR>
430
431 <%
432 print qq!<INPUT TYPE="submit" VALUE="!,
433       $hashref->{pkgpart} ? "Apply changes" : "Add package",
434       qq!" onClick="fchanged(this)">!;
435 %>
436
437 <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>
438 <FONT SIZE="1">Setup expression<BR><INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="<%= $hashref->{setup} %>" onLoad="fchanged(this)"></FONT><BR>
439 <FONT SIZE="1">Recurring espression<BR><INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="<%= $hashref->{recur} %>" onLoad="fchanged(this)"></FONT>
440 </TR></TD>
441 </TABLE>
442
443 </FORM>
444
445 <SCRIPT>
446 if (document.getElementById) {
447   document.write("</DIV>");
448 } else {
449   document.write("</LAYER>");
450 }
451 </SCRIPT>
452
453 <% } %>
454
455 <TAG onLoad="
456     if (document.getElementById) {
457       document.getElementById('d<%= $part_pkg->plan %>').style.visibility = 'visible';
458     } else {
459       document.l<%= $part_pkg->plan %>.visibility = 'visible';
460     }
461 ">
462   </BODY>
463 </HTML>