Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / search / svc_dish.cgi
1 <& elements/svc_Common.html,
2                  'title'       => 'Dish Network Search Results',
3                  'name'        => 'services',
4                  'query'       => $sql_query,
5                  'count_query' => $count_query,
6                  'redirect'    => $link,
7                  'header'      => [ '#',
8                                     'Service',
9                                     'Account #',
10                                     emt('Pkg. Status'),
11                                     FS::UI::Web::cust_header(),
12                                   ],
13                  'fields'      => [ 'svcnum',
14                                     'svc',
15                                     'acctnum',
16                                     sub {
17                                       $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
18                                       $cust_pkg_cache{$_[0]->svcnum}->ucfirst_status
19                                     },
20                                     \&FS::UI::Web::cust_fields,
21                                   ],
22                  'links'       => [ $link,
23                                     $link,
24                                     $link,
25                                     '', # pkg status
26                                     ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
27                                           FS::UI::Web::cust_header()
28                                     ),
29                                   ],
30                  'align' => 'rllr'. FS::UI::Web::cust_aligns(),
31                  'color' => [ 
32                               '',
33                               '',
34                               '',
35                               sub {
36                                 my $c = FS::cust_pkg::statuscolors;
37                                 $c->{$cust_pkg_cache{$_[0]->svcnum}->status };
38                               }, # pkg status
39                               FS::UI::Web::cust_colors(),
40                             ],
41                  'style' => [ 
42                               '',
43                               '',
44                               '',
45                               'b',
46                               FS::UI::Web::cust_styles(),
47                             ],
48              
49 &>
50 <%init>
51
52 die "access denied"
53   unless $FS::CurrentUser::CurrentUser->access_right('List services');
54
55 my %cust_pkg_cache;
56
57 #my $conf = new FS::Conf;
58
59 my $orderby = 'ORDER BY svcnum';
60 my @extra_sql = ();
61 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
62
63   push @extra_sql, 'pkgnum IS NULL'
64     if $cgi->param('magic') eq 'unlinked';
65
66   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
67     my $sortby = $1;
68     $orderby = "ORDER BY $sortby";
69   }
70 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
71   push @extra_sql, "svcpart = $1";
72   if (defined($cgi->param('cancelled'))) {
73     if ($cgi->param('cancelled')) {
74       push @extra_sql, "cust_pkg.cancel IS NOT NULL";
75     } else {
76       push @extra_sql, "cust_pkg.cancel IS NULL";
77     }
78   }
79 }
80
81 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
82                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
83                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
84                 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
85
86 #here is the agent virtualization
87 push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql(
88                    'null_right' => 'View/link unlinked services'
89                  );
90
91 my $extra_sql = 
92   scalar(@extra_sql)
93     ? ' WHERE '. join(' AND ', @extra_sql )
94     : '';
95
96
97 my $count_query = "SELECT COUNT(*) FROM svc_dish $addl_from $extra_sql";
98 my $sql_query = {
99   'table'     => 'svc_dish',
100   'hashref'   => {},
101   'select'    => join(', ',
102                    'svc_dish.*',
103                    'part_svc.svc',
104                    'cust_main.custnum',
105                    FS::UI::Web::cust_sql_fields(),
106                  ),
107   'extra_sql' => $extra_sql,
108   'order_by'  => $orderby,
109   'addl_from' => $addl_from,
110 };
111
112 my $link  = [ "${p}view/svc_dish.cgi?", 'svcnum', ];
113
114 my $link_cust = sub {
115   my $svc_x = shift;
116   $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
117 };
118
119 </%init>