svc_circuit, #23879, #25933, #30830
[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                    if ( $flag eq 'F' ) { #fixed
106                      $f->{'value'} = $columndef->columnvalue;
107                      if (length($columndef->columnvalue)) {
108
109                        if ( $f->{'type'} =~ /^select-?(.*)/ ) {
110                          # try to display this in a user-friendly manner
111                          if ( $f->{'table'} ) { # find matching records
112                            $f->{'value_col'} ||=
113                              dbdef->table($f->{'table'})->primary_key;
114
115                            my @values = split(',', $f->{'value'});
116                            my @recs;
117                            foreach (@values) {
118                              push @recs, qsearchs( $f->{'table'},
119                                          { $f->{'value_col'} => $_ }
120                                          );
121                            }
122                            if ( @recs ) {
123                              my $method = $f->{'name_col'};
124                              if ( $f->{'multiple'} ) {
125                                $f->{'formatted_value'} = [
126                                  map { $_->method } @recs
127                                ];
128                              } else { # there shouldn't be more than one...
129                                $f->{'formatted_value'} = $recs[0]->$method;
130                              }
131                            } # if not, then just let tr-fixed display the
132                              # values as-is
133
134                          } # other select types probably don't matter
135                        } # if it's a select
136
137                        $f->{'type'} = 'fixed';
138
139                      } else { # fixed, null
140                        $f->{'type'} = 'hidden';
141                      }
142
143                    } elsif ( $flag eq 'A' ) { #auto assign from inventory
144                      $f->{'type'} = 'hidden';
145
146                    } elsif ( $flag eq 'M' ) { #manually assign from inventory
147                      $f->{'type'} = 'select-inventory_item';
148                      $f->{'empty_label'} = 'Select inventory item';
149                      $f->{'extra_sql'} = 'WHERE ( svcnum IS NULL ' .
150                         ($object->svcnum && ' OR svcnum = '.$object->svcnum) .
151                         ')';
152                      $f->{'classnum'} = $columndef->columnvalue;
153                      $f->{'disable_empty'} = $object->svcnum ? 1 : 0;
154
155                    } elsif ( $flag eq 'H' ) { #hardware
156                      $f->{'type'}        = 'select-hardware_type';
157                      $f->{'hashref'}     = {
158                                             'classnum'=>$columndef->columnvalue
159                                            };
160
161                    } elsif ( $flag eq 'S' #selectable choice
162                                && $f->{type} !~ /^select-svc/ ) {
163                      $f->{type}    = 'select';
164                      $f->{options} = [ split( /\s*,\s*/,
165                                                 $columndef->columnvalue)
166                                      ];
167                    } # shouldn't this be enforced for all 'S' fields?
168
169                    if ( $f->{'type'} =~ /^select-svc/ )
170                    {
171                      $f->{'include_opt_callback'} =
172                        sub { ( 'pkgnum'  => $pkgnum,
173                                'svcpart' => $svcpart,
174                              );
175                            };
176                    }
177
178                    if ( $f->{'field'} eq 'custnum' && $pkgnum ) {
179                      my $cust_pkg = qsearchs('cust_pkg', {'pkgnum' => $pkgnum});
180                      $object->set('custnum', $cust_pkg->custnum);
181                    }
182
183                    if ( my $cb = $opt{'svc_field_callback'} ) {
184                      &{ $cb }( $cgi, $object, $f);
185                    }
186
187                  },
188
189                  'html_init' => sub {
190                    my $html;
191                    my $cust_main;
192                    if ( $pkgnum ) {
193                      my $cust_pkg = qsearchs('cust_pkg', {'pkgnum' => $pkgnum});
194                      $cust_main = $cust_pkg->cust_main if $cust_pkg;
195                      if ( $cust_main ) {
196                         $html = include( '/elements/small_custview.html',
197                                 $cust_main,
198                                 '',
199                                 1,
200                                 popurl(2). "view/cust_main.cgi"
201                               ). '<BR>';
202                      }
203                    }
204                    $html;
205                  },
206
207                  'html_table_bottom' => sub {
208                    my $svc_x = shift;
209                    my $html = '';
210                    foreach my $field ($svc_x->virtual_fields) {
211                      if ($part_svc->part_svc_column($field)->columnflag ne 'F'){
212                        # If the flag is X, it won't even show up
213                        # in $svc_acct->virtual_fields.
214                        $html .=
215                          $svc_x->pvf($field)->widget( 'HTML',
216                                                       'edit', 
217                                                       $svc_x->getfield($field)
218                                                     );
219                      }
220                    }
221                    $html;
222                  },
223
224                  'html_bottom' => sub {
225                    qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="$pkgnum">!.
226                    qq!<INPUT TYPE="hidden" NAME="svcpart" VALUE="$svcpart">!;
227                  },
228
229                  %opt #pass through/override params
230              )
231 %>
232 <%once>
233
234 sub label_fixup {
235   my( $part_svc, $opt ) = @_;
236
237   $opt->{'name'} ||= $part_svc->svc;
238
239   my $svcdb = $part_svc->svcdb;
240   require "FS/$svcdb.pm";
241
242   if ( UNIVERSAL::can("FS::$svcdb", 'table_info') ) {
243     #$opt->{'name'} ||= "FS::$svcdb"->table_info->{'name'};
244
245     my $fields = "FS::$svcdb"->table_info->{'fields'};
246     $opt->{'fields'} ||= [ grep { $_ ne 'svcnum' } keys %$fields ];
247
248     $opt->{labels} ||= {
249                          map { $_ => ( ref($fields->{$_})
250                                          ? $fields->{$_}{'label'}
251                                          : $fields->{$_}
252                                      );
253                              }
254                          keys %$fields
255                        };
256   }
257
258   #false laziness w/view/svc_Common.html
259   #override default labels with service-definition labels if applicable
260   my $labels = $opt->{labels}; # with -> here
261   foreach my $field ( keys %{ $opt->{labels} } ) {
262     my $col = $part_svc->part_svc_column($field);
263     $labels->{$field} = $col->columnlabel if $col->columnlabel !~ /^\s*$/;
264   }
265
266   if ( $part_svc->has_router ) {
267     # these will be set up as pseudo-fields in the new_ and edit_ callbacks
268     push @{ $opt->{'fields'} }, (
269       { field => 'router_routernum',   type => 'hidden' },
270       { field => 'router_routername',  type => 'text', size => 32 },
271       # router address block selection
272       # (one-to-one for now)
273       { field => 'router_blocknum',
274         type  => 'select-table',
275         table       => 'addr_block',
276         hashref     => { 'routernum' => '0' },
277         agent_virt  => 1,
278         agent_null  => 1,
279         name_col    => 'cidr',
280         order_by    => 'ORDER BY ip_gateway, ip_netmask',
281         empty_label => '(none)',
282         disable_empty => 0,
283       },
284     );
285     $labels->{router_routername}  = 'Attached router name';
286     $labels->{router_blocknum}    = 'Attached address block';
287   }
288 }
289
290 </%once>
291 <%init>
292
293 my %opt = @_;
294
295 #my( $svcnum, $pkgnum, $svcpart, $part_svc );
296 my( $pkgnum, $svcpart, $part_svc );
297
298 #get & untaint pkgnum & svcpart
299 if ( ! $cgi->param('error')
300      && $cgi->param('pkgnum') && $cgi->param('svcpart')
301    )
302 {
303   $cgi->param('pkgnum') =~ /^(\d+)$/ or die 'unparsable pkgnum';
304   $pkgnum = $1;
305   $cgi->param('svcpart') =~ /^(\d+)$/ or die 'unparsable svcpart';
306   $svcpart = $1;
307   #$cgi->delete_all(); #so edit.html treats this correctly as new??
308 }
309
310 </%init>