communigate provisioning phase 2: Account:Settings: RulesAllowed, RPOPAllowed, MailTo...
[freeside.git] / httemplate / search / cust_bill.html
1 <% include( 'elements/search.html',
2                  'title'       => 'Invoice Search Results',
3                  'html_init'   => $html_init,
4                  'menubar'     => $menubar,
5                  'name'        => 'invoices',
6                  'query'       => $sql_query,
7                  'count_query' => $count_query,
8                  'count_addl'  => $count_addl,
9                  'redirect'    => $link,
10                  'header'      => [ 'Invoice #',
11                                     'Balance',
12                                     'Net Amount',
13                                     'Gross Amount',
14                                     'Date',
15                                     FS::UI::Web::cust_header(),
16                                   ],
17                  'fields'      => [
18                    'invnum',
19                    sub { sprintf($money_char.'%.2f', shift->get('owed') ) },
20                    sub { sprintf($money_char.'%.2f', shift->get('net') ) },
21                    sub { sprintf($money_char.'%.2f', shift->charged     ) },
22                    sub { time2str('%b %d %Y', shift->_date ) },
23                    \&FS::UI::Web::cust_fields,
24                  ],
25                  'align' => 'rrrrl'.FS::UI::Web::cust_aligns(),
26                  'links' => [
27                    $link,
28                    $link,
29                    $link,
30                    $link,
31                    $link,
32                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
33                          FS::UI::Web::cust_header()
34                    ),
35                  ],
36                  'color' => [ 
37                               '',
38                               '',
39                               '',
40                               '',
41                               '',
42                               FS::UI::Web::cust_colors(),
43                             ],
44                  'style' => [ 
45                               '',
46                               '',
47                               '',
48                               '',
49                               '',
50                               FS::UI::Web::cust_styles(),
51                             ],
52
53   
54       )
55 %>
56 <%init>
57
58 die "access denied"
59   unless $FS::CurrentUser::CurrentUser->access_right('List invoices');
60
61 my $join_cust_main = 'LEFT JOIN cust_main USING ( custnum )';
62 #here is the agent virtualization
63 my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
64
65 my( $count_query, $sql_query );
66 my $count_addl = '';
67 #my $distinct = '';
68 my %search;
69
70 if ( $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {
71
72   $count_query =
73     "SELECT COUNT(*) FROM cust_bill $join_cust_main".
74     "  WHERE invnum = $2 AND $agentnums_sql"; #agent virtualization
75   $sql_query = {
76     'table'     => 'cust_bill',
77     'addl_from' => $join_cust_main,
78     'hashref'   => { 'invnum' => $2 },
79     #'select'    => '*',
80     'extra_sql' => " AND $agentnums_sql", #agent virtualization
81   };
82
83 } else {
84
85   #some false laziness w/cust_bill::re_X
86   my $orderby = 'ORDER BY cust_bill._date';
87
88   if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
89     $search{'agentnum'} = $1;
90   }
91
92   # begin/end/beginning/ending
93   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, '');
94   $search{'_date'} = [ $beginning, $ending ]
95     unless $beginning == 0 && $ending == 4294967295;
96
97   if ( $cgi->param('invnum_min') =~ /^\s*(\d+)\s*$/ ) {
98     $search{'invnum_min'} = $1;
99   }
100   if ( $cgi->param('invnum_max') =~ /^\s*(\d+)\s*$/ ) {
101     $search{'invnum_max'} = $1;
102   }
103
104   #amounts
105   $search{$_} = [ FS::UI::Web::parse_lt_gt($cgi, $_) ]
106     foreach qw( charged owed );
107
108   $search{'open'} = 1 if $cgi->param('open');
109   $search{'net'}  = 1 if $cgi->param('net' );
110
111   my($query) = $cgi->keywords;
112   if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
113     $search{'open'} = 1 if $1;
114     ($search{'days'}, my $field) = ($2, $3);
115     $field = "_date" if $field eq 'date';
116     $orderby = "ORDER BY cust_bill.$field";
117   }
118
119   if ( $cgi->param('newest_percust') ) {
120     $search{'newest_percust'} = 1;
121     $count_query = "SELECT COUNT(DISTINCT cust_bill.custnum), 'N/A', 'N/A'";
122   }
123
124   my $extra_sql = ' WHERE '. FS::cust_bill->search_sql_where( \%search );
125
126   unless ( $count_query ) {
127     $count_query = 'SELECT COUNT(*), '. join(', ',
128                      map "SUM($_)",
129                          ( 'charged',
130                            FS::cust_bill->net_sql,
131                            FS::cust_bill->owed_sql,
132                          )
133                    );
134     $count_addl = [ '$%.2f invoiced (gross)',
135                     '$%.2f invoiced (net)',
136                     '$%.2f outstanding balance',
137                   ];
138   }
139   $count_query .=  " FROM cust_bill $join_cust_main $extra_sql";
140
141   $sql_query = {
142     'table'     => 'cust_bill',
143     'addl_from' => $join_cust_main,
144     'hashref'   => {},
145     #'select'    => "$distinct ". join(', ',
146     'select'    => join(', ',
147                      'cust_bill.*',
148                      #( map "cust_main.$_", qw(custnum last first company) ),
149                      'cust_main.custnum as cust_main_custnum',
150                      FS::UI::Web::cust_sql_fields(),
151                      FS::cust_bill->owed_sql. ' AS owed',
152                      FS::cust_bill->net_sql.  ' AS net',
153                    ),
154     'extra_sql' => $extra_sql,
155     'order_by'  => $orderby,
156   };
157
158 }
159
160 my $link  = [ "${p}view/cust_bill.cgi?", 'invnum', ];
161 my $clink = sub {
162   my $cust_bill = shift;
163   $cust_bill->cust_main_custnum
164     ? [ "${p}view/cust_main.cgi?", 'custnum' ]
165     : '';
166 };
167
168 my $conf = new FS::Conf;
169 my $money_char = $conf->config('money_char') || '$';
170
171 my $html_init = join("\n", map {
172  ( my $action = $_ ) =~ s/_$//;
173  include('/elements/progress-init.html',
174            $_.'form',
175            [ keys %search ],
176            "../misc/${_}invoices.cgi",
177            { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
178            $_, #key
179         ),
180  qq!<FORM NAME="${_}form">!,
181  ( map { my $f = $_;
182          my @values = ref($search{$f}) ? @{ $search{$f} } : $search{$f};
183          map qq!<INPUT TYPE="hidden" NAME="$f" VALUE="$_">!, @values;
184        }
185        keys %search
186  ),
187  qq!</FORM>!
188 } qw( print_ email_ fax_ ftp_ spool_ ) ). 
189
190 '<SCRIPT TYPE="text/javascript">
191
192 function confirm_print_process() {
193   if ( ! confirm("Are you sure you want to reprint these invoices?") ) {
194     return;
195   }
196   print_process();
197 }
198 function confirm_email_process() {
199   if ( ! confirm("Are you sure you want to re-email these invoices?") ) {
200     return;
201   }
202   email_process();
203 }
204 function confirm_fax_process() {
205   if ( ! confirm("Are you sure you want to re-fax these invoices?") ) {
206     return;
207   }
208   fax_process();
209 }
210 function confirm_ftp_process() {
211   if ( ! confirm("Are you sure you want to re-FTP these invoices?") ) {
212     return;
213   }
214   ftp_process();
215 }
216 function confirm_spool_process() {
217   if ( ! confirm("Are you sure you want to re-spool these invoices?") ) {
218     return;
219   }
220   spool_process();
221 }
222
223 </SCRIPT>';
224
225 my $menubar = [];
226
227 if ( $FS::CurrentUser::CurrentUser->access_right('Resend invoices') ) {
228
229   push @$menubar, 'Print these invoices' =>
230                     "javascript:confirm_print_process()",
231                   'Email these invoices' =>
232                     "javascript:confirm_email_process()";
233
234   push @$menubar, 'Fax these invoices' =>
235                     "javascript:confirm_fax_process()"
236     if $conf->exists('hylafax');
237
238   push @$menubar, 'FTP these invoices' =>
239                     "javascript:confirm_ftp_process()"
240     if $conf->exists('cust_bill-ftpformat');
241
242   push @$menubar, 'Spool these invoices' =>
243                     "javascript:confirm_spool_process()"
244     if $conf->exists('cust_bill-spoolformat');
245
246 }
247
248 </%init>