1 <% include( 'elements/search.html',
2 'title' => 'Invoice Search Results',
3 'html_init' => $html_init,
7 'count_query' => $count_query,
8 'count_addl' => $count_addl,
10 'header' => [ 'Invoice #',
14 FS::UI::Web::cust_header(),
18 sub { sprintf($money_char.'%.2f', shift->get('owed') ) },
19 sub { sprintf($money_char.'%.2f', shift->charged ) },
20 sub { time2str('%b %d %Y', shift->_date ) },
21 \&FS::UI::Web::cust_fields,
23 'align' => 'rrrr'.FS::UI::Web::cust_aligns(),
29 ( map { $_ ne 'Cust. Status' ? $clink : '' }
30 FS::UI::Web::cust_header()
38 FS::UI::Web::cust_colors(),
45 FS::UI::Web::cust_styles(),
54 unless $FS::CurrentUser::CurrentUser->access_right('List invoices');
56 my $join_cust_main = 'LEFT JOIN cust_main USING ( custnum )';
57 #here is the agent virtualization
58 my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
60 my( $count_query, $sql_query );
63 my($begin, $end) = ( '', '' );
65 my($open, $days) = ( '', '' );
66 my($invnum_min, $invnum_max) = ( '', '' );
67 my $newest_percust = '';
68 if ( $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {
70 "SELECT COUNT(*) FROM cust_bill $join_cust_main".
71 " WHERE invnum = $2 AND $agentnums_sql"; #agent virtualization
73 'table' => 'cust_bill',
74 'addl_from' => $join_cust_main,
75 'hashref' => { 'invnum' => $2 },
77 'extra_sql' => " AND $agentnums_sql", #agent virtualization
80 #if ( $cgi->param('begin') || $cgi->param('end')
81 # || $cgi->param('beginning') || $cgi->param('ending')
86 #some false laziness w/cust_bill::re_X
88 my $orderby = 'ORDER BY cust_bill._date';
90 if ( $cgi->param('beginning')
91 && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
92 $begin = str2time($1);
93 push @where, "cust_bill._date >= $begin";
95 if ( $cgi->param('ending')
96 && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
97 $end = str2time($1) + 86399;
98 push @where, "cust_bill._date < $end";
101 if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
103 push @where, "cust_bill._date >= $begin";
105 if ( $cgi->param('end') =~ /^(\d+)$/ ) {
107 push @where, "cust_bill._date < $end";
110 if ( $cgi->param('invnum_min') =~ /^\s*(\d+)\s*$/ ) {
112 push @where, "cust_bill.invnum >= $1";
114 if ( $cgi->param('invnum_max') =~ /^\s*(\d+)\s*$/ ) {
116 push @where, "cust_bill.invnum <= $1";
119 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
121 push @where, "cust_main.agentnum = $agentnum";
125 "charged - ( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
126 WHERE cust_bill_pay.invnum = cust_bill.invnum )
127 - ( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
128 WHERE cust_credit_bill.invnum = cust_bill.invnum )";
130 if ( $cgi->param('open') ) {
131 push @where, "0 != $owed";
135 my($query) = $cgi->keywords;
136 if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
137 ($open, $days, my $field) = ($1, $2, $3);
138 $field = "_date" if $field eq 'date';
139 $orderby = "ORDER BY cust_bill.$field";
140 push @where, "0 != $owed" if $open;
141 push @where, "cust_bill._date < ". (time-86400*$days) if $days;
144 #here is the agent virtualization
145 push @where, $agentnums_sql;
146 my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
148 if ( $cgi->param('newest_percust') ) {
150 $distinct = 'DISTINCT ON ( cust_bill.custnum )';
151 $orderby = 'ORDER BY cust_bill.custnum ASC, cust_bill._date DESC';
152 #$count_query = "SELECT 'N/A', 'N/A', 'N/A'"; #XXXXXXX fix
153 $count_query = "SELECT COUNT(DISTINCT cust_bill.custnum), 'N/A', 'N/A'";
156 unless ( $count_query ) {
157 $count_query = "SELECT COUNT(*), sum(charged), sum($owed)";
158 $count_addl = [ '$%.2f total invoiced',
159 '$%.2f total outstanding balance',
162 $count_query .= " FROM cust_bill $join_cust_main $extra_sql";
165 'table' => 'cust_bill',
166 'addl_from' => $join_cust_main,
168 'select' => "$distinct ". join(', ',
170 #( map "cust_main.$_", qw(custnum last first company) ),
171 'cust_main.custnum as cust_main_custnum',
172 FS::UI::Web::cust_sql_fields(),
175 'extra_sql' => "$extra_sql $orderby"
180 my $link = [ "${p}view/cust_bill.cgi?", 'invnum', ];
182 my $cust_bill = shift;
183 $cust_bill->cust_main_custnum
184 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
188 my $conf = new FS::Conf;
189 my $money_char = $conf->config('money_char') || '$';
191 my $html_init = join("\n", map {
192 ( my $action = $_ ) =~ s/_$//;
193 include('/elements/progress-init.html',
195 [ 'begin', 'end', 'agentnum', 'open', 'days',
196 'invnum_min', 'invnum_max', 'newest_percust',
198 "../misc/${_}invoices.cgi",
199 { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
202 qq!<FORM NAME="${_}form">!,
203 qq!<INPUT TYPE="hidden" NAME="begin" VALUE="$begin">!,
204 qq!<INPUT TYPE="hidden" NAME="end" VALUE="$end">!,
205 qq!<INPUT TYPE="hidden" NAME="agentnum" VALUE="$agentnum">!,
206 qq!<INPUT TYPE="hidden" NAME="open" VALUE="$open">!,
207 qq!<INPUT TYPE="hidden" NAME="days" VALUE="$days">!,
208 qq!<INPUT TYPE="hidden" NAME="invnum_min" VALUE="$invnum_min">!,
209 qq!<INPUT TYPE="hidden" NAME="invnum_max" VALUE="$invnum_max">!,
210 qq!<INPUT TYPE="hidden" NAME="newest_percust" VALUE="$newest_percust">!,
212 } qw( print_ email_ fax_ ) ).
214 '<SCRIPT TYPE="text/javascript">
216 function confirm_print_process() {
217 if ( ! confirm("Are you sure you want to reprint these invoices?") ) {
222 function confirm_email_process() {
223 if ( ! confirm("Are you sure you want to re-email these invoices?") ) {
228 function confirm_fax_process() {
229 if ( ! confirm("Are you sure you want to re-fax these invoices?") ) {
239 'Print these invoices' =>
240 "javascript:confirm_print_process()",
241 'Email these invoices' =>
242 "javascript:confirm_email_process()",
245 push @$menubar, 'Fax these invoices' =>
246 "javascript:confirm_fax_process()"
247 if $conf->exists('hylafax');