hide unused usage columns
[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 Disable new orders <INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"<% $hashref->{disabled} eq 'Y' ? ' CHECKED' : '' %>><BR>
13 <INPUT TYPE="hidden" NAME="svcpart" VALUE="<% $hashref->{svcpart} %>">
14 <BR>
15 Service definitions are the templates for items you offer to your customers.
16 <UL><LI>svc_acct - Accounts - anything with a username (Mailboxes, PPP accounts, shell accounts, RADIUS entries for broadband, etc.)
17     <LI>svc_domain - Domains
18     <LI>svc_forward - mail forwarding
19     <LI>svc_www - Virtual domain website
20     <LI>svc_broadband - Broadband/High-speed Internet service (always-on)
21     <LI>svc_phone - Customer phone numbers
22     <LI>svc_external - Externally-tracked service
23 <!--   <LI>svc_charge - One-time charges (Partially unimplemented)
24        <LI>svc_wo - Work orders (Partially unimplemented)
25 -->
26 </UL>
27 For the selected table, you can give fields default or fixed (unchangable)
28 values, or select an inventory class to manually or automatically fill in
29 that field.
30 <BR><BR>
31
32 % #YUCK.  false laziness w/part_svc.pm.  go away virtual fields, please
33 % my %vfields;
34 % foreach my $svcdb ( FS::part_svc->svc_tables() ) {
35 %   eval "use FS::$svcdb;";
36 %   my $self = "FS::$svcdb"->new;
37 %   $vfields{$svcdb} = {};
38 %   foreach my $field ($self->virtual_fields) { # svc_Common::virtual_fields with a null svcpart returns all of them
39 %     my $pvf = $self->pvf($field);
40 %     $vfields{$svcdb}->{$field} = $pvf;
41 %     #warn "\$vfields{$svcdb}->{$field} = $pvf";
42 %   } #next $field
43 % } #next $svcdb
44 %
45 %  #code duplication w/ edit/part_svc.cgi, should move this hash to part_svc.pm
46 %  # and generalize the subs
47 %  # condition sub is tested to see whether to disable display of this choice
48 %  # params: ( $def, $layer, $field )  (see SUB below)
49 %  my $inv_sub = sub {
50 %                      $_[0]->{disable_inventory}
51 %                        || $_[0]->{'type'} ne 'text'
52 %                    };
53 %  tie my %flag, 'Tie::IxHash',
54 %    ''  => { 'desc' => 'No default', },
55 %    'D' => { 'desc' => 'Default',
56 %             'condition' =>
57 %               sub { $_[0]->{disable_default} }, 
58 %           },
59 %    'F' => { 'desc' => 'Fixed (unchangeable)',
60 %             'condition' =>
61 %               sub { $_[0]->{disable_fixed} }, 
62 %           },
63 %    'S' => { 'desc' => 'Selectable Choice',
64 %             'condition' =>
65 %               sub { !ref($_[0]) || $_[0]->{disable_select} }, 
66 %           },
67 %# need to template-ize httemplate/edit/svc_* first
68 %#    'M' => { 'desc' => 'Manual selection from inventory',
69 %#             'condition' => $inv_sub,
70 %#           },
71 %    'A' => { 'desc' => 'Automatically fill in from inventory',
72 %             'condition' => $inv_sub,
73 %           },
74 %    'X' => { 'desc' => 'Excluded',
75 %             'condition' =>
76 %               sub { ! $vfields{$_[1]}->{$_[2]} },
77 %
78 %           },
79 %  ;
80 %  
81 %  my @dbs = $hashref->{svcdb}
82 %             ? ( $hashref->{svcdb} )
83 %             : FS::part_svc->svc_tables();
84 %
85 %  tie my %svcdb, 'Tie::IxHash', map { $_=>$_ } grep dbdef->table($_), @dbs;
86 %  my $widget = new HTML::Widgets::SelectLayers(
87 %    #'selected_layer' => $p_svcdb,
88 %    'selected_layer' => $hashref->{svcdb} || 'svc_acct',
89 %    'options'        => \%svcdb,
90 %    'form_name'      => 'dummy',
91 %    #'form_action'    => 'process/part_svc.cgi',
92 %    'form_action'    => 'part_svc.cgi', #self
93 %    'form_text'      => [ qw( svc svcpart ) ],
94 %    'form_checkbox'  => [ 'disabled' ],
95 %    'layer_callback' => sub {
96 %      my $layer = shift;
97 %      
98 %      my $html = qq!<INPUT TYPE="hidden" NAME="svcdb" VALUE="$layer">!;
99 %
100 %      my $columns = 3;
101 %      my $count = 0;
102 %      my @part_export =
103 %        map { qsearch( 'part_export', {exporttype => $_ } ) }
104 %          keys %{FS::part_export::export_info($layer)};
105 %      $html .= '<BR><BR>'. table(). 
106 %               "<TR><TH COLSPAN=$columns>Exports</TH></TR><TR>";
107 %      foreach my $part_export ( @part_export ) {
108 %        $html .= '<TD><INPUT TYPE="checkbox"'.
109 %                 ' NAME="exportnum'. $part_export->exportnum. '"  VALUE="1" ';
110 %        $html .= 'CHECKED'
111 %          if ( $clone || $part_svc->svcpart ) #null svcpart search causing error
112 %              && qsearchs( 'export_svc', {
113 %                                   exportnum => $part_export->exportnum,
114 %                                   svcpart   => $clone || $part_svc->svcpart });
115 %        $html .= '>'. $part_export->exportnum. ': '. $part_export->exporttype.
116 %                 ' to '. $part_export->machine. '</TD>';
117 %        $count++;
118 %        $html .= '</TR><TR>' unless $count % $columns;
119 %      }
120 %      $html .= '</TR></TABLE><BR><BR>';
121 %
122 %      $html .= include('/elements/table-grid.html', 'cellpadding' => 4 ).
123 %               '<TR>'.
124 %                 '<TH CLASS="grid" BGCOLOR="#cccccc">Field</TH>'.
125 %                 '<TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=2>Modifier</TH>'.
126 %               '</TR>';
127 %
128 %      my $bgcolor1 = '#eeeeee';
129 %      my $bgcolor2 = '#ffffff';
130 %      my $bgcolor;
131 %
132 %      #yucky kludge
133 %      my @fields = defined( dbdef->table($layer) )
134 %                      ? grep {
135 %                               $_ ne 'svcnum' &&
136 %                               ( !FS::part_svc->svc_table_fields($layer)
137 %                                     ->{$_}->{disable_part_svc_column}   ||
138 %                                 $part_svc->part_svc_column($_)->columnflag
139 %                               )
140 %                             }
141 %                             fields($layer)
142 %                      : ();
143 %      push @fields, 'usergroup' if $layer eq 'svc_acct'; #kludge
144 %      $part_svc->svcpart($clone) if $clone; #haha, undone below
145 %
146 %
147 %      foreach my $field (@fields) {
148 %
149 %        #my $def = $defs{$layer}{$field};
150 %        my $def = FS::part_svc->svc_table_fields($layer)->{$field};
151 %        my $label = $def->{'def_label'} || $def->{'label'};
152 %        my $formatter = $def->{'format'} || sub { shift };
153 %        my $part_svc_column = $part_svc->part_svc_column($field);
154 %        my $value = &$formatter($part_svc_column->columnvalue);
155 %        my $flag = $part_svc_column->columnflag;
156 %
157 %        if ( $bgcolor eq $bgcolor1 ) {
158 %          $bgcolor = $bgcolor2;
159 %        } else {
160 %          $bgcolor = $bgcolor1;
161 %        }
162 %        
163 %        $html .= qq!<TR><TD CLASS="grid" BGCOLOR="$bgcolor" ALIGN="right">!.
164 %                 ( $label || $field ).
165 %                 "</TD>";
166 %        $flag = '' if $def->{type} eq 'disabled';
167 %
168 %        $html .= qq!<TD CLASS="grid" BGCOLOR="$bgcolor">!;
169 %
170 %        if ( $def->{type} eq 'disabled' ) {
171 %        
172 %          $html .= 'No default';
173 %
174 %        } else {
175 %
176 %          $html .= qq!<SELECT NAME="${layer}__${field}_flag"!.
177 %                      qq! onChange="${layer}__${field}_flag_changed(this)">!;
178 %
179 %          foreach my $f ( keys %flag ) {
180 %
181 %            #here is where the SUB from above is called, to skip some choices
182 %            next if $flag{$f}->{condition}
183 %                 && &{ $flag{$f}->{condition} }( $def, $layer, $field );
184 %
185 %            $html .= qq!<OPTION VALUE="$f"!.
186 %                     ' SELECTED'x($flag eq $f ).
187 %                     '>'. $flag{$f}->{desc};
188 %
189 %          }
190 %
191 %          $html .= '</SELECT>';
192 %
193 %          $html .= join("\n",
194 %            '<SCRIPT>',
195 %            "  function ${layer}__${field}_flag_changed(what) {",
196 %            '    var f = what.options[what.selectedIndex].value;',
197 %            '    if ( f == "" || f == "X" ) { //disable',
198 %            "      what.form.${layer}__${field}.disabled = true;".
199 %            "      what.form.${layer}__${field}.style.backgroundColor = '#dddddd';".
200 %            "      if ( what.form.${layer}__${field}_classnum ) {".
201 %            "        what.form.${layer}__${field}_classnum.disabled = true;".
202 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#dddddd';".
203 %            "      }".
204 %            '    } else if ( f == "D" || f == "F" || f =="S" ) { //enable, text box',
205 %            "      what.form.${layer}__${field}.disabled = false;".
206 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
207 %            "      if ( f == 'S' || '${field}' == 'usergroup' ) {". # kludge
208 %            "        what.form.${layer}__${field}.multiple = true;".
209 %            "      } else {".
210 %            "        what.form.${layer}__${field}.multiple = false;".
211 %            "      }".
212 %            "      what.form.${layer}__${field}.style.display = '';".
213 %            "      if ( what.form.${layer}__${field}_classnum ) {".
214 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
215 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
216 %            "        what.form.${layer}__${field}_classnum.style.display = 'none';".
217 %            "      }".
218 %            '    } else if ( f == "M" || f == "A" ) { //enable, inventory',
219 %            "      what.form.${layer}__${field}.disabled = false;".
220 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
221 %            "      what.form.${layer}__${field}.style.display = 'none';".
222 %            "      if ( what.form.${layer}__${field}_classnum ) {".
223 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
224 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
225 %            "        what.form.${layer}__${field}_classnum.style.display = '';".
226 %            "      }".
227 %            '    }',
228 %            '  }',
229 %            '</SCRIPT>',
230 %          );
231 %
232 %        }
233 %
234 %        $html .= qq!</TD><TD CLASS="grid" BGCOLOR="$bgcolor">!;
235 %
236 %        my $disabled = $flag ? ''
237 %                             : 'DISABLED STYLE="background-color: #dddddd"';
238 %
239 %        if ( !$def->{type} || $def->{type} eq 'text' ) {
240 %
241 %          my $nodisplay = ' STYLE="display:none"';
242 %          my $is_inv = ( $flag =~ /^[MA]$/ );
243 %
244 %          $html .=
245 %            qq!<INPUT TYPE="text" NAME="${layer}__${field}" VALUE="$value" !.
246 %            $disabled.
247 %            ( $is_inv ? $nodisplay : $disabled ).
248 %            '>';
249 %
250 %          $html .= include('/elements/select-table.html',
251 %                             'element_name' => "${layer}__${field}_classnum",
252 %                             'element_etc'  => ( $is_inv
253 %                                                   ? $disabled
254 %                                                   : $nodisplay
255 %                                               ),
256 %                             'table'        => 'inventory_class',
257 %                             'name_col'     => 'classname',
258 %                             'value'        => $value,
259 %                             'empty_label'  => 'Select inventory class',
260 %                          );
261 %
262 %        } elsif ( $def->{type} eq 'select' ) {
263 %
264 %          $html .= qq!<SELECT NAME="${layer}__${field}" $disabled!;
265 %          $html .= ' MULTIPLE' if $flag eq 'S';
266 %          $html .= '>';
267 %          $html .= '<OPTION> </OPTION>' unless $value;
268 %          if ( $def->{select_table} ) {
269 %            foreach my $record ( qsearch( $def->{select_table}, {} ) ) {
270 %              my $rvalue = $record->getfield($def->{select_key});
271 %              my $select_label = $def->{select_label};
272 %              $html .= qq!<OPTION VALUE="$rvalue"!.
273 %                  (grep(/^$rvalue$/, split(',',$value)) ? ' SELECTED>' : '>' ).
274 %                  $record->$select_label(). '</OPTION>';
275 %            } #next $record
276 %          } else { # select_list
277 %            foreach my $item ( @{$def->{select_list}} ) {
278 %              $html .= qq!<OPTION VALUE="$item"!.
279 %                    (grep(/^$item$/, split(',',$value)) ? ' SELECTED>' : '>' ).
280 %                    $item. '</OPTION>';
281 %            } #next $item
282 %          } #endif
283 %          $html .= '</SELECT>';
284 %
285 %        } elsif ( $def->{type} eq 'radius_usergroup_selector' ) {
286 %
287 %          #XXX disable the RADIUS usergroup selector?  ugh it sure does need
288 %          #an overhaul, people have dum group problems because of it
289 %
290 %          $html .= FS::svc_acct::radius_usergroup_selector(
291 %            [ split(',', $value) ], "${layer}__${field}" );
292 %
293 %        } elsif ( $def->{type} eq 'disabled' ) {
294 %
295 %          $html .=
296 %            qq!<INPUT TYPE="hidden" NAME="${layer}__${field}" VALUE="">!;
297 %
298 %        } else {
299 %
300 %          $html .= '<font color="#ff0000">unknown type'. $def->{type};
301 %
302 %        }
303 %
304 %        $html .= "</TD></TR>\n";
305 %
306 %      } #foreach my $field (@fields) {
307 %
308 %      $part_svc->svcpart('') if $clone; #undone
309 %      $html .= "</TABLE>";
310 %
311 %      $html .= include('/elements/progress-init.html',
312 %                         $layer, #form name
313 %                         [ qw(svc svcpart disabled exportnum), @fields ],
314 %                         'process/part_svc.cgi',
315 %                         $p.'browse/part_svc.cgi',
316 %                         $layer,
317 %                      );
318 %      $html .= '<BR><INPUT NAME="submit" TYPE="button" VALUE="'.
319 %               ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '" '.
320 %               ' onClick="document.'. "$layer.submit.disabled=true; ".
321 %               "fixup(document.$layer); $layer". 'process();">';
322 %
323 %      #$html .= '<BR><INPUT TYPE="submit" VALUE="'.
324 %      #         ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '">';
325 %
326 %      $html;
327 %
328 %    },
329 %  );
330 %
331 %
332
333 Table <% $widget->html %>
334
335 <% include('/elements/footer.html') %>
336
337 <%init>
338
339 die "access denied"
340   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
341
342 my $part_svc;
343 my $clone = '';
344 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {#clone
345   #$cgi->param('clone') =~ /^(\d+)$/ or die "malformed query: $query";
346   $part_svc = qsearchs('part_svc', { 'svcpart'=>$1 } )
347     or die "unknown svcpart: $1";
348   $clone = $part_svc->svcpart;
349   $part_svc->svcpart('');
350 } elsif ( $cgi->keywords ) { #edit
351   my($query) = $cgi->keywords;
352   $query =~ /^(\d+)$/ or die "malformed query: $query";
353   $part_svc=qsearchs('part_svc', { 'svcpart'=>$1 } )
354     or die "unknown svcpart: $1";
355 } else { #adding
356   $part_svc = new FS::part_svc {};
357 }
358
359 my $action = $part_svc->svcpart ? 'Edit' : 'Add';
360 my $hashref = $part_svc->hashref;
361 #   my $p_svcdb = $part_svc->svcdb || 'svc_acct';
362
363
364
365 </%init>
366
367
368