agent virtualization, take one (stuff from "inactive" changeset snuck into cust_main...
[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('agentnum') =~ /^(\d+)$/ ) {
56        $agentnum = $1;
57        push @where, "cust_main.agentnum = $agentnum";
58      }
59
60      my $owed =
61        "charged - ( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
62                     WHERE cust_bill_pay.invnum = cust_bill.invnum )
63                 - ( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
64                     WHERE cust_credit_bill.invnum = cust_bill.invnum )";
65
66      if ( $cgi->param('open') ) {
67        push @where, "0 != $owed";
68        $open = 1;
69      }
70
71      my($query) = $cgi->keywords;
72      if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
73        ($open, $days, my $field) = ($1, $2, $3);
74        $field = "_date" if $field eq 'date';
75        $orderby = "ORDER BY cust_bill.$field";
76        push @where, "0 != $owed" if $open;
77        push @where, "cust_bill._date < ". (time-86400*$days) if $days;
78      }
79
80      #here is the agent virtualization
81      push @where, $agentnums_sql;
82      my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
83
84      if ( $cgi->param('newest_percust') ) {
85        $distinct = 'DISTINCT ON ( cust_bill.custnum )';
86        $orderby = 'ORDER BY cust_bill.custnum ASC, cust_bill._date DESC';
87        #$count_query = "SELECT 'N/A', 'N/A', 'N/A'"; #XXXXXXX fix
88        $count_query = "SELECT COUNT(DISTINCT cust_bill.custnum), 'N/A', 'N/A'";
89      }
90    
91      unless ( $count_query ) {
92        $count_query = "SELECT COUNT(*), sum(charged), sum($owed)";
93        $count_addl = [ '$%.2f total invoiced',
94                        '$%.2f total outstanding balance',
95                      ];
96      }
97      $count_query .=  " FROM cust_bill $join_cust_main $extra_sql";
98    
99      $sql_query = {
100        'table'     => 'cust_bill',
101        'addl_from' => $join_cust_main,
102        'hashref'   => {},
103        'select'    => "$distinct ". join(', ',
104                         'cust_bill.*',
105                         #( map "cust_main.$_", qw(custnum last first company) ),
106                         'cust_main.custnum as cust_main_custnum',
107                         FS::UI::Web::cust_sql_fields(),
108                         "$owed as owed",
109                       ),
110        'extra_sql' => "$extra_sql $orderby"
111      };
112    
113    }
114
115    my $link  = [ "${p}view/cust_bill.cgi?", 'invnum', ];
116    my $clink = sub {
117      my $cust_bill = shift;
118      $cust_bill->cust_main_custnum
119        ? [ "${p}view/cust_main.cgi?", 'custnum' ]
120        : '';
121    };
122
123   my $conf = new FS::Conf;
124   my $money_char = $conf->config('money_char') || '$';
125
126   my $html_init = join("\n", map {
127     ( my $action = $_ ) =~ s/_$//;
128     include('/elements/progress-init.html',
129               $_.'form',
130               [ 'begin', 'end', 'agentnum', 'open', 'days', 'newest_percust' ],
131               "../misc/${_}invoices.cgi",
132               { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
133               $_, #key
134            ),
135     qq!<FORM NAME="${_}form">!,
136     qq!<INPUT TYPE="hidden" NAME="begin"     VALUE="$begin">!,
137     qq!<INPUT TYPE="hidden" NAME="end"       VALUE="$end">!,
138     qq!<INPUT TYPE="hidden" NAME="agentnum"  VALUE="$agentnum">!,
139     qq!<INPUT TYPE="hidden" NAME="open"      VALUE="$open">!,
140     qq!<INPUT TYPE="hidden" NAME="days"      VALUE="$days">!,
141     qq!</FORM>!
142   } qw( print_ email_ fax_ ) );
143
144   my $menubar =  [
145                    'Main menu' => $p,
146                    'Print these invoices' =>
147                      "javascript:print_process()",
148                    'Email these invoices' =>
149                      "javascript:email_process()",
150                  ];
151
152   push @$menubar, 'Fax these invoices' =>
153                     "javascript:fax_process()"
154     if $conf->exists('hylafax');
155
156 %><%= include( 'elements/search.html',
157                  'title'       => 'Invoice Search Results',
158                  'html_init'   => $html_init,
159                  'menubar'     => $menubar,
160                  'name'        => 'invoices',
161                  'query'       => $sql_query,
162                  'count_query' => $count_query,
163                  'count_addl'  => $count_addl,
164                  'redirect'    => $link,
165                  'header'      => [ 'Invoice #',
166                                     'Balance',
167                                     'Amount',
168                                     'Date',
169                                     FS::UI::Web::cust_header(),
170                                   ],
171                  'fields'      => [
172                    'invnum',
173                    sub { sprintf($money_char.'%.2f', shift->get('owed') ) },
174                    sub { sprintf($money_char.'%.2f', shift->charged     ) },
175                    sub { time2str('%b %d %Y', shift->_date ) },
176                    \&FS::UI::Web::cust_fields,
177                  ],
178                  'align' => 'rrrrll',
179                  'links' => [
180                    $link,
181                    $link,
182                    $link,
183                    $link,
184                    ( map { $clink } FS::UI::Web::cust_header() ),
185                  ],
186   
187       )
188 %>