advanced invoice serach, groundwork to add reprint/fax/email links to invoice search...
[freeside.git] / httemplate / search / cust_bill.html
1 <%
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";
10      $sql_query = {
11        'table'     => 'cust_bill',
12        'hashref'   => { 'invnum' => $2 },
13        #'select'    => '*',
14      };
15    } else {
16    #if ( $cgi->param('begin') || $cgi->param('end')
17    #     || $cgi->param('beginning') || $cgi->param('ending')
18    #     || $cgi->keywords
19    #   )
20    #{
21
22      my @where;
23      my $orderby = 'ORDER BY cust_bill._date';
24
25      if ( $cgi->param('beginning')
26           && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
27        $begin = str2time($1);
28        push @where, "cust_bill._date >= $begin";
29      }
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";
34      }
35
36      if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
37        $begin = $1;
38        push @where, "cust_bill._date >= $begin";
39      }
40      if ( $cgi->param('end') =~ /^(\d+)$/ ) {
41        $end = $1;
42        push @where, "cust_bill._date < $end";
43      }
44
45      if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
46        $agentnum = $1;
47        push @where, "cust_main.agentnum = $agentnum";
48      }
49
50      my $owed =
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 )";
55
56      if ( $cgi->param('open') ) {
57        push @where, "0 != $owed";
58        $open = 1;
59      }
60
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;
68      }
69
70      my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
71
72      my $addl_from = 'left join cust_main using ( custnum )';
73
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'";
79      }
80    
81      unless ( $count_query ) {
82        $count_query = "SELECT COUNT(*), sum(charged), sum($owed)";
83        $count_addl = [ '$%.2f total invoiced',
84                        '$%.2f total outstanding balance',
85                      ];
86      }
87      $count_query .=  " FROM cust_bill $addl_from $extra_sql";
88    
89      $sql_query = {
90        'table'     => 'cust_bill',
91        'addl_from' => $addl_from,
92        'hashref'   => {},
93        'select'    => "$distinct ". join(', ',
94                         'cust_bill.*',
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) ),
98                         "$owed as owed",
99                       ),
100        'extra_sql' => "$extra_sql $orderby"
101      };
102    
103    }
104
105    my $link  = [ "${p}view/cust_bill.cgi?", 'invnum', ];
106    my $clink = sub {
107      my $cust_bill = shift;
108      my $cust_main = $cust_bill->cust_main;
109      $cust_main
110        ? [ "${p}view/cust_main.cgi?", 'custnum' ]
111        : '';
112    };
113
114   my $conf = new FS::Conf;
115   my $money_char = $conf->config('money_char') || '$';
116
117   my $html_init = join("\n", map {
118     ( my $action = $_ ) =~ s/_$//;
119     include('/elements/progress-init.html',
120               $_.'form',
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...
124               $_, #key
125            ),
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">!,
132     qq!</FORM>!
133   } qw( print_ email_ fax_ ) );
134
135   my $menubar =  [
136                    'Main menu' => $p,
137                    'Print these invoices' =>
138                      "javascript:print_process()",
139                    'Email these invoices' =>
140                      "javascript:email_process()",
141                  ];
142
143   push @$menubar, 'Fax these invoices' =>
144                     "javascript:fax_process()"
145     if $conf->exists('hylafax');
146
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,
155                  'redirect'    => $link,
156                  'header'      =>
157                    [ 'Invoice #', qw(Balance Amount Date), 'Contact name',
158                      'Company' ],
159                  'fields'      => [
160                    'invnum',
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;
166                          # $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);
173                          } else {
174                            "WARNING: can't find cust_main.custnum ".
175                               $cust_bill->custnum. ' (cust_bill.invnum '.
176                               $cust_bill->invnum. ')';
177                          }
178                        },
179                    sub { my $cust_main = shift->cust_main;
180                          $cust_main ? $cust_main->company : '';
181                        },
182                  ],
183                  'align' => 'rrrrll',
184                  'links' => [
185                    $link,
186                    $link,
187                    $link,
188                    $link,
189                    $clink,
190                    $clink,
191                  ],
192   
193       )
194 %>