Will things ever be the same again?
[freeside.git] / httemplate / search / cust_bill.html
1 %
2 %
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;
6 %
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*$/ ) {
14 %     $count_query =
15 %       "SELECT COUNT(*) FROM cust_bill $join_cust_main".
16 %       "  WHERE invnum = $2 AND $agentnums_sql"; #agent virtualization
17 %     $sql_query = {
18 %       'table'     => 'cust_bill',
19 %       'addl_from' => $join_cust_main,
20 %       'hashref'   => { 'invnum' => $2 },
21 %       #'select'    => '*',
22 %       'extra_sql' => " AND $agentnums_sql", #agent virtualization
23 %     };
24 %   } else {
25 %   #if ( $cgi->param('begin') || $cgi->param('end')
26 %   #     || $cgi->param('beginning') || $cgi->param('ending')
27 %   #     || $cgi->keywords
28 %   #   )
29 %   #{
30 %
31 %     #some false laziness w/cust_bill::re_X
32 %     my @where;
33 %     my $orderby = 'ORDER BY cust_bill._date';
34 %
35 %     if ( $cgi->param('beginning')
36 %          && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
37 %       $begin = str2time($1);
38 %       push @where, "cust_bill._date >= $begin";
39 %     }
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";
44 %     }
45 %
46 %     if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
47 %       $begin = $1;
48 %       push @where, "cust_bill._date >= $begin";
49 %     }
50 %     if ( $cgi->param('end') =~ /^(\d+)$/ ) {
51 %       $end = $1;
52 %       push @where, "cust_bill._date < $end";
53 %     }
54 %
55 %     if ( $cgi->param('invnum_min') =~ /^\s*(\d+)\s*$/ ) {
56 %       push @where, "cust_bill.invnum >= $1";
57 %     }
58 %     if ( $cgi->param('invnum_max') =~ /^\s*(\d+)\s*$/ ) {
59 %       push @where, "cust_bill.invnum <= $1";
60 %     }
61 %
62 %     if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
63 %       $agentnum = $1;
64 %       push @where, "cust_main.agentnum = $agentnum";
65 %     }
66 %
67 %     my $owed =
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 )";
72 %
73 %     if ( $cgi->param('open') ) {
74 %       push @where, "0 != $owed";
75 %       $open = 1;
76 %     }
77 %
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;
85 %     }
86 %
87 %     #here is the agent virtualization
88 %     push @where, $agentnums_sql;
89 %     my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
90 %
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'";
96 %     }
97 %   
98 %     unless ( $count_query ) {
99 %       $count_query = "SELECT COUNT(*), sum(charged), sum($owed)";
100 %       $count_addl = [ '$%.2f total invoiced',
101 %                       '$%.2f total outstanding balance',
102 %                     ];
103 %     }
104 %     $count_query .=  " FROM cust_bill $join_cust_main $extra_sql";
105 %   
106 %     $sql_query = {
107 %       'table'     => 'cust_bill',
108 %       'addl_from' => $join_cust_main,
109 %       'hashref'   => {},
110 %       'select'    => "$distinct ". join(', ',
111 %                        'cust_bill.*',
112 %                        #( map "cust_main.$_", qw(custnum last first company) ),
113 %                        'cust_main.custnum as cust_main_custnum',
114 %                        FS::UI::Web::cust_sql_fields(),
115 %                        "$owed as owed",
116 %                      ),
117 %       'extra_sql' => "$extra_sql $orderby"
118 %     };
119 %   
120 %   }
121 %
122 %   my $link  = [ "${p}view/cust_bill.cgi?", 'invnum', ];
123 %   my $clink = sub {
124 %     my $cust_bill = shift;
125 %     $cust_bill->cust_main_custnum
126 %       ? [ "${p}view/cust_main.cgi?", 'custnum' ]
127 %       : '';
128 %   };
129 %
130 %  my $conf = new FS::Conf;
131 %  my $money_char = $conf->config('money_char') || '$';
132 %
133 %  my $html_init = join("\n", map {
134 %    ( my $action = $_ ) =~ s/_$//;
135 %    include('/elements/progress-init.html',
136 %              $_.'form',
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...
140 %              $_, #key
141 %           ),
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">!,
148 %    qq!</FORM>!
149 %  } qw( print_ email_ fax_ ) );
150 %
151 %  my $menubar =  [
152 %                   'Main menu' => $p,
153 %                   'Print these invoices' =>
154 %                     "javascript:print_process()",
155 %                   'Email these invoices' =>
156 %                     "javascript:email_process()",
157 %                 ];
158 %
159 %  push @$menubar, 'Fax these invoices' =>
160 %                    "javascript:fax_process()"
161 %    if $conf->exists('hylafax');
162 %
163 %
164 <% include( 'elements/search.html',
165                  'title'       => 'Invoice Search Results',
166                  'html_init'   => $html_init,
167                  'menubar'     => $menubar,
168                  'name'        => 'invoices',
169                  'query'       => $sql_query,
170                  'count_query' => $count_query,
171                  'count_addl'  => $count_addl,
172                  'redirect'    => $link,
173                  'header'      => [ 'Invoice #',
174                                     'Balance',
175                                     'Amount',
176                                     'Date',
177                                     FS::UI::Web::cust_header(),
178                                   ],
179                  'fields'      => [
180                    'invnum',
181                    sub { sprintf($money_char.'%.2f', shift->get('owed') ) },
182                    sub { sprintf($money_char.'%.2f', shift->charged     ) },
183                    sub { time2str('%b %d %Y', shift->_date ) },
184                    \&FS::UI::Web::cust_fields,
185                  ],
186                  'align' => 'rrrrll',
187                  'links' => [
188                    $link,
189                    $link,
190                    $link,
191                    $link,
192                    ( map { $clink } FS::UI::Web::cust_header() ),
193                  ],
194   
195       )
196 %>