hide communigate fields in svc_acct and svc_domain when there's no communigate export...
[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 $communigate = 0;
105 %      my @part_export =
106 %        map { qsearch( 'part_export', {exporttype => $_ } ) }
107 %          keys %{FS::part_export::export_info($layer)};
108 %      $html .= '<BR><BR>'. table(). 
109 %               "<TR><TH COLSPAN=$columns>Exports</TH></TR><TR>";
110 %      foreach my $part_export ( @part_export ) {
111 %        $communigate++ if $part_export->exporttype =~ /^communigate/;
112 %        $html .= '<TD><INPUT TYPE="checkbox"'.
113 %                 ' NAME="exportnum'. $part_export->exportnum. '"  VALUE="1" ';
114 %        $html .= 'CHECKED'
115 %          if ( $clone || $part_svc->svcpart ) #null svcpart search causing error
116 %              && qsearchs( 'export_svc', {
117 %                                   exportnum => $part_export->exportnum,
118 %                                   svcpart   => $clone || $part_svc->svcpart });
119 %        $html .= '>'.$part_export->exportnum. ': ';
120 %        $html .= $part_export->exportname . '<DIV ALIGN="right"><FONT SIZE=-1>'
121 %          if ( $part_export->exportname );
122 %        $html .= $part_export->exporttype. ' to '. $part_export->machine;
123 %        $html .= '</FONT></DIV>' if ( $part_export->exportname );
124 %        $html .= '</TD>';
125 %        $count++;
126 %        $html .= '</TR><TR>' unless $count % $columns;
127 %      }
128 %      $html .= '</TR></TABLE><BR><BR>';
129 %
130 %      $html .= include('/elements/table-grid.html', 'cellpadding' => 4 ).
131 %               '<TR>'.
132 %                 '<TH CLASS="grid" BGCOLOR="#cccccc">Field</TH>'.
133 %                 '<TH CLASS="grid" BGCOLOR="#cccccc">Label</TH>'.
134 %                 '<TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=2>Modifier</TH>'.
135 %               '</TR>';
136 %
137 %      my $bgcolor1 = '#eeeeee';
138 %      my $bgcolor2 = '#ffffff';
139 %      my $bgcolor;
140 %
141 %      #yucky kludge
142 %      my @fields = ();
143 %      if ( defined( dbdef->table($layer) ) ) {
144 %        @fields = grep {
145 %            $_ ne 'svcnum'
146 %            && ( $communigate || !$communigate_fields{$layer}->{$_} )
147 %            && ( !FS::part_svc->svc_table_fields($layer)
148 %                   ->{$_}->{disable_part_svc_column}
149 %                 || $part_svc->part_svc_column($_)->columnflag
150 %               )
151 %        } fields($layer);
152 %      }
153 %      push @fields, 'usergroup' if $layer eq 'svc_acct'; #kludge
154 %      $part_svc->svcpart($clone) if $clone; #haha, undone below
155 %
156 %
157 %      foreach my $field (@fields) {
158 %
159 %        #a few lines of false laziness w/browse/part_svc.cgi
160 %        my $def = FS::part_svc->svc_table_fields($layer)->{$field};
161 %        my $def_info  = $def->{'def_info'};
162 %        my $formatter = $def->{'format'} || sub { shift };
163 %
164 %        my $part_svc_column = $part_svc->part_svc_column($field);
165 %        my $label = $part_svc_column->columnlabel || $def->{'label'};
166 %        my $value = &$formatter($part_svc_column->columnvalue);
167 %        my $flag  = $part_svc_column->columnflag;
168 %
169 %        if ( $bgcolor eq $bgcolor1 ) {
170 %          $bgcolor = $bgcolor2;
171 %        } else {
172 %          $bgcolor = $bgcolor1;
173 %        }
174 %        
175 %        $html .= qq!<TR><TD ROWSPAN=2 CLASS="grid" BGCOLOR="$bgcolor" ALIGN="right">!.
176 %                 ( $def->{'label'} || $field ).
177 %                 "</TD>";
178 %
179 %        $html .= qq!<TD ROWSPAN=2 CLASS="grid" BGCOLOR="$bgcolor"><INPUT NAME="${layer}__${field}_label" VALUE="!. encode_entities($label). '" STYLE="text-align:right"></TD>';
180 %
181 %        $flag = '' if $def->{type} eq 'disabled';
182 %
183 %        $html .= qq!<TD CLASS="grid" BGCOLOR="$bgcolor">!;
184 %
185 %        if ( $def->{type} eq 'disabled' ) {
186 %        
187 %          $html .= 'No default';
188 %
189 %        } else {
190 %
191 %          $html .= qq!<SELECT NAME="${layer}__${field}_flag"!.
192 %                      qq! onChange="${layer}__${field}_flag_changed(this)">!;
193 %
194 %          foreach my $f ( keys %flag ) {
195 %
196 %            # need to template-ize more httemplate/edit/svc_* first
197 %            next if $f eq 'M' and $layer !~ /^svc_(broadband|external|phone)$/;
198 %
199 %            #here is where the SUB from above is called, to skip some choices
200 %            next if $flag{$f}->{condition}
201 %                 && &{ $flag{$f}->{condition} }( $def, $layer, $field );
202 %
203 %            $html .= qq!<OPTION VALUE="$f"!.
204 %                     ' SELECTED'x($flag eq $f ).
205 %                     '>'. $flag{$f}->{desc};
206 %
207 %          }
208 %
209 %          $html .= '</SELECT>';
210 %
211 %          $html .= join("\n",
212 %            '<SCRIPT>',
213 %            "  function ${layer}__${field}_flag_changed(what) {",
214 %            '    var f = what.options[what.selectedIndex].value;',
215 %            '    if ( f == "" || f == "X" ) { //disable',
216 %            "      what.form.${layer}__${field}.disabled = true;".
217 %            "      what.form.${layer}__${field}.style.backgroundColor = '#dddddd';".
218 %            "      if ( what.form.${layer}__${field}_classnum ) {".
219 %            "        what.form.${layer}__${field}_classnum.disabled = true;".
220 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#dddddd';".
221 %            "      }".
222 %            '    } else if ( f == "D" || f == "F" || f =="S" ) { //enable, text box',
223 %            "      what.form.${layer}__${field}.disabled = false;".
224 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
225 %            "      if ( f == 'S' || '${field}' == 'usergroup' ) {". # kludge
226 %            "        what.form.${layer}__${field}.multiple = true;".
227 %            "      } else {".
228 %            "        what.form.${layer}__${field}.multiple = false;".
229 %            "      }".
230 %            "      what.form.${layer}__${field}.style.display = '';".
231 %            "      if ( what.form.${layer}__${field}_classnum ) {".
232 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
233 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
234 %            "        what.form.${layer}__${field}_classnum.style.display = 'none';".
235 %            "      }".
236 %            '    } else if ( f == "M" || f == "A" ) { //enable, inventory',
237 %            "      what.form.${layer}__${field}.disabled = false;".
238 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
239 %            "      what.form.${layer}__${field}.style.display = 'none';".
240 %            "      if ( what.form.${layer}__${field}_classnum ) {".
241 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
242 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
243 %            "        what.form.${layer}__${field}_classnum.style.display = '';".
244 %            "      }".
245 %            '    }',
246 %            '  }',
247 %            '</SCRIPT>',
248 %          );
249 %
250 %        }
251 %
252 %        $html .= qq!</TD><TD CLASS="grid" BGCOLOR="$bgcolor">!;
253 %
254 %        my $disabled = $flag ? ''
255 %                             : 'DISABLED STYLE="background-color: #dddddd"';
256 %
257 %        if ( !$def->{type} || $def->{type} eq 'text' ) {
258 %
259 %          my $nodisplay = ' STYLE="display:none"';
260 %          my $is_inv = ( $flag =~ /^[MA]$/ );
261 %
262 %          $html .=
263 %            qq!<INPUT TYPE="text" NAME="${layer}__${field}" VALUE="$value" !.
264 %            $disabled.
265 %            ( $is_inv ? $nodisplay : $disabled ).
266 %            '>';
267 %
268 %          $html .= include('/elements/select-table.html',
269 %                             'element_name' => "${layer}__${field}_classnum",
270 %                             'element_etc'  => ( $is_inv
271 %                                                   ? $disabled
272 %                                                   : $nodisplay
273 %                                               ),
274 %                             'table'        => 'inventory_class',
275 %                             'name_col'     => 'classname',
276 %                             'value'        => $value,
277 %                             'empty_label'  => 'Select inventory class',
278 %                          );
279 %
280 %        } elsif ( $def->{type} eq 'checkbox' ) {
281 %
282 %          $html .= include('/elements/checkbox.html',
283 %                             'field'      => $layer.'__'.$field,
284 %                             'curr_value' => $value,
285 %                             'value'      => 'Y',
286 %                          );
287 %
288 %        } elsif ( $def->{type} eq 'select' ) {
289 %
290 %          $html .= qq!<SELECT NAME="${layer}__${field}" $disabled!;
291 %          $html .= ' MULTIPLE' if $flag eq 'S';
292 %          $html .= '>';
293 %          $html .= '<OPTION> </OPTION>' unless $value;
294 %          if ( $def->{select_table} ) {
295 %            foreach my $record ( qsearch( $def->{select_table}, {} ) ) {
296 %              my $rvalue = $record->getfield($def->{select_key});
297 %              my $select_label = $def->{select_label};
298 %              $html .= qq!<OPTION VALUE="$rvalue"!.
299 %                  (grep(/^$rvalue$/, split(',',$value)) ? ' SELECTED>' : '>' ).
300 %                  $record->$select_label(). '</OPTION>';
301 %            } #next $record
302 %          } elsif ( $def->{select_list} ) {
303 %            foreach my $item ( @{$def->{select_list}} ) {
304 %              $html .= qq!<OPTION VALUE="$item"!.
305 %                    (grep(/^$item$/, split(',',$value)) ? ' SELECTED>' : '>' ).
306 %                    $item. '</OPTION>';
307 %            } #next $item
308 %          } elsif ( $def->{select_hash} ) {
309 %            if ( ref($def->{select_hash}) eq 'ARRAY' ) {
310 %              tie my %hash, 'Tie::IxHash', @{ $def->{select_hash} };
311 %              $def->{select_hash} = \%hash;
312 %            }
313 %            foreach my $key ( keys %{$def->{select_hash}} ) {
314 %              $html .= qq!<OPTION VALUE="$key"!.
315 %                    (grep(/^$key$/, split(',',$value)) ? ' SELECTED>' : '>' ).
316 %                    $def->{select_hash}{$key}. '</OPTION>';
317 %            } #next $key
318 %          } #endif
319 %          $html .= '</SELECT>';
320 %
321 %        } elsif ( $def->{type} eq 'textarea' ) {
322 %
323 %          $html .=
324 %            qq!<TEXTAREA NAME="${layer}__${field}">!. encode_entities($value).
325 %            '</TEXTAREA>';
326 %
327 %        } elsif ( $def->{type} eq 'select-svc_pbx.html' ) {
328 %
329 %          $html .= include('/elements/select-svc_pbx.html',
330 %                             'curr_value'   => $value,
331 %                             'element_name' => "${layer}__${field}",
332 %                             'element_etc'  => $disabled,
333 %                             'multiple'     => ($flag eq 'S'),
334 %                          );
335 %
336 %        } elsif ( $def->{type} eq 'radius_usergroup_selector' ) {
337 %
338 %          #XXX disable the RADIUS usergroup selector?  ugh it sure does need
339 %          #an overhaul, people have dum group problems because of it
340 %
341 %          $html .= FS::svc_acct::radius_usergroup_selector(
342 %            [ split(',', $value) ], "${layer}__${field}" );
343 %
344 %        } elsif ( $def->{type} eq 'communigate_pro-accessmodes' ) {
345 %
346 %          $html .= include('/elements/communigate_pro-accessmodes.html',
347 %                             'element_name_prefix' => "${layer}__${field}_",
348 %                             'curr_value'          => $value,
349 %                             #doesn't work#'element_etc'  => $disabled,
350 %                          );
351 %
352 %        } elsif ( $def->{type} eq 'disabled' ) {
353 %
354 %          $html .=
355 %            qq!<INPUT TYPE="hidden" NAME="${layer}__${field}" VALUE="">!;
356 %
357 %        } else {
358 %
359 %          $html .= '<font color="#ff0000">unknown type '. $def->{type};
360 %
361 %        }
362 %
363 %        $html .= "</TD></TR>\n";
364
365 %        $def_info = "($def_info)" if $def_info;
366 %        $html .=
367 %          qq!<TR>!.
368 %          qq!  <TD COLSPAN=2 BGCOLOR="$bgcolor" ALIGN="center" !.
369 %          qq!      STYLE="padding:0; border-top: none">!.
370 %          qq!    <FONT SIZE="-1"><I>$def_info</I></FONT>!.
371 %          qq!  </TD>!.
372 %          qq!</TR>\n!;
373 %
374 %      } #foreach my $field (@fields) {
375 %
376 %      $part_svc->svcpart('') if $clone; #undone
377 %      $html .= "</TABLE>";
378 %
379 %      $html .= include('/elements/progress-init.html',
380 %                         $layer, #form name
381 %                         [ qw(svc svcpart disabled exportnum), @fields ],
382 %                         'process/part_svc.cgi',
383 %                         $p.'browse/part_svc.cgi',
384 %                         $layer,
385 %                      );
386 %      $html .= '<BR><INPUT NAME="submit" TYPE="button" VALUE="'.
387 %               ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '" '.
388 %               ' onClick="document.'. "$layer.submit.disabled=true; ".
389 %               "fixup(document.$layer); $layer". 'process();">';
390 %
391 %      #$html .= '<BR><INPUT TYPE="submit" VALUE="'.
392 %      #         ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '">';
393 %
394 %      $html;
395 %
396 %    },
397 %  );
398 %
399 %
400
401 Table <% $widget->html %>
402
403 <% include('/elements/footer.html') %>
404
405 <%init>
406
407 die "access denied"
408   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
409
410 my $part_svc;
411 my $clone = '';
412 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {#clone
413   #$cgi->param('clone') =~ /^(\d+)$/ or die "malformed query: $query";
414   $part_svc = qsearchs('part_svc', { 'svcpart'=>$1 } )
415     or die "unknown svcpart: $1";
416   $clone = $part_svc->svcpart;
417   $part_svc->svcpart('');
418 } elsif ( $cgi->keywords ) { #edit
419   my($query) = $cgi->keywords;
420   $query =~ /^(\d+)$/ or die "malformed query: $query";
421   $part_svc=qsearchs('part_svc', { 'svcpart'=>$1 } )
422     or die "unknown svcpart: $1";
423 } else { #adding
424   $part_svc = new FS::part_svc {};
425 }
426
427 my $action = $part_svc->svcpart ? 'Edit' : 'Add';
428 my $hashref = $part_svc->hashref;
429 #   my $p_svcdb = $part_svc->svcdb || 'svc_acct';
430
431 my %communigate_fields = (
432   'svc_acct'        => { map { $_=>1 }
433                            qw( file_quota file_maxnum file_maxsize
434                                password_selfchange password_recover
435                              ),
436                            grep /^cgp_/, fields('svc_acct')
437                        },
438   'svc_domain'      => { map { $_=>1 }
439                            qw( max_accounts trailer parent_svcnum ),
440                            grep /^(cgp|acct_def)_/, fields('svc_domain')
441                        },
442   #'svc_forward'     => { map { $_=>1 } qw( ) },
443   #'svc_mailinglist' => { map { $_=>1 } qw( ) },
444   #'svc_cert'        => { map { $_=>1 } qw( ) },
445 );
446
447 </%init>
448
449
450