reincorporate the changes from http://www.sisd.com/cgi-bin/viewcvs.cgi/freeside/httem...
[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 % #YUCK.  false laziness w/part_svc.pm.  go away virtual fields, please
60 % my %vfields;
61 % foreach my $svcdb ( FS::part_svc->svc_tables() ) {
62 %   eval "use FS::$svcdb;";
63 %   my $self = "FS::$svcdb"->new;
64 %   $vfields{$svcdb} = {};
65 %   foreach my $field ($self->virtual_fields) { # svc_Common::virtual_fields with a null svcpart returns all of them
66 %     my $pvf = $self->pvf($field);
67 %     $vfields{$svcdb}->{$field} = $pvf;
68 %     #warn "\$vfields{$svcdb}->{$field} = $pvf";
69 %   } #next $field
70 % } #next $svcdb
71 %
72 %  #code duplication w/ edit/part_svc.cgi, should move this hash to part_svc.pm
73 %  # and generalize the subs
74 %  # condition sub is tested to see whether to disable display of this choice
75 %  # params: ( $def, $layer, $field )  (see SUB below)
76 %  my $inv_sub = sub {
77 %                      $_[0]->{disable_inventory}
78 %                        || $_[0]->{'type'} ne 'text'
79 %                    };
80 %  tie my %flag, 'Tie::IxHash',
81 %    ''  => { 'desc' => 'No default', },
82 %    'D' => { 'desc' => 'Default',
83 %             'condition' =>
84 %               sub { $_[0]->{disable_default} }, 
85 %           },
86 %    'F' => { 'desc' => 'Fixed (unchangeable)',
87 %             'condition' =>
88 %               sub { $_[0]->{disable_fixed} }, 
89 %           },
90 %    'S' => { 'desc' => 'Selectable Choice',
91 %             'condition' =>
92 %               sub { !ref($_[0]) || $_[0]->{disable_select} }, 
93 %           },
94 %# need to template-ize httemplate/edit/svc_* first
95 %#    'M' => { 'desc' => 'Manual selection from inventory',
96 %#             'condition' => $inv_sub,
97 %#           },
98 %    'A' => { 'desc' => 'Automatically fill in from inventory',
99 %             'condition' => $inv_sub,
100 %           },
101 %    'X' => { 'desc' => 'Excluded',
102 %             'condition' =>
103 %               sub { ! $vfields{$_[1]}->{$_[2]} },
104 %
105 %           },
106 %  ;
107 %  
108 %  my @dbs = $hashref->{svcdb}
109 %             ? ( $hashref->{svcdb} )
110 %             : FS::part_svc->svc_tables();
111 %
112 %  tie my %svcdb, 'Tie::IxHash', map { $_=>$_ } grep dbdef->table($_), @dbs;
113 %  my $widget = new HTML::Widgets::SelectLayers(
114 %    #'selected_layer' => $p_svcdb,
115 %    'selected_layer' => $hashref->{svcdb} || 'svc_acct',
116 %    'options'        => \%svcdb,
117 %    'form_name'      => 'dummy',
118 %    #'form_action'    => 'process/part_svc.cgi',
119 %    'form_action'    => 'part_svc.cgi', #self
120 %    'form_text'      => [ qw( svc svcpart ) ],
121 %    'form_checkbox'  => [ 'disabled' ],
122 %    'layer_callback' => sub {
123 %      my $layer = shift;
124 %      
125 %      my $html = qq!<INPUT TYPE="hidden" NAME="svcdb" VALUE="$layer">!;
126 %
127 %      my $columns = 3;
128 %      my $count = 0;
129 %      my @part_export =
130 %        map { qsearch( 'part_export', {exporttype => $_ } ) }
131 %          keys %{FS::part_export::export_info($layer)};
132 %      $html .= '<BR><BR>'. table(). 
133 %               "<TR><TH COLSPAN=$columns>Exports</TH></TR><TR>";
134 %      foreach my $part_export ( @part_export ) {
135 %        $html .= '<TD><INPUT TYPE="checkbox"'.
136 %                 ' NAME="exportnum'. $part_export->exportnum. '"  VALUE="1" ';
137 %        $html .= 'CHECKED'
138 %          if ( $clone || $part_svc->svcpart ) #null svcpart search causing error
139 %              && qsearchs( 'export_svc', {
140 %                                   exportnum => $part_export->exportnum,
141 %                                   svcpart   => $clone || $part_svc->svcpart });
142 %        $html .= '>'. $part_export->exportnum. ': '. $part_export->exporttype.
143 %                 ' to '. $part_export->machine. '</TD>';
144 %        $count++;
145 %        $html .= '</TR><TR>' unless $count % $columns;
146 %      }
147 %      $html .= '</TR></TABLE><BR><BR>';
148 %
149 %      $html .= include('/elements/table-grid.html', 'cellpadding' => 4 ).
150 %               '<TR>'.
151 %                 '<TH CLASS="grid" BGCOLOR="#cccccc">Field</TH>'.
152 %                 '<TH CLASS="grid" BGCOLOR="#cccccc" COLSPAN=2>Modifier</TH>'.
153 %               '</TR>';
154 %
155 %      my $bgcolor1 = '#eeeeee';
156 %      my $bgcolor2 = '#ffffff';
157 %      my $bgcolor;
158 %
159 %      #yucky kludge
160 %      my @fields = defined( dbdef->table($layer) )
161 %                      ? grep { $_ ne 'svcnum' } fields($layer)
162 %                      : ();
163 %      push @fields, 'usergroup' if $layer eq 'svc_acct'; #kludge
164 %      $part_svc->svcpart($clone) if $clone; #haha, undone below
165 %
166 %
167 %      foreach my $field (@fields) {
168 %
169 %        my $part_svc_column = $part_svc->part_svc_column($field);
170 %        my $value = $part_svc_column->columnvalue;
171 %        my $flag = $part_svc_column->columnflag;
172 %        #my $def = $defs{$layer}{$field};
173 %        my $def = FS::part_svc->svc_table_fields($layer)->{$field};
174 %        my $label = $def->{'def_label'} || $def->{'label'};
175 %
176 %        if ( $bgcolor eq $bgcolor1 ) {
177 %          $bgcolor = $bgcolor2;
178 %        } else {
179 %          $bgcolor = $bgcolor1;
180 %        }
181 %        
182 %        $html .= qq!<TR><TD CLASS="grid" BGCOLOR="$bgcolor" ALIGN="right">!.
183 %                 ( $label || $field ).
184 %                 "</TD>";
185 %        $flag = '' if $def->{type} eq 'disabled';
186 %
187 %        $html .= qq!<TD CLASS="grid" BGCOLOR="$bgcolor">!;
188 %
189 %        if ( $def->{type} eq 'disabled' ) {
190 %        
191 %          $html .= 'No default';
192 %
193 %        } else {
194 %
195 %          $html .= qq!<SELECT NAME="${layer}__${field}_flag"!.
196 %                      qq! onChange="${layer}__${field}_flag_changed(this)">!;
197 %
198 %          foreach my $f ( keys %flag ) {
199 %
200 %            #here is where the SUB from above is called, to skip some choices
201 %            next if $flag{$f}->{condition}
202 %                 && &{ $flag{$f}->{condition} }( $def, $layer, $field );
203 %
204 %            $html .= qq!<OPTION VALUE="$f"!.
205 %                     ' SELECTED'x($flag eq $f ).
206 %                     '>'. $flag{$f}->{desc};
207 %
208 %          }
209 %
210 %          $html .= '</SELECT>';
211 %
212 %          $html .= join("\n",
213 %            '<SCRIPT>',
214 %            "  function ${layer}__${field}_flag_changed(what) {",
215 %            '    var f = what.options[what.selectedIndex].value;',
216 %            '    if ( f == "" || f == "X" ) { //disable',
217 %            "      what.form.${layer}__${field}.disabled = true;".
218 %            "      what.form.${layer}__${field}.style.backgroundColor = '#dddddd';".
219 %            "      if ( what.form.${layer}__${field}_classnum ) {".
220 %            "        what.form.${layer}__${field}_classnum.disabled = true;".
221 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#dddddd';".
222 %            "      }".
223 %            '    } else if ( f == "D" || f == "F" || f =="S" ) { //enable, text box',
224 %            "      what.form.${layer}__${field}.disabled = false;".
225 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
226 %            "      if ( f == 'S' || '${field}' == 'usergroup' ) {". # kludge
227 %            "        what.form.${layer}__${field}.multiple = true;".
228 %            "      } else {".
229 %            "        what.form.${layer}__${field}.multiple = false;".
230 %            "      }".
231 %            "      what.form.${layer}__${field}.style.display = '';".
232 %            "      if ( what.form.${layer}__${field}_classnum ) {".
233 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
234 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
235 %            "        what.form.${layer}__${field}_classnum.style.display = 'none';".
236 %            "      }".
237 %            '    } else if ( f == "M" || f == "A" ) { //enable, inventory',
238 %            "      what.form.${layer}__${field}.disabled = false;".
239 %            "      what.form.${layer}__${field}.style.backgroundColor = '#ffffff';".
240 %            "      what.form.${layer}__${field}.style.display = 'none';".
241 %            "      if ( what.form.${layer}__${field}_classnum ) {".
242 %            "        what.form.${layer}__${field}_classnum.disabled = false;".
243 %            "        what.form.${layer}__${field}_classnum.style.backgroundColor = '#ffffff';".
244 %            "        what.form.${layer}__${field}_classnum.style.display = '';".
245 %            "      }".
246 %            '    }',
247 %            '  }',
248 %            '</SCRIPT>',
249 %          );
250 %
251 %        }
252 %
253 %        $html .= qq!</TD><TD CLASS="grid" BGCOLOR="$bgcolor">!;
254 %
255 %        my $disabled = $flag ? ''
256 %                             : 'DISABLED STYLE="background-color: #dddddd"';
257 %
258 %        if ( !$def->{type} || $def->{type} eq 'text' ) {
259 %
260 %          my $nodisplay = ' STYLE="display:none"';
261 %          my $is_inv = ( $flag =~ /^[MA]$/ );
262 %
263 %          $html .=
264 %            qq!<INPUT TYPE="text" NAME="${layer}__${field}" VALUE="$value" !.
265 %            $disabled.
266 %            ( $is_inv ? $nodisplay : $disabled ).
267 %            '>';
268 %
269 %          $html .= include('/elements/select-table.html',
270 %                             'element_name' => "${layer}__${field}_classnum",
271 %                             'element_etc'  => ( $is_inv
272 %                                                   ? $disabled
273 %                                                   : $nodisplay
274 %                                               ),
275 %                             'table'        => 'inventory_class',
276 %                             'name_col'     => 'classname',
277 %                             'value'        => $value,
278 %                             'empty_label'  => 'Select inventory class',
279 %                          );
280 %
281 %        } elsif ( $def->{type} eq 'select' ) {
282 %
283 %          $html .= qq!<SELECT NAME="${layer}__${field}" $disabled!;
284 %          $html .= ' MULTIPLE' if $flag eq 'S';
285 %          $html .= '>';
286 %          $html .= '<OPTION> </OPTION>' unless $value;
287 %          if ( $def->{select_table} ) {
288 %            foreach my $record ( qsearch( $def->{select_table}, {} ) ) {
289 %              my $rvalue = $record->getfield($def->{select_key});
290 %              $html .= qq!<OPTION VALUE="$rvalue"!.
291 %                  (grep(/^$rvalue$/, split(',',$value)) ? ' SELECTED>' : '>' ).
292 %                  $record->getfield($def->{select_label}). '</OPTION>';
293 %            } #next $record
294 %          } else { # select_list
295 %            foreach my $item ( @{$def->{select_list}} ) {
296 %              $html .= qq!<OPTION VALUE="$item"!.
297 %                    (grep(/^$item$/, split(',',$value)) ? ' SELECTED>' : '>' ).
298 %                    $item. '</OPTION>';
299 %            } #next $item
300 %          } #endif
301 %          $html .= '</SELECT>';
302 %
303 %        } elsif ( $def->{type} eq 'radius_usergroup_selector' ) {
304 %
305 %          #XXX disable the RADIUS usergroup selector?  ugh it sure does need
306 %          #an overhaul, people have dum group problems because of it
307 %
308 %          $html .= FS::svc_acct::radius_usergroup_selector(
309 %            [ split(',', $value) ], "${layer}__${field}" );
310 %
311 %        } elsif ( $def->{type} eq 'disabled' ) {
312 %
313 %          $html .=
314 %            qq!<INPUT TYPE="hidden" NAME="${layer}__${field}" VALUE="">!;
315 %
316 %        } else {
317 %
318 %          $html .= '<font color="#ff0000">unknown type'. $def->{type};
319 %
320 %        }
321 %
322 %        $html .= "</TD></TR>\n";
323 %
324 %      } #foreach my $field (@fields) {
325 %
326 %      $part_svc->svcpart('') if $clone; #undone
327 %      $html .= "</TABLE>";
328 %
329 %      $html .= include('/elements/progress-init.html',
330 %                         $layer, #form name
331 %                         [ qw(svc svcpart disabled exportnum), @fields ],
332 %                         'process/part_svc.cgi',
333 %                         $p.'browse/part_svc.cgi',
334 %                         $layer,
335 %                      );
336 %      $html .= '<BR><INPUT NAME="submit" TYPE="button" VALUE="'.
337 %               ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '" '.
338 %               ' onClick="document.'. "$layer.submit.disabled=true; ".
339 %               "fixup(document.$layer); $layer". 'process();">';
340 %
341 %      #$html .= '<BR><INPUT TYPE="submit" VALUE="'.
342 %      #         ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '">';
343 %
344 %      $html;
345 %
346 %    },
347 %  );
348 %
349 %
350
351 Table <% $widget->html %>
352   </BODY>
353 </HTML>
354