add conditions for customer cancelled packages, RT#42043
[freeside.git] / httemplate / search / svc_external.cgi
1 <& elements/svc_Common.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 <%init>
46
47 die "access denied"
48   unless $FS::CurrentUser::CurrentUser->access_right('List services');
49
50 my $conf = new FS::Conf;
51
52 my %svc_external;
53 my @extra_sql = ();
54 my $orderby = 'ORDER BY svcnum';
55
56 my $link = [ "${p}view/svc_external.cgi?", 'svcnum' ];
57 my $redirect = $link;
58
59 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
60
61   push @extra_sql, 'pkgnum IS NULL'
62     if $cgi->param('magic') eq 'unlinked';
63
64   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
65     my $sortby = $1;
66     $orderby = "ORDER BY $sortby";
67   }
68
69 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
70
71   push @extra_sql, "svcpart = $1";
72
73 } elsif ( $cgi->param('title') =~ /^(.*)$/ ) {
74
75   $svc_external{'title'} = $1;
76   $orderby = 'ORDER BY id';
77
78   # is this linked from anywhere???
79   # if( $cgi->param('history') == 1 ) {
80   #   @h_svc_external=qsearch('h_svc_external',{ title => $1 });
81   # }
82
83 } elsif ( $cgi->param('id') =~ /^([\w\-\.]+)$/ ) {
84
85   $svc_external{'id'} = $1;
86
87 }
88
89 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
90                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
91                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
92                 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
93
94 #here is the agent virtualization
95 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql(
96                    'null_right' => 'View/link unlinked services'
97                  );
98
99 my $extra_sql = '';
100 if ( @extra_sql ) {
101   $extra_sql = ( keys(%svc_external) ? ' AND ' : ' WHERE ' ).
102                join(' AND ', @extra_sql );
103 }
104
105 my $count_query = "SELECT COUNT(*) FROM svc_external $addl_from ";
106 if ( keys %svc_external ) {
107   $count_query .= ' WHERE '.
108                     join(' AND ', map "$_ = ". dbh->quote($svc_external{$_}),
109                                       keys %svc_external
110                         );
111 }
112 $count_query .= $extra_sql;
113
114 my $sql_query = {
115   'table'     => 'svc_external',
116   'hashref'   => \%svc_external,
117   'select'    => join(', ',
118                    'svc_external.*',
119                    'part_svc.svc',
120                    'cust_main.custnum',
121                    FS::UI::Web::cust_sql_fields(),
122                  ),
123   'extra_sql' => $extra_sql,
124   'order_by'  => $orderby,
125   'addl_from' => $addl_from,
126 };
127
128 #smaller false laziness w/svc_*.cgi here
129 my $link_cust = sub {
130   my $svc_x = shift;
131   $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
132 };
133
134
135 </%init>