certificates ala communigate, RT#7515
[freeside.git] / httemplate / edit / part_svc.cgi
1 <% include('/elements/header.html', "$action Service Definition",
2            menubar('View all service definitions' => "${p}browse/part_svc.cgi"),
3            #" onLoad=\"visualize()\""
4           )
5 %>
6
7 <FORM NAME="dummy">
8
9       Service Part #<% $part_svc->svcpart ? $part_svc->svcpart : "(NEW)" %>
10 <BR><BR>
11 Service  <INPUT TYPE="text" NAME="svc" VALUE="<% $hashref->{svc} %>"><BR>
12 Disable new orders <INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"<% $hashref->{disabled} eq 'Y' ? ' CHECKED' : '' %>><BR>
13 <INPUT TYPE="hidden" NAME="svcpart" VALUE="<% $hashref->{svcpart} %>">
14 <BR>
15 Service definitions are the templates for items you offer to your customers.
16 <UL><LI>svc_acct - Accounts - anything with a username (Mailboxes, PPP accounts, shell accounts, RADIUS entries for broadband, etc.)
17     <LI>svc_domain - Domains
18     <LI>svc_cert - Certificates
19     <LI>svc_forward - Mail forwarding
20     <LI>svc_mailinglist - Mailing list
21     <LI>svc_www - Virtual domain website
22     <LI>svc_broadband - Broadband/High-speed Internet service (always-on)
23     <LI>svc_phone - Customer phone numbers
24     <LI>svc_pbx - Customer PBXs
25     <LI>svc_external - Externally-tracked service
26 <!--   <LI>svc_charge - One-time charges (Partially unimplemented)
27        <LI>svc_wo - Work orders (Partially unimplemented)
28 -->
29 </UL>
30 For the selected table, you can give fields default or fixed (unchangable)
31 values, or select an inventory class to manually or automatically fill in
32 that field.
33 <BR><BR>
34
35 % #YUCK.  false laziness w/part_svc.pm.  go away virtual fields, please
36 % my %vfields;
37 % foreach my $svcdb ( FS::part_svc->svc_tables() ) {
38 %   eval "use FS::$svcdb;";
39 %   my $self = "FS::$svcdb"->new;
40 %   $vfields{$svcdb} = {};
41 %   foreach my $field ($self->virtual_fields) { # svc_Common::virtual_fields with a null svcpart returns all of them
42 %     my $pvf = $self->pvf($field);
43 %     $vfields{$svcdb}->{$field} = $pvf;
44 %     #warn "\$vfields{$svcdb}->{$field} = $pvf";
45 %   } #next $field
46 % } #next $svcdb
47 %
48 %  #code duplication w/ edit/part_svc.cgi, should move this hash to part_svc.pm
49 %  # and generalize the subs
50 %  # condition sub is tested to see whether to disable display of this choice
51 %  # params: ( $def, $layer, $field )  (see SUB below)
52 %  my $inv_sub = sub {
53 %                      $_[0]->{disable_inventory}
54 %                        || $_[0]->{'type'} ne 'text'
55 %                    };
56 %  tie my %flag, 'Tie::IxHash',
57 %    ''  => { 'desc' => 'No default', },
58 %    'D' => { 'desc' => 'Default',
59 %             'condition' =>
60 %               sub { $_[0]->{disable_default} }, 
61 %           },
62 %    'F' => { 'desc' => 'Fixed (unchangeable)',
63 %             'condition' =>
64 %               sub { $_[0]->{disable_fixed} }, 
65 %           },
66 %    'S' => { 'desc' => 'Selectable Choice',
67 %             'condition' =>
68 %               sub { !ref($_[0]) || $_[0]->{disable_select} }, 
69 %           },
70 %    'M' => { 'desc' => 'Manual selection from inventory',
71 %             'condition' => $inv_sub,
72 %           },
73 %    'A' => { 'desc' => 'Automatically fill in from inventory',
74 %             'condition' => $inv_sub,
75 %           },
76 %    'X' => { 'desc' => 'Excluded',
77 %             'condition' =>
78 %               sub { ! $vfields{$_[1]}->{$_[2]} },
79 %
80 %           },
81 %  ;
82 %  
83 %  my @dbs = $hashref->{svcdb}
84 %             ? ( $hashref->{svcdb} )
85 %             : FS::part_svc->svc_tables();
86 %
87 %  tie my %svcdb, 'Tie::IxHash', map { $_=>$_ } grep dbdef->table($_), @dbs;
88 %  my $widget = new HTML::Widgets::SelectLayers(
89 %    #'selected_layer' => $p_svcdb,
90 %    'selected_layer' => $hashref->{svcdb} || 'svc_acct',
91 %    'options'        => \%svcdb,
92 %    'form_name'      => 'dummy',
93 %    #'form_action'    => 'process/part_svc.cgi',
94 %    'form_action'    => 'part_svc.cgi', #self
95 %    'form_text'      => [ qw( svc svcpart ) ],
96 %    'form_checkbox'  => [ 'disabled' ],
97 %    'layer_callback' => sub {
98 %      my $layer = shift;
99 %      
100 %      my $html = qq!<INPUT TYPE="hidden" NAME="svcdb" VALUE="$layer">!;
101 %
102 %      my $columns = 3;
103 %      my $count = 0;
104 %      my @part_export =
105 %        map { qsearch( 'part_export', {exporttype => $_ } ) }
106 %          keys %{FS::part_export::export_info($layer)};
107 %      $html .= '<BR><BR>'. table(). 
108 %               "<TR><TH COLSPAN=$columns>Exports</TH></TR><TR>";
109 %      foreach my $part_export ( @part_export ) {
110 %        $html .= '<TD><INPUT TYPE="checkbox"'.
111 %                 ' NAME="exportnum'. $part_export->exportnum. '"  VALUE="1" ';
112 %        $html .= 'CHECKED'
113 %          if ( $clone || $part_svc->svcpart ) #null svcpart search causing error
114 %              && qsearchs( 'export_svc', {
115 %                                   exportnum => $part_export->exportnum,
116 %                                   svcpart   => $clone || $part_svc->svcpart });
117 %        $html .= '>'. $part_export->exportnum. ': '. $part_export->exporttype.
118 %                 ' to '. $part_export->machine. '</TD>';
119 %        $count++;
120 %        $html .= '</TR><TR>' unless $count % $columns;
121 %      }
122 %      $html .= '</TR></TABLE><BR><BR>';
123 %
124 %      $html .= include('/elements/table-grid.html', 'cellpadding' => 4 ).
125 %               '<TR>'.
126 %                 '<TH CLASS="grid" BGCOLOR="#cccccc">Field</TH>'.
127 %                 '<TH CLASS="grid" BGCOLOR="#cccccc">Label</TH>'.
128 %                 '<TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=2>Modifier</TH>'.
129 %               '</TR>';
130 %
131 %      my $bgcolor1 = '#eeeeee';
132 %      my $bgcolor2 = '#ffffff';
133 %      my $bgcolor;
134 %
135 %      #yucky kludge
136 %      my @fields = defined( dbdef->table($layer) )
137 %                      ? grep {
138 %                               $_ ne 'svcnum' &&
139 %                               ( !FS::part_svc->svc_table_fields($layer)
140 %                                     ->{$_}->{disable_part_svc_column}   ||
141 %                                 $part_svc->part_svc_column($_)->columnflag
142 %                               )
143 %                             }
144 %                             fields($layer)
145 %                      : ();
146 %      push @fields, 'usergroup' if $layer eq 'svc_acct'; #kludge
147 %      $part_svc->svcpart($clone) if $clone; #haha, undone below
148 %
149 %
150 %      foreach my $field (@fields) {
151 %
152 %        #a few lines of false laziness w/browse/part_svc.cgi
153 %        my $def = FS::part_svc->svc_table_fields($layer)->{$field};
154 %        my $def_info  = $def->{'def_info'};
155 %        my $formatter = $def->{'format'} || sub { shift };
156 %
157 %        my $part_svc_column = $part_svc->part_svc_column($field);
158 %        my $label = $part_svc_column->columnlabel || $def->{'label'};
159 %        my $value = &$formatter($part_svc_column->columnvalue);
160 %        my $flag  = $part_svc_column->columnflag;
161 %
162 %        if ( $bgcolor eq $bgcolor1 ) {
163 %          $bgcolor = $bgcolor2;
164 %        } else {
165 %          $bgcolor = $bgcolor1;
166 %        }
167 %        
168 %        $html .= qq!<TR><TD ROWSPAN=2 CLASS="grid" BGCOLOR="$bgcolor" ALIGN="right">!.
169 %                 ( $def->{'label'} || $field ).
170 %                 "</TD>";
171 %
172 %        $html .= qq!<TD ROWSPAN=2 CLASS="grid" BGCOLOR="$bgcolor"><INPUT NAME="${layer}__${field}_label" VALUE="!. encode_entities($label). '" STYLE="text-align:right"></TD>';
173 %
174 %        $flag = '' if $def->{type} eq 'disabled';
175 %
176 %        $html .= qq!<TD CLASS="grid" BGCOLOR="$bgcolor">!;
177 %
178 %        if ( $def->{type} eq 'disabled' ) {
179 %        
180 %          $html .= 'No default';
181 %
182 %        } else {
183 %
184 %          $html .= qq!<SELECT NAME="${layer}__${field}_flag"!.
185 %                      qq! onChange="${layer}__${field}_flag_changed(this)">!;
186 %
187 %          foreach my $f ( keys %flag ) {
188 %
189 %            # need to template-ize more httemplate/edit/svc_* first
190 %            next if $f eq 'M' and $layer !~ /^svc_(broadband|external|phone)$/;
191 %
192 %            #here is where the SUB from above is called, to skip some choices
193 %            next if $flag{$f}->{condition}
194 %                 && &{ $flag{$f}->{condition} }( $def, $layer, $field );
195 %
196 %            $html .= qq!<OPTION VALUE="$f"!.
197 %                     ' SELECTED'x($flag eq $f ).
198 %                     '>'. $flag{$f}->{desc};
199 %
200 %          }
201 %
202 %          $html .= '</SELECT>';
203 %
204 %          $html .= join("\n",
205 %            '<SCRIPT>',
206 %            "  function ${layer}__${field}_flag_changed(what) {",
207 %            '    var f = what.options[what.selectedIndex].value;',
208 %            '    if ( f == "" || f == "X" ) { //disable',
209 %            "      what.form.${layer}__${field}.disabled = true;".
210 %            "      what.form.${layer}__${field}.style.backgroundColor = '#dddddd';".
211 %            "      if ( what.form.${layer}__${field}_classnum ) {".
212 %            "        what.form.${layer}__${field}_classnum.disabled = true;".
213 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#dddddd';".
214 %            "      }".
215 %            '    } else if ( f == "D" || f == "F" || f =="S" ) { //enable, text box',
216 %            "      what.form.${layer}__${field}.disabled = false;".
217 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
218 %            "      if ( f == 'S' || '${field}' == 'usergroup' ) {". # kludge
219 %            "        what.form.${layer}__${field}.multiple = true;".
220 %            "      } else {".
221 %            "        what.form.${layer}__${field}.multiple = false;".
222 %            "      }".
223 %            "      what.form.${layer}__${field}.style.display = '';".
224 %            "      if ( what.form.${layer}__${field}_classnum ) {".
225 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
226 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
227 %            "        what.form.${layer}__${field}_classnum.style.display = 'none';".
228 %            "      }".
229 %            '    } else if ( f == "M" || f == "A" ) { //enable, inventory',
230 %            "      what.form.${layer}__${field}.disabled = false;".
231 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
232 %            "      what.form.${layer}__${field}.style.display = 'none';".
233 %            "      if ( what.form.${layer}__${field}_classnum ) {".
234 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
235 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
236 %            "        what.form.${layer}__${field}_classnum.style.display = '';".
237 %            "      }".
238 %            '    }',
239 %            '  }',
240 %            '</SCRIPT>',
241 %          );
242 %
243 %        }
244 %
245 %        $html .= qq!</TD><TD CLASS="grid" BGCOLOR="$bgcolor">!;
246 %
247 %        my $disabled = $flag ? ''
248 %                             : 'DISABLED STYLE="background-color: #dddddd"';
249 %
250 %        if ( !$def->{type} || $def->{type} eq 'text' ) {
251 %
252 %          my $nodisplay = ' STYLE="display:none"';
253 %          my $is_inv = ( $flag =~ /^[MA]$/ );
254 %
255 %          $html .=
256 %            qq!<INPUT TYPE="text" NAME="${layer}__${field}" VALUE="$value" !.
257 %            $disabled.
258 %            ( $is_inv ? $nodisplay : $disabled ).
259 %            '>';
260 %
261 %          $html .= include('/elements/select-table.html',
262 %                             'element_name' => "${layer}__${field}_classnum",
263 %                             'element_etc'  => ( $is_inv
264 %                                                   ? $disabled
265 %                                                   : $nodisplay
266 %                                               ),
267 %                             'table'        => 'inventory_class',
268 %                             'name_col'     => 'classname',
269 %                             'value'        => $value,
270 %                             'empty_label'  => 'Select inventory class',
271 %                          );
272 %
273 %        } elsif ( $def->{type} eq 'checkbox' ) {
274 %
275 %          $html .= include('/elements/checkbox.html',
276 %                             'field'      => $layer.'__'.$field,
277 %                             'curr_value' => $value,
278 %                             'value'      => 'Y',
279 %                          );
280 %
281 %        } elsif ( $def->{type} eq 'select' ) {
282 %
283 %          $html .= qq!<SELECT NAME="${layer}__${field}" $disabled!;
284 %          $html .= ' MULTIPLE' if $flag eq 'S';
285 %          $html .= '>';
286 %          $html .= '<OPTION> </OPTION>' unless $value;
287 %          if ( $def->{select_table} ) {
288 %            foreach my $record ( qsearch( $def->{select_table}, {} ) ) {
289 %              my $rvalue = $record->getfield($def->{select_key});
290 %              my $select_label = $def->{select_label};
291 %              $html .= qq!<OPTION VALUE="$rvalue"!.
292 %                  (grep(/^$rvalue$/, split(',',$value)) ? ' SELECTED>' : '>' ).
293 %                  $record->$select_label(). '</OPTION>';
294 %            } #next $record
295 %          } elsif ( $def->{select_list} ) {
296 %            foreach my $item ( @{$def->{select_list}} ) {
297 %              $html .= qq!<OPTION VALUE="$item"!.
298 %                    (grep(/^$item$/, split(',',$value)) ? ' SELECTED>' : '>' ).
299 %                    $item. '</OPTION>';
300 %            } #next $item
301 %          } elsif ( $def->{select_hash} ) {
302 %            if ( ref($def->{select_hash}) eq 'ARRAY' ) {
303 %              tie my %hash, 'Tie::IxHash', @{ $def->{select_hash} };
304 %              $def->{select_hash} = \%hash;
305 %            }
306 %            foreach my $key ( keys %{$def->{select_hash}} ) {
307 %              $html .= qq!<OPTION VALUE="$key"!.
308 %                    (grep(/^$key$/, split(',',$value)) ? ' SELECTED>' : '>' ).
309 %                    $def->{select_hash}{$key}. '</OPTION>';
310 %            } #next $key
311 %          } #endif
312 %          $html .= '</SELECT>';
313 %
314 %        } elsif ( $def->{type} eq 'textarea' ) {
315 %
316 %          $html .=
317 %            qq!<TEXTAREA NAME="${layer}__${field}">!. encode_entities($value).
318 %            '</TEXTAREA>';
319 %
320 %        } elsif ( $def->{type} eq 'select-svc_pbx.html' ) {
321 %
322 %          $html .= include('/elements/select-svc_pbx.html',
323 %                             'curr_value'   => $value,
324 %                             'element_name' => "${layer}__${field}",
325 %                             'element_etc'  => $disabled,
326 %                             'multiple'     => ($flag eq 'S'),
327 %                          );
328 %
329 %        } elsif ( $def->{type} eq 'radius_usergroup_selector' ) {
330 %
331 %          #XXX disable the RADIUS usergroup selector?  ugh it sure does need
332 %          #an overhaul, people have dum group problems because of it
333 %
334 %          $html .= FS::svc_acct::radius_usergroup_selector(
335 %            [ split(',', $value) ], "${layer}__${field}" );
336 %
337 %        } elsif ( $def->{type} eq 'communigate_pro-accessmodes' ) {
338 %
339 %          $html .= include('/elements/communigate_pro-accessmodes.html',
340 %                             'element_name_prefix' => "${layer}__${field}_",
341 %                             'curr_value'          => $value,
342 %                             #doesn't work#'element_etc'  => $disabled,
343 %                          );
344 %
345 %        } elsif ( $def->{type} eq 'disabled' ) {
346 %
347 %          $html .=
348 %            qq!<INPUT TYPE="hidden" NAME="${layer}__${field}" VALUE="">!;
349 %
350 %        } else {
351 %
352 %          $html .= '<font color="#ff0000">unknown type '. $def->{type};
353 %
354 %        }
355 %
356 %        $html .= "</TD></TR>\n";
357
358 %        $def_info = "($def_info)" if $def_info;
359 %        $html .=
360 %          qq!<TR>!.
361 %          qq!  <TD COLSPAN=2 BGCOLOR="$bgcolor" ALIGN="center" !.
362 %          qq!      STYLE="padding:0; border-top: none">!.
363 %          qq!    <FONT SIZE="-1"><I>$def_info</I></FONT>!.
364 %          qq!  </TD>!.
365 %          qq!</TR>\n!;
366 %
367 %      } #foreach my $field (@fields) {
368 %
369 %      $part_svc->svcpart('') if $clone; #undone
370 %      $html .= "</TABLE>";
371 %
372 %      $html .= include('/elements/progress-init.html',
373 %                         $layer, #form name
374 %                         [ qw(svc svcpart disabled exportnum), @fields ],
375 %                         'process/part_svc.cgi',
376 %                         $p.'browse/part_svc.cgi',
377 %                         $layer,
378 %                      );
379 %      $html .= '<BR><INPUT NAME="submit" TYPE="button" VALUE="'.
380 %               ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '" '.
381 %               ' onClick="document.'. "$layer.submit.disabled=true; ".
382 %               "fixup(document.$layer); $layer". 'process();">';
383 %
384 %      #$html .= '<BR><INPUT TYPE="submit" VALUE="'.
385 %      #         ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '">';
386 %
387 %      $html;
388 %
389 %    },
390 %  );
391 %
392 %
393
394 Table <% $widget->html %>
395
396 <% include('/elements/footer.html') %>
397
398 <%init>
399
400 die "access denied"
401   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
402
403 my $part_svc;
404 my $clone = '';
405 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {#clone
406   #$cgi->param('clone') =~ /^(\d+)$/ or die "malformed query: $query";
407   $part_svc = qsearchs('part_svc', { 'svcpart'=>$1 } )
408     or die "unknown svcpart: $1";
409   $clone = $part_svc->svcpart;
410   $part_svc->svcpart('');
411 } elsif ( $cgi->keywords ) { #edit
412   my($query) = $cgi->keywords;
413   $query =~ /^(\d+)$/ or die "malformed query: $query";
414   $part_svc=qsearchs('part_svc', { 'svcpart'=>$1 } )
415     or die "unknown svcpart: $1";
416 } else { #adding
417   $part_svc = new FS::part_svc {};
418 }
419
420 my $action = $part_svc->svcpart ? 'Edit' : 'Add';
421 my $hashref = $part_svc->hashref;
422 #   my $p_svcdb = $part_svc->svcdb || 'svc_acct';
423
424
425
426 </%init>
427
428
429