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