6428960ddf1f5451e03c89cb6850a8ea730c3344
[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 %# need to template-ize httemplate/edit/svc_* first
69 %#    'M' => { 'desc' => 'Manual selection from inventory',
70 %#             'condition' => $inv_sub,
71 %#           },
72 %    'A' => { 'desc' => 'Automatically fill in from inventory',
73 %             'condition' => $inv_sub,
74 %           },
75 %    'X' => { 'desc' => 'Excluded',
76 %             'condition' =>
77 %               sub { ! $vfields{$_[1]}->{$_[2]} },
78 %
79 %           },
80 %  ;
81 %  
82 %  my @dbs = $hashref->{svcdb}
83 %             ? ( $hashref->{svcdb} )
84 %             : FS::part_svc->svc_tables();
85 %
86 %  tie my %svcdb, 'Tie::IxHash', map { $_=>$_ } grep dbdef->table($_), @dbs;
87 %  my $widget = new HTML::Widgets::SelectLayers(
88 %    #'selected_layer' => $p_svcdb,
89 %    'selected_layer' => $hashref->{svcdb} || 'svc_acct',
90 %    'options'        => \%svcdb,
91 %    'form_name'      => 'dummy',
92 %    #'form_action'    => 'process/part_svc.cgi',
93 %    'form_action'    => 'part_svc.cgi', #self
94 %    'form_text'      => [ qw( svc svcpart ) ],
95 %    'form_checkbox'  => [ 'disabled' ],
96 %    'layer_callback' => sub {
97 %      my $layer = shift;
98 %      
99 %      my $html = qq!<INPUT TYPE="hidden" NAME="svcdb" VALUE="$layer">!;
100 %
101 %      my $columns = 3;
102 %      my $count = 0;
103 %      my @part_export =
104 %        map { qsearch( 'part_export', {exporttype => $_ } ) }
105 %          keys %{FS::part_export::export_info($layer)};
106 %      $html .= '<BR><BR>'. table(). 
107 %               "<TR><TH COLSPAN=$columns>Exports</TH></TR><TR>";
108 %      foreach my $part_export ( @part_export ) {
109 %        $html .= '<TD><INPUT TYPE="checkbox"'.
110 %                 ' NAME="exportnum'. $part_export->exportnum. '"  VALUE="1" ';
111 %        $html .= 'CHECKED'
112 %          if ( $clone || $part_svc->svcpart ) #null svcpart search causing error
113 %              && qsearchs( 'export_svc', {
114 %                                   exportnum => $part_export->exportnum,
115 %                                   svcpart   => $clone || $part_svc->svcpart });
116 %        $html .= '>'. $part_export->exportnum. ': '. $part_export->exporttype.
117 %                 ' to '. $part_export->machine. '</TD>';
118 %        $count++;
119 %        $html .= '</TR><TR>' unless $count % $columns;
120 %      }
121 %      $html .= '</TR></TABLE><BR><BR>';
122 %
123 %      $html .= include('/elements/table-grid.html', 'cellpadding' => 4 ).
124 %               '<TR>'.
125 %                 '<TH CLASS="grid" BGCOLOR="#cccccc">Field</TH>'.
126 %                 '<TH CLASS="grid" BGCOLOR="#cccccc">Label</TH>'.
127 %                 '<TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=2>Modifier</TH>'.
128 %               '</TR>';
129 %
130 %      my $bgcolor1 = '#eeeeee';
131 %      my $bgcolor2 = '#ffffff';
132 %      my $bgcolor;
133 %
134 %      #yucky kludge
135 %      my @fields = defined( dbdef->table($layer) )
136 %                      ? grep {
137 %                               $_ ne 'svcnum' &&
138 %                               ( !FS::part_svc->svc_table_fields($layer)
139 %                                     ->{$_}->{disable_part_svc_column}   ||
140 %                                 $part_svc->part_svc_column($_)->columnflag
141 %                               )
142 %                             }
143 %                             fields($layer)
144 %                      : ();
145 %      push @fields, 'usergroup' if $layer eq 'svc_acct'; #kludge
146 %      $part_svc->svcpart($clone) if $clone; #haha, undone below
147 %
148 %
149 %      foreach my $field (@fields) {
150 %
151 %        #a few lines of false laziness w/browse/part_svc.cgi
152 %        my $def = FS::part_svc->svc_table_fields($layer)->{$field};
153 %        my $def_info  = $def->{'def_info'};
154 %        my $formatter = $def->{'format'} || sub { shift };
155 %
156 %        my $part_svc_column = $part_svc->part_svc_column($field);
157 %        my $label = $part_svc_column->columnlabel || $def->{'label'};
158 %        my $value = &$formatter($part_svc_column->columnvalue);
159 %        my $flag  = $part_svc_column->columnflag;
160 %
161 %        if ( $bgcolor eq $bgcolor1 ) {
162 %          $bgcolor = $bgcolor2;
163 %        } else {
164 %          $bgcolor = $bgcolor1;
165 %        }
166 %        
167 %        $html .= qq!<TR><TD ROWSPAN=2 CLASS="grid" BGCOLOR="$bgcolor" ALIGN="right">!.
168 %                 ( $def->{'label'} || $field ).
169 %                 "</TD>";
170 %
171 %        $html .= qq!<TD ROWSPAN=2 CLASS="grid" BGCOLOR="$bgcolor"><INPUT NAME="${layer}__${field}_label" VALUE="!. encode_entities($label). '" STYLE="text-align:right"></TD>';
172 %
173 %        $flag = '' if $def->{type} eq 'disabled';
174 %
175 %        $html .= qq!<TD CLASS="grid" BGCOLOR="$bgcolor">!;
176 %
177 %        if ( $def->{type} eq 'disabled' ) {
178 %        
179 %          $html .= 'No default';
180 %
181 %        } else {
182 %
183 %          $html .= qq!<SELECT NAME="${layer}__${field}_flag"!.
184 %                      qq! onChange="${layer}__${field}_flag_changed(this)">!;
185 %
186 %          foreach my $f ( keys %flag ) {
187 %
188 %            #here is where the SUB from above is called, to skip some choices
189 %            next if $flag{$f}->{condition}
190 %                 && &{ $flag{$f}->{condition} }( $def, $layer, $field );
191 %
192 %            $html .= qq!<OPTION VALUE="$f"!.
193 %                     ' SELECTED'x($flag eq $f ).
194 %                     '>'. $flag{$f}->{desc};
195 %
196 %          }
197 %
198 %          $html .= '</SELECT>';
199 %
200 %          $html .= join("\n",
201 %            '<SCRIPT>',
202 %            "  function ${layer}__${field}_flag_changed(what) {",
203 %            '    var f = what.options[what.selectedIndex].value;',
204 %            '    if ( f == "" || f == "X" ) { //disable',
205 %            "      what.form.${layer}__${field}.disabled = true;".
206 %            "      what.form.${layer}__${field}.style.backgroundColor = '#dddddd';".
207 %            "      if ( what.form.${layer}__${field}_classnum ) {".
208 %            "        what.form.${layer}__${field}_classnum.disabled = true;".
209 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#dddddd';".
210 %            "      }".
211 %            '    } else if ( f == "D" || f == "F" || f =="S" ) { //enable, text box',
212 %            "      what.form.${layer}__${field}.disabled = false;".
213 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
214 %            "      if ( f == 'S' || '${field}' == 'usergroup' ) {". # kludge
215 %            "        what.form.${layer}__${field}.multiple = true;".
216 %            "      } else {".
217 %            "        what.form.${layer}__${field}.multiple = false;".
218 %            "      }".
219 %            "      what.form.${layer}__${field}.style.display = '';".
220 %            "      if ( what.form.${layer}__${field}_classnum ) {".
221 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
222 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
223 %            "        what.form.${layer}__${field}_classnum.style.display = 'none';".
224 %            "      }".
225 %            '    } else if ( f == "M" || f == "A" ) { //enable, inventory',
226 %            "      what.form.${layer}__${field}.disabled = false;".
227 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
228 %            "      what.form.${layer}__${field}.style.display = 'none';".
229 %            "      if ( what.form.${layer}__${field}_classnum ) {".
230 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
231 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
232 %            "        what.form.${layer}__${field}_classnum.style.display = '';".
233 %            "      }".
234 %            '    }',
235 %            '  }',
236 %            '</SCRIPT>',
237 %          );
238 %
239 %        }
240 %
241 %        $html .= qq!</TD><TD CLASS="grid" BGCOLOR="$bgcolor">!;
242 %
243 %        my $disabled = $flag ? ''
244 %                             : 'DISABLED STYLE="background-color: #dddddd"';
245 %
246 %        if ( !$def->{type} || $def->{type} eq 'text' ) {
247 %
248 %          my $nodisplay = ' STYLE="display:none"';
249 %          my $is_inv = ( $flag =~ /^[MA]$/ );
250 %
251 %          $html .=
252 %            qq!<INPUT TYPE="text" NAME="${layer}__${field}" VALUE="$value" !.
253 %            $disabled.
254 %            ( $is_inv ? $nodisplay : $disabled ).
255 %            '>';
256 %
257 %          $html .= include('/elements/select-table.html',
258 %                             'element_name' => "${layer}__${field}_classnum",
259 %                             'element_etc'  => ( $is_inv
260 %                                                   ? $disabled
261 %                                                   : $nodisplay
262 %                                               ),
263 %                             'table'        => 'inventory_class',
264 %                             'name_col'     => 'classname',
265 %                             'value'        => $value,
266 %                             'empty_label'  => 'Select inventory class',
267 %                          );
268 %
269 %        } elsif ( $def->{type} eq 'select' ) {
270 %
271 %          $html .= qq!<SELECT NAME="${layer}__${field}" $disabled!;
272 %          $html .= ' MULTIPLE' if $flag eq 'S';
273 %          $html .= '>';
274 %          $html .= '<OPTION> </OPTION>' unless $value;
275 %          if ( $def->{select_table} ) {
276 %            foreach my $record ( qsearch( $def->{select_table}, {} ) ) {
277 %              my $rvalue = $record->getfield($def->{select_key});
278 %              my $select_label = $def->{select_label};
279 %              $html .= qq!<OPTION VALUE="$rvalue"!.
280 %                  (grep(/^$rvalue$/, split(',',$value)) ? ' SELECTED>' : '>' ).
281 %                  $record->$select_label(). '</OPTION>';
282 %            } #next $record
283 %          } else { # select_list
284 %            foreach my $item ( @{$def->{select_list}} ) {
285 %              $html .= qq!<OPTION VALUE="$item"!.
286 %                    (grep(/^$item$/, split(',',$value)) ? ' SELECTED>' : '>' ).
287 %                    $item. '</OPTION>';
288 %            } #next $item
289 %          } #endif
290 %          $html .= '</SELECT>';
291 %
292 %        } elsif ( $def->{type} eq 'select-svc_pbx.html' ) {
293 %
294 %          $html .= include('/elements/select-svc_pbx.html',
295 %                             'curr_value'   => $value,
296 %                             'element_name' => "${layer}__${field}",
297 %                             'element_etc'  => $disabled,
298 %                             'multiple'     => ($flag eq 'S'),
299 %                          );
300 %
301 %        } elsif ( $def->{type} eq 'radius_usergroup_selector' ) {
302 %
303 %          #XXX disable the RADIUS usergroup selector?  ugh it sure does need
304 %          #an overhaul, people have dum group problems because of it
305 %
306 %          $html .= FS::svc_acct::radius_usergroup_selector(
307 %            [ split(',', $value) ], "${layer}__${field}" );
308 %
309 %        } elsif ( $def->{type} eq 'communigate_pro-accessmodes' ) {
310 %
311 %          $html .= include('/elements/communigate_pro-accessmodes.html',
312 %                             'element_name_prefix' => "${layer}__${field}_",
313 %                             'curr_value'          => $value,
314 %                             #doesn't work#'element_etc'  => $disabled,
315 %                          );
316 %
317 %        } elsif ( $def->{type} eq 'disabled' ) {
318 %
319 %          $html .=
320 %            qq!<INPUT TYPE="hidden" NAME="${layer}__${field}" VALUE="">!;
321 %
322 %        } else {
323 %
324 %          $html .= '<font color="#ff0000">unknown type '. $def->{type};
325 %
326 %        }
327 %
328 %        $html .= "</TD></TR>\n";
329
330 %        $def_info = "($def_info)" if $def_info;
331 %        $html .=
332 %          qq!<TR>!.
333 %          qq!  <TD COLSPAN=2 BGCOLOR="$bgcolor" ALIGN="center" !.
334 %          qq!      STYLE="padding:0; border-top: none">!.
335 %          qq!    <FONT SIZE="-1"><I>$def_info</I></FONT>!.
336 %          qq!  </TD>!.
337 %          qq!</TR>\n!;
338 %
339 %      } #foreach my $field (@fields) {
340 %
341 %      $part_svc->svcpart('') if $clone; #undone
342 %      $html .= "</TABLE>";
343 %
344 %      $html .= include('/elements/progress-init.html',
345 %                         $layer, #form name
346 %                         [ qw(svc svcpart disabled exportnum), @fields ],
347 %                         'process/part_svc.cgi',
348 %                         $p.'browse/part_svc.cgi',
349 %                         $layer,
350 %                      );
351 %      $html .= '<BR><INPUT NAME="submit" TYPE="button" VALUE="'.
352 %               ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '" '.
353 %               ' onClick="document.'. "$layer.submit.disabled=true; ".
354 %               "fixup(document.$layer); $layer". 'process();">';
355 %
356 %      #$html .= '<BR><INPUT TYPE="submit" VALUE="'.
357 %      #         ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '">';
358 %
359 %      $html;
360 %
361 %    },
362 %  );
363 %
364 %
365
366 Table <% $widget->html %>
367
368 <% include('/elements/footer.html') %>
369
370 <%init>
371
372 die "access denied"
373   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
374
375 my $part_svc;
376 my $clone = '';
377 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {#clone
378   #$cgi->param('clone') =~ /^(\d+)$/ or die "malformed query: $query";
379   $part_svc = qsearchs('part_svc', { 'svcpart'=>$1 } )
380     or die "unknown svcpart: $1";
381   $clone = $part_svc->svcpart;
382   $part_svc->svcpart('');
383 } elsif ( $cgi->keywords ) { #edit
384   my($query) = $cgi->keywords;
385   $query =~ /^(\d+)$/ or die "malformed query: $query";
386   $part_svc=qsearchs('part_svc', { 'svcpart'=>$1 } )
387     or die "unknown svcpart: $1";
388 } else { #adding
389   $part_svc = new FS::part_svc {};
390 }
391
392 my $action = $part_svc->svcpart ? 'Edit' : 'Add';
393 my $hashref = $part_svc->hashref;
394 #   my $p_svcdb = $part_svc->svcdb || 'svc_acct';
395
396
397
398 </%init>
399
400
401