commissions based on actual invoiced amounts, RT#21002
[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' => $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                 sub {
35                     my $pa = shift;
36                     return '' unless $pa->part_export;
37                     $pa->part_export->exportname;
38                 },
39                 sub {
40                     my $pa = shift;
41                     return '' unless $pa->cust_svc;
42                     my($label,$value) = $pa->cust_svc->label; 
43                     $label . ": " . $value;
44                 },
45                 \&FS::UI::Web::cust_fields,
46                 '',
47               ],
48               'align'         => 'rllllllc'.FS::UI::Web::cust_aligns(),
49               'links'         => [
50                 '',
51                 '',
52                 '',
53                 '',
54                 '',
55                 '', #XXX #$export_link - to what exactly?
56                 $svc_link,
57                 ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
58                       FS::UI::Web::cust_header()
59                 ),
60                 '',
61               ],
62               'color' => [ 
63                            '',
64                            '',
65                            '',
66                            '',
67                            '',
68                            '',
69                            '',
70                            FS::UI::Web::cust_colors(),
71                            '',
72                          ],
73               'style' => [ 
74                            '',
75                            '',
76                            '',
77                            '',
78                            '',
79                            '',
80                            '',
81                            FS::UI::Web::cust_styles(),
82                            '',
83                          ],
84       )
85 %>
86 <%init>
87
88 die "access denied"
89   unless ( $FS::CurrentUser::CurrentUser->access_right('List inventory')
90          );
91
92 my @search = ();
93
94 push @search, "availbatch = '$1'" 
95     if ( $cgi->param('availbatch') =~ /^([\w\d \/\:\-\.]+)$/ ); 
96   
97 push @search, "countrycode = '$1'" 
98     if ( $cgi->param('countrycode') =~ /^(\d{1,3})$/ );
99
100 push @search, "phone_avail.state = '$1'" 
101     if ( $cgi->param('state') =~ /^(\w{2})$/ );
102
103 # i know that the regexps match more than NPA/NXX, but this is good enough now
104 push @search, "npa = '$1'" 
105     if ( $cgi->param('npa') =~ /^(\d{3})$/ ); 
106 push @search, "nxx = '$1'" 
107     if ( $cgi->param('npa') =~ /^\d{3}$/ && $cgi->param('nxx') =~ /^(\d{3})$/ ); 
108
109 push @search, "name = '$1'" 
110     if ( $cgi->param('ratecenter') =~ /^([\w \-\.]+)$/ );
111
112 push @search, "svcnum is null" 
113     if ( $cgi->param('avail_status') eq 'AVAIL' );
114 push @search, "svcnum is not null" 
115     if ( $cgi->param('avail_status') eq 'UNAVAIL' );
116
117 #  #here is the agent virtualization
118 #  push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
119
120 my $search = scalar(@search) 
121                ? ' WHERE '. join(' AND ', @search)
122                : '';
123
124
125 my $addl_from = ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
126                 #' LEFT JOIN part_svc  USING ( svcpart ) '.
127                 ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
128                 ' LEFT JOIN cust_main USING ( custnum ) ';
129
130 my $count_query = "SELECT COUNT(*) FROM phone_avail $search"; #$addl_from?
131
132 my $hashref = {};
133 $hashref->{'ordernum'} = $1 if $cgi->param('ordernum') =~ /^(\d+)$/;
134
135 my $link_cust = sub {
136   my $phone_avail = shift;
137   if ( $phone_avail->svcnum ) {
138     my $cust_svc = $phone_avail->cust_svc;
139     if ( $cust_svc->pkgnum ) {
140       #my $cust_main = $cust_svc->cust_pkg->cust_main;
141       return [ "${p}view/cust_main.cgi?", 'custnum' ];
142     }
143   }
144   '';
145 };
146
147 my $svc_link = sub {
148     my $phone_avail = shift;
149     my $cust_svc = $phone_avail->cust_svc;
150     if ( $cust_svc ) {
151         return [ "${p}view/svc_phone.cgi?", 'svcnum'];
152     }
153     '';
154 };
155
156 </%init>