This commit was generated by cvs2svn to compensate for changes in r8593,
[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">Label</TH>'.
126 %                 '<TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=2>Modifier</TH>'.
127 %               '</TR>';
128 %
129 %      my $bgcolor1 = '#eeeeee';
130 %      my $bgcolor2 = '#ffffff';
131 %      my $bgcolor;
132 %
133 %      #yucky kludge
134 %      my @fields = defined( dbdef->table($layer) )
135 %                      ? grep {
136 %                               $_ ne 'svcnum' &&
137 %                               ( !FS::part_svc->svc_table_fields($layer)
138 %                                     ->{$_}->{disable_part_svc_column}   ||
139 %                                 $part_svc->part_svc_column($_)->columnflag
140 %                               )
141 %                             }
142 %                             fields($layer)
143 %                      : ();
144 %      push @fields, 'usergroup' if $layer eq 'svc_acct'; #kludge
145 %      $part_svc->svcpart($clone) if $clone; #haha, undone below
146 %
147 %
148 %      foreach my $field (@fields) {
149 %
150 %        #a few lines of false laziness w/browse/part_svc.cgi
151 %        my $def = FS::part_svc->svc_table_fields($layer)->{$field};
152 %        my $def_info  = $def->{'def_info'};
153 %        my $formatter = $def->{'format'} || sub { shift };
154 %
155 %        my $part_svc_column = $part_svc->part_svc_column($field);
156 %        my $label = $part_svc_column->columnlabel || $def->{'label'};
157 %        my $value = &$formatter($part_svc_column->columnvalue);
158 %        my $flag  = $part_svc_column->columnflag;
159 %
160 %        if ( $bgcolor eq $bgcolor1 ) {
161 %          $bgcolor = $bgcolor2;
162 %        } else {
163 %          $bgcolor = $bgcolor1;
164 %        }
165 %        
166 %        $html .= qq!<TR><TD ROWSPAN=2 CLASS="grid" BGCOLOR="$bgcolor" ALIGN="right">!.
167 %                 ( $def->{'label'} || $field ).
168 %                 "</TD>";
169 %
170 %        $html .= qq!<TD ROWSPAN=2 CLASS="grid" BGCOLOR="$bgcolor"><INPUT NAME="${layer}__${field}_label" VALUE="!. encode_entities($label). '" STYLE="text-align:right"></TD>';
171 %
172 %        $flag = '' if $def->{type} eq 'disabled';
173 %
174 %        $html .= qq!<TD CLASS="grid" BGCOLOR="$bgcolor">!;
175 %
176 %        if ( $def->{type} eq 'disabled' ) {
177 %        
178 %          $html .= 'No default';
179 %
180 %        } else {
181 %
182 %          $html .= qq!<SELECT NAME="${layer}__${field}_flag"!.
183 %                      qq! onChange="${layer}__${field}_flag_changed(this)">!;
184 %
185 %          foreach my $f ( keys %flag ) {
186 %
187 %            #here is where the SUB from above is called, to skip some choices
188 %            next if $flag{$f}->{condition}
189 %                 && &{ $flag{$f}->{condition} }( $def, $layer, $field );
190 %
191 %            $html .= qq!<OPTION VALUE="$f"!.
192 %                     ' SELECTED'x($flag eq $f ).
193 %                     '>'. $flag{$f}->{desc};
194 %
195 %          }
196 %
197 %          $html .= '</SELECT>';
198 %
199 %          $html .= join("\n",
200 %            '<SCRIPT>',
201 %            "  function ${layer}__${field}_flag_changed(what) {",
202 %            '    var f = what.options[what.selectedIndex].value;',
203 %            '    if ( f == "" || f == "X" ) { //disable',
204 %            "      what.form.${layer}__${field}.disabled = true;".
205 %            "      what.form.${layer}__${field}.style.backgroundColor = '#dddddd';".
206 %            "      if ( what.form.${layer}__${field}_classnum ) {".
207 %            "        what.form.${layer}__${field}_classnum.disabled = true;".
208 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#dddddd';".
209 %            "      }".
210 %            '    } else if ( f == "D" || f == "F" || f =="S" ) { //enable, text box',
211 %            "      what.form.${layer}__${field}.disabled = false;".
212 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
213 %            "      if ( f == 'S' || '${field}' == 'usergroup' ) {". # kludge
214 %            "        what.form.${layer}__${field}.multiple = true;".
215 %            "      } else {".
216 %            "        what.form.${layer}__${field}.multiple = false;".
217 %            "      }".
218 %            "      what.form.${layer}__${field}.style.display = '';".
219 %            "      if ( what.form.${layer}__${field}_classnum ) {".
220 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
221 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
222 %            "        what.form.${layer}__${field}_classnum.style.display = 'none';".
223 %            "      }".
224 %            '    } else if ( f == "M" || f == "A" ) { //enable, inventory',
225 %            "      what.form.${layer}__${field}.disabled = false;".
226 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
227 %            "      what.form.${layer}__${field}.style.display = 'none';".
228 %            "      if ( what.form.${layer}__${field}_classnum ) {".
229 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
230 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
231 %            "        what.form.${layer}__${field}_classnum.style.display = '';".
232 %            "      }".
233 %            '    }',
234 %            '  }',
235 %            '</SCRIPT>',
236 %          );
237 %
238 %        }
239 %
240 %        $html .= qq!</TD><TD CLASS="grid" BGCOLOR="$bgcolor">!;
241 %
242 %        my $disabled = $flag ? ''
243 %                             : 'DISABLED STYLE="background-color: #dddddd"';
244 %
245 %        if ( !$def->{type} || $def->{type} eq 'text' ) {
246 %
247 %          my $nodisplay = ' STYLE="display:none"';
248 %          my $is_inv = ( $flag =~ /^[MA]$/ );
249 %
250 %          $html .=
251 %            qq!<INPUT TYPE="text" NAME="${layer}__${field}" VALUE="$value" !.
252 %            $disabled.
253 %            ( $is_inv ? $nodisplay : $disabled ).
254 %            '>';
255 %
256 %          $html .= include('/elements/select-table.html',
257 %                             'element_name' => "${layer}__${field}_classnum",
258 %                             'element_etc'  => ( $is_inv
259 %                                                   ? $disabled
260 %                                                   : $nodisplay
261 %                                               ),
262 %                             'table'        => 'inventory_class',
263 %                             'name_col'     => 'classname',
264 %                             'value'        => $value,
265 %                             'empty_label'  => 'Select inventory class',
266 %                          );
267 %
268 %        } elsif ( $def->{type} eq 'select' ) {
269 %
270 %          $html .= qq!<SELECT NAME="${layer}__${field}" $disabled!;
271 %          $html .= ' MULTIPLE' if $flag eq 'S';
272 %          $html .= '>';
273 %          $html .= '<OPTION> </OPTION>' unless $value;
274 %          if ( $def->{select_table} ) {
275 %            foreach my $record ( qsearch( $def->{select_table}, {} ) ) {
276 %              my $rvalue = $record->getfield($def->{select_key});
277 %              my $select_label = $def->{select_label};
278 %              $html .= qq!<OPTION VALUE="$rvalue"!.
279 %                  (grep(/^$rvalue$/, split(',',$value)) ? ' SELECTED>' : '>' ).
280 %                  $record->$select_label(). '</OPTION>';
281 %            } #next $record
282 %          } else { # select_list
283 %            foreach my $item ( @{$def->{select_list}} ) {
284 %              $html .= qq!<OPTION VALUE="$item"!.
285 %                    (grep(/^$item$/, split(',',$value)) ? ' SELECTED>' : '>' ).
286 %                    $item. '</OPTION>';
287 %            } #next $item
288 %          } #endif
289 %          $html .= '</SELECT>';
290 %
291 %        } elsif ( $def->{type} eq 'radius_usergroup_selector' ) {
292 %
293 %          #XXX disable the RADIUS usergroup selector?  ugh it sure does need
294 %          #an overhaul, people have dum group problems because of it
295 %
296 %          $html .= FS::svc_acct::radius_usergroup_selector(
297 %            [ split(',', $value) ], "${layer}__${field}" );
298 %
299 %        } elsif ( $def->{type} eq 'disabled' ) {
300 %
301 %          $html .=
302 %            qq!<INPUT TYPE="hidden" NAME="${layer}__${field}" VALUE="">!;
303 %
304 %        } else {
305 %
306 %          $html .= '<font color="#ff0000">unknown type'. $def->{type};
307 %
308 %        }
309 %
310 %        $html .= "</TD></TR>\n";
311
312 %        $def_info = "($def_info)" if $def_info;
313 %        $html .=
314 %          qq!<TR>!.
315 %          qq!  <TD COLSPAN=2 BGCOLOR="$bgcolor" ALIGN="center" !.
316 %          qq!      STYLE="padding:0; border-top: none">!.
317 %          qq!    <FONT SIZE="-1"><I>$def_info</I></FONT>!.
318 %          qq!  </TD>!.
319 %          qq!</TR>\n!;
320 %
321 %      } #foreach my $field (@fields) {
322 %
323 %      $part_svc->svcpart('') if $clone; #undone
324 %      $html .= "</TABLE>";
325 %
326 %      $html .= include('/elements/progress-init.html',
327 %                         $layer, #form name
328 %                         [ qw(svc svcpart disabled exportnum), @fields ],
329 %                         'process/part_svc.cgi',
330 %                         $p.'browse/part_svc.cgi',
331 %                         $layer,
332 %                      );
333 %      $html .= '<BR><INPUT NAME="submit" TYPE="button" VALUE="'.
334 %               ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '" '.
335 %               ' onClick="document.'. "$layer.submit.disabled=true; ".
336 %               "fixup(document.$layer); $layer". 'process();">';
337 %
338 %      #$html .= '<BR><INPUT TYPE="submit" VALUE="'.
339 %      #         ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '">';
340 %
341 %      $html;
342 %
343 %    },
344 %  );
345 %
346 %
347
348 Table <% $widget->html %>
349
350 <% include('/elements/footer.html') %>
351
352 <%init>
353
354 die "access denied"
355   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
356
357 my $part_svc;
358 my $clone = '';
359 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {#clone
360   #$cgi->param('clone') =~ /^(\d+)$/ or die "malformed query: $query";
361   $part_svc = qsearchs('part_svc', { 'svcpart'=>$1 } )
362     or die "unknown svcpart: $1";
363   $clone = $part_svc->svcpart;
364   $part_svc->svcpart('');
365 } elsif ( $cgi->keywords ) { #edit
366   my($query) = $cgi->keywords;
367   $query =~ /^(\d+)$/ or die "malformed query: $query";
368   $part_svc=qsearchs('part_svc', { 'svcpart'=>$1 } )
369     or die "unknown svcpart: $1";
370 } else { #adding
371   $part_svc = new FS::part_svc {};
372 }
373
374 my $action = $part_svc->svcpart ? 'Edit' : 'Add';
375 my $hashref = $part_svc->hashref;
376 #   my $p_svcdb = $part_svc->svcdb || 'svc_acct';
377
378
379
380 </%init>
381
382
383