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