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