service and package disable!
[freeside.git] / httemplate / edit / part_pkg.cgi
1 <!-- $Id: part_pkg.cgi,v 1.7 2001-12-27 09:26:14 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 } 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 my %plans = (
161
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     'setup' => 'what.setup_fee.value',
173     'recur' => 'what.recur_fee.value',
174   },
175
176   'flat_comission' => {
177     'name' => 'Flat rate with recurring referral comission as credit',
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       'comission_amount' => { 'name' => 'Comission amount',
186                               'default' => 0,
187                             },
188       'comission_depth'  => { 'name' => 'Number of layers',
189                               'default' => 1,
190                             },
191     },
192     'setup' => 'what.setup_fee.value',
193     '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 + \';\'',
194   },
195
196 );
197
198 %>
199
200 <SCRIPT>
201 var layer = null;
202
203 function changed(what) {
204   layer = what.options[what.selectedIndex].value;
205 <% foreach my $layer ( keys %plans ) { %>
206   if (layer == "<%= $layer %>" ) {
207     <% foreach my $not ( grep { $_ ne $layer } keys %plans ) { %>
208       if (document.getElementById) {
209         document.getElementById('d<%= $not %>').style.visibility = "hidden";
210       } else {
211         document.l<%= $not %>.visibility = "hidden";
212       }
213     <% } %>
214     if (document.getElementById) {
215       document.getElementById('d<%= $layer %>').style.visibility = "visible";
216     } else {
217       document.l<%= $layer %>.visibility = "visible";
218     }
219   }
220 <% } %>
221 }
222
223 </SCRIPT>
224 <BR>
225 Price plan <SELECT NAME="plan" SIZE=1 onChange="changed(this);">
226 <OPTION>
227 <% foreach my $layer (keys %plans ) { %>
228 <OPTION VALUE="<%= $layer %>"<%= ' SELECTED'x($layer eq $part_pkg->plan) %>><%= $plans{$layer}->{'name'} %>
229 <% } %>
230 </SELECT></FORM>
231
232 <SCRIPT>
233 function fchanged(what) {
234   fixup(what.form);
235 }
236
237 function fixup(what) {
238 <% foreach my $f ( qw( pkg comment freq ), @fixups ) { %>
239   what.<%= $f %>.value = document.dummy.<%= $f %>.value;
240 <% } %>
241 <% foreach my $f ( qw( setuptax recurtax disabled ) ) { %>
242   if (document.dummy.<%= $f %>.checked)
243     what.<%= $f %>.value = 'Y';
244   else
245     what.<%= $f %>.value = '';
246 <% } %>
247   what.plan.value = document.dummy.plan.options[document.dummy.plan.selectedIndex].value;
248 <% foreach my $p ( keys %plans ) { %>
249   if ( what.plan.value == "<%= $p %>" ) {
250     what.setup.value = <%= $plans{$p}->{setup} %>;
251     what.recur.value = <%= $plans{$p}->{recur} %>;
252   }
253 <% } %>
254 }
255 </SCRIPT>
256
257 <% my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
258                     split("\n", $part_pkg->plandata );
259    #foreach my $layer ( 'konq_kludge', keys %plans ) { 
260    foreach my $layer ( 'konq_kludge', keys %plans ) {
261      my $visibility = "hidden";
262 %>
263 <SCRIPT>
264 if (document.getElementById) {
265     document.write("<DIV ID=\"d<%= $layer %>\" STYLE=\"visibility: <%= $visibility %>; position: absolute\">");
266 } else {
267 <% $visibility="show" if $visibility eq "visible"; %>
268     document.write("<LAYER ID=\"l<%= $layer %>\" VISIBILITY=\"<%= $visibility %>\">");
269 }
270 </SCRIPT>
271
272 <FORM NAME="<%= $layer %>" ACTION="process/part_pkg.cgi" METHOD=POST onSubmit="fixup(this)">
273 <INPUT TYPE="hidden" NAME="plan" VALUE="<%= $part_pkg->plan %>">
274 <INPUT TYPE="hidden" NAME="pkg" VALUE="<%= $hashref->{pkg} %>">
275 <INPUT TYPE="hidden" NAME="comment" VALUE="$<%= $hashref->{comment} %>">
276 <INPUT TYPE="hidden" NAME="freq" VALUE="<%= $hashref->{freq} %>">
277 <INPUT TYPE="hidden" NAME="setuptax" VALUE="<%= $hashref->{setuptax} %>">
278 <INPUT TYPE="hidden" NAME="recurtax" VALUE="<%= $hashref->{recurtax} %>">
279 <INPUT TYPE="hidden" NAME="disabled" VALUE="<%= $hashref->{disabled} %>">
280 <% foreach my $f ( @fixups ) { %>
281 <INPUT TYPE="hidden" NAME="<%= $f %>" VALUE="">
282 <% } %>
283
284 <%
285 if ( $cgi->param('clone') ) {
286   print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
287 }
288 if ( $cgi->param('pkgnum') ) {
289   print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
290 }
291 %>
292
293 <INPUT TYPE="hidden" NAME="pkgpart" VALUE="<%= $hashref->{pkgpart} %>">
294 <%= ntable("#cccccc",2) %>
295
296 <% my $href = $plans{$layer}->{'fields'};
297    foreach my $field ( keys %{ $href } ) { %>
298 <TR><TD ALIGN="right"><%= $href->{$field}{'name'} %></TD>
299 <TD><INPUT TYPE="text" NAME="<%= $field %>" VALUE="<%= exists($plandata{$field}) ? $plandata{$field} : $href->{$field}{'default'} %>" onChange="fchanged(this)"></TD></TR>
300 <% } %>
301 </TABLE>
302 <INPUT TYPE="hidden" NAME="plandata" VALUE="<%= join(',', keys %{ $href } ) %>">
303 <FONT SIZE="1">
304 <BR><BR>
305 Setup expression<BR><INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="<%= $hashref->{setup} %>" onLoad="fchanged(this)"><BR>
306 Recurring espression<BR><INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="<%= $hashref->{recur} %>" onLoad="fchanged(this)"><BR>
307 </FONT>
308
309 <%
310 print qq!<BR><INPUT TYPE="submit" VALUE="!,
311       $hashref->{pkgpart} ? "Apply changes" : "Add package",
312       qq!" onClick="fchanged(this)">!;
313 %>
314
315 </FORM>
316
317 <SCRIPT>
318 if (document.getElementById) {
319   document.write("</DIV>");
320 } else {
321   document.write("</LAYER>");
322 }
323 </SCRIPT>
324
325 <% } %>
326
327 <TAG onLoad="
328     if (document.getElementById) {
329       document.getElementById('d<%= $part_pkg->plan %>').style.visibility = 'visible';
330     } else {
331       document.l<%= $part_pkg->plan %>.visibility = 'visible';
332     }
333 ">
334   </BODY>
335 </HTML>