combine ticket notification scrips, #15353
[freeside.git] / httemplate / search / svc_external.cgi
1 <% include( 'elements/search.html',
2                  'title'             => 'External service search results',
3                  'name'              => 'external services',
4                  'query'             => $sql_query,
5                  'count_query'       => $count_query,
6                  'redirect'          => $redirect,
7                  'header'            => [ '#',
8                                           'Service',
9                                           ( FS::Msgcat::_gettext('svc_external-id') || 'External ID' ),
10                                           ( FS::Msgcat::_gettext('svc_external-title') || 'Title' ),
11                                           FS::UI::Web::cust_header(),
12                                         ],
13                  'fields'            => [ 'svcnum',
14                                           'svc',
15                                           'id',
16                                           'title',
17                                           \&FS::UI::Web::cust_fields,
18                                         ],
19                  'links'             => [ $link,
20                                           $link,
21                                           $link,
22                                           $link,
23                                           ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
24                                                 FS::UI::Web::cust_header()
25                                           ),
26                                         ],
27                  'align' => 'rlrr'.
28                             FS::UI::Web::cust_aligns(),
29                  'color' => [ 
30                               '',
31                               '',
32                               '',
33                               '',
34                               FS::UI::Web::cust_colors(),
35                             ],
36                  'style' => [ 
37                               '',
38                               '',
39                               '',
40                               '',
41                               FS::UI::Web::cust_styles(),
42                             ],
43           )
44 %>
45
46 <%init>
47
48 die "access denied"
49   unless $FS::CurrentUser::CurrentUser->access_right('List services');
50
51 my $conf = new FS::Conf;
52
53 my %svc_external;
54 my @extra_sql = ();
55 my $orderby = 'ORDER BY svcnum';
56
57 my $link = [ "${p}view/svc_external.cgi?", 'svcnum' ];
58 my $redirect = $link;
59
60 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
61
62   push @extra_sql, 'pkgnum IS NULL'
63     if $cgi->param('magic') eq 'unlinked';
64
65   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
66     my $sortby = $1;
67     $orderby = "ORDER BY $sortby";
68   }
69
70 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
71
72   push @extra_sql, "svcpart = $1";
73
74 } elsif ( $cgi->param('title') =~ /^(.*)$/ ) {
75
76   $svc_external{'title'} = $1;
77   $orderby = 'ORDER BY id';
78
79   # is this linked from anywhere???
80   # if( $cgi->param('history') == 1 ) {
81   #   @h_svc_external=qsearch('h_svc_external',{ title => $1 });
82   # }
83
84 } elsif ( $cgi->param('id') =~ /^([\w\-\.]+)$/ ) {
85
86   $svc_external{'id'} = $1;
87
88 }
89
90 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
91                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
92                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
93                 ' LEFT JOIN cust_main USING ( custnum ) ';
94
95 #here is the agent virtualization
96 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql(
97                    'null_right' => 'View/link unlinked services'
98                  );
99
100 my $extra_sql = '';
101 if ( @extra_sql ) {
102   $extra_sql = ( keys(%svc_external) ? ' AND ' : ' WHERE ' ).
103                join(' AND ', @extra_sql );
104 }
105
106 my $count_query = "SELECT COUNT(*) FROM svc_external $addl_from ";
107 if ( keys %svc_external ) {
108   $count_query .= ' WHERE '.
109                     join(' AND ', map "$_ = ". dbh->quote($svc_external{$_}),
110                                       keys %svc_external
111                         );
112 }
113 $count_query .= $extra_sql;
114
115 my $sql_query = {
116   'table'     => 'svc_external',
117   'hashref'   => \%svc_external,
118   'select'    => join(', ',
119                    'svc_external.*',
120                    'part_svc.svc',
121                    'cust_main.custnum',
122                    FS::UI::Web::cust_sql_fields(),
123                  ),
124   'extra_sql' => $extra_sql,
125   'order_by'  => $orderby,
126   'addl_from' => $addl_from,
127 };
128
129 #smaller false laziness w/svc_*.cgi here
130 my $link_cust = sub {
131   my $svc_x = shift;
132   $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
133 };
134
135
136 </%init>