RT#40641: unprovisioning preserved cancelled services [fixed for cust_svc without...
[freeside.git] / httemplate / search / cust_svc.html
1 <& elements/search.html,
2           'title'       => emt('Service search results'),
3           'name'        => emt('services'),
4           'query'       => $sql_query,
5           'count_query' => $count_query,
6           'redirect'    => $link,
7           'header'      => [ emt('#'),
8                              emt('Service'),
9                              emt('Pkg. Status'),
10                              # package?
11                              FS::UI::Web::cust_header(),
12                            ],
13           'fields'      => [ 'svcnum',
14                              sub { 
15                                #$_[0]->svc. ': '. $_[0]->label;
16                                my($label, $value, $svcdb) = $_[0]->label;
17                                my $id = $_[0]->agent_svcid
18                                       ? $_[0]->agent_svcid.': '
19                                       : '';
20                                "$label: $id$value";
21                              },
22                              sub {
23                                $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_pkg;
24                                return '' unless $cust_pkg_cache{$_[0]->svcnum};
25                                $cust_pkg_cache{$_[0]->svcnum}->ucfirst_status
26                              },
27                              # package?
28                              \&FS::UI::Web::cust_fields,
29                            ],
30           'links'       => [ $link,
31                              $link,
32                              '', # pkg status
33                              # package?
34                              ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
35                                  FS::UI::Web::cust_header()
36                              ),
37                            ],
38           'align'       => 'rlr'. FS::UI::Web::cust_aligns(),
39           'color'       => [ 
40                              '',
41                              '',
42                              sub {
43                                $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_pkg;
44                                return '' unless $cust_pkg_cache{$_[0]->svcnum};
45                                my $c = FS::cust_pkg::statuscolors;
46                                $c->{$cust_pkg_cache{$_[0]->svcnum}->status };
47                              }, # pkg status
48                              FS::UI::Web::cust_colors(),
49                            ],
50           'style'       => [ 
51                              '',
52                              '',
53                              'b', # pkg status
54                              FS::UI::Web::cust_styles(),
55                            ],
56 &>
57 <%init>
58
59 die "access denied"
60   unless $FS::CurrentUser::CurrentUser->access_right('List services');
61
62 my %cust_pkg_cache;
63
64 my $sql_query;
65
66 my $orderby = 'ORDER BY cust_svc.svcnum'; #has to be ordered by something
67                                           #for pagination to work
68
69 if ( length( $cgi->param('search_svc') ) ) {
70
71   $sql_query = {
72     FS::cust_svc->smart_search_param(
73      'search' => scalar($cgi->param('search_svc'))
74     )
75   };
76
77 } else {
78
79   my $addl_from = ' LEFT JOIN part_svc  USING ( svcpart ) '.
80                   ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
81                   FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
82
83   my @extra_sql = ();
84
85   if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
86
87     $cgi->param('svcdb') =~ /^(svc_\w+)$/ or die "unknown svcdb";
88     push @extra_sql, "svcdb = '$1'";
89     $addl_from .= " LEFT JOIN $1 USING ( svcnum ) ";
90
91     push @extra_sql, 'pkgnum IS NULL'
92       if $cgi->param('magic') eq 'unlinked';
93
94     if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
95       my $sortby = $1;
96       $orderby = "ORDER BY $sortby";
97     }
98
99   } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
100
101     push @extra_sql, "svcpart = $1";
102     if (defined($cgi->param('cancelled'))) {
103       if ($cgi->param('cancelled')) {
104         push @extra_sql, "cust_pkg.cancel IS NOT NULL";
105       } else {
106         push @extra_sql, "cust_pkg.cancel IS NULL";
107       }
108     }
109
110   } else {
111     errorpage("No search term specified");
112   }
113
114   #here is the agent virtualization
115   push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql( 
116                      'null_right' => 'View/link unlinked services'
117                    );
118
119   my $extra_sql = ' WHERE '. join(' AND ', @extra_sql );
120
121   $sql_query = {
122     'select'     => 'cust_svc.*, part_svc.*',
123     'table'      => 'cust_svc',
124     'addl_from'  => $addl_from,
125     'hashref'    => {},
126     'extra_sql'  => $extra_sql,
127   };
128
129 }
130
131 # at this point the query must provide all fields from 
132 # cust_svc and part_svc, and must include join_cust_main.
133 $sql_query->{'select'} = join(', ',
134                                     $sql_query->{'select'},
135                                     'cust_main.custnum',
136                                     FS::UI::Web::cust_sql_fields(),
137                              );
138 $sql_query->{'order_by'} = $orderby;
139
140 my $count_query = "SELECT COUNT(*) FROM cust_svc ". $sql_query->{addl_from}.
141                                                ' '. $sql_query->{extra_sql};
142
143 my $link = sub {
144   my $cust_svc = shift;
145   my $url;
146   if ( $cust_svc->svcpart ) {
147     $url = svc_url(
148       'm'        => $m,
149       'action'   => 'view',
150       'svcdb'    => $cust_svc->svcdb, #we have it from the joined search
151       'query'     => '',
152     );
153   } else { # bizarre unlinked service case
154     $url = $p.'view/svc_Common.html?svcdb='.$cust_svc->svcdb.';svcnum=';
155   }
156   [ $url, 'svcnum' ];
157 };
158
159 my $link_cust = sub {
160   my $cust_svc = shift;
161   if ( $cust_svc->custnum ) {
162     [ "${p}view/cust_main.cgi?", 'custnum' ];
163   } else {
164     '';
165   }
166 };
167
168 </%init>