fix unapplied payment report, RT#73048, fallout from #25944
[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                                           emt('Pkg. Status'),
12                                           FS::UI::Web::cust_header(),
13                                         ],
14                  'fields'            => [ 'svcnum',
15                                           'svc',
16                                           'id',
17                                           'title',
18                                           sub {
19                                             $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
20                                             return '' unless $cust_pkg_cache{$_[0]->svcnum};
21                                             $cust_pkg_cache{$_[0]->svcnum}->ucfirst_status
22                                           },
23                                           \&FS::UI::Web::cust_fields,
24                                         ],
25                  'links'             => [ $link,
26                                           $link,
27                                           $link,
28                                           $link,
29                                           '', # pkg status
30                                           ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
31                                                 FS::UI::Web::cust_header()
32                                           ),
33                                         ],
34                  'align' => 'rlrrr'.
35                             FS::UI::Web::cust_aligns(),
36                  'color' => [ 
37                               '',
38                               '',
39                               '',
40                               '',
41                               sub {
42                                 $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
43                                 return '' unless $cust_pkg_cache{$_[0]->svcnum};
44                                 my $c = FS::cust_pkg::statuscolors;
45                                 $c->{$cust_pkg_cache{$_[0]->svcnum}->status };
46                               }, # pkg status
47                               FS::UI::Web::cust_colors(),
48                             ],
49                  'style' => [ 
50                               '',
51                               '',
52                               '',
53                               '',
54                               'b',
55                               FS::UI::Web::cust_styles(),
56                             ],
57           
58 &>
59 <%init>
60
61 die "access denied"
62   unless $FS::CurrentUser::CurrentUser->access_right('List services');
63
64 my %cust_pkg_cache;
65
66 my $conf = new FS::Conf;
67
68 my %svc_external;
69 my @extra_sql = ();
70 my $orderby = 'ORDER BY svcnum';
71
72 my $link = [ "${p}view/svc_external.cgi?", 'svcnum' ];
73 my $redirect = $link;
74
75 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
76
77   push @extra_sql, 'pkgnum IS NULL'
78     if $cgi->param('magic') eq 'unlinked';
79
80   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
81     my $sortby = $1;
82     $orderby = "ORDER BY $sortby";
83   }
84
85 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
86
87   push @extra_sql, "svcpart = $1";
88   if (defined($cgi->param('cancelled'))) {
89     if ($cgi->param('cancelled')) {
90       push @extra_sql, "cust_pkg.cancel IS NOT NULL";
91     } else {
92       push @extra_sql, "cust_pkg.cancel IS NULL";
93     }
94   }
95
96 } elsif ( $cgi->param('title') =~ /^(.*)$/ ) {
97
98   $svc_external{'title'} = $1;
99   $orderby = 'ORDER BY id';
100
101   # is this linked from anywhere???
102   # if( $cgi->param('history') == 1 ) {
103   #   @h_svc_external=qsearch('h_svc_external',{ title => $1 });
104   # }
105
106 } elsif ( $cgi->param('id') =~ /^([\w\-\.]+)$/ ) {
107
108   $svc_external{'id'} = $1;
109
110 }
111
112 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
113                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
114                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
115                 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
116
117 #here is the agent virtualization
118 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql(
119                    'null_right' => 'View/link unlinked services'
120                  );
121
122 my $extra_sql = '';
123 if ( @extra_sql ) {
124   $extra_sql = ( keys(%svc_external) ? ' AND ' : ' WHERE ' ).
125                join(' AND ', @extra_sql );
126 }
127
128 my $count_query = "SELECT COUNT(*) FROM svc_external $addl_from ";
129 if ( keys %svc_external ) {
130   $count_query .= ' WHERE '.
131                     join(' AND ', map "$_ = ". dbh->quote($svc_external{$_}),
132                                       keys %svc_external
133                         );
134 }
135 $count_query .= $extra_sql;
136
137 my $sql_query = {
138   'table'     => 'svc_external',
139   'hashref'   => \%svc_external,
140   'select'    => join(', ',
141                    'svc_external.*',
142                    'part_svc.svc',
143                    'cust_main.custnum',
144                    FS::UI::Web::cust_sql_fields(),
145                  ),
146   'extra_sql' => $extra_sql,
147   'order_by'  => $orderby,
148   'addl_from' => $addl_from,
149 };
150
151 #smaller false laziness w/svc_*.cgi here
152 my $link_cust = sub {
153   my $svc_x = shift;
154   $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
155 };
156
157
158 </%init>