summaryrefslogtreecommitdiff
path: root/httemplate/edit/elements/svc_Common.html
blob: e1c30908041424cbee226b4d091afbd10fd563c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<% include( 'edit.html',

                 'menubar' => [],

                 'error_callback' => sub {
                   my( $cgi, $svc_x, $fields, $opt ) = @_;
                   #$svcnum = $svc_x->svcnum;
                   $pkgnum  = $cgi->param('pkgnum');
                   $svcpart = $cgi->param('svcpart');

                   $part_svc = qsearchs( 'part_svc', { svcpart=>$svcpart });
                   die "No part_svc entry!" unless $part_svc;

                   label_fixup($part_svc, $opt);

                   $svc_x->setfield('svcpart', $svcpart);

                   if ( my $cb = $opt{'svc_error_callback'} ) {
                     my $cust_pkg = $pkgnum
                                      ? qsearchs('cust_pkg', {pkgnum=>$pkgnum})
                                      : ''; #?
                     &{ $cb }( $cgi,$svc_x, $part_svc,$cust_pkg, $fields,$opt);
                   }

                 },

                 'edit_callback' => sub {
                   my( $cgi, $svc_x, $fields, $opt ) = @_;
                   #$svcnum = $svc_x->svcnum;
                   my $cust_svc = $svc_x->cust_svc
                     or die "Unknown (cust_svc) svcnum!";

                   $pkgnum  = $cust_svc->pkgnum;
                   $svcpart = $cust_svc->svcpart;
  
                   $part_svc = qsearchs ('part_svc', { svcpart=>$svcpart });
                   die "No part_svc entry!" unless $part_svc;

                   label_fixup($part_svc, $opt);
                   
                   if ( my $cb = $opt{'svc_edit_callback'} ) {
                     my $cust_pkg = $pkgnum
                                      ? qsearchs('cust_pkg', {pkgnum=>$pkgnum})
                                      : ''; #?
                     &{ $cb }( $cgi,$svc_x, $part_svc,$cust_pkg, $fields,$opt);
                   }

                   if ( $part_svc->has_router ) {
                     my $router = qsearchs('router', {svcnum => $svc_x->svcnum});
                     if ( $router ) {
                       $svc_x->set("router_$_", $router->get($_))
                         foreach ('routername', 'routernum');
                       my ($block) = $router->addr_block; # one-to-one for now
                       if ( $block ) {
                         $svc_x->set('router_blocknum', $block->blocknum);
                         # silly, but necessary...make the currently 
                         # assigned block appear on the list
                         my ($field) = grep {ref($_) and 
                                             $_->{field} eq 'router_blocknum'}
                                         @$fields;
                         $field->{extra_sql} = 
                           ' OR routernum = '.$router->routernum;
                         $field->{curr_value} = $block->blocknum;
                       }
                     }
                   }

                 },

                 'new_hashref_callback' => sub {
                   #my( $cgi, $svc_x ) = @_;

                   { pkgnum  => $pkgnum,
                     svcpart => $svcpart,
                   };

                 },

                 'new_callback' => sub {
                   my( $cgi, $svc_x, $fields, $opt ) = @_;

                   $part_svc = qsearchs( 'part_svc', { svcpart=>$svcpart });
                   die "No part_svc entry!" unless $part_svc;

                   label_fixup($part_svc, $opt);

                   #$svcnum='';

                   if ( my $cb = $opt{'svc_new_callback'} ) {
                     my $cust_pkg = $pkgnum
                                     ? qsearchs('cust_pkg', {pkgnum=>$pkgnum})
                                     : ''; #?
                     &{ $cb }( $cgi,$svc_x, $part_svc,$cust_pkg, $fields,$opt);
                   }

                   $svc_x->set_default_and_fixed;
                 },

                 'field_callback' => sub {
                   my ($cgi, $object, $f) = @_;

                   my $columndef = $part_svc->part_svc_column($f->{'field'});
                   my $flag = $columndef->columnflag;

                   $f->{'required'} = 1
                     if $columndef->required;

                   if ( $flag eq 'F' ) { #fixed
                     $f->{'value'} = $columndef->columnvalue;
                     if (length($columndef->columnvalue)) {

                       if ( $f->{'type'} =~ /^select-?(.*)/ ) {
                         # try to display this in a user-friendly manner
                         if ( $f->{'table'} ) { # find matching records
                           $f->{'value_col'} ||=
                             dbdef->table($f->{'table'})->primary_key;

                           my @values = split(',', $f->{'value'});
                           my @recs;
                           foreach (@values) {
                             push @recs, qsearchs( $f->{'table'},
                                         { $f->{'value_col'} => $_ }
                                         );
                           }
                           if ( @recs ) {
                             my $method = $f->{'name_col'};
                             if ( $f->{'multiple'} ) {
                               $f->{'formatted_value'} = [
                                 map { $_->method } @recs
                               ];
                             } else { # there shouldn't be more than one...
                               $f->{'formatted_value'} = $recs[0]->$method;
                             }
                           } # if not, then just let tr-fixed display the
                             # values as-is

                         } # other select types probably don't matter
                       } # if it's a select

                       $f->{'type'} = 'fixed';

                     } else { # fixed, null
                       $f->{'type'} = 'hidden';
                     }

                   } elsif ( $flag eq 'A' ) { #auto assign from inventory
                     $f->{'type'} = 'hidden';

                   } elsif ( $flag eq 'M' ) { #manually assign from inventory
                     $f->{'type'} = 'select-inventory_item';
                     $f->{'empty_label'} = 'Select inventory item';
                     $f->{'extra_sql'} = 'WHERE ( svcnum IS NULL ' .
                        ($object->svcnum && ' OR svcnum = '.$object->svcnum) .
                        ')';
                     $f->{'classnum'} = $columndef->columnvalue;
                     $f->{'disable_empty'} = $object->svcnum ? 1 : 0;

                   } elsif ( $flag eq 'H' ) { #hardware
                     $f->{'type'}        = 'select-hardware_type';
                     $f->{'hashref'}     = {
                                            'classnum'=>$columndef->columnvalue
                                           };

                   } elsif ( $flag eq 'S' #selectable choice
                               && $f->{type} !~ /^select-svc/ ) {
                     $f->{type}    = 'select';
                     $f->{options} = [ split( /\s*,\s*/,
                                                $columndef->columnvalue)
                                     ];
                   } # shouldn't this be enforced for all 'S' fields?

                   elsif ( $flag eq 'P' ) { #form fcc_477 values
                     $f->{type}    = 'fixed';
                     my $cust_pkg = FS::Record::qsearchs({
                       'table'   => 'cust_pkg',
                       'hashref' => { 'pkgnum' => $object->{Hash}->{pkgnum} }
                     });
                     my $fcc_record = $cust_pkg->fcc_477_record('broadband_'.$columndef->columnvalue.'stream') if $cust_pkg;
                     $f->{'value'} = $fcc_record->{Hash}->{optionvalue} ? $fcc_record->{Hash}->{optionvalue} * 1000 : '';
                   } # end 477 values

                   if ( $f->{'type'} =~ /^select-svc/ )
                   {
                     $f->{'include_opt_callback'} =
                       sub { ( 'pkgnum'  => $pkgnum,
                               'svcpart' => $svcpart,
                             );
                           };
                   }

                   if ( $f->{'field'} eq 'custnum' && $pkgnum ) {
                     my $cust_pkg = qsearchs('cust_pkg', {'pkgnum' => $pkgnum});
                     $object->set('custnum', $cust_pkg->custnum);
                   }

                   if ( my $cb = $opt{'svc_field_callback'} ) {
                     &{ $cb }( $cgi, $object, $f);
                   }

                 },

                 'html_init' => sub {
                   my $html;
                   my $cust_main;
                   if ( $pkgnum ) {
                     my $cust_pkg = qsearchs('cust_pkg', {'pkgnum' => $pkgnum});
                     $cust_main = $cust_pkg->cust_main if $cust_pkg;
                     if ( $cust_main ) {
                        $html = include( '/elements/small_custview.html',
                                $cust_main,
                                '',
                                1,
                                popurl(2). "view/cust_main.cgi"
                              ). '<BR>';
                     }
                   }
                   $html;
                 },

                 'html_table_bottom' => sub {
                   my $svc_x = shift;
                   my $html = '';
                   foreach my $field ($svc_x->virtual_fields) {
                     if ($part_svc->part_svc_column($field)->columnflag ne 'F'){
                       # If the flag is X, it won't even show up
                       # in $svc_acct->virtual_fields.
                       $html .=
                         $svc_x->pvf($field)->widget( 'HTML',
                                                      'edit', 
                                                      $svc_x->getfield($field)
                                                    );
                     }
                   }
                   $html;
                 },

                 'html_bottom' => sub {
                   qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="$pkgnum">!.
                   qq!<INPUT TYPE="hidden" NAME="svcpart" VALUE="$svcpart">!;
                 },

                 %opt #pass through/override params
             )
