summaryrefslogtreecommitdiff
path: root/httemplate/search/elements/svc_Common.html
blob: 56c75bba3285ce1a6d3e7b747e1e039efd0aff82 (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
<& search.html, %opt &>
<%doc>
Currently does nothing but insert the classnames for fields chosen from an
inventory class.
</%doc>
<%init>
my %opt = @_;
my $query = $opt{query};
my $svcdb = $query->{'table'};

# to avoid looking up the inventory class of every service in the database,
# keep as much of the base query as possible.
my $item_query = { %$query };
$item_query->{'table'} = 'inventory_item';
$item_query->{'addl_from'} = 
  " JOIN ( $svcdb ". $query->{'addl_from'} .
  ") ON inventory_item.svcnum = $svcdb.svcnum ".
  " JOIN inventory_class ON (inventory_item.classnum = inventory_class.classnum)";
# avoid conflict with inventory_item.agentnum
$item_query->{'extra_sql'} =~ s/ agentnum/ cust_main.agentnum/g;
$item_query->{'select'} = 'inventory_item.svcnum, '.
                          'inventory_item.svc_field, '.
                          'inventory_class.classname';
my @items = qsearch($item_query);
my %item_fields;
foreach my $i (@items) {
  $item_fields{ $i->svc_field } ||= {};
  $item_fields{ $i->svc_field }{ $i->svcnum } = $i->classname;
}

$opt{'sort_fields'} ||= [];
for ( my $i = 0; $i < @{ $opt{'fields'} }; $i++ ) {
  my $f = $opt{'fields'}[$i];
  next if ref($f); # it's not a plain table column
  $opt{'sort_fields'}[$i] ||= $f;
  my $classnames = $item_fields{$f}; # hashref of svcnum -> classname
  next if !$classnames; # there are no inventory items in this column
  $opt{'fields'}[$i] = sub {
    my $svc = $_[0];
    if ( exists($classnames->{$svc->svcnum}) ) {
      return $svc->$f . '<BR><I>('. $classnames->{$svc->svcnum} . ')</I>';
    } else {
      return $svc->$f;
    }
  }; #sub
}

</%init>