Merge branch 'patch-19' of https://github.com/gjones2/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                                  # package?
10                                  FS::UI::Web::cust_header(),
11                                ],
12               'fields'      => [ 'svcnum',
13                                  sub { 
14                                    #$_[0]->svc. ': '. $_[0]->label;
15                                    my($label, $value, $svcdb) = $_[0]->label;
16                                    my $id = $_[0]->agent_svcid
17                                               ? $_[0]->agent_svcid.': '
18                                               : '';
19                                    "$label: $id$value";
20                                  },
21                                  # package?
22                                  \&FS::UI::Web::cust_fields,
23                                ],
24               'links'       => [ $link,
25                                  $link,
26                                  # package?
27                                  ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
28                                        FS::UI::Web::cust_header()
29                                  ),
30                                ],
31               'align' => 'rl'. FS::UI::Web::cust_aligns(),
32               'color' => [ 
33                            '',
34                            '',
35                            FS::UI::Web::cust_colors(),
36                          ],
37               'style' => [ 
38                            '',
39                            '',
40                            FS::UI::Web::cust_styles(),
41                          ],
42 &>
43 <%init>
44
45 die "access denied"
46   unless $FS::CurrentUser::CurrentUser->access_right('List services');
47
48 my $sql_query;
49
50 my $orderby = 'ORDER BY svcnum'; #has to be ordered by something
51                                  #for pagination to work
52
53 if ( length( $cgi->param('search_svc') ) ) {
54
55   $sql_query = {
56     FS::cust_svc->smart_search_param(
57      'search' => scalar($cgi->param('search_svc'))
58     )
59   };
60
61 } else {
62
63   my $addl_from = ' LEFT JOIN part_svc  USING ( svcpart ) '.
64                   ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
65                   FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
66
67   my @extra_sql = ();
68
69   if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
70
71     $cgi->param('svcdb') =~ /^(svc_\w+)$/ or die "unknown svcdb";
72     push @extra_sql, "svcdb = '$1'";
73     $addl_from .= " LEFT JOIN $1 USING ( svcnum ) ";
74
75     push @extra_sql, 'pkgnum IS NULL'
76       if $cgi->param('magic') eq 'unlinked';
77
78     if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
79       my $sortby = $1;
80       $orderby = "ORDER BY $sortby";
81     }
82
83   } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
84
85     push @extra_sql, "svcpart = $1";
86
87   } else {
88     errorpage("No search term specified");
89   }
90
91   #here is the agent virtualization
92   push @extra_sql, $FS::CurrentUser::CurrentUser->agentnums_sql( 
93                      'null_right' => 'View/link unlinked services'
94                    );
95
96   my $extra_sql = ' WHERE '. join(' AND ', @extra_sql );
97
98   $sql_query = {
99     'table'      => 'cust_svc',
100     'addl_from'  => $addl_from,
101     'hashref'    => {},
102     'extra_sql'  => $extra_sql,
103   };
104
105 }
106
107 $sql_query->{'select'} = join(', ',
108                                     'cust_svc.*',
109                                     'part_svc.*',
110                                     'cust_main.custnum',
111                                     FS::UI::Web::cust_sql_fields(),
112                              );
113 $sql_query->{'order_by'} = $orderby;
114
115 my $count_query = "SELECT COUNT(*) FROM cust_svc ". $sql_query->{addl_from}.
116                                                ' '. $sql_query->{extra_sql};
117
118 my $link = sub {
119   my $cust_svc = shift;
120   my $url = svc_url(
121     'm'        => $m,
122     'action'   => 'view',
123     #'part_svc' => $cust_svc->part_svc,
124     'svcdb'    => $cust_svc->svcdb, #we have it from the joined search
125     #'svc'      => $cust_svc, #redundant
126     'query'     => '',
127   );
128   [ $url, 'svcnum' ];
129 };
130
131 my $link_cust = sub {
132   my $cust_svc = shift;
133   if ( $cust_svc->custnum ) {
134     [ "${p}view/cust_main.cgi?", 'custnum' ];
135   } else {
136     '';
137   }
138 };
139
140 </%init>