d34ed509a51abc4989501646862db7abc2ac41d7
[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
36 % if ( $custnum ) { 
37
38   <& /elements/header.html, mt("View [_1]: [_2]",$label,$value) &>
39
40   <& /elements/small_custview.html, $custnum, '', 1,
41      "${p}view/cust_main.cgi" &>
42   <BR>
43
44 % } else { 
45
46   <& /elements/header.html, mt("View [_1]: [_2]",$label,$value), menubar(
47       emt("Cancel this (unaudited) [_1]",$label) =>
48             "javascript:areyousure(\'${p}misc/cancel-unaudited.cgi?$svcnum\')"
49   ) &>
50
51 % } 
52
53 % if ( $opt{radius_usage} ) {
54     <& svc_radius_usage.html,
55               'svc'      => $svc_x,
56               'part_svc' => $part_svc,
57               'cust_pkg' => $cust_pkg,
58     &>
59 % }
60
61 <% mt('Service #') |h %><B><% $svcnum %></B>
62 % if ( $custnum ) {
63 %   my $url = $opt{'edit_url'} || $p. 'edit/'. $opt{'table'}. '.cgi?';
64 <& /view/elements/svc_edit_link.html, 'svc' => $svc_x, 'edit_url' => $url &>
65 % }
66 <BR>
67
68 <% ntable("#cccccc") %><TR><TD><% ntable("#cccccc",2) %>
69
70 % my @inventory_items = $svc_x->inventory_item;
71 % foreach my $f ( @$fields ) {
72 %
73 %   my($field, $type, $value);
74 %   if ( ref($f) ) {
75 %     $field = $f->{'field'};
76 %     $type  = $f->{'type'} || 'text';
77 %     if ( $f->{'value_callback'} ) {
78 %       my $hack_strict_refs = \&{ $f->{'value_callback'} };
79 %       $value = &$hack_strict_refs($svc_x);
80 %     } else {
81 %       $value = exists($f->{'value'})
82 %                  ? $f->{'value'}
83 %                  : encode_entities($svc_x->$field);
84 %     }
85 %   } else {
86 %     $field = $f;
87 %     $type = 'text';
88 %     $value = encode_entities($svc_x->$field);
89 %   }
90 %
91 %   my $columndef = $part_svc->part_svc_column($field);
92 %   if ( $columndef->columnflag =~ /^[MA]$/ && $columndef->columnvalue =~ /,/ )
93 %   {
94 %     # inventory-select field with multiple classes
95 %     # show the class name to disambiguate
96 %     my ($item) = grep { $_->svc_field eq $field } @inventory_items;
97 %     my $class = qsearchs('inventory_class', { classnum => $item->classnum });
98 %     $value .= ' <i>('. $class->classname . ')</i>' if $class;
99 %   }
100 %   unless ($columndef->columnflag eq 'F' && !length($columndef->columnvalue)) {
101
102       <TR>
103         <TD ALIGN="right">
104           <% ( $opt{labels} && exists $opt{labels}->{$field} )
105                   ? $opt{labels}->{$field}
106                   : $field
107           %>
108         </TD>
109
110 %       $value = time2str($date_format,$value) if ( $type eq 'date' && $value );
111 %       $value = time2str("$date_format %H:%M",$value) if ( $type eq 'datetime' && $value );
112 %       $value = $value eq 'Y' ? emt('Yes') : emt('No') if ( $type eq 'checkbox' );
113 %       #eventually more options for <SELECT>, etc. fields
114
115         <TD BGCOLOR="#ffffff"><% $value %><TD>
116
117       </TR>
118
119 %   }
120 %
121 % } 
122
123 % foreach (sort { $a cmp $b } $svc_x->virtual_fields) { 
124   <% $svc_x->pvf($_)->widget('HTML', 'view', $svc_x->getfield($_)) %>
125 % } 
126
127
128 </TABLE></TD></TR></TABLE>
129
130 <BR>
131
132 <& svc_devices.html,
133      'svc_x' => $svc_x,
134      'table' => $svc_x->device_table,
135 &>
136
137 % if ( defined($opt{'html_foot'}) ) {
138
139   <% ref($opt{'html_foot'})
140        ? &{ $opt{'html_foot'} }($svc_x)
141        : $opt{'html_foot'}
142   %>
143   <BR>
144
145 % }
146
147 % if ( $cust_svc ) {
148 <& /elements/table-tickets.html, object => $cust_svc &>
149 % }
150
151 <% joblisting({'svcnum'=>$svcnum}, 1) %>
152
153 <% include('/elements/footer.html') %>
154 <%init>
155
156 die "access denied"
157   unless $FS::CurrentUser::CurrentUser->access_right('View customer services');
158
159 my(%opt) = @_;
160
161 my $conf = new FS::Conf;
162 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
163
164 my $table = $opt{'table'};
165
166 my $fields = $opt{'fields'}
167              #|| [ grep { $_ ne 'svcnum' } dbdef->table($table)->columns ];
168              || [ grep { $_ ne 'svcnum' } fields($table) ];
169
170 my $svcnum;
171 if ( $cgi->param('svcnum') ) {
172   $cgi->param('svcnum') =~ /^(\d+)$/ or die "unparseable svcnum";
173   $svcnum = $1;
174 } else {
175   my($query) = $cgi->keywords;
176   $query =~ /^(\d+)$/ or die "no svcnum";
177   $svcnum = $1;
178 }
179 my $svc_x = qsearchs({
180   'select'    => $opt{'table'}.'.*',
181   'table'     => $opt{'table'},
182   'addl_from' => ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
183                  ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
184                  ' LEFT JOIN cust_main USING ( custnum ) ',
185   'hashref'   => { 'svcnum' => $svcnum },
186   'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql(
187                             'null_right' => 'View/link unlinked services'
188                           ),
189 }) or die "Unknown svcnum $svcnum in ". $opt{'table'}. " table\n";
190
191 my $cust_svc = $svc_x->cust_svc;
192 my ($label, $value, $svcdb, $part_svc );
193 my $labels = $opt{labels}; #not -> here
194
195 if ( $cust_svc ) {
196   ($label, $value, $svcdb) = $cust_svc->label;
197
198   $part_svc = $cust_svc->part_svc;
199
200   #false laziness w/edit/svc_Common.html
201   #override default labels with service-definition labels if applicable
202   foreach my $field ( keys %$labels ) {
203     my $col = $part_svc->part_svc_column($field);
204     $labels->{$field} = $col->columnlabel if $col->columnlabel !~ /^\s*$/;
205   }
206 } else {
207   $label = "Unlinked $table";
208   $value = $svc_x->label;
209   $svcdb = $table;
210   # just to satisfy callbacks
211   $part_svc = FS::part_svc->new({ svcpart => 0, svcdb => $table });
212 }
213
214 my $pkgnum = $cust_svc->pkgnum if $cust_svc;
215
216 my($cust_pkg, $custnum);
217 if ($pkgnum) {
218   $cust_pkg = $cust_svc->cust_pkg;
219   $custnum = $cust_pkg->custnum;
220 } else {
221   $cust_pkg = '';
222   $custnum = '';
223 }
224
225 # attached routers
226 if ( my $router = qsearchs('router', { svcnum => $svc_x->svcnum }) ) {
227   push @$fields, qw(router_routername router_block);
228   $labels->{'router_routername'} = 'Attached router';
229   $labels->{'router_block'} = 'Attached address block';
230   $svc_x->set('router_routername', $router->routername);
231   my $block = qsearchs('addr_block', { routernum => $router->routernum });
232   if ( $block ) {
233     $svc_x->set('router_block', $block->cidr);
234   } else {
235     $svc_x->set('router_block', '<i>(none)</i>');
236   }
237 }
238
239 &{ $opt{'svc_callback'} }( $cgi, $svc_x, $part_svc, $cust_pkg, $fields, \%opt ) 
240     if $opt{'svc_callback'};
241 </%init>