show customer on availability report, RT#10520
[freeside.git] / httemplate / search / phone_avail.html
1 <% include( 'elements/search.html',
2               'title'         => 'Phone Number (DID) Search Results',
3               'name_singular' => 'phone number',
4               'query'         => {
5                                    'table'   => 'phone_avail',
6                                    'hashref' => {},
7                                    'select'  => join(', ',
8                                        'phone_avail.*',
9                                        'cust_main.custnum',
10                                        FS::UI::Web::cust_sql_fields(),
11                                    ),
12                                    'extra_sql' => $search,
13                                    'addl_from' => $addl_from,
14                                  },
15               'count_query'   => $count_query,
16               'header'        => [ '#',
17                                    'State',
18                                    'Phone Number',
19                                    'Rate Center',
20                                    'Batch',
21                                    'Export',
22                                    'Service',
23                                     FS::UI::Web::cust_header(),
24                                  ],
25               'fields'        => [
26                 'availnum',
27                 'state',
28                 sub { my $pn = shift;
29                       '+'. $pn->countrycode. ' '.
30                         $pn->npa. ' '.  $pn->nxx. '-'. $pn->station;
31                     },
32                 sub { shift->get('name') },
33                 'availbatch',
34                 'exportnum', #XXX
35                 #sub { },
36                 'svcnum', #XXX
37                 \&FS::UI::Web::cust_fields,
38               ],
39               'align'         => 'rllllll'.FS::UI::Web::cust_aligns(),
40               'links'         => [
41                 '',
42                 '',
43                 '',
44                 '',
45                 '',
46                 '', #XXX #$export_link
47                 '', #XXX #$svc_link
48                 ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
49                       FS::UI::Web::cust_header()
50                 ),
51               ],
52               'color' => [ 
53                            '',
54                            '',
55                            '',
56                            '',
57                            '',
58                            '',
59                            '',
60                            FS::UI::Web::cust_colors(),
61                          ],
62               'style' => [ 
63                            '',
64                            '',
65                            '',
66                            '',
67                            '',
68                            '',
69                            '',
70                            FS::UI::Web::cust_styles(),
71                          ],
72       )
73 %>
74 <%init>
75
76 die "access denied"
77   unless $FS::CurrentUser::CurrentUser->access_right('Import');
78
79 my @search = ();
80
81 push @search, "availbatch = '$1'" 
82     if ( $cgi->param('availbatch') =~ /^([\w\d \/\:\-\.]+)$/ ); 
83   
84 push @search, "countrycode = '$1'" 
85     if ( $cgi->param('countrycode') =~ /^(\d{1,3})$/ );
86
87 push @search, "phone_avail.state = '$1'" 
88     if ( $cgi->param('state') =~ /^(\w{2})$/ );
89
90 # i know that the regexps match more than NPA/NXX, but this is good enough now
91 push @search, "npa = '$1'" 
92     if ( $cgi->param('npa') =~ /^(\d{3})$/ ); 
93 push @search, "nxx = '$1'" 
94     if ( $cgi->param('npa') =~ /^\d{3}$/ && $cgi->param('nxx') =~ /^(\d{3})$/ ); 
95
96 push @search, "name = '$1'" 
97     if ( $cgi->param('ratecenter') =~ /^([\w \-\.]+)$/ );
98
99 push @search, "svcnum is null" 
100     if ( $cgi->param('avail_status') eq 'AVAIL' );
101 push @search, "svcnum is not null" 
102     if ( $cgi->param('avail_status') eq 'UNAVAIL' );
103
104 #  #here is the agent virtualization
105 #  push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
106
107 my $search = scalar(@search) 
108                ? ' WHERE '. join(' AND ', @search)
109                : '';
110
111
112 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
113                 #' LEFT JOIN part_svc  USING ( svcpart ) '.
114                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
115                 ' LEFT JOIN cust_main USING ( custnum ) ';
116
117 my $count_query = "SELECT COUNT(*) FROM phone_avail $search"; #$addl_from?
118
119 my $link_cust = sub {
120   my $phone_avail = shift;
121   if ( $phone_avail->svcnum ) {
122     my $cust_svc = $phone_avail->cust_svc;
123     if ( $cust_svc->pkgnum ) {
124       #my $cust_main = $cust_svc->cust_pkg->cust_main;
125       return [ "${p}view/cust_main.cgi?", 'custnum' ];
126     }
127   }
128   '';
129 };
130
131 </%init>