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 ( map "cust_main.$_", qw(last first company) ),
101 'extra_sql' => "$extra_sql $orderby"
106 my $link = [ "${p}view/cust_bill.cgi?", 'invnum', ];
108 my $cust_bill = shift;
109 my $cust_main = $cust_bill->cust_main;
111 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
115 my $conf = new FS::Conf;
116 my $money_char = $conf->config('money_char') || '$';
118 my $html_init = join("\n", map {
119 ( my $action = $_ ) =~ s/_$//;
120 include('/elements/progress-init.html',
122 [ 'begin', 'end', 'agentnum', 'open', 'days', 'newest_percust' ],
123 "../misc/${_}invoices.cgi",
124 { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
127 qq!<FORM NAME="${_}form">!,
128 qq!<INPUT TYPE="hidden" NAME="begin" VALUE="$begin">!,
129 qq!<INPUT TYPE="hidden" NAME="end" VALUE="$end">!,
130 qq!<INPUT TYPE="hidden" NAME="agentnum" VALUE="$agentnum">!,
131 qq!<INPUT TYPE="hidden" NAME="open" VALUE="$open">!,
132 qq!<INPUT TYPE="hidden" NAME="days" VALUE="$days">!,
134 } qw( print_ email_ fax_ ) );
138 'Print these invoices' =>
139 "javascript:print_process()",
140 'Email these invoices' =>
141 "javascript:email_process()",
144 push @$menubar, 'Fax these invoices' =>
145 "javascript:fax_process()"
146 if $conf->exists('hylafax');
148 %><%= include( 'elements/search.html',
149 'title' => 'Invoice Search Results',
150 'html_init' => $html_init,
151 'menubar' => $menubar,
152 'name' => 'invoices',
153 'query' => $sql_query,
154 'count_query' => $count_query,
155 'count_addl' => $count_addl,
158 [ 'Invoice #', qw(Balance Amount Date), 'Contact name',
162 sub { sprintf($money_char.'%.2f', shift->get('owed') ) },
163 sub { sprintf($money_char.'%.2f', shift->charged ) },
164 sub { time2str('%b %d %Y', shift->_date ) },
165 sub { my $cust_bill = shift;
166 # my $cust_main = $cust_bill->cust_main;
168 # ? $cust_main->get('last'). ', '. $cust_main->first
169 # : "WARNING: can't find cust_main.custnum ".
170 # $cust_bill->custnum. ' (cust_bill.invnum '.
171 # $cust_bill->invnum. ')';
172 if ( $cust_bill->cust_main_custnum ) {
173 FS::cust_main::name($cust_bill);
175 "WARNING: can't find cust_main.custnum ".
176 $cust_bill->custnum. ' (cust_bill.invnum '.
177 $cust_bill->invnum. ')';
180 sub { my $cust_main = shift->cust_main;
181 $cust_main ? $cust_main->company : '';