fee0b411ea3301f2d7b27bfab6e36588a867d12e
[freeside.git] / httemplate / edit / part_pkg.cgi
1 <!-- $Id: part_pkg.cgi,v 1.6 2001-10-20 12:17:59 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 itable("#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></TABLE>';
100
101 my $thead =  "\n\n". ntable('#cccccc', 2). <<END;
102 <TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH><TH BGCOLOR="#dcdcdc">Service</TH></TR>
103 END
104
105 unless ( $cgi->param('clone') ) {
106   #print <<END, $thead;
107   print <<END, itable(), '<TR><TD VALIGN="top">', $thead;
108 <BR><BR>Enter the quantity of each service this package includes.<BR><BR>
109 END
110 }
111
112 my @fixups = ();
113 my $count = 0;
114 my $columns = 3;
115 my @part_svc = qsearch( 'part_svc', {} );
116 foreach my $part_svc ( @part_svc ) {
117   my $svcpart = $part_svc->svcpart;
118   my $pkg_svc = qsearchs( 'pkg_svc', {
119     'pkgpart'  => $cgi->param('clone') || $part_pkg->pkgpart,
120     'svcpart'  => $svcpart,
121   } ) || new FS::pkg_svc ( {
122     'pkgpart'  => $cgi->param('clone') || $part_pkg->pkgpart,
123     'svcpart'  => $svcpart,
124     'quantity' => 0,
125   });
126   #? #next unless $pkg_svc;
127
128   push @fixups, "pkg_svc$svcpart";
129
130   unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
131     print '<TR>'; # if $count == 0 ;
132     print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
133           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
134           qq!"></TD><TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
135           qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
136 #    print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
137     $count+=1;
138     foreach ( 1 .. $columns-1 ) {
139       print "</TABLE></TD><TD VALIGN=\"top\">$thead"
140         if $count == int( $_ * scalar(@part_svc) / $columns );
141     }
142   } else {
143     print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
144           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
145   }
146 }
147
148 unless ( $cgi->param('clone') ) {
149   print "</TR></TABLE></TD></TR></TABLE>";
150   #print "</TR></TABLE>";
151 }
152
153 # prolly should be in database
154 my %plans = (
155
156   'flat' => {
157     'name' => 'Flat rate',
158     'fields' => {
159       'setup_fee' => { 'name' => 'Setup fee for this package',
160                        'default' => 0,
161                      },
162       'recur_fee' => { 'name' => 'Recurring fee for this package',
163                        'default' => 0,
164                       },
165     },
166     'setup' => 'what.setup_fee.value',
167     'recur' => 'what.recur_fee.value',
168   },
169
170   'flat_comission' => {
171     'name' => 'Flat rate with recurring referral comission as credit',
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       'comission_amount' => { 'name' => 'Comission amount',
180                               'default' => 0,
181                             },
182       'comission_depth'  => { 'name' => 'Number of layers',
183                               'default' => 1,
184                             },
185     },
186     'setup' => 'what.setup_fee.value',
187     '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 + \';\'',
188   },
189
190 );
191
192 %>
193
194 <SCRIPT>
195 var layer = null;
196
197 function changed(what) {
198   layer = what.options[what.selectedIndex].value;
199 <% foreach my $layer ( keys %plans ) { %>
200   if (layer == "<%= $layer %>" ) {
201     <% foreach my $not ( grep { $_ ne $layer } keys %plans ) { %>
202       if (document.getElementById) {
203         document.getElementById('d<%= $not %>').style.visibility = "hidden";
204       } else {
205         document.l<%= $not %>.visibility = "hidden";
206       }
207     <% } %>
208     if (document.getElementById) {
209       document.getElementById('d<%= $layer %>').style.visibility = "visible";
210     } else {
211       document.l<%= $layer %>.visibility = "visible";
212     }
213   }
214 <% } %>
215 }
216
217 </SCRIPT>
218 <BR>
219 Price plan <SELECT NAME="plan" SIZE=1 onChange="changed(this);">
220 <OPTION>
221 <% foreach my $layer (keys %plans ) { %>
222 <OPTION VALUE="<%= $layer %>"<%= ' SELECTED'x($layer eq $part_pkg->plan) %>><%= $plans{$layer}->{'name'} %>
223 <% } %>
224 </SELECT></FORM>
225
226 <SCRIPT>
227 function fchanged(what) {
228   fixup(what.form);
229 }
230
231 function fixup(what) {
232 <% foreach my $f ( qw( pkg comment freq ), @fixups ) { %>
233   what.<%= $f %>.value = document.dummy.<%= $f %>.value;
234 <% } %>
235 <% foreach my $f ( qw( setuptax recurtax ) ) { %>
236   if (document.dummy.<%= $f %>.checked)
237     what.<%= $f %>.value = 'Y';
238   else
239     what.<%= $f %>.value = '';
240 <% } %>
241   what.plan.value = document.dummy.plan.options[document.dummy.plan.selectedIndex].value;
242 <% foreach my $p ( keys %plans ) { %>
243   if ( what.plan.value == "<%= $p %>" ) {
244     what.setup.value = <%= $plans{$p}->{setup} %>;
245     what.recur.value = <%= $plans{$p}->{recur} %>;
246   }
247 <% } %>
248 }
249 </SCRIPT>
250
251 <% my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
252                     split("\n", $part_pkg->plandata );
253    #foreach my $layer ( 'konq_kludge', keys %plans ) { 
254    foreach my $layer ( 'konq_kludge', keys %plans ) {
255      my $visibility = "hidden";
256 %>
257 <SCRIPT>
258 if (document.getElementById) {
259     document.write("<DIV ID=\"d<%= $layer %>\" STYLE=\"visibility: <%= $visibility %>; position: absolute\">");
260 } else {
261 <% $visibility="show" if $visibility eq "visible"; %>
262     document.write("<LAYER ID=\"l<%= $layer %>\" VISIBILITY=\"<%= $visibility %>\">");
263 }
264 </SCRIPT>
265
266 <FORM NAME="<%= $layer %>" ACTION="process/part_pkg.cgi" METHOD=POST onSubmit="fixup(this)">
267 <INPUT TYPE="hidden" NAME="plan" VALUE="<%= $part_pkg->plan %>">
268 <INPUT TYPE="hidden" NAME="pkg" VALUE="<%= $hashref->{pkg} %>">
269 <INPUT TYPE="hidden" NAME="comment" VALUE="$<%= $hashref->{comment} %>">
270 <INPUT TYPE="hidden" NAME="freq" VALUE="<%= $hashref->{freq} %>">
271 <INPUT TYPE="hidden" NAME="setuptax" VALUE="<%= $hashref->{setuptax} %>">
272 <INPUT TYPE="hidden" NAME="recurtax" VALUE="<%= $hashref->{recurtax} %>">
273 <% foreach my $f ( @fixups ) { %>
274 <INPUT TYPE="hidden" NAME="<%= $f %>" VALUE="">
275 <% } %>
276
277 <%
278 if ( $cgi->param('clone') ) {
279   print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
280 }
281 if ( $cgi->param('pkgnum') ) {
282   print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
283 }
284 %>
285
286 <INPUT TYPE="hidden" NAME="pkgpart" VALUE="<%= $hashref->{pkgpart} %>">
287 <%= itable("#cccccc",2) %>
288
289 <% my $href = $plans{$layer}->{'fields'};
290    foreach my $field ( keys %{ $href } ) { %>
291 <TR><TD ALIGN="right"><%= $href->{$field}{'name'} %></TD>
292 <TD><INPUT TYPE="text" NAME="<%= $field %>" VALUE="<%= exists($plandata{$field}) ? $plandata{$field} : $href->{$field}{'default'} %>" onChange="fchanged(this)"></TD></TR>
293 <% } %>
294 </TABLE>
295 <INPUT TYPE="hidden" NAME="plandata" VALUE="<%= join(',', keys %{ $href } ) %>">
296 <FONT SIZE="1">
297 <BR><BR>
298 Setup expression<BR><INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="<%= $hashref->{setup} %>" onLoad="fchanged(this)"><BR>
299 Recurring espression<BR><INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="<%= $hashref->{recur} %>" onLoad="fchanged(this)"><BR>
300 </FONT>
301
302 <%
303 print qq!<BR><INPUT TYPE="submit" VALUE="!,
304       $hashref->{pkgpart} ? "Apply changes" : "Add package",
305       qq!" onClick="fchanged(this)">!;
306 %>
307
308 </FORM>
309
310 <SCRIPT>
311 if (document.getElementById) {
312   document.write("</DIV>");
313 } else {
314   document.write("</LAYER>");
315 }
316 </SCRIPT>
317
318 <% } %>
319
320 <TAG onLoad="
321     if (document.getElementById) {
322       document.getElementById('d<%= $part_pkg->plan %>').style.visibility = 'visible';
323     } else {
324       document.l<%= $part_pkg->plan %>.visibility = 'visible';
325     }
326 ">
327   </BODY>
328 </HTML>