2 my( $count_query, $sql_query );
3 my( $count_addl ) = ( '' );
4 my( $distinct ) = ( '' );
5 my($begin, $end) = ( '', '' );
6 my($agentnum) = ( '' );
7 my($open, $days) = ( '', '' );
8 if ( $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {
9 $count_query = "SELECT COUNT(*) FROM cust_bill WHERE invnum = $2";
11 'table' => 'cust_bill',
12 'hashref' => { 'invnum' => $2 },
16 #if ( $cgi->param('begin') || $cgi->param('end')
17 # || $cgi->param('beginning') || $cgi->param('ending')
22 #some false laziness w/cust_bill::re_X
24 my $orderby = 'ORDER BY cust_bill._date';
26 if ( $cgi->param('beginning')
27 && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
28 $begin = str2time($1);
29 push @where, "cust_bill._date >= $begin";
31 if ( $cgi->param('ending')
32 && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
33 $end = str2time($1) + 86399;
34 push @where, "cust_bill._date < $end";
37 if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
39 push @where, "cust_bill._date >= $begin";
41 if ( $cgi->param('end') =~ /^(\d+)$/ ) {
43 push @where, "cust_bill._date < $end";
46 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
48 push @where, "cust_main.agentnum = $agentnum";
52 "charged - ( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
53 WHERE cust_bill_pay.invnum = cust_bill.invnum )
54 - ( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
55 WHERE cust_credit_bill.invnum = cust_bill.invnum )";
57 if ( $cgi->param('open') ) {
58 push @where, "0 != $owed";
62 my($query) = $cgi->keywords;
63 if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
64 ($open, $days, my $field) = ($1, $2, $3);
65 $field = "_date" if $field eq 'date';
66 $orderby = "ORDER BY cust_bill.$field";
67 push @where, "0 != $owed" if $open;
68 push @where, "cust_bill._date < ". (time-86400*$days) if $days;
71 my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
73 my $addl_from = 'left join cust_main using ( custnum )';
75 if ( $cgi->param('newest_percust') ) {
76 $distinct = 'DISTINCT ON ( cust_bill.custnum )';
77 $orderby = 'ORDER BY cust_bill.custnum ASC, cust_bill._date DESC';
78 #$count_query = "SELECT 'N/A', 'N/A', 'N/A'"; #XXXXXXX fix
79 $count_query = "SELECT COUNT(DISTINCT cust_bill.custnum), 'N/A', 'N/A'";
82 unless ( $count_query ) {
83 $count_query = "SELECT COUNT(*), sum(charged), sum($owed)";
84 $count_addl = [ '$%.2f total invoiced',
85 '$%.2f total outstanding balance',
88 $count_query .= " FROM cust_bill $addl_from $extra_sql";
91 'table' => 'cust_bill',
92 'addl_from' => $addl_from,
94 'select' => "$distinct ". join(', ',
96 #( map "cust_main.$_", qw(custnum last first company) ),
97 'cust_main.custnum as cust_main_custnum',
98 FS::UI::Web::cust_sql_fields(),
101 'extra_sql' => "$extra_sql $orderby"
106 my $link = [ "${p}view/cust_bill.cgi?", 'invnum', ];
108 my $cust_bill = shift;
109 $cust_bill->cust_main_custnum
110 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
114 my $conf = new FS::Conf;
115 my $money_char = $conf->config('money_char') || '$';
117 my $html_init = join("\n", map {
118 ( my $action = $_ ) =~ s/_$//;
119 include('/elements/progress-init.html',
121 [ 'begin', 'end', 'agentnum', 'open', 'days', 'newest_percust' ],
122 "../misc/${_}invoices.cgi",
123 { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
126 qq!<FORM NAME="${_}form">!,
127 qq!<INPUT TYPE="hidden" NAME="begin" VALUE="$begin">!,
128 qq!<INPUT TYPE="hidden" NAME="end" VALUE="$end">!,
129 qq!<INPUT TYPE="hidden" NAME="agentnum" VALUE="$agentnum">!,
130 qq!<INPUT TYPE="hidden" NAME="open" VALUE="$open">!,
131 qq!<INPUT TYPE="hidden" NAME="days" VALUE="$days">!,
133 } qw( print_ email_ fax_ ) );
137 'Print these invoices' =>
138 "javascript:print_process()",
139 'Email these invoices' =>
140 "javascript:email_process()",
143 push @$menubar, 'Fax these invoices' =>
144 "javascript:fax_process()"
145 if $conf->exists('hylafax');
147 %><%= include( 'elements/search.html',
148 'title' => 'Invoice Search Results',
149 'html_init' => $html_init,
150 'menubar' => $menubar,
151 'name' => 'invoices',
152 'query' => $sql_query,
153 'count_query' => $count_query,
154 'count_addl' => $count_addl,
156 'header' => [ 'Invoice #',
160 FS::UI::Web::cust_header(),
164 sub { sprintf($money_char.'%.2f', shift->get('owed') ) },
165 sub { sprintf($money_char.'%.2f', shift->charged ) },
166 sub { time2str('%b %d %Y', shift->_date ) },
167 \&FS::UI::Web::cust_fields,
175 ( map { $clink } FS::UI::Web::cust_header() ),