fix advanced package report, RT#40292, fallout from RT#20173
[freeside.git] / httemplate / search / cust_svc.html
1 <& elements/search.html,
2           'title'       => emt('Service search results'),
3               'name'        => emt('services'),
4               'query'       => $sql_query,
5               'count_query' => $count_query,
6               'redirect'    => $link,
7               'header'      => [ emt('#'),
8                                  emt('Service'),
9                                  # package?
10                                  FS::UI::Web::cust_header(),
11                                ],
12               'fields'      => [ 'svcnum',
13                                  sub { 
14                                    #$_[0]->svc. ': '. $_[0]->label;
15                                    my($label, $value, $svcdb) = $_[0]->label;
16                                    my $id = $_[0]->agent_svcid
17                                               ? $_[0]->agent_svcid.': '
18                                               : '';
19                                    "$label: $id$value";
20                                  },
21                                  # package?
22                                  \&FS::UI::Web::cust_fields,
23                                ],
24               'links'       => [ $link,
25                                  $link,
26                                  # package?
27                                  ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
28                                        FS::UI::Web::cust_header()
29                                  ),
30                                ],
31               'align' => 'rl'. FS::UI::Web::cust_aligns(),
32               'color' => [ 
33                            '',
34                            '',
35                            FS::UI::Web::cust_colors(),
36                          ],
37               'style' => [ 
38                            '',
39                            '',
40                            FS::UI::Web::cust_styles(),
41                          ],
42 &>
43 <%init>
44
45 die "access denied"
46   unless $FS::CurrentUser::CurrentUser->access_right('List services');
47
48 my $sql_query;
49
50 my $orderby = 'ORDER BY cust_svc.svcnum'; #has to be ordered by something
51                                           #for pagination to work
52
53 if ( length( $cgi->param('search_svc') ) ) {
54
55   $sql_query = {
56     FS::cust_svc->smart_search_param(
57      'search' => scalar($cgi->param('search_svc'))
58     )
59   };
60
61 } else {
62
63   my $addl_from = ' LEFT JOIN part_svc  USING ( svcpart ) '.
64                   ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
65                   FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
66
67   my @extra_sql = ();
68
69   if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
70
71     $cgi->param('svcdb') =~ /^(svc_\w+)$/ or die "unknown svcdb";
72     push @extra_sql, "svcdb = '$1'";
73     $addl_from .= " LEFT JOIN $1 USING ( svcnum ) ";
74
75     push @extra_sql, 'pkgnum IS NULL'
76       if $cgi->param('magic') eq 'unlinked';
77
78     if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
79       my $sortby = $1;
80       $orderby = "ORDER BY $sortby";
81     }
82
83   } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
84
85     push @extra_sql, "svcpart = $1";
86
87   } else {
88     errorpage("No search term specified");
89   }
90
91   #here is the agent virtualization
92   push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql( 
93                      'null_right' => 'View/link unlinked services'
94                    );
95
96   my $extra_sql = ' WHERE '. join(' AND ', @extra_sql );
97
98   $sql_query = {
99     'select'     => 'cust_svc.*, part_svc.*',
100     'table'      => 'cust_svc',
101     'addl_from'  => $addl_from,
102     'hashref'    => {},
103     'extra_sql'  => $extra_sql,
104   };
105
106 }
107
108 # at this point the query must provide all fields from 
109 # cust_svc and part_svc, and must include join_cust_main.
110 $sql_query->{'select'} = join(', ',
111                                     $sql_query->{'select'},
112                                     'cust_main.custnum',
113                                     FS::UI::Web::cust_sql_fields(),
114                              );
115 $sql_query->{'order_by'} = $orderby;
116
117 my $count_query = "SELECT COUNT(*) FROM cust_svc ". $sql_query->{addl_from}.
118                                                ' '. $sql_query->{extra_sql};
119
120 my $link = sub {
121   my $cust_svc = shift;
122   my $url;
123   if ( $cust_svc->svcpart ) {
124     $url = svc_url(
125       'm'        => $m,
126       'action'   => 'view',
127       'svcdb'    => $cust_svc->svcdb, #we have it from the joined search
128       'query'     => '',
129     );
130   } else { # bizarre unlinked service case
131     $url = $p.'view/svc_Common.html?svcdb='.$cust_svc->svcdb.';svcnum=';
132   }
133   [ $url, 'svcnum' ];
134 };
135
136 my $link_cust = sub {
137   my $cust_svc = shift;
138   if ( $cust_svc->custnum ) {
139     [ "${p}view/cust_main.cgi?", 'custnum' ];
140   } else {
141     '';
142   }
143 };
144
145 </%init>