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