add selfservice_access selection to part_svc, RT#15628
[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
13 Self-service access:
14 <SELECT NAME="selfservice_access">
15 % tie my %selfservice_access, 'Tie::IxHash', #false laziness w/browse/part_svc
16 %   ''         => 'Yes',
17 %   'hidden'   => 'Hidden',
18 %   'readonly' => 'Read-only',
19 % ;
20 % for (keys %selfservice_access) {
21   <OPTION VALUE="<% $_ %>"
22           <% $_ eq $hashref->{'selfservice_access'} ? 'SELECTED' : '' %>
23   ><% $selfservice_access{$_} %>
24 % }
25 </SELECT><BR>
26
27 <INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"<% $hashref->{disabled} eq 'Y' ? ' CHECKED' : '' %>>&nbsp;Disable new orders<BR>
28
29 <INPUT TYPE="checkbox" NAME="preserve" VALUE="Y"<% $hashref->{'preserve'} eq 'Y' ? ' CHECKED' : '' %>>&nbsp;Preserve this service on package cancellation<BR>
30
31
32 <INPUT TYPE="hidden" NAME="svcpart" VALUE="<% $hashref->{svcpart} %>">
33
34 <BR>
35
36
37 % #YUCK.  false laziness w/part_svc.pm.  go away virtual fields, please
38 % my %vfields;
39 % foreach my $svcdb ( FS::part_svc->svc_tables() ) {
40 %   eval "use FS::$svcdb;";
41 %   my $self = "FS::$svcdb"->new;
42 %   $vfields{$svcdb} = {};
43 %   foreach my $field ($self->virtual_fields) { # svc_Common::virtual_fields with a null svcpart returns all of them
44 %     my $pvf = $self->pvf($field);
45 %     $vfields{$svcdb}->{$field} = $pvf;
46 %     #warn "\$vfields{$svcdb}->{$field} = $pvf";
47 %   } #next $field
48 % } #next $svcdb
49 %
50 %  #code duplication w/ edit/part_svc.cgi, should move this hash to part_svc.pm
51 %  # and generalize the subs
52 %  # condition sub is tested to see whether to disable display of this choice
53 %  # params: ( $def, $layer, $field )  (see SUB below)
54 %  my $inv_sub = sub {
55 %                      $_[0]->{disable_inventory}
56 %                        || $_[0]->{'type'} ne 'text'
57 %                    };
58 %  tie my %flag, 'Tie::IxHash',
59 %    ''  => { 'desc' => 'No default', },
60 %    'D' => { 'desc' => 'Default',
61 %             'condition' =>
62 %               sub { $_[0]->{disable_default} }, 
63 %           },
64 %    'F' => { 'desc' => 'Fixed (unchangeable)',
65 %             'condition' =>
66 %               sub { $_[0]->{disable_fixed} }, 
67 %           },
68 %    'S' => { 'desc' => 'Selectable Choice',
69 %             'condition' =>
70 %               sub { !ref($_[0]) || $_[0]->{disable_select} }, 
71 %           },
72 %    'M' => { 'desc' => 'Manual selection from inventory',
73 %             'condition' => $inv_sub,
74 %           },
75 %    'A' => { 'desc' => 'Automatically fill in from inventory',
76 %             'condition' => $inv_sub,
77 %           },
78 %    'H' => { 'desc' => 'Select from hardware class',
79 %             'condition' => sub { $_[0]->{type} ne 'select-hardware' },
80 %           },
81 %    'X' => { 'desc' => 'Excluded',
82 %             'condition' =>
83 %               sub { ! $vfields{$_[1]}->{$_[2]} },
84 %
85 %           },
86 %  ;
87 %  
88 %  my @dbs = $hashref->{svcdb}
89 %             ? ( $hashref->{svcdb} )
90 %             : FS::part_svc->svc_tables();
91 %
92 %  tie my %svcdb, 'Tie::IxHash', map { $_=>$_ } grep dbdef->table($_), @dbs;
93 %  my $widget = new HTML::Widgets::SelectLayers(
94 %    #'selected_layer' => $p_svcdb,
95 %    'selected_layer' => $hashref->{svcdb} || 'svc_acct',
96 %    'options'        => \%svcdb,
97 %    'form_name'      => 'dummy',
98 %    #'form_action'    => 'process/part_svc.cgi',
99 %    'form_action'    => 'part_svc.cgi', #self
100 %    'form_text'      => [ qw( svc svcpart ) ],
101 %    'form_select'    => [ 'selfservice_access' ],
102 %    'form_checkbox'  => [ 'disabled', 'preserve' ],
103 %    'layer_callback' => sub {
104 %      my $layer = shift;
105 %      
106 %      my $html = qq!<INPUT TYPE="hidden" NAME="svcdb" VALUE="$layer">!;
107 %
108 %      $html .= $svcdb_info;
109 %
110 %      my $columns = 3;
111 %      my $count = 0;
112 %      my $communigate = 0;
113 %      my @part_export =
114 %        map { qsearch( 'part_export', {exporttype => $_ } ) }
115 %          keys %{FS::part_export::export_info($layer)};
116 %      $html .= '<BR><BR>'. include('/elements/table.html') . 
117 %               "<TR><TH COLSPAN=$columns>Exports</TH></TR><TR>";
118 %      foreach my $part_export ( @part_export ) {
119 %        $communigate++ if $part_export->exporttype =~ /^communigate/;
120 %        $html .= '<TD><INPUT TYPE="checkbox"'.
121 %                 ' NAME="exportnum'. $part_export->exportnum. '"  VALUE="1" ';
122 %        $html .= 'CHECKED'
123 %          if ( $clone || $part_svc->svcpart ) #null svcpart search causing error
124 %              && qsearchs( 'export_svc', {
125 %                                   exportnum => $part_export->exportnum,
126 %                                   svcpart   => $clone || $part_svc->svcpart });
127 %        $html .= '>'.$part_export->exportnum. ': ';
128 %        $html .= $part_export->exportname . '<DIV ALIGN="right"><FONT SIZE=-1>'
129 %          if ( $part_export->exportname );
130 %        $html .= $part_export->exporttype. ' to '. $part_export->machine;
131 %        $html .= '</FONT></DIV>' if ( $part_export->exportname );
132 %        $html .= '</TD>';
133 %        $count++;
134 %        $html .= '</TR><TR>' unless $count % $columns;
135 %      }
136 %      $html .= '</TR></TABLE><BR><BR>'. $mod_info;
137 %
138 %      $html .= include('/elements/table-grid.html', 'cellpadding' => 4 ).
139 %               '<TR>'.
140 %                 '<TH CLASS="grid" BGCOLOR="#cccccc">Field</TH>'.
141 %                 '<TH CLASS="grid" BGCOLOR="#cccccc">Label</TH>'.
142 %                 '<TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=2>Modifier</TH>'.
143 %               '</TR>';
144 %
145 %      my $bgcolor1 = '#eeeeee';
146 %      my $bgcolor2 = '#ffffff';
147 %      my $bgcolor;
148 %
149 %      #yucky kludge
150 %      my @fields = ();
151 %      if ( defined( dbdef->table($layer) ) ) {
152 %        @fields = grep {
153 %            $_ ne 'svcnum'
154 %            && ( $communigate || !$communigate_fields{$layer}->{$_} )
155 %            && ( !FS::part_svc->svc_table_fields($layer)
156 %                   ->{$_}->{disable_part_svc_column}
157 %                 || $part_svc->part_svc_column($_)->columnflag
158 %               )
159 %        } fields($layer);
160 %      }
161 %      push @fields, 'usergroup' 
162 %        if $layer eq 'svc_acct'
163 %          or ( $layer eq 'svc_broadband' and 
164 %               $conf->exists('svc_broadband-radius') ); # double kludge
165 %               # (but we do want to check the config, right?)
166 %      $part_svc->svcpart($clone) if $clone; #haha, undone below
167 %
168 %
169 %      foreach my $field (@fields) {
170 %
171 %        #a few lines of false laziness w/browse/part_svc.cgi
172 %        my $def = FS::part_svc->svc_table_fields($layer)->{$field};
173 %        my $def_info  = $def->{'def_info'};
174 %        my $formatter = $def->{'format'} || sub { shift };
175 %
176 %        my $part_svc_column = $part_svc->part_svc_column($field);
177 %        my $label = $part_svc_column->columnlabel || $def->{'label'};
178 %        my $value = &$formatter($part_svc_column->columnvalue);
179 %        my $flag  = $part_svc_column->columnflag;
180 %
181 %        if ( $bgcolor eq $bgcolor1 ) {
182 %          $bgcolor = $bgcolor2;
183 %        } else {
184 %          $bgcolor = $bgcolor1;
185 %        }
186 %        
187 %        $html .= qq!<TR><TD ROWSPAN=2 CLASS="grid" BGCOLOR="$bgcolor" ALIGN="right">!.
188 %                 ( $def->{'label'} || $field ).
189 %                 "</TD>";
190 %
191 %        $html .= qq!<TD ROWSPAN=2 CLASS="grid" BGCOLOR="$bgcolor"><INPUT NAME="${layer}__${field}_label" VALUE="!. encode_entities($label). '" STYLE="text-align:right"></TD>';
192 %
193 %        $flag = '' if $def->{type} eq 'disabled';
194 %
195 %        $html .= qq!<TD CLASS="grid" BGCOLOR="$bgcolor">!;
196 %
197 %        if ( $def->{type} eq 'disabled' ) {
198 %        
199 %          $html .= 'No default';
200 %
201 %        } else {
202 %
203 %          $html .= qq!<SELECT NAME="${layer}__${field}_flag"!.
204 %                      qq! onChange="${layer}__${field}_flag_changed(this)">!;
205 %
206 %          foreach my $f ( keys %flag ) {
207 %
208 %            # need to template-ize more httemplate/edit/svc_* first
209 %            next if $f eq 'M' and $layer !~ /^svc_(broadband|external|phone|dish)$/;
210 %
211 %            #here is where the SUB from above is called, to skip some choices
212 %            next if $flag{$f}->{condition}
213 %                 && &{ $flag{$f}->{condition} }( $def, $layer, $field );
214 %
215 %            $html .= qq!<OPTION VALUE="$f"!.
216 %                     ' SELECTED'x($flag eq $f ).
217 %                     '>'. $flag{$f}->{desc};
218 %
219 %          }
220 %
221 %          $html .= '</SELECT>';
222 %
223 %          $html .= join("\n",
224 %            '<SCRIPT>',
225 %            "  function ${layer}__${field}_flag_changed(what) {",
226 %            '    var f = what.options[what.selectedIndex].value;',
227 %            '    if ( f == "" || f == "X" ) { //disable',
228 %            "      what.form.${layer}__${field}.disabled = true;".
229 %            "      what.form.${layer}__${field}.style.backgroundColor = '#dddddd';".
230 %            "      if ( what.form.${layer}__${field}_classnum ) {".
231 %            "        what.form.${layer}__${field}_classnum.disabled = true;".
232 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#dddddd';".
233 %            "      }".
234 %            '    } else if ( f == "D" || f == "F" || f =="S" ) { //enable, text box',
235 %            "      what.form.${layer}__${field}.disabled = false;".
236 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
237 %            "      if ( f == 'S' || '${field}' == 'usergroup' ) {". # kludge
238 %            "        what.form.${layer}__${field}.multiple = true;".
239 %            "      } else {".
240 %            "        what.form.${layer}__${field}.multiple = false;".
241 %            "      }".
242 %            "      what.form.${layer}__${field}.style.display = '';".
243 %            "      if ( what.form.${layer}__${field}_classnum ) {".
244 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
245 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
246 %            "        what.form.${layer}__${field}_classnum.style.display = 'none';".
247 %            "      }".
248 %            '    } else if ( f == "M" || f == "A" || f == "H" ) { '.
249 %                   '//enable, inventory',
250 %            "      what.form.${layer}__${field}.disabled = false;".
251 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
252 %            "      what.form.${layer}__${field}.style.display = 'none';".
253 %            "      if ( what.form.${layer}__${field}_classnum ) {".
254 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
255 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
256 %            "        what.form.${layer}__${field}_classnum.style.display = '';".
257 %            "      }".
258 %            '    }',
259 %            '  }',
260 %            '</SCRIPT>',
261 %          );
262 %
263 %        }
264 %
265 %        $html .= qq!</TD><TD CLASS="grid" BGCOLOR="$bgcolor">!;
266 %
267 %        my $disabled = $flag ? ''
268 %                             : 'DISABLED STYLE="background-color: #dddddd"';
269 %        my $nodisplay = ' STYLE="display:none"';
270 %
271 %        if ( !$def->{type} || $def->{type} eq 'text' ) {
272 %
273 %          my $is_inv = ( $flag =~ /^[MA]$/ );
274 %
275 %          $html .=
276 %            qq!<INPUT TYPE="text" NAME="${layer}__${field}" VALUE="$value" !.
277 %            $disabled.
278 %            ( $is_inv ? $nodisplay : $disabled ).
279 %            '>';
280 %
281 %          $html .= include('/elements/select-table.html',
282 %                             'element_name' => "${layer}__${field}_classnum",
283 %                             'element_etc'  => ( $is_inv
284 %                                                   ? $disabled
285 %                                                   : $nodisplay
286 %                                               ),
287 %                             'table'        => 'inventory_class',
288 %                             'name_col'     => 'classname',
289 %                             'value'        => $value,
290 %                             'empty_label'  => 'Select inventory class',
291 %                          );
292 %
293 %        } elsif ( $def->{type} eq 'checkbox' ) {
294 %
295 %          $html .= include('/elements/checkbox.html',
296 %                             'field'      => $layer.'__'.$field,
297 %                             'curr_value' => $value,
298 %                             'value'      => 'Y',
299 %                          );
300 %
301 %        } elsif ( $def->{type} eq 'select' ) {
302 %
303 %          $html .= qq!<SELECT NAME="${layer}__${field}" $disabled!;
304 %          $html .= ' MULTIPLE' if $flag eq 'S';
305 %          $html .= '>';
306 %          $html .= '<OPTION> </OPTION>' unless $value;
307 %          if ( $def->{select_table} ) {
308 %            foreach my $record ( qsearch( $def->{select_table}, {} ) ) {
309 %              my $rvalue = $record->getfield($def->{select_key});
310 %              my $select_label = $def->{select_label};
311 %              $html .= qq!<OPTION VALUE="$rvalue"!.
312 %                  (grep(/^$rvalue$/, split(',',$value)) ? ' SELECTED>' : '>' ).
313 %                  $record->$select_label(). '</OPTION>';
314 %            } #next $record
315 %          } elsif ( $def->{select_list} ) {
316 %            foreach my $item ( @{$def->{select_list}} ) {
317 %              $html .= qq!<OPTION VALUE="$item"!.
318 %                    (grep(/^$item$/, split(',',$value)) ? ' SELECTED>' : '>' ).
319 %                    $item. '</OPTION>';
320 %            } #next $item
321 %          } elsif ( $def->{select_hash} ) {
322 %            if ( ref($def->{select_hash}) eq 'ARRAY' ) {
323 %              tie my %hash, 'Tie::IxHash', @{ $def->{select_hash} };
324 %              $def->{select_hash} = \%hash;
325 %            }
326 %            foreach my $key ( keys %{$def->{select_hash}} ) {
327 %              $html .= qq!<OPTION VALUE="$key"!.
328 %                    (grep(/^$key$/, split(',',$value)) ? ' SELECTED>' : '>' ).
329 %                    $def->{select_hash}{$key}. '</OPTION>';
330 %            } #next $key
331 %          } #endif
332 %          $html .= '</SELECT>';
333 %
334 %        } elsif ( $def->{type} eq 'textarea' ) {
335 %
336 %          $html .=
337 %            qq!<TEXTAREA NAME="${layer}__${field}">!. encode_entities($value).
338 %            '</TEXTAREA>';
339 %
340 %        } elsif ( $def->{type} =~ /select-(.*?).html/ ) {
341 %
342 %          $html .= include("/elements/".$def->{type},
343 %                             'curr_value'   => $value,
344 %                             'element_name' => "${layer}__${field}",
345 %                             'element_etc'  => $disabled,
346 %                             'multiple'     => ($def->{multiple} ||
347 %                                                $flag eq 'S'),
348 %                                 # allow the table def to force 'multiple'
349 %                          );
350 %
351 %        } elsif ( $def->{type} eq 'communigate_pro-accessmodes' ) {
352 %
353 %          $html .= include('/elements/communigate_pro-accessmodes.html',
354 %                             'element_name_prefix' => "${layer}__${field}_",
355 %                             'curr_value'          => $value,
356 %                             #doesn't work#'element_etc'  => $disabled,
357 %                          );
358 %
359 %        } elsif ( $def->{type} eq 'select-hardware' ) {
360 %
361 %          $html .= qq!<INPUT TYPE="text" NAME="${layer}__${field}" $disabled>!;
362 %          $html .= include('/elements/select-hardware_class.html',
363 %                             'curr_value'    => $value,
364 %                             'element_name'  => "${layer}__${field}_classnum",
365 %                             'element_etc'   => $flag ne 'H' && $nodisplay,
366 %                             'empty_label'   => 'Select hardware class',
367 %                          );
368 %
369 %        } elsif ( $def->{type} eq 'disabled' ) {
370 %
371 %          $html .=
372 %            qq!<INPUT TYPE="hidden" NAME="${layer}__${field}" VALUE="">!;
373 %
374 %        } else {
375 %
376 %          $html .= '<font color="#ff0000">unknown type '. $def->{type};
377 %
378 %        }
379 %
380 %        $html .= "</TD></TR>\n";
381
382 %        $def_info = "($def_info)" if $def_info;
383 %        $html .=
384 %          qq!<TR>!.
385 %          qq!  <TD COLSPAN=2 BGCOLOR="$bgcolor" ALIGN="center" !.
386 %          qq!      STYLE="padding:0; border-top: none">!.
387 %          qq!    <FONT SIZE="-1"><I>$def_info</I></FONT>!.
388 %          qq!  </TD>!.
389 %          qq!</TR>\n!;
390 %
391 %      } #foreach my $field (@fields) {
392 %
393 %      $part_svc->svcpart('') if $clone; #undone
394 %      $html .= "</TABLE>";
395 %
396 %      $html .= include('/elements/progress-init.html',
397 %                         $layer, #form name
398 %                         [ qw(svc svcpart selfservice_access disabled preserve
399 %                              exportnum),
400 %                           @fields ],
401 %                         'process/part_svc.cgi',
402 %                         $p.'browse/part_svc.cgi',
403 %                         $layer,
404 %                      );
405 %      $html .= '<BR><INPUT NAME="submit" TYPE="button" VALUE="'.
406 %               ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '" '.
407 %               ' onClick="document.'. "$layer.submit.disabled=true; ".
408 %               "fixup(document.$layer); $layer". 'process();">';
409 %
410 %      #$html .= '<BR><INPUT TYPE="submit" VALUE="'.
411 %      #         ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '">';
412 %
413 %      $html;
414 %
415 %    },
416 %  );
417 %
418 %
419
420 Table <% $widget->html %>
421
422 <% include('/elements/footer.html') %>
423
424 <%init>
425
426 die "access denied"
427   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
428
429 my $conf = FS::Conf->new;
430 my $part_svc;
431 my $clone = '';
432 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {#clone
433   #$cgi->param('clone') =~ /^(\d+)$/ or die "malformed query: $query";
434   $part_svc = qsearchs('part_svc', { 'svcpart'=>$1 } )
435     or die "unknown svcpart: $1";
436   $clone = $part_svc->svcpart;
437   $part_svc->svcpart('');
438 } elsif ( $cgi->keywords ) { #edit
439   my($query) = $cgi->keywords;
440   $query =~ /^(\d+)$/ or die "malformed query: $query";
441   $part_svc=qsearchs('part_svc', { 'svcpart'=>$1 } )
442     or die "unknown svcpart: $1";
443 } else { #adding
444   $part_svc = new FS::part_svc {};
445 }
446
447 my $action = $part_svc->svcpart ? 'Edit' : 'Add';
448 my $hashref = $part_svc->hashref;
449 #   my $p_svcdb = $part_svc->svcdb || 'svc_acct';
450
451 my %communigate_fields = (
452   'svc_acct'        => { map { $_=>1 }
453                            qw( file_quota file_maxnum file_maxsize
454                                password_selfchange password_recover
455                              ),
456                            grep /^cgp_/, fields('svc_acct')
457                        },
458   'svc_domain'      => { map { $_=>1 }
459                            qw( max_accounts trailer parent_svcnum ),
460                            grep /^(cgp|acct_def)_/, fields('svc_domain')
461                        },
462   #'svc_forward'     => { map { $_=>1 } qw( ) },
463   #'svc_mailinglist' => { map { $_=>1 } qw( ) },
464   #'svc_cert'        => { map { $_=>1 } qw( ) },
465 );
466
467 my $svcdb_info = '
468 <TABLE>
469   <TR>
470     <TH ALIGN="left">Generic</TH>
471     <TH ALIGN="left">Access</TH>
472     <TH ALIGN="left">Telephony</TH>
473 <!--    <TH>Hosting</TH>
474     <TH>Colocation</TH>
475 -->
476   </TR>
477   <TR>
478     <TD VALIGN="top">
479       <UL STYLE="margin:0">
480         <LI><B>svc_acct</B>: Accounts - anything with a username (mailbox, shell, RADIUS, etc.)
481         <LI><B>svc_hardware</B>: Equipment supplied to customers
482         <LI><B>svc_external</B>: Externally-tracked service
483       </UL>
484     </TD>
485     <TD VALIGN="top">
486       <UL STYLE="margin:0">
487         <LI><B>svc_dsl</B>: DSL
488         <LI><B>svc_broadband</B>: Wireless broadband
489         <LI><B>svc_dish</B>: DISH Network
490       </UL>
491     </TD>
492     <TD VALIGN="top">
493       <UL STYLE="margin:0">
494         <LI><B>svc_phone</B>: Customer phone number
495         <LI><B>svc_pbx</B>: Customer PBX
496       </UL>
497     </TD>
498   </TR>
499 </TABLE>
500 <BR>
501 <TABLE>
502   <TR>
503     <TH ALIGN="left">Hosting</TH>
504     <TH ALIGN="left">Colocation</TH>
505   </TR>
506     <TD VALIGN="top">
507       <UL STYLE="margin:0">
508         <LI><B>svc_domain</B>: Domain
509         <LI><B>svc_cert</B>: Certificate
510         <LI><B>svc_forward</B>: Mail forwarding
511         <LI><B>svc_mailinglist</B>: Mailing list
512         <LI><B>svc_www</B>: Virtual domain website
513       </UL>
514     </TD>
515     <TD VALIGN="top">
516       <UL STYLE="margin:0">
517         <LI><B>svc_port</B>: Customer router/switch port
518       </UL>
519     </TD>
520   </TR>
521 <TABLE>
522 <!--   <LI>svc_charge - One-time charges (Partially unimplemented)
523        <LI>svc_wo - Work orders (Partially unimplemented)
524 -->
525 ';
526
527 my $mod_info = '
528 For the selected table, you can give fields default or fixed (unchangable)
529 values, or select an inventory class to manually or automatically fill in
530 that field.
531 ';
532
533 </%init>
534
535
536