Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / view / elements / svc_Common.html
1 <%doc>
2
3 #Example:
4
5   include( 'elements/svc_Common.html, 
6
7              'table' => 'svc_something'
8
9              'labels' => {
10                            'column' => 'Label',
11                          },
12
13              #listref - each item is a literal column name (or method) or
14              # (notyet) coderef.  if not specified all columns (except for the
15              #primary key) will be viewable
16              'fields' => [
17                          ]
18
19              # defaults to "edit/$table.cgi?", will have svcnum appended
20              'edit_url' => 
21
22              #at the very bottom (well, as low as you can go from here)
23              'html_foot'  => '',
24
25          )
26
27 </%doc>
28 <SCRIPT>
29 function areyousure(href) {
30 % my $delmsg = emt("Permanently delete this [_1]?", $label);
31   if (confirm(<% $delmsg |js_string %>) == true)
32     window.location.href = href;
33 }
34 </SCRIPT>
35 <STYLE>
36   td.content {
37     background-color: #ffffff;
38   }
39   .error {
40     color: #ff0000;
41     font-weight: bold;
42   }
43 </STYLE>
44
45 % if ( $custnum ) { 
46
47   <& /elements/header.html, mt("View [_1]: [_2]",$label,$value) &>
48
49   <& /elements/small_custview.html, $custnum, '', 1,
50      "${p}view/cust_main.cgi" &>
51   <BR>
52
53 % } else { 
54
55   <& /elements/header.html, mt("View [_1]: [_2]",$label,$value), menubar(
56       emt("Cancel this (unaudited) [_1]",$label) =>
57             "javascript:areyousure(\'${p}misc/cancel-unaudited.cgi?$svcnum\')"
58   ) &>
59
60 % } 
61
62 % if ( $opt{radius_usage} ) {
63     <& svc_radius_usage.html,
64               'svc'      => $svc_x,
65               'part_svc' => $part_svc,
66               'cust_pkg' => $cust_pkg,
67     &>
68 % }
69
70 <% mt('Service #') |h %><B><% $svcnum %></B>
71 % if ( $custnum ) {
72 %   my $url = $opt{'edit_url'} || $p. 'edit/'. $opt{'table'}. '.cgi?';
73 <& /view/elements/svc_edit_link.html, 'svc' => $svc_x, 'edit_url' => $url &>
74 % }
75 <BR>
76
77 <% ntable("#cccccc") %><TR><TD><% ntable("#cccccc",2) %>
78
79 % foreach my $f ( @$fields ) {
80 %   my ($field, $label, $value) = &{ $format_field }($f);
81 %   next if !$field; 
82       <TR>
83         <TD ALIGN="right">
84           <% $label %>
85         </TD>
86
87         <TD CLASS="content">
88           <% $value %>
89         </TD>
90       </TR>
91
92 % }
93
94 % foreach (sort { $a cmp $b } $svc_x->virtual_fields) { 
95   <% $svc_x->pvf($_)->widget('HTML', 'view', $svc_x->getfield($_)) %>
96 % } 
97
98
99 </TABLE></TD></TR></TABLE>
100
101 <BR>
102
103 <& svc_devices.html,
104      'svc_x' => $svc_x,
105      'table' => $svc_x->device_table,
106 &>
107
108 % if ( defined($opt{'html_foot'}) ) {
109
110   <% ref($opt{'html_foot'})
111        ? &{ $opt{'html_foot'} }($svc_x)
112        : $opt{'html_foot'}
113   %>
114   <BR>
115
116 % }
117
118 % if ( $cust_svc ) {
119 <& /elements/table-tickets.html, object => $cust_svc &>
120 % }
121
122 <% joblisting({'svcnum'=>$svcnum}, 1) %>
123
124 <% include('/elements/footer.html') %>
125 <%init>
126
127 die "access denied"
128   unless $FS::CurrentUser::CurrentUser->access_right('View customer services');
129
130 my(%opt) = @_;
131
132 my $conf = new FS::Conf;
133 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
134
135 my $table = $opt{'table'};
136
137 my $fields = $opt{'fields'}
138              #|| [ grep { $_ ne 'svcnum' } dbdef->table($table)->columns ];
139              || [ grep { $_ ne 'svcnum' } fields($table) ];
140
141 my $svcnum;
142 if ( $cgi->param('svcnum') ) {
143   $cgi->param('svcnum') =~ /^(\d+)$/ or die "unparseable svcnum";
144   $svcnum = $1;
145 } else {
146   my($query) = $cgi->keywords;
147   $query =~ /^(\d+)$/ or die "no svcnum";
148   $svcnum = $1;
149 }
150 my $svc_x = qsearchs({
151   'select'    => $opt{'table'}.'.*',
152   'table'     => $opt{'table'},
153   'addl_from' => ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
154                  ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
155                  ' LEFT JOIN cust_main USING ( custnum ) ',
156   'hashref'   => { 'svcnum' => $svcnum },
157   'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql(
158                             'null_right' => 'View/link unlinked services'
159                           ),
160 }) or die "Unknown svcnum $svcnum in ". $opt{'table'}. " table\n";
161
162 my $cust_svc = $svc_x->cust_svc;
163 my ($label, $value, $svcdb, $part_svc );
164 my $labels = $opt{labels} || {};
165
166 if ( $cust_svc ) {
167   ($label, $value, $svcdb) = $cust_svc->label;
168
169   $part_svc = $cust_svc->part_svc;
170
171   #false laziness w/edit/svc_Common.html
172   #override default labels with service-definition labels if applicable
173   foreach my $field ( keys %$labels ) {
174     my $col = $part_svc->part_svc_column($field);
175     $labels->{$field} = $col->columnlabel if $col->columnlabel !~ /^\s*$/;
176   }
177 } else {
178   $label = "Unlinked $table";
179   $value = $svc_x->label;
180   $svcdb = $table;
181   # just to satisfy callbacks
182   $part_svc = FS::part_svc->new({ svcpart => 0, svcdb => $table });
183 }
184
185 my $pkgnum = $cust_svc->pkgnum if $cust_svc;
186
187 my($cust_pkg, $custnum);
188 if ($pkgnum) {
189   $cust_pkg = $cust_svc->cust_pkg;
190   $custnum = $cust_pkg->custnum;
191 } else {
192   $cust_pkg = '';
193   $custnum = '';
194 }
195
196 # attached routers
197 if ( my $router = qsearchs('router', { svcnum => $svc_x->svcnum }) ) {
198   push @$fields,
199     'router_routername',
200     'router_block';
201
202   $labels->{'router_routername'} = 'Attached router';
203   $labels->{'router_block'} = 'Attached address block';
204   $svc_x->set('router_routername', $router->routername);
205   my $block = qsearchs('addr_block', { routernum => $router->routernum });
206   if ( $block ) {
207     $svc_x->set('router_block', $block->cidr);
208   } else {
209     $svc_x->set('router_block', '(none)');
210   }
211 }
212
213 my @inventory_items = $svc_x->inventory_item;
214
215 my $format_field = sub {
216   my $f = shift;
217   my($field, $type, $value);
218   if ( ref($f) ) {
219     $field = $f->{'field'};
220     $type  = $f->{'type'} || 'text';
221   } else {
222     $field = $f;
223     $type = 'text';
224   }
225   warn "$field\t$type\t$value\n";
226
227   my $columndef = $part_svc->part_svc_column($field);
228   # skip fields that are fixed and empty
229   if ( $columndef->columnflag eq 'F'
230        and length($columndef->columnvalue) == 0 ) {
231     return;
232   }
233
234   # things that override the column value: value_callback, select
235   if ( ref($f) and $f->{'value_callback'} ) {
236
237     my $hack_strict_refs = \&{ $f->{'value_callback'} };
238     $value = &$hack_strict_refs($svc_x);
239
240   } elsif ( $type eq 'select-table' ) {
241     # imitates the /elements/select-table interface
242     $value = $svc_x->$field;
243
244     my $value_col = $f->{'value_col'} ||
245                     dbdef->table($f->{'table'})->primary_key;
246     my $name_col = $f->{'name_col'} or die 'name_col required';
247     # we don't yet support multiple-valued fields here
248     my $obj = qsearchs($f->{'table'}, { $value_col => $value });
249     if ( $obj ) {
250       $value = $obj->$name_col; # can be any method of the object
251     } else {
252       # show the raw value, but mark it as an error
253       $value = '<SPAN CLASS="error">' . $f->{'table'} . ' ' .
254                 encode_entities($value) . '</SPAN>';
255     }
256
257   } else {
258     $value = encode_entities($svc_x->$field);
259   }
260
261   # inventory-select field with multiple classes
262   # show the class name to disambiguate
263   if ( $columndef->columnflag =~ /^[MA]$/ && $columndef->columnvalue =~ /,/ )
264   {
265     my ($item) = grep { $_->svc_field eq $field } @inventory_items;
266     my $class = qsearchs('inventory_class', { classnum => $item->classnum });
267     $value .= ' <i>('. $class->classname . ')</i>' if $class;
268   }
269
270   # formatting tweaks
271   if ( $type eq 'date' and $value ) {
272     $value = time2str($date_format,$value)
273   } elsif ( $type eq 'datetime' and $value ) {
274     $value = time2str("$date_format %H:%M",$value)
275   } elsif ( $type eq 'checkbox' ) {
276     $value = $value eq 'Y' ? emt('Yes') : emt('No');
277   } elsif ( $type =~ /(input-)?mac_addr/ and $value =~ /\w/) {
278     my $vendor = Net::MAC::Vendor::lookup($value)->[0];
279     $value .= " ($vendor)" if $vendor;
280     $value = $m->scomp('/elements/mac_addr.html', $value);
281   }
282
283   # 'link' option
284   my $href;
285   if ( ref($f) and exists $f->{'link'} ) {
286     my $link = $f->{'link'};
287     if ( ref($link) eq 'CODE' ) {
288       $link = &{$link}($svc_x);
289     }
290     if ( ref($link) eq 'ARRAY' ) {
291       my ($base, $method) = @$link;
292       $href = $base . $svc_x->$method();
293     } elsif ( !ref($link) ) {
294       $href = $link;
295     }
296
297     if ( $href ) {
298       $value = qq!<A HREF="$href">$value</A>!;
299     }
300   }
301
302   my $label = $opt{labels}->{$field} || $field;
303   return ($field, $label, $value);
304 };
305
306 &{ $opt{'svc_callback'} }( $cgi, $svc_x, $part_svc, $cust_pkg, $fields, \%opt ) 
307     if $opt{'svc_callback'};
308 </%init>