RT# 78356 - added speed test fields for broadband service and new modifier to get...
[freeside.git] / httemplate / edit / elements / svc_Common.html
1 <% include( 'edit.html',
2
3                  'menubar' => [],
4
5                  'error_callback' => sub {
6                    my( $cgi, $svc_x, $fields, $opt ) = @_;
7                    #$svcnum = $svc_x->svcnum;
8                    $pkgnum  = $cgi->param('pkgnum');
9                    $svcpart = $cgi->param('svcpart');
10
11                    $part_svc = qsearchs( 'part_svc', { svcpart=>$svcpart });
12                    die "No part_svc entry!" unless $part_svc;
13
14                    label_fixup($part_svc, $opt);
15
16                    $svc_x->setfield('svcpart', $svcpart);
17
18                    if ( my $cb = $opt{'svc_error_callback'} ) {
19                      my $cust_pkg = $pkgnum
20                                       ? qsearchs('cust_pkg', {pkgnum=>$pkgnum})
21                                       : ''; #?
22                      &{ $cb }( $cgi,$svc_x, $part_svc,$cust_pkg, $fields,$opt);
23                    }
24
25                  },
26
27                  'edit_callback' => sub {
28                    my( $cgi, $svc_x, $fields, $opt ) = @_;
29                    #$svcnum = $svc_x->svcnum;
30                    my $cust_svc = $svc_x->cust_svc
31                      or die "Unknown (cust_svc) svcnum!";
32
33                    $pkgnum  = $cust_svc->pkgnum;
34                    $svcpart = $cust_svc->svcpart;
35   
36                    $part_svc = qsearchs ('part_svc', { svcpart=>$svcpart });
37                    die "No part_svc entry!" unless $part_svc;
38
39                    label_fixup($part_svc, $opt);
40                    
41                    if ( my $cb = $opt{'svc_edit_callback'} ) {
42                      my $cust_pkg = $pkgnum
43                                       ? qsearchs('cust_pkg', {pkgnum=>$pkgnum})
44                                       : ''; #?
45                      &{ $cb }( $cgi,$svc_x, $part_svc,$cust_pkg, $fields,$opt);
46                    }
47
48                    if ( $part_svc->has_router ) {
49                      my $router = qsearchs('router', {svcnum => $svc_x->svcnum});
50                      if ( $router ) {
51                        $svc_x->set("router_$_", $router->get($_))
52                          foreach ('routername', 'routernum');
53                        my ($block) = $router->addr_block; # one-to-one for now
54                        if ( $block ) {
55                          $svc_x->set('router_blocknum', $block->blocknum);
56                          # silly, but necessary...make the currently 
57                          # assigned block appear on the list
58                          my ($field) = grep {ref($_) and 
59                                              $_->{field} eq 'router_blocknum'}
60                                          @$fields;
61                          $field->{extra_sql} = 
62                            ' OR routernum = '.$router->routernum;
63                          $field->{curr_value} = $block->blocknum;
64                        }
65                      }
66                    }
67
68                  },
69
70                  'new_hashref_callback' => sub {
71                    #my( $cgi, $svc_x ) = @_;
72
73                    { pkgnum  => $pkgnum,
74                      svcpart => $svcpart,
75                    };
76
77                  },
78
79                  'new_callback' => sub {
80                    my( $cgi, $svc_x, $fields, $opt ) = @_;
81
82                    $part_svc = qsearchs( 'part_svc', { svcpart=>$svcpart });
83                    die "No part_svc entry!" unless $part_svc;
84
85                    label_fixup($part_svc, $opt);
86
87                    #$svcnum='';
88
89                    if ( my $cb = $opt{'svc_new_callback'} ) {
90                      my $cust_pkg = $pkgnum
91                                      ? qsearchs('cust_pkg', {pkgnum=>$pkgnum})
92                                      : ''; #?
93                      &{ $cb }( $cgi,$svc_x, $part_svc,$cust_pkg, $fields,$opt);
94                    }
95
96                    $svc_x->set_default_and_fixed;
97                  },
98
99                  'field_callback' => sub {
100                    my ($cgi, $object, $f) = @_;
101
102                    my $columndef = $part_svc->part_svc_column($f->{'field'});
103                    my $flag = $columndef->columnflag;
104
105                    $f->{'required'} = 1
106                      if $columndef->required;
107
108                    if ( $flag eq 'F' ) { #fixed
109                      $f->{'value'} = $columndef->columnvalue;
110                      if (length($columndef->columnvalue)) {
111
112                        if ( $f->{'type'} =~ /^select-?(.*)/ ) {
113                          # try to display this in a user-friendly manner
114                          if ( $f->{'table'} ) { # find matching records
115                            $f->{'value_col'} ||=
116                              dbdef->table($f->{'table'})->primary_key;
117
118                            my @values = split(',', $f->{'value'});
119                            my @recs;
120                            foreach (@values) {
121                              push @recs, qsearchs( $f->{'table'},
122                                          { $f->{'value_col'} => $_ }
123                                          );
124                            }
125                            if ( @recs ) {
126                              my $method = $f->{'name_col'};
127                              if ( $f->{'multiple'} ) {
128                                $f->{'formatted_value'} = [
129                                  map { $_->method } @recs
130                                ];
131                              } else { # there shouldn't be more than one...
132                                $f->{'formatted_value'} = $recs[0]->$method;
133                              }
134                            } # if not, then just let tr-fixed display the
135                              # values as-is
136
137                          } # other select types probably don't matter
138                        } # if it's a select
139
140                        $f->{'type'} = 'fixed';
141
142                      } else { # fixed, null
143                        $f->{'type'} = 'hidden';
144                      }
145
146                    } elsif ( $flag eq 'A' ) { #auto assign from inventory
147                      $f->{'type'} = 'hidden';
148
149                    } elsif ( $flag eq 'M' ) { #manually assign from inventory
150                      $f->{'type'} = 'select-inventory_item';
151                      $f->{'empty_label'} = 'Select inventory item';
152                      $f->{'extra_sql'} = 'WHERE ( svcnum IS NULL ' .
153                         ($object->svcnum && ' OR svcnum = '.$object->svcnum) .
154                         ')';
155                      $f->{'classnum'} = $columndef->columnvalue;
156                      $f->{'disable_empty'} = $object->svcnum ? 1 : 0;
157
158                    } elsif ( $flag eq 'H' ) { #hardware
159                      $f->{'type'}        = 'select-hardware_type';
160                      $f->{'hashref'}     = {
161                                             'classnum'=>$columndef->columnvalue
162                                            };
163
164                    } elsif ( $flag eq 'S' #selectable choice
165                                && $f->{type} !~ /^select-svc/ ) {
166                      $f->{type}    = 'select';
167                      $f->{options} = [ split( /\s*,\s*/,
168                                                 $columndef->columnvalue)
169                                      ];
170                    } # shouldn't this be enforced for all 'S' fields?
171
172                    elsif ( $flag eq 'P' ) { #form fcc_477 values
173                      $f->{type}    = 'fixed';
174                      my $cust_pkg = FS::Record::qsearchs({
175                        'table'   => 'cust_pkg',
176                        'hashref' => { 'pkgnum' => $object->{Hash}->{pkgnum} }
177                      });
178                      my $fcc_record = $cust_pkg->fcc_477_record('broadband_'.$columndef->columnvalue.'stream') if $cust_pkg;
179                      $f->{'value'} = $fcc_record->{Hash}->{optionvalue} ? $fcc_record->{Hash}->{optionvalue} * 1000 : '';
180                    } # end 477 values
181
182                    if ( $f->{'type'} =~ /^select-svc/ )
183                    {
184                      $f->{'include_opt_callback'} =
185                        sub { ( 'pkgnum'  => $pkgnum,
186                                'svcpart' => $svcpart,
187                              );
188                            };
189                    }
190
191                    if ( $f->{'field'} eq 'custnum' && $pkgnum ) {
192                      my $cust_pkg = qsearchs('cust_pkg', {'pkgnum' => $pkgnum});
193                      $object->set('custnum', $cust_pkg->custnum);
194                    }
195
196                    if ( my $cb = $opt{'svc_field_callback'} ) {
197                      &{ $cb }( $cgi, $object, $f);
198                    }
199
200                  },
201
202                  'html_init' => sub {
203                    my $html;
204                    my $cust_main;
205                    if ( $pkgnum ) {
206                      my $cust_pkg = qsearchs('cust_pkg', {'pkgnum' => $pkgnum});
207                      $cust_main = $cust_pkg->cust_main if $cust_pkg;
208                      if ( $cust_main ) {
209                         $html = include( '/elements/small_custview.html',
210                                 $cust_main,
211                                 '',
212                                 1,
213                                 popurl(2). "view/cust_main.cgi"
214                               ). '<BR>';
215                      }
216                    }
217                    $html;
218                  },
219
220                  'html_table_bottom' => sub {
221                    my $svc_x = shift;
222                    my $html = '';
223                    foreach my $field ($svc_x->virtual_fields) {
224                      if ($part_svc->part_svc_column($field)->columnflag ne 'F'){
225                        # If the flag is X, it won't even show up
226                        # in $svc_acct->virtual_fields.
227                        $html .=
228                          $svc_x->pvf($field)->widget( 'HTML',
229                                                       'edit', 
230                                                       $svc_x->getfield($field)
231                                                     );
232                      }
233                    }
234                    $html;
235                  },
236
237                  'html_bottom' => sub {
238                    qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="$pkgnum">!.
239                    qq!<INPUT TYPE="hidden" NAME="svcpart" VALUE="$svcpart">!;
240                  },
241
242                  %opt #pass through/override params
243              )
244 %>
245 <%once>
246
247 sub label_fixup {
248   my( $part_svc, $opt ) = @_;
249
250   $opt->{'name'} ||= $part_svc->svc;
251
252   my $svcdb = $part_svc->svcdb;
253   require "FS/$svcdb.pm";
254
255   if ( UNIVERSAL::can("FS::$svcdb", 'table_info') ) {
256     #$opt->{'name'} ||= "FS::$svcdb"->table_info->{'name'};
257
258     my $fields = "FS::$svcdb"->table_info->{'fields'};
259     $opt->{'fields'} ||= [ grep { $_ ne 'svcnum' } keys %$fields ];
260
261     $opt->{labels} ||= {
262                          map { $_ => ( ref($fields->{$_})
263                                          ? $fields->{$_}{'label'}
264                                          : $fields->{$_}
265                                      );
266                              }
267                          keys %$fields
268                        };
269   }
270
271   #false laziness w/view/svc_Common.html
272   #override default labels with service-definition labels if applicable
273   my $labels = $opt->{labels}; # with -> here
274   foreach my $field ( keys %{ $opt->{labels} } ) {
275     my $col = $part_svc->part_svc_column($field);
276     $labels->{$field} = $col->columnlabel if $col->columnlabel !~ /^\s*$/;
277   }
278
279   if ( $part_svc->has_router ) {
280     # these will be set up as pseudo-fields in the new_ and edit_ callbacks
281     push @{ $opt->{'fields'} }, (
282       { field => 'router_routernum',   type => 'hidden' },
283       { field => 'router_routername',  type => 'text', size => 32 },
284       # router address block selection
285       # (one-to-one for now)
286       { field => 'router_blocknum',
287         type  => 'select-table',
288         table       => 'addr_block',
289         hashref     => { 'routernum' => '0' },
290         agent_virt  => 1,
291         agent_null  => 1,
292         name_col    => 'cidr',
293         order_by    => 'ORDER BY ip_gateway, ip_netmask',
294         empty_label => '(none)',
295         disable_empty => 0,
296       },
297     );
298     $labels->{router_routername}  = 'Attached router name';
299     $labels->{router_blocknum}    = 'Attached address block';
300   }
301 }
302
303 </%once>
304 <%init>
305
306 my %opt = @_;
307
308 #my( $svcnum, $pkgnum, $svcpart, $part_svc );
309 my( $pkgnum, $svcpart, $part_svc );
310
311 #get & untaint pkgnum & svcpart
312 if ( ! $cgi->param('error')
313      && $cgi->param('pkgnum') && $cgi->param('svcpart')
314    )
315 {
316   $cgi->param('pkgnum') =~ /^(\d+)$/ or die 'unparsable pkgnum';
317   $pkgnum = $1;
318   $cgi->param('svcpart') =~ /^(\d+)$/ or die 'unparsable svcpart';
319   $svcpart = $1;
320   #$cgi->delete_all(); #so edit.html treats this correctly as new??
321 }
322
323 </%init>