3 my $join_cust_main = 'LEFT JOIN cust_main USING ( custnum )';
4 #here is the agent virtualization
5 my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
7 my( $count_query, $sql_query );
8 my( $count_addl ) = ( '' );
9 my( $distinct ) = ( '' );
10 my($begin, $end) = ( '', '' );
11 my($agentnum) = ( '' );
12 my($open, $days) = ( '', '' );
13 if ( $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {
15 "SELECT COUNT(*) FROM cust_bill $join_cust_main".
16 " WHERE invnum = $2 AND $agentnums_sql"; #agent virtualization
18 'table' => 'cust_bill',
19 'addl_from' => $join_cust_main,
20 'hashref' => { 'invnum' => $2 },
22 'extra_sql' => " AND $agentnums_sql", #agent virtualization
25 #if ( $cgi->param('begin') || $cgi->param('end')
26 # || $cgi->param('beginning') || $cgi->param('ending')
31 #some false laziness w/cust_bill::re_X
33 my $orderby = 'ORDER BY cust_bill._date';
35 if ( $cgi->param('beginning')
36 && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
37 $begin = str2time($1);
38 push @where, "cust_bill._date >= $begin";
40 if ( $cgi->param('ending')
41 && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
42 $end = str2time($1) + 86399;
43 push @where, "cust_bill._date < $end";
46 if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
48 push @where, "cust_bill._date >= $begin";
50 if ( $cgi->param('end') =~ /^(\d+)$/ ) {
52 push @where, "cust_bill._date < $end";
55 if ( $cgi->param('invnum_min') =~ /^\s*(\d+)\s*$/ ) {
56 push @where, "cust_bill.invnum >= $1";
58 if ( $cgi->param('invnum_max') =~ /^\s*(\d+)\s*$/ ) {
59 push @where, "cust_bill.invnum <= $1";
62 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
64 push @where, "cust_main.agentnum = $agentnum";
68 "charged - ( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
69 WHERE cust_bill_pay.invnum = cust_bill.invnum )
70 - ( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
71 WHERE cust_credit_bill.invnum = cust_bill.invnum )";
73 if ( $cgi->param('open') ) {
74 push @where, "0 != $owed";
78 my($query) = $cgi->keywords;
79 if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
80 ($open, $days, my $field) = ($1, $2, $3);
81 $field = "_date" if $field eq 'date';
82 $orderby = "ORDER BY cust_bill.$field";
83 push @where, "0 != $owed" if $open;
84 push @where, "cust_bill._date < ". (time-86400*$days) if $days;
87 #here is the agent virtualization
88 push @where, $agentnums_sql;
89 my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
91 if ( $cgi->param('newest_percust') ) {
92 $distinct = 'DISTINCT ON ( cust_bill.custnum )';
93 $orderby = 'ORDER BY cust_bill.custnum ASC, cust_bill._date DESC';
94 #$count_query = "SELECT 'N/A', 'N/A', 'N/A'"; #XXXXXXX fix
95 $count_query = "SELECT COUNT(DISTINCT cust_bill.custnum), 'N/A', 'N/A'";
98 unless ( $count_query ) {
99 $count_query = "SELECT COUNT(*), sum(charged), sum($owed)";
100 $count_addl = [ '$%.2f total invoiced',
101 '$%.2f total outstanding balance',
104 $count_query .= " FROM cust_bill $join_cust_main $extra_sql";
107 'table' => 'cust_bill',
108 'addl_from' => $join_cust_main,
110 'select' => "$distinct ". join(', ',
112 #( map "cust_main.$_", qw(custnum last first company) ),
113 'cust_main.custnum as cust_main_custnum',
114 FS::UI::Web::cust_sql_fields(),
117 'extra_sql' => "$extra_sql $orderby"
122 my $link = [ "${p}view/cust_bill.cgi?", 'invnum', ];
124 my $cust_bill = shift;
125 $cust_bill->cust_main_custnum
126 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
130 my $conf = new FS::Conf;
131 my $money_char = $conf->config('money_char') || '$';
133 my $html_init = join("\n", map {
134 ( my $action = $_ ) =~ s/_$//;
135 include('/elements/progress-init.html',
137 [ 'begin', 'end', 'agentnum', 'open', 'days', 'newest_percust' ],
138 "../misc/${_}invoices.cgi",
139 { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
142 qq!<FORM NAME="${_}form">!,
143 qq!<INPUT TYPE="hidden" NAME="begin" VALUE="$begin">!,
144 qq!<INPUT TYPE="hidden" NAME="end" VALUE="$end">!,
145 qq!<INPUT TYPE="hidden" NAME="agentnum" VALUE="$agentnum">!,
146 qq!<INPUT TYPE="hidden" NAME="open" VALUE="$open">!,
147 qq!<INPUT TYPE="hidden" NAME="days" VALUE="$days">!,
149 } qw( print_ email_ fax_ ) );
153 'Print these invoices' =>
154 "javascript:print_process()",
155 'Email these invoices' =>
156 "javascript:email_process()",
159 push @$menubar, 'Fax these invoices' =>
160 "javascript:fax_process()"
161 if $conf->exists('hylafax');
163 %><%= include( 'elements/search.html',
164 'title' => 'Invoice Search Results',
165 'html_init' => $html_init,
166 'menubar' => $menubar,
167 'name' => 'invoices',
168 'query' => $sql_query,
169 'count_query' => $count_query,
170 'count_addl' => $count_addl,
172 'header' => [ 'Invoice #',
176 FS::UI::Web::cust_header(),
180 sub { sprintf($money_char.'%.2f', shift->get('owed') ) },
181 sub { sprintf($money_char.'%.2f', shift->charged ) },
182 sub { time2str('%b %d %Y', shift->_date ) },
183 \&FS::UI::Web::cust_fields,
191 ( map { $clink } FS::UI::Web::cust_header() ),