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