stray closing /TABLE in the no-ticket case
[freeside.git] / httemplate / search / svc_forward.cgi
1 <& elements/search.html,
2                  'title'             => "Mail forward Search Results",
3                  'name'              => 'mail forwards',
4                  'query'             => $sql_query,
5                  'count_query'       => $count_query,
6                  'redirect'          => $link,
7                  'header'            => [ '#',
8                                           'Service',
9                                           'Mail to',
10                                           'Forwards to',
11                                           emt('Pkg. Status'),
12                                           FS::UI::Web::cust_header(),
13                                         ],
14                  'fields'            => [ 'svcnum',
15                                           'svc',
16                                           $format_src,
17                                           $format_dst,
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_src,
28                                           $link_dst,
29                                           '', # pkg status
30                                           ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
31                                                 FS::UI::Web::cust_header()
32                                           ),
33                                         ],
34                  'align' => 'rlllr'. FS::UI::Web::cust_aligns(),
35                  'color' => [ 
36                               '',
37                               '',
38                               '',
39                               '',
40                               sub {
41                                 $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
42                                 return '' unless $cust_pkg_cache{$_[0]->svcnum};
43                                 my $c = FS::cust_pkg::statuscolors;
44                                 $c->{$cust_pkg_cache{$_[0]->svcnum}->status };
45                               }, # pkg status
46                               FS::UI::Web::cust_colors(),
47                             ],
48                  'style' => [ 
49                               '',
50                               '',
51                               '',
52                               '',
53                               'b',
54                               FS::UI::Web::cust_styles(),
55                             ],
56              
57 &>
58 <%init>
59
60 die "access denied"
61   unless $FS::CurrentUser::CurrentUser->access_right('List services');
62
63 my %cust_pkg_cache;
64
65 my $conf = new FS::Conf;
66
67 my $orderby = 'ORDER BY svcnum';
68 my @extra_sql = ();
69 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
70
71   push @extra_sql, 'pkgnum IS NULL'
72     if $cgi->param('magic') eq 'unlinked';
73
74   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
75     my $sortby = $1;
76     $orderby = "ORDER BY $sortby";
77   }
78
79 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
80   push @extra_sql, "svcpart = $1";
81   if (defined($cgi->param('cancelled'))) {
82     if ($cgi->param('cancelled')) {
83       push @extra_sql, "cust_pkg.cancel IS NOT NULL";
84     } else {
85       push @extra_sql, "cust_pkg.cancel IS NULL";
86     }
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                 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
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   scalar(@extra_sql)
102     ? ' WHERE '. join(' AND ', @extra_sql )
103     : '';
104
105 my $count_query = "SELECT COUNT(*) FROM svc_forward $addl_from $extra_sql";
106 my $sql_query = {
107   'table'     => 'svc_forward',
108   'hashref'   => {},
109   'select'    => join(', ',
110                    'svc_forward.*',
111                    'part_svc.svc',
112                    'cust_main.custnum',
113                    FS::UI::Web::cust_sql_fields(),
114                  ),
115   'extra_sql' => $extra_sql,
116   'order_by'  => $orderby,
117   'addl_from' => $addl_from,
118 };
119
120 #        <TH>Service #<BR><FONT SIZE=-1>(click to view forward)</FONT></TH>
121 #        <TH>Mail to<BR><FONT SIZE=-1>(click to view account)</FONT></TH>
122 #        <TH>Forwards to<BR><FONT SIZE=-1>(click to view account)</FONT></TH>
123
124 my $link = [ "${p}view/svc_forward.cgi?", 'svcnum' ];
125
126 my $format_src = sub {
127   my $svc_forward = shift;
128   if ( $svc_forward->srcsvc_acct ) {
129     $svc_forward->srcsvc_acct->email;
130   } else {
131     my $src = $svc_forward->src;
132     $src = "<I>(anything)</I>$src" if $src =~ /^@/;
133     $src;
134   }
135 };
136
137 my $link_src = sub {
138   my $svc_forward = shift;
139   if ( $svc_forward->srcsvc_acct ) {
140     [ "${p}view/svc_acct.cgi?", 'srcsvc' ];
141   } else {
142     '';
143   }
144 };
145
146 my $format_dst = sub {
147   my $svc_forward = shift;
148   if ( $svc_forward->dstsvc_acct ) {
149     $svc_forward->dstsvc_acct->email;
150   } else {
151     $svc_forward->dst;
152   }
153 };
154
155 my $link_dst = sub {
156   my $svc_forward = shift;
157   if ( $svc_forward->dstsvc_acct ) {
158     [ "${p}view/svc_acct.cgi?", 'dstsvc' ];
159   } else {
160     '';
161   }
162 };
163
164 #smaller false laziness w/svc_*.cgi here
165 my $link_cust = sub {
166   my $svc_x = shift;
167   $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
168 };
169
170 </%init>