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