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')
23 my $orderby = 'ORDER BY cust_bill._date';
25 if ( $cgi->param('beginning')
26 && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
27 $begin = str2time($1);
28 push @where, "cust_bill._date >= $begin";
30 if ( $cgi->param('ending')
31 && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
32 $end = str2time($1) + 86399;
33 push @where, "cust_bill._date < $end";
36 if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
38 push @where, "cust_bill._date >= $begin";
40 if ( $cgi->param('end') =~ /^(\d+)$/ ) {
42 push @where, "cust_bill._date < $end";
45 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
47 push @where, "cust_main.agentnum = $agentnum";
51 "charged - ( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
52 WHERE cust_bill_pay.invnum = cust_bill.invnum )
53 - ( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
54 WHERE cust_credit_bill.invnum = cust_bill.invnum )";
56 if ( $cgi->param('open') ) {
57 push @where, "0 != $owed";
61 my($query) = $cgi->keywords;
62 if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
63 ($open, $days, my $field) = ($1, $2, $3);
64 $field = "_date" if $field eq 'date';
65 $orderby = "ORDER BY cust_bill.$field";
66 push @where, "0 != $owed" if $open;
67 push @where, "cust_bill._date < ". (time-86400*$days) if $days;
70 my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
72 my $addl_from = 'left join cust_main using ( custnum )';
74 if ( $cgi->param('newest_percust') ) {
75 $distinct = 'DISTINCT ON ( cust_bill.custnum )';
76 $orderby = 'ORDER BY cust_bill.custnum ASC, cust_bill._date DESC';
77 #$count_query = "SELECT 'N/A', 'N/A', 'N/A'"; #XXXXXXX fix
78 $count_query = "SELECT COUNT(DISTINCT cust_bill.custnum), 'N/A', 'N/A'";
81 unless ( $count_query ) {
82 $count_query = "SELECT COUNT(*), sum(charged), sum($owed)";
83 $count_addl = [ '$%.2f total invoiced',
84 '$%.2f total outstanding balance',
87 $count_query .= " FROM cust_bill $addl_from $extra_sql";
90 'table' => 'cust_bill',
91 'addl_from' => $addl_from,
93 'select' => "$distinct ". join(', ',
95 #( map "cust_main.$_", qw(custnum last first company) ),
96 'cust_main.custnum as cust_main_custnum',
97 ( map "cust_main.$_", qw(last first company) ),
100 'extra_sql' => "$extra_sql $orderby"
105 my $link = [ "${p}view/cust_bill.cgi?", 'invnum', ];
107 my $cust_bill = shift;
108 my $cust_main = $cust_bill->cust_main;
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' ],
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,
157 [ 'Invoice #', qw(Balance Amount Date), 'Contact name',
161 sub { sprintf($money_char.'%.2f', shift->get('owed') ) },
162 sub { sprintf($money_char.'%.2f', shift->charged ) },
163 sub { time2str('%b %d %Y', shift->_date ) },
164 sub { my $cust_bill = shift;
165 # my $cust_main = $cust_bill->cust_main;
167 # ? $cust_main->get('last'). ', '. $cust_main->first
168 # : "WARNING: can't find cust_main.custnum ".
169 # $cust_bill->custnum. ' (cust_bill.invnum '.
170 # $cust_bill->invnum. ')';
171 if ( $cust_bill->cust_main_custnum ) {
172 FS::cust_main::name($cust_bill);
174 "WARNING: can't find cust_main.custnum ".
175 $cust_bill->custnum. ' (cust_bill.invnum '.
176 $cust_bill->invnum. ')';
179 sub { my $cust_main = shift->cust_main;
180 $cust_main ? $cust_main->company : '';