first pass at VoIP rating
[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 my $clone_part_pkg = '';
24 if ( $cgi->param('clone') ) {
25   $action = 'Custom Pricing';
26   $clone_part_pkg= qsearchs('part_pkg', { 'pkgpart' => $cgi->param('clone') } );
27   $part_pkg ||= $clone_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 <%= header("$action Package Definition", menubar(
48   'Main Menu' => popurl(2),
49   'View all packages' => popurl(2). 'browse/part_pkg.cgi',
50 )) %>
51
52 <% #), ' onLoad="visualize()"'); %>
53
54 <% if ( $cgi->param('error') ) { %>
55   <FONT SIZE="+1" COLOR="#ff0000">Error: <%= $cgi->param('error') %></FONT>
56 <% } %>
57
58 <% #print '<FORM ACTION="', popurl(1), 'process/part_pkg.cgi" METHOD=POST>'; %>
59
60 <FORM NAME="dummy">
61
62 <%
63 #if ( $cgi->param('clone') ) {
64 #  print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
65 #}
66 #if ( $cgi->param('pkgnum') ) {
67 #  print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
68 #}
69 #
70 #print qq!<INPUT TYPE="hidden" NAME="pkgpart" VALUE="$hashref->{pkgpart}">!,
71 %>
72
73 <%= itable('',8,1) %><TR><TD VALIGN="top">
74
75 Package information
76
77 <%= ntable("#cccccc",2) %>
78   <TR>
79     <TD ALIGN="right">Package Definition #</TD>
80     <TD BGCOLOR="#ffffff">
81       <%= $hashref->{pkgpart} ? $hashref->{pkgpart} : "(NEW)" %>
82     </TD>
83   </TR>
84   <TR>
85     <TD ALIGN="right">Package (customer-visible)</TD>
86     <TD>
87       <INPUT TYPE="text" NAME="pkg" SIZE=32 VALUE="<%= $part_pkg->pkg %>">
88     </TD>
89   </TR>
90   <TR>
91     <TD ALIGN="right">Comment (customer-hidden)</TD>
92     <TD>
93       <INPUT TYPE="text" NAME="comment" SIZE=32 VALUE="<%=$part_pkg->comment%>">
94     </TD>
95   </TR>
96
97   <TR>
98     <TD ALIGN="right">Disable new orders</TD>
99     <TD>
100       <INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"<%= $hashref->{disabled} eq 'Y' ? ' CHECKED' : '' %>
101     </TD>
102   </TR>
103
104 </TABLE>
105
106 </TD><TD VALIGN="top">
107
108 Tax information
109 <%= ntable("#cccccc", 2) %>
110   <TR>
111     <TD ALIGN="right">Setup fee tax exempt</TD>
112     <TD>
113 <%
114
115 print '<INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y"';
116 print ' CHECKED' if $hashref->{setuptax} eq "Y";
117 print '>';
118
119 print <<END;
120 </TD></TR>
121 <TR><TD ALIGN="right">Recurring fee tax exempt</TD><TD>
122 END
123
124 print '<INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y"';
125 print ' CHECKED' if $hashref->{recurtax} eq "Y";
126 print '>';
127
128 print '</TD></TR>';
129
130 my $conf = new FS::Conf;
131 #false laziness w/ view/cust_main.cgi quick order
132 if ( $conf->exists('enable_taxclasses') ) {
133   print '<TR><TD ALIGN="right">Tax class</TD><TD><SELECT NAME="taxclass">';
134   my $sth = dbh->prepare('SELECT DISTINCT taxclass FROM cust_main_county')
135     or die dbh->errstr;
136   $sth->execute or die $sth->errstr;
137   foreach my $taxclass ( map $_->[0], @{$sth->fetchall_arrayref} ) {
138     print qq!<OPTION VALUE="$taxclass"!;
139     print ' SELECTED' if $taxclass eq $hashref->{taxclass};
140     print qq!>$taxclass</OPTION>!;
141   }
142   print '</SELECT></TD></TR>';
143 } else {
144   print
145     '<INPUT TYPE="hidden" NAME="taxclass" VALUE="'. $hashref->{taxclass}. '">';
146 }
147
148 %>
149
150 </TABLE>
151
152 </TD></TR></TABLE>
153
154 <%
155
156 my $thead =  "\n\n". ntable('#cccccc', 2).
157              '<TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH>';
158 $thead .=  '<TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Primary</FONT></TH>'
159   if dbdef->table('pkg_svc')->column('primary_svc');
160 $thead .= '<TH BGCOLOR="#dcdcdc">Service</TH></TR>';
161
162 #unless ( $cgi->param('clone') ) {
163 #dunno why...
164 unless ( 0 ) {
165   #print <<END, $thead;
166   print <<END, itable('', 4, 1), '<TR><TD VALIGN="top">', $thead;
167 <BR><BR>Services included
168 END
169 }
170
171 my @fixups = ();
172 my $count = 0;
173 my $columns = 3;
174 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
175 foreach my $part_svc ( @part_svc ) {
176   my $svcpart = $part_svc->svcpart;
177   my $pkgpart = $cgi->param('clone') || $part_pkg->pkgpart;
178   my $pkg_svc = $pkgpart && qsearchs( 'pkg_svc', {
179     'pkgpart'  => $pkgpart,
180     'svcpart'  => $svcpart,
181   } ) || new FS::pkg_svc ( {
182     'pkgpart'     => $pkgpart,
183     'svcpart'     => $svcpart,
184     'quantity'    => 0,
185     'primary_svc' => '',
186   });
187   #? #next unless $pkg_svc;
188
189   push @fixups, "pkg_svc$svcpart";
190
191   #unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
192   #dunno why...
193   unless ( 0 ) {
194     print '<TR>'; # if $count == 0 ;
195     print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
196           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
197           qq!"></TD>!;
198     if ( dbdef->table('pkg_svc')->column('primary_svc') ) {
199       print qq!<TD><INPUT TYPE="radio" NAME="pkg_svc_primary" VALUE="$svcpart"!;
200       print ' CHECKED' if $pkg_svc->primary_svc =~ /^Y/i;
201       print '></TD>';
202     }
203     print qq!<TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
204           qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
205 #    print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
206     $count+=1;
207     foreach ( 1 .. $columns-1 ) {
208       print "</TABLE></TD><TD VALIGN=\"top\">$thead"
209         if $count == int( $_ * scalar(@part_svc) / $columns );
210     }
211   } else {
212     print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
213           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
214   }
215 }
216
217 #unless ( $cgi->param('clone') ) {
218 #dunno why...
219 unless ( 0 ) {
220   print "</TR></TABLE></TD></TR></TABLE>";
221   #print "</TR></TABLE>";
222 }
223
224 foreach my $f ( qw( clone pkgnum ) ) {
225   print qq!<INPUT TYPE="hidden" NAME="$f" VALUE="!. $cgi->param($f). '">';
226 }
227 print '<INPUT TYPE="hidden" NAME="pkgpart" VALUE="'. $part_pkg->pkgpart. '">';
228
229 # prolly should be in database
230 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
231
232 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
233                     split("\n", ($clone_part_pkg||$part_pkg)->plandata );
234 #warn join("\n", map { "$_: $plandata{$_}" } keys %plandata ). "\n";
235
236 tie my %options, 'Tie::IxHash', map { $_=>$plans{$_}->{'name'} } keys %plans;
237
238 my @form_select = ();
239 if ( $conf->exists('enable_taxclasses') ) {
240   push @form_select, 'taxclass';
241 } else {
242   push @fixups, 'taxclass'; #hidden
243 }
244
245 my @form_radio = ();
246 if ( dbdef->table('pkg_svc')->column('primary_svc') ) {
247   push @form_radio, 'pkg_svc_primary';
248 }
249
250 tie my %freq, 'Tie::IxHash', %FS::part_pkg::freq;
251 if ( $part_pkg->dbdef_table->column('freq')->type =~ /(int)/i ) {
252   delete $freq{$_} foreach grep { ! /^\d+$/ } keys %freq;
253 }
254
255 my $widget = new HTML::Widgets::SelectLayers(
256   'selected_layer' => $part_pkg->plan,
257   'options'        => \%options,
258   'form_name'      => 'dummy',
259   'form_action'    => 'process/part_pkg.cgi',
260   'form_text'      => [ qw(pkg comment clone pkgnum pkgpart), @fixups ],
261   'form_checkbox'  => [ qw(setuptax recurtax disabled) ],
262   'form_radio'     => \@form_radio,
263   'form_select'    => \@form_select,
264   'layer_callback' => sub {
265     my $layer = shift;
266     my $html = qq!<INPUT TYPE="hidden" NAME="plan" VALUE="$layer">!.
267                ntable("#cccccc",2);
268     $html .= '
269       <TR>
270         <TD ALIGN="right">Recurring fee frequency </TD>
271         <TD><SELECT NAME="freq">
272     ';
273
274     my @freq = keys %freq;
275     @freq = grep { /^\d+$/ } @freq
276       if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
277     foreach my $freq ( @freq ) {
278       $html .= qq(<OPTION VALUE="$freq");
279       $html .= ' SELECTED' if $freq eq $part_pkg->freq;
280       $html .= ">$freq{$freq}";
281     }
282     $html .= '</SELECT></TD></TR>';
283
284     my $href = $plans{$layer}->{'fields'};
285     foreach my $field ( exists($plans{$layer}->{'fieldorder'})
286                           ? @{$plans{$layer}->{'fieldorder'}}
287                           : keys %{ $href }
288                       ) {
289
290       $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
291
292       if ( ! exists($href->{$field}{'type'}) ) {
293         $html .= qq!<INPUT TYPE="text" NAME="$field" VALUE="!.
294                  ( exists($plandata{$field})
295                      ? $plandata{$field}
296                      : $href->{$field}{'default'} ).
297                  qq!" onChange="fchanged(this)">!;
298       } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
299         $html .= '<SELECT';
300         $html .= ' MULTIPLE'
301           if $href->{$field}{'type'} eq 'select_multiple';
302         $html .= qq! NAME="$field" onChange="fchanged(this)">!;
303         foreach my $record (
304           qsearch( $href->{$field}{'select_table'},
305                    $href->{$field}{'select_hash'}   )
306         ) {
307           my $value = $record->getfield($href->{$field}{'select_key'});
308           $html .= qq!<OPTION VALUE="$value"!.
309                    (  $plandata{$field} =~ /(^|, *)$value *(,|$)/
310                         ? ' SELECTED'
311                         : ''          ).
312                    '>'. $record->getfield($href->{$field}{'select_label'})
313         }
314         $html .= '</SELECT>';
315       }
316
317       $html .= '</TD></TR>';
318     }
319     $html .= '</TABLE>';
320
321     $html .= '<INPUT TYPE="hidden" NAME="plandata" VALUE="'.
322              join(',', keys %{ $href } ). '">'.
323              '<BR><BR>';
324              
325     $html .= '<INPUT TYPE="submit" VALUE="'.
326              ( $hashref->{pkgpart} ? "Apply changes" : "Add package" ).
327              '" onClick="fchanged(this)">';
328
329     $html;
330
331   },
332 );
333
334 %>
335
336 <BR><BR>Price plan <%= $widget->html %>
337   </BODY>
338 </HTML>