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