cc9145f45a29d22bbc6f4494eebb9328b116842a
[freeside.git] / httemplate / edit / part_svc.cgi
1 %
2 %my $part_svc;
3 %my $clone = '';
4 %if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {#clone
5 %  #$cgi->param('clone') =~ /^(\d+)$/ or die "malformed query: $query";
6 %  $part_svc = qsearchs('part_svc', { 'svcpart'=>$1 } )
7 %    or die "unknown svcpart: $1";
8 %  $clone = $part_svc->svcpart;
9 %  $part_svc->svcpart('');
10 %} elsif ( $cgi->keywords ) { #edit
11 %  my($query) = $cgi->keywords;
12 %  $query =~ /^(\d+)$/ or die "malformed query: $query";
13 %  $part_svc=qsearchs('part_svc', { 'svcpart'=>$1 } )
14 %    or die "unknown svcpart: $1";
15 %} else { #adding
16 %  $part_svc = new FS::part_svc {};
17 %}
18 %
19 %my $action = $part_svc->svcpart ? 'Edit' : 'Add';
20 %my $hashref = $part_svc->hashref;
21 %#   my $p_svcdb = $part_svc->svcdb || 'svc_acct';
22 %
23 %
24 %           #" onLoad=\"visualize()\""
25 %
26
27 <% include("/elements/header.html","$action Service Definition",
28            menubar( 'Main Menu'         => $p,
29                     'View all service definitions' => "${p}browse/part_svc.cgi"
30                   ),
31            )
32 %>
33
34 <FORM NAME="dummy">
35
36       Service Part #<% $part_svc->svcpart ? $part_svc->svcpart : "(NEW)" %>
37 <BR><BR>
38 Service  <INPUT TYPE="text" NAME="svc" VALUE="<% $hashref->{svc} %>"><BR>
39 Disable new orders <INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"<% $hashref->{disabled} eq 'Y' ? ' CHECKED' : '' %>><BR>
40 <INPUT TYPE="hidden" NAME="svcpart" VALUE="<% $hashref->{svcpart} %>">
41 <BR>
42 Service definitions are the templates for items you offer to your customers.
43 <UL><LI>svc_acct - Accounts - anything with a username (Mailboxes, PPP accounts, shell accounts, RADIUS entries for broadband, etc.)
44     <LI>svc_domain - Domains
45     <LI>svc_forward - mail forwarding
46     <LI>svc_www - Virtual domain website
47     <LI>svc_broadband - Broadband/High-speed Internet service (always-on)
48     <LI>svc_phone - Customer phone numbers
49     <LI>svc_external - Externally-tracked service
50 <!--   <LI>svc_charge - One-time charges (Partially unimplemented)
51        <LI>svc_wo - Work orders (Partially unimplemented)
52 -->
53 </UL>
54 For the selected table, you can give fields default or fixed (unchangable)
55 values, or select an inventory class to manually or automatically fill in
56 that field.
57 <BR><BR>
58 %
59 %
60 %#these might belong somewhere else for other user interfaces 
61 %#pry need to eventually create stuff that's shared amount UIs
62 %my $conf = new FS::Conf;
63 %my %defs = (
64 %
65 %  'svc_acct' => {
66 %    'dir'       => 'Home directory',
67 %    'uid'       => 'UID (set to fixed and blank for no UIDs)',
68 %    'slipip'    => 'IP address',
69 %#    'popnum'    => qq!<A HREF="$p/browse/svc_acct_pop.cgi/">POP number</A>!,
70 %    'popnum'    => {
71 %                     desc => 'Access number',
72 %                     type => 'select',
73 %                     select_table => 'svc_acct_pop',
74 %                     select_key   => 'popnum',
75 %                     select_label => 'city',
76 %                     disable_select => 1,
77 %                   },
78 %    'username'  => {
79 %                     desc => 'Username',
80 %                     type => 'text',
81 %                     disable_default => 1,
82 %                     disable_fixed => 1,
83 %                     disable_select => 1,
84 %                   },
85 %    'quota'     => { 
86 %                     desc => '',
87 %                     type => 'text',
88 %                     disable_inventory => 1,
89 %                     disable_select => 1,
90 %                   },
91 %    '_password' => 'Password',
92 %    'gid'       => 'GID (when blank, defaults to UID)',
93 %    'shell'     => {
94 %                     #desc =>'Shell (all service definitions should have a default or fixed shell that is present in the <b>shells</b> configuration file, set to blank for no shell tracking)',
95 %                     desc =>'Shell ( set to blank for no shell tracking)',
96 %                     type =>'select',
97 %                     select_list => [ $conf->config('shells') ],
98 %                     disable_inventory => 1,
99 %                     disable_select => 1,
100 %                   },
101 %    'finger'    => 'Real name (GECOS)',
102 %    'domsvc'    => {
103 %                     desc =>'svcnum from svc_domain',
104 %                     type =>'select',
105 %                     select_table => 'svc_domain',
106 %                     select_key   => 'svcnum',
107 %                     select_label => 'domain',
108 %                     disable_inventory => 1,
109 %                     disable_select => 1,
110 %                   },
111 %    'usergroup' => {
112 %                     desc =>'RADIUS groups',
113 %                     type =>'radius_usergroup_selector',
114 %                     disable_select => 1,
115 %                     disable_inventory => 1,
116 %                   },
117 %    'seconds'   => { desc => '',
118 %                     type => 'text',
119 %                     disable_inventory => 1,
120 %                     disable_select => 1,
121 %                   },
122 %  },
123 %
124 %  'svc_domain' => {
125 %    'domain'    => 'Domain',
126 %  },
127 %
128 %  'svc_forward' => {
129 %    'srcsvc'    => 'service from which mail is to be forwarded',
130 %    'dstsvc'    => 'service to which mail is to be forwarded',
131 %    'dst'       => 'someone@another.domain.com to use when dstsvc is 0',
132 %  },
133 %
134 %#  'svc_charge' => {
135 %#    'amount'    => 'amount',
136 %#  },
137 %#  'svc_wo' => {
138 %#    'worker'    => 'Worker',
139 %#    '_date'      => 'Date',
140 %#  },
141 %
142 %  'svc_www' => {
143 %    #'recnum' => '',
144 %    #'usersvc' => '',
145 %  },
146 %
147 %  'svc_broadband' => {
148 %    'speed_down' => 'Maximum download speed for this service in Kbps.  0 denotes unlimited.',
149 %    'speed_up' => 'Maximum upload speed for this service in Kbps.  0 denotes unlimited.',
150 %    'ip_addr' => 'IP address.  Leave blank for automatic assignment.',
151 %    'blocknum' => 'Address block.',
152 %  },
153 %
154 %  'svc_phone' => {
155 %    'countrycode' => { desc => 'Country code',
156 %                       type => 'text',
157 %                       disable_inventory => 1,
158 %                       disable_select => 1,
159 %                     },
160 %    'phonenum'    => 'Phone number',
161 %    'pin'         => { desc => 'Personal Identification Number',
162 %                       type => 'text',
163 %                       disable_inventory => 1,
164 %                       disable_select => 1,
165 %                     },
166 %  },
167 %
168 %  'svc_external' => {
169 %    #'id' => '',
170 %    #'title' => '',
171 %  },
172 %
173 %);
174 %
175 %  my %vfields;
176 %  foreach my $svcdb (grep dbdef->table($_), keys %defs ) {
177 %    my $self = "FS::$svcdb"->new;
178 %    $vfields{$svcdb} = {};
179 %    foreach my $field ($self->virtual_fields) { # svc_Common::virtual_fields with a null svcpart returns all of them
180 %      my $pvf = $self->pvf($field);
181 %      my @list = $pvf->list;
182 %      if (scalar @list) {
183 %        $defs{$svcdb}->{$field} = { desc        => $pvf->label,
184 %                                    type        => 'select',
185 %                                    select_list => \@list };
186 %      } else {
187 %        $defs{$svcdb}->{$field} = $pvf->label;
188 %      } #endif
189 %      $vfields{$svcdb}->{$field} = $pvf;
190 %      warn "\$vfields{$svcdb}->{$field} = $pvf";
191 %    } #next $field
192 %  } #next $svcdb
193 %
194 %  #code duplication w/ edit/part_svc.cgi, should move this hash to part_svc.pm
195 %  # and generalize the subs
196 %  # condition sub is tested to see whether to disable display of this choice
197 %  # params: ( $def, $layer, $field )  (see SUB below)
198 %  my $inv_sub = sub {
199 %    ref($_[0]) && (    $_[0]->{disable_inventory} 
200 %                    || $_[0]->{'type'} ne 'text'  )
201 %  };
202 %  tie my %flag, 'Tie::IxHash',
203 %    ''  => { 'desc' => 'No default', },
204 %    'D' => { 'desc' => 'Default',
205 %             'condition' =>
206 %               sub { ref($_[0]) && $_[0]->{disable_default} }, 
207 %           },
208 %    'F' => { 'desc' => 'Fixed (unchangeable)',
209 %             'condition' =>
210 %               sub { ref($_[0]) && $_[0]->{disable_fixed} }, 
211 %           },
212 %    'S' => { 'desc' => 'Selectable Choice',
213 %             'condition' =>
214 %               sub { !ref($_[0]) || $_[0]->{disable_select} }, 
215 %           },
216 %# need to template-ize httemplate/edit/svc_* first
217 %#    'M' => { 'desc' => 'Manual selection from inventory',
218 %#             'condition' => $inv_sub,
219 %#           },
220 %    'A' => { 'desc' => 'Automatically fill in from inventory',
221 %             'condition' => $inv_sub,
222 %           },
223 %    'X' => { 'desc' => 'Excluded',
224 %             'condition' =>
225 %               sub { ! $vfields{$_[1]}->{$_[2]} },
226 %
227 %           },
228 %  ;
229 %  
230 %  my @dbs = $hashref->{svcdb}
231 %             ? ( $hashref->{svcdb} )
232 %             : qw( svc_acct svc_domain svc_forward svc_www svc_broadband svc_phone svc_external );
233 %
234 %  tie my %svcdb, 'Tie::IxHash', map { $_=>$_ } grep dbdef->table($_), @dbs;
235 %  my $widget = new HTML::Widgets::SelectLayers(
236 %    #'selected_layer' => $p_svcdb,
237 %    'selected_layer' => $hashref->{svcdb} || 'svc_acct',
238 %    'options'        => \%svcdb,
239 %    'form_name'      => 'dummy',
240 %    #'form_action'    => 'process/part_svc.cgi',
241 %    'form_action'    => 'part_svc.cgi', #self
242 %    'form_text'      => [ qw( svc svcpart ) ],
243 %    'form_checkbox'  => [ 'disabled' ],
244 %    'layer_callback' => sub {
245 %      my $layer = shift;
246 %      
247 %      my $html = qq!<INPUT TYPE="hidden" NAME="svcdb" VALUE="$layer">!;
248 %
249 %      my $columns = 3;
250 %      my $count = 0;
251 %      my @part_export =
252 %        map { qsearch( 'part_export', {exporttype => $_ } ) }
253 %          keys %{FS::part_export::export_info($layer)};
254 %      $html .= '<BR><BR>'. table(). 
255 %               "<TR><TH COLSPAN=$columns>Exports</TH></TR><TR>";
256 %      foreach my $part_export ( @part_export ) {
257 %        $html .= '<TD><INPUT TYPE="checkbox"'.
258 %                 ' NAME="exportnum'. $part_export->exportnum. '"  VALUE="1" ';
259 %        $html .= 'CHECKED'
260 %          if ( $clone || $part_svc->svcpart ) #null svcpart search causing error
261 %              && qsearchs( 'export_svc', {
262 %                                   exportnum => $part_export->exportnum,
263 %                                   svcpart   => $clone || $part_svc->svcpart });
264 %        $html .= '>'. $part_export->exportnum. ': '. $part_export->exporttype.
265 %                 ' to '. $part_export->machine. '</TD>';
266 %        $count++;
267 %        $html .= '</TR><TR>' unless $count % $columns;
268 %      }
269 %      $html .= '</TR></TABLE><BR><BR>';
270 %
271 %      $html .= include('/elements/table-grid.html', 'cellpadding' => 4 ).
272 %               '<TR>'.
273 %                 '<TH CLASS="grid" BGCOLOR="#cccccc">Field</TH>'.
274 %                 '<TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=2>Modifier</TH>'.
275 %               '</TR>';
276 %
277 %      my $bgcolor1 = '#eeeeee';
278 %      my $bgcolor2 = '#ffffff';
279 %      my $bgcolor;
280 %
281 %      #yucky kludge
282 %      my @fields = defined( dbdef->table($layer) )
283 %                      ? grep { $_ ne 'svcnum' } fields($layer)
284 %                      : ();
285 %      push @fields, 'usergroup' if $layer eq 'svc_acct'; #kludge
286 %      $part_svc->svcpart($clone) if $clone; #haha, undone below
287 %
288 %
289 %      foreach my $field (@fields) {
290 %
291 %        my $part_svc_column = $part_svc->part_svc_column($field);
292 %        my $value = $part_svc_column->columnvalue;
293 %        my $flag = $part_svc_column->columnflag;
294 %        my $def = $defs{$layer}{$field};
295 %        my $desc = ref($def) ? $def->{desc} : $def;
296 %
297 %        if ( $bgcolor eq $bgcolor1 ) {
298 %          $bgcolor = $bgcolor2;
299 %        } else {
300 %          $bgcolor = $bgcolor1;
301 %        }
302 %        
303 %        $html .= qq!<TR><TD CLASS="grid" BGCOLOR="$bgcolor" ALIGN="right">!.
304 %                 $field;
305 %        $html .= "- <FONT SIZE=-1>$desc</FONT>" if $desc;
306 %        $html .=  "</TD>";
307 %        $flag = '' if ref($def) && $def->{type} eq 'disabled';
308 %
309 %        $html .= qq!<TD CLASS="grid" BGCOLOR="$bgcolor">!;
310 %
311 %        if ( ref($def) && $def->{type} eq 'disabled' ) {
312 %        
313 %          $html .= 'No default';
314 %
315 %        } else {
316 %
317 %          $html .= qq!<SELECT NAME="${layer}__${field}_flag"!.
318 %                      qq! onChange="${layer}__${field}_flag_changed(this)">!;
319 %
320 %          foreach my $f ( keys %flag ) {
321 %
322 %            #here is where the SUB from above is called, to skip some choices
323 %            next if $flag{$f}->{condition}
324 %                 && &{ $flag{$f}->{condition} }( $def, $layer, $field );
325 %
326 %            $html .= qq!<OPTION VALUE="$f"!.
327 %                     ' SELECTED'x($flag eq $f ).
328 %                     '>'. $flag{$f}->{desc};
329 %
330 %          }
331 %
332 %          $html .= '</SELECT>';
333 %
334 %          $html .= join("\n",
335 %            '<SCRIPT>',
336 %            "  function ${layer}__${field}_flag_changed(what) {",
337 %            '    var f = what.options[what.selectedIndex].value;',
338 %            '    if ( f == "" || f == "X" ) { //disable',
339 %            "      what.form.${layer}__${field}.disabled = true;".
340 %            "      what.form.${layer}__${field}.style.backgroundColor = '#dddddd';".
341 %            "      if ( what.form.${layer}__${field}_classnum ) {".
342 %            "        what.form.${layer}__${field}_classnum.disabled = true;".
343 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#dddddd';".
344 %            "      }".
345 %            '    } else if ( f == "D" || f == "F" || f =="S" ) { //enable, text box',
346 %            "      what.form.${layer}__${field}.disabled = false;".
347 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
348 %            "      what.form.${layer}__${field}.style.display = '';".
349 %            "      if ( what.form.${layer}__${field}_classnum ) {".
350 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
351 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
352 %            "        what.form.${layer}__${field}_classnum.style.display = 'none';".
353 %            "      }".
354 %            '    } else if ( f == "M" || f == "A" ) { //enable, inventory',
355 %            "      what.form.${layer}__${field}.disabled = false;".
356 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
357 %            "      what.form.${layer}__${field}.style.display = 'none';".
358 %            "      if ( what.form.${layer}__${field}_classnum ) {".
359 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
360 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
361 %            "        what.form.${layer}__${field}_classnum.style.display = '';".
362 %            "      }".
363 %            '    }',
364 %            '  }',
365 %            '</SCRIPT>',
366 %          );
367 %
368 %        }
369 %
370 %        $html .= qq!</TD><TD CLASS="grid" BGCOLOR="$bgcolor">!;
371 %
372 %        my $disabled = $flag ? ''
373 %                             : 'DISABLED STYLE="background-color: #dddddd"';
374 %
375 %        if ( ! ref($def) || $def->{type} eq 'text' ) {
376 %
377 %          my $nodisplay = ' STYLE="display:none"';
378 %          my $is_inv = ( $flag =~ /^[MA]$/ );
379 %
380 %          $html .=
381 %            qq!<INPUT TYPE="text" NAME="${layer}__${field}" VALUE="$value" !.
382 %            $disabled.
383 %            ( $is_inv ? $nodisplay : $disabled ).
384 %            '>';
385 %
386 %          $html .= include('/elements/select-table.html',
387 %                             'element_name' => "${layer}__${field}_classnum",
388 %                             'element_etc'  => ( $is_inv
389 %                                                   ? $disabled
390 %                                                   : $nodisplay
391 %                                               ),
392 %                             'table'        => 'inventory_class',
393 %                             'name_col'     => 'classname',
394 %                             'value'        => $value,
395 %                             'empty_label'  => 'Select inventory class',
396 %                          );
397 %
398 %        } elsif ( $def->{type} eq 'select' ) {
399 %
400 %          $html .= qq!<SELECT NAME="${layer}__${field}" $disabled!;
401 %          $html .= ' multiple' if $flag == 'S';
402 %          $html .= '>';
403 %          $html .= '<OPTION> </OPTION>' unless $value;
404 %          if ( $def->{select_table} ) {
405 %            foreach my $record ( qsearch( $def->{select_table}, {} ) ) {
406 %              my $rvalue = $record->getfield($def->{select_key});
407 %              $html .= qq!<OPTION VALUE="$rvalue"!.
408 %                  (grep(/^$rvalue$/, split(',',$value)) ? ' SELECTED>' : '>' ).
409 %                  $record->getfield($def->{select_label}). '</OPTION>';
410 %            } #next $record
411 %          } else { # select_list
412 %            foreach my $item ( @{$def->{select_list}} ) {
413 %              $html .= qq!<OPTION VALUE="$item"!.
414 %                    (grep(/^$item$/, split(',',$value)) ? ' SELECTED>' : '>' ).
415 %                    $item. '</OPTION>';
416 %            } #next $item
417 %          } #endif
418 %          $html .= '</SELECT>';
419 %
420 %        } elsif ( $def->{type} eq 'radius_usergroup_selector' ) {
421 %
422 %          #XXX disable the RADIUS usergroup selector?  ugh it sure does need
423 %          #an overhaul, people have dum group problems because of it
424 %
425 %          $html .= FS::svc_acct::radius_usergroup_selector(
426 %            [ split(',', $value) ], "${layer}__${field}" );
427 %
428 %        } elsif ( $def->{type} eq 'disabled' ) {
429 %
430 %          $html .=
431 %            qq!<INPUT TYPE="hidden" NAME="${layer}__${field}" VALUE="">!;
432 %
433 %        } else {
434 %
435 %          $html .= '<font color="#ff0000">unknown type'. $def->{type};
436 %
437 %        }
438 %
439 %        $html .= "</TD></TR>\n";
440 %
441 %      } #foreach my $field (@fields) {
442 %
443 %      $part_svc->svcpart('') if $clone; #undone
444 %      $html .= "</TABLE>";
445 %
446 %      $html .= include('/elements/progress-init.html',
447 %                         $layer, #form name
448 %                         [ qw(svc svcpart disabled exportnum), @fields ],
449 %                         'process/part_svc.cgi',
450 %                         $p.'browse/part_svc.cgi',
451 %                         $layer,
452 %                      );
453 %      $html .= '<BR><INPUT NAME="submit" TYPE="button" VALUE="'.
454 %               ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '" '.
455 %               ' onClick="document.'. "$layer.submit.disabled=true; ".
456 %               "fixup(document.$layer); $layer". 'process();">';
457 %
458 %      #$html .= '<BR><INPUT TYPE="submit" VALUE="'.
459 %      #         ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '">';
460 %
461 %      $html;
462 %
463 %    },
464 %  );
465 %
466 %
467
468 Table <% $widget->html %>
469   </BODY>
470 </HTML>
471