Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / search / svc_external.cgi
1 <& elements/svc_Common.html,
2                  'title'             => 'External service search results',
3                  'name'              => 'external services',
4                  'query'             => $sql_query,
5                  'count_query'       => $count_query,
6                  'redirect'          => $redirect,
7                  'header'            => [ '#',
8                                           'Service',
9                                           ( FS::Msgcat::_gettext('svc_external-id') || 'External ID' ),
10                                           ( FS::Msgcat::_gettext('svc_external-title') || 'Title' ),
11                                           emt('Pkg. Status'),
12                                           FS::UI::Web::cust_header(),
13                                         ],
14                  'fields'            => [ 'svcnum',
15                                           'svc',
16                                           'id',
17                                           'title',
18                                           sub {
19                                             $cust_pkg_cache{$_[0]->svcnum} ||= $_[0]->cust_svc->cust_pkg;
20                                             $cust_pkg_cache{$_[0]->svcnum}->ucfirst_status
21                                           },
22                                           \&FS::UI::Web::cust_fields,
23                                         ],
24                  'links'             => [ $link,
25                                           $link,
26                                           $link,
27                                           $link,
28                                           '', # pkg status
29                                           ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
30                                                 FS::UI::Web::cust_header()
31                                           ),
32                                         ],
33                  'align' => 'rlrrr'.
34                             FS::UI::Web::cust_aligns(),
35                  'color' => [ 
36                               '',
37                               '',
38                               '',
39                               '',
40                               sub {
41                                 my $c = FS::cust_pkg::statuscolors;
42                                 $c->{$cust_pkg_cache{$_[0]->svcnum}->status };
43                               }, # pkg status
44                               FS::UI::Web::cust_colors(),
45                             ],
46                  'style' => [ 
47                               '',
48                               '',
49                               '',
50                               '',
51                               'b',
52                               FS::UI::Web::cust_styles(),
53                             ],
54           
55 &>
56 <%init>
57
58 die "access denied"
59   unless $FS::CurrentUser::CurrentUser->access_right('List services');
60
61 my %cust_pkg_cache;
62
63 my $conf = new FS::Conf;
64
65 my %svc_external;
66 my @extra_sql = ();
67 my $orderby = 'ORDER BY svcnum';
68
69 my $link = [ "${p}view/svc_external.cgi?", 'svcnum' ];
70 my $redirect = $link;
71
72 if ( $cgi->param('magic') =~ /^(all|unlinked)$/ ) {
73
74   push @extra_sql, 'pkgnum IS NULL'
75     if $cgi->param('magic') eq 'unlinked';
76
77   if ( $cgi->param('sortby') =~ /^(\w+)$/ ) {
78     my $sortby = $1;
79     $orderby = "ORDER BY $sortby";
80   }
81
82 } elsif ( $cgi->param('svcpart') =~ /^(\d+)$/ ) {
83
84   push @extra_sql, "svcpart = $1";
85   if (defined($cgi->param('cancelled'))) {
86     if ($cgi->param('cancelled')) {
87       push @extra_sql, "cust_pkg.cancel IS NOT NULL";
88     } else {
89       push @extra_sql, "cust_pkg.cancel IS NULL";
90     }
91   }
92
93 } elsif ( $cgi->param('title') =~ /^(.*)$/ ) {
94
95   $svc_external{'title'} = $1;
96   $orderby = 'ORDER BY id';
97
98   # is this linked from anywhere???
99   # if( $cgi->param('history') == 1 ) {
100   #   @h_svc_external=qsearch('h_svc_external',{ title => $1 });
101   # }
102
103 } elsif ( $cgi->param('id') =~ /^([\w\-\.]+)$/ ) {
104
105   $svc_external{'id'} = $1;
106
107 }
108
109 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
110                 ' LEFT JOIN part_svc  USING ( svcpart ) '.
111                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
112                 FS::UI::Web::join_cust_main('cust_pkg', 'cust_pkg');
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 = '';
120 if ( @extra_sql ) {
121   $extra_sql = ( keys(%svc_external) ? ' AND ' : ' WHERE ' ).
122                join(' AND ', @extra_sql );
123 }
124
125 my $count_query = "SELECT COUNT(*) FROM svc_external $addl_from ";
126 if ( keys %svc_external ) {
127   $count_query .= ' WHERE '.
128                     join(' AND ', map "$_ = ". dbh->quote($svc_external{$_}),
129                                       keys %svc_external
130                         );
131 }
132 $count_query .= $extra_sql;
133
134 my $sql_query = {
135   'table'     => 'svc_external',
136   'hashref'   => \%svc_external,
137   'select'    => join(', ',
138                    'svc_external.*',
139                    'part_svc.svc',
140                    'cust_main.custnum',
141                    FS::UI::Web::cust_sql_fields(),
142                  ),
143   'extra_sql' => $extra_sql,
144   'order_by'  => $orderby,
145   'addl_from' => $addl_from,
146 };
147
148 #smaller false laziness w/svc_*.cgi here
149 my $link_cust = sub {
150   my $svc_x = shift;
151   $svc_x->custnum ? [ "${p}view/cust_main.cgi?", 'custnum' ] : '';
152 };
153
154
155 </%init>