%>
<%once>

sub label_fixup {
  my( $part_svc, $opt ) = @_;

  $opt->{'name'} ||= $part_svc->svc;

  my $svcdb = $part_svc->svcdb;
  require "FS/$svcdb.pm";

  if ( UNIVERSAL::can("FS::$svcdb", 'table_info') ) {
    #$opt->{'name'} ||= "FS::$svcdb"->table_info->{'name'};

    my $fields = "FS::$svcdb"->table_info->{'fields'};
    $opt->{'fields'} ||= [ grep { $_ ne 'svcnum' } keys %$fields ];

    $opt->{labels} ||= {
                         map { $_ => ( ref($fields->{$_})
                                         ? $fields->{$_}{'label'}
                                         : $fields->{$_}
                                     );
                             }
                         keys %$fields
                       };
  }

  #false laziness w/view/svc_Common.html
  #override default labels with service-definition labels if applicable
  my $labels = $opt->{labels}; # with -> here
  foreach my $field ( keys %{ $opt->{labels} } ) {
    my $col = $part_svc->part_svc_column($field);
    $labels->{$field} = $col->columnlabel if $col->columnlabel !~ /^\s*$/;
  }

  if ( $part_svc->has_router ) {
    # these will be set up as pseudo-fields in the new_ and edit_ callbacks
    push @{ $opt->{'fields'} }, (
      { field => 'router_routernum',   type => 'hidden' },
      { field => 'router_routername',  type => 'text', size => 32 },
      # router address block selection
      # (one-to-one for now)
      { field => 'router_blocknum',
        type  => 'select-table',
        table       => 'addr_block',
        hashref     => { 'routernum' => '0' },
        agent_virt  => 1,
        agent_null  => 1,
        name_col    => 'cidr',
        order_by    => 'ORDER BY ip_gateway, ip_netmask',
        empty_label => '(none)',
        disable_empty => 0,
      },
    );
    $labels->{router_routername}  = 'Attached router name';
    $labels->{router_blocknum}    = 'Attached address block';
  }
}

</%once>
<%init>

my %opt = @_;

#my( $svcnum, $pkgnum, $svcpart, $part_svc );
my( $pkgnum, $svcpart, $part_svc );

#get & untaint pkgnum & svcpart
if ( ! $cgi->param('error')
     && $cgi->param('pkgnum') && $cgi->param('svcpart')
   )
{
  $cgi->param('pkgnum') =~ /^(\d+)$/ or die 'unparsable pkgnum';
  $pkgnum = $1;
  $cgi->param('svcpart') =~ /^(\d+)$/ or die 'unparsable svcpart';
  $svcpart = $1;
  #$cgi->delete_all(); #so edit.html treats this correctly as new??
}

</%init>