e899ad073dc857d9ab2de0d570fcd5556849ee90
[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 <INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"<% $hashref->{disabled} eq 'Y' ? ' CHECKED' : '' %>>&nbsp;Disable new orders<BR>
13 <INPUT TYPE="checkbox" NAME="preserve" VALUE="Y"<% $hashref->{'preserve'} eq 'Y' ? ' CHECKED' : '' %>>&nbsp;Preserve this service on package cancellation<BR>
14 <INPUT TYPE="hidden" NAME="svcpart" VALUE="<% $hashref->{svcpart} %>">
15
16 <BR>
17
18
19 % #YUCK.  false laziness w/part_svc.pm.  go away virtual fields, please
20 % my %vfields;
21 % foreach my $svcdb ( FS::part_svc->svc_tables() ) {
22 %   eval "use FS::$svcdb;";
23 %   my $self = "FS::$svcdb"->new;
24 %   $vfields{$svcdb} = {};
25 %   foreach my $field ($self->virtual_fields) { # svc_Common::virtual_fields with a null svcpart returns all of them
26 %     my $pvf = $self->pvf($field);
27 %     $vfields{$svcdb}->{$field} = $pvf;
28 %     #warn "\$vfields{$svcdb}->{$field} = $pvf";
29 %   } #next $field
30 % } #next $svcdb
31 %
32 %  #code duplication w/ edit/part_svc.cgi, should move this hash to part_svc.pm
33 %  # and generalize the subs
34 %  # condition sub is tested to see whether to disable display of this choice
35 %  # params: ( $def, $layer, $field )  (see SUB below)
36 %  my $inv_sub = sub {
37 %                      $_[0]->{disable_inventory}
38 %                        || $_[0]->{'type'} ne 'text'
39 %                    };
40 %  tie my %flag, 'Tie::IxHash',
41 %    ''  => { 'desc' => 'No default', },
42 %    'D' => { 'desc' => 'Default',
43 %             'condition' =>
44 %               sub { $_[0]->{disable_default} }, 
45 %           },
46 %    'F' => { 'desc' => 'Fixed (unchangeable)',
47 %             'condition' =>
48 %               sub { $_[0]->{disable_fixed} }, 
49 %           },
50 %    'S' => { 'desc' => 'Selectable Choice',
51 %             'condition' =>
52 %               sub { !ref($_[0]) || $_[0]->{disable_select} }, 
53 %           },
54 %    'M' => { 'desc' => 'Manual selection from inventory',
55 %             'condition' => $inv_sub,
56 %           },
57 %    'A' => { 'desc' => 'Automatically fill in from inventory',
58 %             'condition' => $inv_sub,
59 %           },
60 %    'H' => { 'desc' => 'Select from hardware class',
61 %             'condition' => sub { $_[0]->{type} ne 'select-hardware' },
62 %           },
63 %    'X' => { 'desc' => 'Excluded',
64 %             'condition' =>
65 %               sub { ! $vfields{$_[1]}->{$_[2]} },
66 %
67 %           },
68 %  ;
69 %  
70 %  my @dbs = $hashref->{svcdb}
71 %             ? ( $hashref->{svcdb} )
72 %             : FS::part_svc->svc_tables();
73 %
74 %  tie my %svcdb, 'Tie::IxHash', map { $_=>$_ } grep dbdef->table($_), @dbs;
75 %  my $widget = new HTML::Widgets::SelectLayers(
76 %    #'selected_layer' => $p_svcdb,
77 %    'selected_layer' => $hashref->{svcdb} || 'svc_acct',
78 %    'options'        => \%svcdb,
79 %    'form_name'      => 'dummy',
80 %    #'form_action'    => 'process/part_svc.cgi',
81 %    'form_action'    => 'part_svc.cgi', #self
82 %    'form_text'      => [ qw( svc svcpart ) ],
83 %    'form_checkbox'  => [ 'disabled', 'preserve' ],
84 %    'layer_callback' => sub {
85 %      my $layer = shift;
86 %      
87 %      my $html = qq!<INPUT TYPE="hidden" NAME="svcdb" VALUE="$layer">!;
88 %
89 %      $html .= $svcdb_info;
90 %
91 %      my $columns = 3;
92 %      my $count = 0;
93 %      my $communigate = 0;
94 %      my @part_export =
95 %        map { qsearch( 'part_export', {exporttype => $_ } ) }
96 %          keys %{FS::part_export::export_info($layer)};
97 %      $html .= '<BR><BR>'. include('/elements/table.html') . 
98 %               "<TR><TH COLSPAN=$columns>Exports</TH></TR><TR>";
99 %      foreach my $part_export ( @part_export ) {
100 %        $communigate++ if $part_export->exporttype =~ /^communigate/;
101 %        $html .= '<TD><INPUT TYPE="checkbox"'.
102 %                 ' NAME="exportnum'. $part_export->exportnum. '"  VALUE="1" ';
103 %        $html .= 'CHECKED'
104 %          if ( $clone || $part_svc->svcpart ) #null svcpart search causing error
105 %              && qsearchs( 'export_svc', {
106 %                                   exportnum => $part_export->exportnum,
107 %                                   svcpart   => $clone || $part_svc->svcpart });
108 %        $html .= '>'.$part_export->exportnum. ': ';
109 %        $html .= $part_export->exportname . '<DIV ALIGN="right"><FONT SIZE=-1>'
110 %          if ( $part_export->exportname );
111 %        $html .= $part_export->exporttype. ' to '. $part_export->machine;
112 %        $html .= '</FONT></DIV>' if ( $part_export->exportname );
113 %        $html .= '</TD>';
114 %        $count++;
115 %        $html .= '</TR><TR>' unless $count % $columns;
116 %      }
117 %      $html .= '</TR></TABLE><BR><BR>'. $mod_info;
118 %
119 %      $html .= include('/elements/table-grid.html', 'cellpadding' => 4 ).
120 %               '<TR>'.
121 %                 '<TH CLASS="grid" BGCOLOR="#cccccc">Field</TH>'.
122 %                 '<TH CLASS="grid" BGCOLOR="#cccccc">Label</TH>'.
123 %                 '<TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=2>Modifier</TH>'.
124 %               '</TR>';
125 %
126 %      my $bgcolor1 = '#eeeeee';
127 %      my $bgcolor2 = '#ffffff';
128 %      my $bgcolor;
129 %
130 %      #yucky kludge
131 %      my @fields = ();
132 %      if ( defined( dbdef->table($layer) ) ) {
133 %        @fields = grep {
134 %            $_ ne 'svcnum'
135 %            && ( $communigate || !$communigate_fields{$layer}->{$_} )
136 %            && ( !FS::part_svc->svc_table_fields($layer)
137 %                   ->{$_}->{disable_part_svc_column}
138 %                 || $part_svc->part_svc_column($_)->columnflag
139 %               )
140 %        } fields($layer);
141 %      }
142 %      push @fields, 'usergroup' 
143 %        if $layer eq 'svc_acct'
144 %          or ( $layer eq 'svc_broadband' and 
145 %               $conf->exists('svc_broadband-radius') ); # double kludge
146 %               # (but we do want to check the config, right?)
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|dish)$/;
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" || f == "H" ) { '.
230 %                   '//enable, inventory',
231 %            "      what.form.${layer}__${field}.disabled = false;".
232 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
233 %            "      what.form.${layer}__${field}.style.display = 'none';".
234 %            "      if ( what.form.${layer}__${field}_classnum ) {".
235 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
236 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
237 %            "        what.form.${layer}__${field}_classnum.style.display = '';".
238 %            "      }".
239 %            '    }',
240 %            '  }',
241 %            '</SCRIPT>',
242 %          );
243 %
244 %        }
245 %
246 %        $html .= qq!</TD><TD CLASS="grid" BGCOLOR="$bgcolor">!;
247 %
248 %        my $disabled = $flag ? ''
249 %                             : 'DISABLED STYLE="background-color: #dddddd"';
250 %        my $nodisplay = ' STYLE="display:none"';
251 %
252 %        if ( !$def->{type} || $def->{type} eq 'text' ) {
253 %
254 %          my $is_inv = ( $flag =~ /^[MA]$/ );
255 %
256 %          $html .=
257 %            qq!<INPUT TYPE="text" NAME="${layer}__${field}" VALUE="$value" !.
258 %            $disabled.
259 %            ( $is_inv ? $nodisplay : $disabled ).
260 %            '>';
261 %
262 %          $html .= include('/elements/select-table.html',
263 %                             'element_name' => "${layer}__${field}_classnum",
264 %                             'element_etc'  => ( $is_inv
265 %                                                   ? $disabled
266 %                                                   : $nodisplay
267 %                                               ),
268 %                             'table'        => 'inventory_class',
269 %                             'name_col'     => 'classname',
270 %                             'value'        => $value,
271 %                             'empty_label'  => 'Select inventory class',
272 %                          );
273 %
274 %        } elsif ( $def->{type} eq 'checkbox' ) {
275 %
276 %          $html .= include('/elements/checkbox.html',
277 %                             'field'      => $layer.'__'.$field,
278 %                             'curr_value' => $value,
279 %                             'value'      => 'Y',
280 %                          );
281 %
282 %        } elsif ( $def->{type} eq 'select' ) {
283 %
284 %          $html .= qq!<SELECT NAME="${layer}__${field}" $disabled!;
285 %          $html .= ' MULTIPLE' if $flag eq 'S';
286 %          $html .= '>';
287 %          $html .= '<OPTION> </OPTION>' unless $value;
288 %          if ( $def->{select_table} ) {
289 %            foreach my $record ( qsearch( $def->{select_table}, {} ) ) {
290 %              my $rvalue = $record->getfield($def->{select_key});
291 %              my $select_label = $def->{select_label};
292 %              $html .= qq!<OPTION VALUE="$rvalue"!.
293 %                  (grep(/^$rvalue$/, split(',',$value)) ? ' SELECTED>' : '>' ).
294 %                  $record->$select_label(). '</OPTION>';
295 %            } #next $record
296 %          } elsif ( $def->{select_list} ) {
297 %            foreach my $item ( @{$def->{select_list}} ) {
298 %              $html .= qq!<OPTION VALUE="$item"!.
299 %                    (grep(/^$item$/, split(',',$value)) ? ' SELECTED>' : '>' ).
300 %                    $item. '</OPTION>';
301 %            } #next $item
302 %          } elsif ( $def->{select_hash} ) {
303 %            if ( ref($def->{select_hash}) eq 'ARRAY' ) {
304 %              tie my %hash, 'Tie::IxHash', @{ $def->{select_hash} };
305 %              $def->{select_hash} = \%hash;
306 %            }
307 %            foreach my $key ( keys %{$def->{select_hash}} ) {
308 %              $html .= qq!<OPTION VALUE="$key"!.
309 %                    (grep(/^$key$/, split(',',$value)) ? ' SELECTED>' : '>' ).
310 %                    $def->{select_hash}{$key}. '</OPTION>';
311 %            } #next $key
312 %          } #endif
313 %          $html .= '</SELECT>';
314 %
315 %        } elsif ( $def->{type} eq 'textarea' ) {
316 %
317 %          $html .=
318 %            qq!<TEXTAREA NAME="${layer}__${field}">!. encode_entities($value).
319 %            '</TEXTAREA>';
320 %
321 %        } elsif ( $def->{type} =~ /select-(.*?).html/ ) {
322 %
323 %          $html .= include("/elements/".$def->{type},
324 %                             'curr_value'   => $value,
325 %                             'element_name' => "${layer}__${field}",
326 %                             'element_etc'  => $disabled,
327 %                             'multiple'     => ($def->{multiple} ||
328 %                                                $flag eq 'S'),
329 %                                 # allow the table def to force 'multiple'
330 %                          );
331 %
332 %        } elsif ( $def->{type} eq 'communigate_pro-accessmodes' ) {
333 %
334 %          $html .= include('/elements/communigate_pro-accessmodes.html',
335 %                             'element_name_prefix' => "${layer}__${field}_",
336 %                             'curr_value'          => $value,
337 %                             #doesn't work#'element_etc'  => $disabled,
338 %                          );
339 %
340 %        } elsif ( $def->{type} eq 'select-hardware' ) {
341 %
342 %          $html .= qq!<INPUT TYPE="text" NAME="${layer}__${field}" $disabled>!;
343 %          $html .= include('/elements/select-hardware_class.html',
344 %                             'curr_value'    => $value,
345 %                             'element_name'  => "${layer}__${field}_classnum",
346 %                             'element_etc'   => $flag ne 'H' && $nodisplay,
347 %                             'empty_label'   => 'Select hardware class',
348 %                          );
349 %
350 %        } elsif ( $def->{type} eq 'disabled' ) {
351 %
352 %          $html .=
353 %            qq!<INPUT TYPE="hidden" NAME="${layer}__${field}" VALUE="">!;
354 %
355 %        } else {
356 %
357 %          $html .= '<font color="#ff0000">unknown type '. $def->{type};
358 %
359 %        }
360 %
361 %        $html .= "</TD></TR>\n";
362
363 %        $def_info = "($def_info)" if $def_info;
364 %        $html .=
365 %          qq!<TR>!.
366 %          qq!  <TD COLSPAN=2 BGCOLOR="$bgcolor" ALIGN="center" !.
367 %          qq!      STYLE="padding:0; border-top: none">!.
368 %          qq!    <FONT SIZE="-1"><I>$def_info</I></FONT>!.
369 %          qq!  </TD>!.
370 %          qq!</TR>\n!;
371 %
372 %      } #foreach my $field (@fields) {
373 %
374 %      $part_svc->svcpart('') if $clone; #undone
375 %      $html .= "</TABLE>";
376 %
377 %      $html .= include('/elements/progress-init.html',
378 %                         $layer, #form name
379 %                         [ qw(svc svcpart disabled preserve exportnum),
380 %                           @fields ],
381 %                         'process/part_svc.cgi',
382 %                         $p.'browse/part_svc.cgi',
383 %                         $layer,
384 %                      );
385 %      $html .= '<BR><INPUT NAME="submit" TYPE="button" VALUE="'.
386 %               ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '" '.
387 %               ' onClick="document.'. "$layer.submit.disabled=true; ".
388 %               "fixup(document.$layer); $layer". 'process();">';
389 %
390 %      #$html .= '<BR><INPUT TYPE="submit" VALUE="'.
391 %      #         ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '">';
392 %
393 %      $html;
394 %
395 %    },
396 %  );
397 %
398 %
399
400 Table <% $widget->html %>
401
402 <% include('/elements/footer.html') %>
403
404 <%init>
405
406 die "access denied"
407   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
408
409 my $conf = FS::Conf->new;
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 my $svcdb_info = '
448 <TABLE>
449   <TR>
450     <TH ALIGN="left">Generic</TH>
451     <TH ALIGN="left">Access</TH>
452     <TH ALIGN="left">Telephony</TH>
453 <!--    <TH>Hosting</TH>
454     <TH>Colocation</TH>
455 -->
456   </TR>
457   <TR>
458     <TD VALIGN="top">
459       <UL STYLE="margin:0">
460         <LI><B>svc_acct</B>: Accounts - anything with a username (mailbox, shell, RADIUS, etc.)
461         <LI><B>svc_hardware</B>: Equipment supplied to customers
462         <LI><B>svc_external</B>: Externally-tracked service
463       </UL>
464     </TD>
465     <TD VALIGN="top">
466       <UL STYLE="margin:0">
467         <LI><B>svc_dsl</B>: DSL
468         <LI><B>svc_broadband</B>: Wireless broadband
469         <LI><B>svc_dish</B>: DISH Network
470       </UL>
471     </TD>
472     <TD VALIGN="top">
473       <UL STYLE="margin:0">
474         <LI><B>svc_phone</B>: Customer phone number
475         <LI><B>svc_pbx</B>: Customer PBX
476       </UL>
477     </TD>
478   </TR>
479 </TABLE>
480 <BR>
481 <TABLE>
482   <TR>
483     <TH ALIGN="left">Hosting</TH>
484     <TH ALIGN="left">Colocation</TH>
485   </TR>
486     <TD VALIGN="top">
487       <UL STYLE="margin:0">
488         <LI><B>svc_domain</B>: Domain
489         <LI><B>svc_cert</B>: Certificate
490         <LI><B>svc_forward</B>: Mail forwarding
491         <LI><B>svc_mailinglist</B>: Mailing list
492         <LI><B>svc_www</B>: Virtual domain website
493       </UL>
494     </TD>
495     <TD VALIGN="top">
496       <UL STYLE="margin:0">
497         <LI><B>svc_port</B>: Customer router/switch port
498       </UL>
499     </TD>
500   </TR>
501 <TABLE>
502 <!--   <LI>svc_charge - One-time charges (Partially unimplemented)
503        <LI>svc_wo - Work orders (Partially unimplemented)
504 -->
505 ';
506
507 my $mod_info = '
508 For the selected table, you can give fields default or fixed (unchangable)
509 values, or select an inventory class to manually or automatically fill in
510 that field.
511 ';
512
513 </%init>
514
515
516