b2b4630ae9ccb2f664c2d4d9f2fc3d9b7bd15259
[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('List inventory')
78         || $FS::CurrentUser::CurrentUser->access_right('List services')
79          );
80
81 my @search = ();
82
83 push @search, "availbatch = '$1'" 
84     if ( $cgi->param('availbatch') =~ /^([\w\d \/\:\-\.]+)$/ ); 
85   
86 push @search, "countrycode = '$1'" 
87     if ( $cgi->param('countrycode') =~ /^(\d{1,3})$/ );
88
89 push @search, "phone_avail.state = '$1'" 
90     if ( $cgi->param('state') =~ /^(\w{2})$/ );
91
92 # i know that the regexps match more than NPA/NXX, but this is good enough now
93 push @search, "npa = '$1'" 
94     if ( $cgi->param('npa') =~ /^(\d{3})$/ ); 
95 push @search, "nxx = '$1'" 
96     if ( $cgi->param('npa') =~ /^\d{3}$/ && $cgi->param('nxx') =~ /^(\d{3})$/ ); 
97
98 push @search, "name = '$1'" 
99     if ( $cgi->param('ratecenter') =~ /^([\w \-\.]+)$/ );
100
101 push @search, "svcnum is null" 
102     if ( $cgi->param('avail_status') eq 'AVAIL' );
103 push @search, "svcnum is not null" 
104     if ( $cgi->param('avail_status') eq 'UNAVAIL' );
105
106 #  #here is the agent virtualization
107 #  push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
108
109 my $search = scalar(@search) 
110                ? ' WHERE '. join(' AND ', @search)
111                : '';
112
113
114 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
115                 #' LEFT JOIN part_svc  USING ( svcpart ) '.
116                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
117                 ' LEFT JOIN cust_main USING ( custnum ) ';
118
119 my $count_query = "SELECT COUNT(*) FROM phone_avail $search"; #$addl_from?
120
121 my $link_cust = sub {
122   my $phone_avail = shift;
123   if ( $phone_avail->svcnum ) {
124     my $cust_svc = $phone_avail->cust_svc;
125     if ( $cust_svc->pkgnum ) {
126       #my $cust_main = $cust_svc->cust_pkg->cust_main;
127       return [ "${p}view/cust_main.cgi?", 'custnum' ];
128     }
129   }
130   '';
131 };
132
133 </%init>