51925c0c506551564df03bdd27ca1a87e4515551
[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_pbx - Customer PBXs
23     <LI>svc_external - Externally-tracked service
24 <!--   <LI>svc_charge - One-time charges (Partially unimplemented)
25        <LI>svc_wo - Work orders (Partially unimplemented)
26 -->
27 </UL>
28 For the selected table, you can give fields default or fixed (unchangable)
29 values, or select an inventory class to manually or automatically fill in
30 that field.
31 <BR><BR>
32
33 % #YUCK.  false laziness w/part_svc.pm.  go away virtual fields, please
34 % my %vfields;
35 % foreach my $svcdb ( FS::part_svc->svc_tables() ) {
36 %   eval "use FS::$svcdb;";
37 %   my $self = "FS::$svcdb"->new;
38 %   $vfields{$svcdb} = {};
39 %   foreach my $field ($self->virtual_fields) { # svc_Common::virtual_fields with a null svcpart returns all of them
40 %     my $pvf = $self->pvf($field);
41 %     $vfields{$svcdb}->{$field} = $pvf;
42 %     #warn "\$vfields{$svcdb}->{$field} = $pvf";
43 %   } #next $field
44 % } #next $svcdb
45 %
46 %  #code duplication w/ edit/part_svc.cgi, should move this hash to part_svc.pm
47 %  # and generalize the subs
48 %  # condition sub is tested to see whether to disable display of this choice
49 %  # params: ( $def, $layer, $field )  (see SUB below)
50 %  my $inv_sub = sub {
51 %                      $_[0]->{disable_inventory}
52 %                        || $_[0]->{'type'} ne 'text'
53 %                    };
54 %  tie my %flag, 'Tie::IxHash',
55 %    ''  => { 'desc' => 'No default', },
56 %    'D' => { 'desc' => 'Default',
57 %             'condition' =>
58 %               sub { $_[0]->{disable_default} }, 
59 %           },
60 %    'F' => { 'desc' => 'Fixed (unchangeable)',
61 %             'condition' =>
62 %               sub { $_[0]->{disable_fixed} }, 
63 %           },
64 %    'S' => { 'desc' => 'Selectable Choice',
65 %             'condition' =>
66 %               sub { !ref($_[0]) || $_[0]->{disable_select} }, 
67 %           },
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 %            # need to template-ize more httemplate/edit/svc_* first
188 %            next if $f eq 'M' and $layer !~ /^svc_(broadband|external|phone)$/;
189 %
190 %            #here is where the SUB from above is called, to skip some choices
191 %            next if $flag{$f}->{condition}
192 %                 && &{ $flag{$f}->{condition} }( $def, $layer, $field );
193 %
194 %            $html .= qq!<OPTION VALUE="$f"!.
195 %                     ' SELECTED'x($flag eq $f ).
196 %                     '>'. $flag{$f}->{desc};
197 %
198 %          }
199 %
200 %          $html .= '</SELECT>';
201 %
202 %          $html .= join("\n",
203 %            '<SCRIPT>',
204 %            "  function ${layer}__${field}_flag_changed(what) {",
205 %            '    var f = what.options[what.selectedIndex].value;',
206 %            '    if ( f == "" || f == "X" ) { //disable',
207 %            "      what.form.${layer}__${field}.disabled = true;".
208 %            "      what.form.${layer}__${field}.style.backgroundColor = '#dddddd';".
209 %            "      if ( what.form.${layer}__${field}_classnum ) {".
210 %            "        what.form.${layer}__${field}_classnum.disabled = true;".
211 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#dddddd';".
212 %            "      }".
213 %            '    } else if ( f == "D" || f == "F" || f =="S" ) { //enable, text box',
214 %            "      what.form.${layer}__${field}.disabled = false;".
215 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
216 %            "      if ( f == 'S' || '${field}' == 'usergroup' ) {". # kludge
217 %            "        what.form.${layer}__${field}.multiple = true;".
218 %            "      } else {".
219 %            "        what.form.${layer}__${field}.multiple = false;".
220 %            "      }".
221 %            "      what.form.${layer}__${field}.style.display = '';".
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 = 'none';".
226 %            "      }".
227 %            '    } else if ( f == "M" || f == "A" ) { //enable, inventory',
228 %            "      what.form.${layer}__${field}.disabled = false;".
229 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
230 %            "      what.form.${layer}__${field}.style.display = 'none';".
231 %            "      if ( what.form.${layer}__${field}_classnum ) {".
232 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
233 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
234 %            "        what.form.${layer}__${field}_classnum.style.display = '';".
235 %            "      }".
236 %            '    }',
237 %            '  }',
238 %            '</SCRIPT>',
239 %          );
240 %
241 %        }
242 %
243 %        $html .= qq!</TD><TD CLASS="grid" BGCOLOR="$bgcolor">!;
244 %
245 %        my $disabled = $flag ? ''
246 %                             : 'DISABLED STYLE="background-color: #dddddd"';
247 %
248 %        if ( !$def->{type} || $def->{type} eq 'text' ) {
249 %
250 %          my $nodisplay = ' STYLE="display:none"';
251 %          my $is_inv = ( $flag =~ /^[MA]$/ );
252 %
253 %          $html .=
254 %            qq!<INPUT TYPE="text" NAME="${layer}__${field}" VALUE="$value" !.
255 %            $disabled.
256 %            ( $is_inv ? $nodisplay : $disabled ).
257 %            '>';
258 %
259 %          $html .= include('/elements/select-table.html',
260 %                             'element_name' => "${layer}__${field}_classnum",
261 %                             'element_etc'  => ( $is_inv
262 %                                                   ? $disabled
263 %                                                   : $nodisplay
264 %                                               ),
265 %                             'table'        => 'inventory_class',
266 %                             'name_col'     => 'classname',
267 %                             'value'        => $value,
268 %                             'empty_label'  => 'Select inventory class',
269 %                          );
270 %
271 %        } elsif ( $def->{type} eq 'checkbox' ) {
272 %
273 %          $html .= include('/elements/checkbox.html',
274 %                             'field'      => $layer.'__'.$field,
275 %                             'curr_value' => $value,
276 %                             'value'      => 'Y',
277 %                          );
278 %
279 %        } elsif ( $def->{type} eq 'select' ) {
280 %
281 %          $html .= qq!<SELECT NAME="${layer}__${field}" $disabled!;
282 %          $html .= ' MULTIPLE' if $flag eq 'S';
283 %          $html .= '>';
284 %          $html .= '<OPTION> </OPTION>' unless $value;
285 %          if ( $def->{select_table} ) {
286 %            foreach my $record ( qsearch( $def->{select_table}, {} ) ) {
287 %              my $rvalue = $record->getfield($def->{select_key});
288 %              my $select_label = $def->{select_label};
289 %              $html .= qq!<OPTION VALUE="$rvalue"!.
290 %                  (grep(/^$rvalue$/, split(',',$value)) ? ' SELECTED>' : '>' ).
291 %                  $record->$select_label(). '</OPTION>';
292 %            } #next $record
293 %          } else { # select_list
294 %            foreach my $item ( @{$def->{select_list}} ) {
295 %              $html .= qq!<OPTION VALUE="$item"!.
296 %                    (grep(/^$item$/, split(',',$value)) ? ' SELECTED>' : '>' ).
297 %                    $item. '</OPTION>';
298 %            } #next $item
299 %          } #endif
300 %          $html .= '</SELECT>';
301 %
302 %        } elsif ( $def->{type} eq 'select-svc_pbx.html' ) {
303 %
304 %          $html .= include('/elements/select-svc_pbx.html',
305 %                             'curr_value'   => $value,
306 %                             'element_name' => "${layer}__${field}",
307 %                             'element_etc'  => $disabled,
308 %                             'multiple'     => ($flag eq 'S'),
309 %                          );
310 %
311 %        } elsif ( $def->{type} eq 'radius_usergroup_selector' ) {
312 %
313 %          #XXX disable the RADIUS usergroup selector?  ugh it sure does need
314 %          #an overhaul, people have dum group problems because of it
315 %
316 %          $html .= FS::svc_acct::radius_usergroup_selector(
317 %            [ split(',', $value) ], "${layer}__${field}" );
318 %
319 %        } elsif ( $def->{type} eq 'communigate_pro-accessmodes' ) {
320 %
321 %          $html .= include('/elements/communigate_pro-accessmodes.html',
322 %                             'element_name_prefix' => "${layer}__${field}_",
323 %                             'curr_value'          => $value,
324 %                             #doesn't work#'element_etc'  => $disabled,
325 %                          );
326 %
327 %        } elsif ( $def->{type} eq 'disabled' ) {
328 %
329 %          $html .=
330 %            qq!<INPUT TYPE="hidden" NAME="${layer}__${field}" VALUE="">!;
331 %
332 %        } else {
333 %
334 %          $html .= '<font color="#ff0000">unknown type '. $def->{type};
335 %
336 %        }
337 %
338 %        $html .= "</TD></TR>\n";
339
340 %        $def_info = "($def_info)" if $def_info;
341 %        $html .=
342 %          qq!<TR>!.
343 %          qq!  <TD COLSPAN=2 BGCOLOR="$bgcolor" ALIGN="center" !.
344 %          qq!      STYLE="padding:0; border-top: none">!.
345 %          qq!    <FONT SIZE="-1"><I>$def_info</I></FONT>!.
346 %          qq!  </TD>!.
347 %          qq!</TR>\n!;
348 %
349 %      } #foreach my $field (@fields) {
350 %
351 %      $part_svc->svcpart('') if $clone; #undone
352 %      $html .= "</TABLE>";
353 %
354 %      $html .= include('/elements/progress-init.html',
355 %                         $layer, #form name
356 %                         [ qw(svc svcpart disabled exportnum), @fields ],
357 %                         'process/part_svc.cgi',
358 %                         $p.'browse/part_svc.cgi',
359 %                         $layer,
360 %                      );
361 %      $html .= '<BR><INPUT NAME="submit" TYPE="button" VALUE="'.
362 %               ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '" '.
363 %               ' onClick="document.'. "$layer.submit.disabled=true; ".
364 %               "fixup(document.$layer); $layer". 'process();">';
365 %
366 %      #$html .= '<BR><INPUT TYPE="submit" VALUE="'.
367 %      #         ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '">';
368 %
369 %      $html;
370 %
371 %    },
372 %  );
373 %
374 %
375
376 Table <% $widget->html %>
377
378 <% include('/elements/footer.html') %>
379
380 <%init>
381
382 die "access denied"
383   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
384
385 my $part_svc;
386 my $clone = '';
387 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {#clone
388   #$cgi->param('clone') =~ /^(\d+)$/ or die "malformed query: $query";
389   $part_svc = qsearchs('part_svc', { 'svcpart'=>$1 } )
390     or die "unknown svcpart: $1";
391   $clone = $part_svc->svcpart;
392   $part_svc->svcpart('');
393 } elsif ( $cgi->keywords ) { #edit
394   my($query) = $cgi->keywords;
395   $query =~ /^(\d+)$/ or die "malformed query: $query";
396   $part_svc=qsearchs('part_svc', { 'svcpart'=>$1 } )
397     or die "unknown svcpart: $1";
398 } else { #adding
399   $part_svc = new FS::part_svc {};
400 }
401
402 my $action = $part_svc->svcpart ? 'Edit' : 'Add';
403 my $hashref = $part_svc->hashref;
404 #   my $p_svcdb = $part_svc->svcdb || 'svc_acct';
405
406
407
408 </%init>
409
410
411