legacy tax exemption report fix, no _date
[freeside.git] / httemplate / search / cust_tax_exempt.cgi
1 <% include( 'elements/search.html',
2                  'title'       => 'Legacy tax exemptions',
3                  'name'        => 'legacy tax exemptions',
4                  'query'       => $query,
5                  'count_query' => $count_query,
6                  'count_addl'  => [ $money_char. '%.2f total', ],
7                  'header'      => [
8                    '#',
9                    'Month',
10                    'Amount',
11                    FS::UI::Web::cust_header(),
12                  ],
13                  'fields'      => [
14                    'exemptpkgnum',
15                    sub { $_[0]->month. '/'. $_[0]->year; },
16                    sub { $money_char. $_[0]->amount; },
17
18                    \&FS::UI::Web::cust_fields,
19                  ],
20                  'links'       => [
21                    '',
22                    '',
23                    '',
24
25                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
26                          FS::UI::Web::cust_header()
27                    ),
28                  ],
29                  'align' => 'rrr'.FS::UI::Web::cust_aligns(),
30                  'color' => [ 
31                               '',
32                               '',
33                               '',
34                               FS::UI::Web::cust_colors(),
35                             ],
36                  'style' => [ 
37                               '',
38                               '',
39                               '',
40                               FS::UI::Web::cust_styles(),
41                             ],
42            )
43 %>
44 <%init>
45
46 my $join_cust = "
47     LEFT JOIN cust_main USING ( custnum )
48 ";
49
50 die "access denied"
51   unless $FS::CurrentUser::CurrentUser->access_right('View customer tax exemptions');
52
53 my @where = ();
54
55 #my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
56 #if ( $beginning || $ending ) {
57 #  push @where, "_date >= $beginning",
58 #               "_date <= $ending";
59 #               #"payby != 'COMP';
60 #}
61
62 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
63   push @where, "agentnum = $1";
64 }
65
66 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
67   push @where,  "cust_main.custnum = $1";
68 }
69
70 if ( $cgi->param('out') ) {
71
72   push @where, "
73     0 = (
74       SELECT COUNT(*) FROM cust_main_county AS county_out
75       WHERE (    county_out.county  = cust_main.county
76               OR ( county_out.county IS NULL AND cust_main.county  =  '' )
77               OR ( county_out.county  =  ''  AND cust_main.county IS NULL)
78               OR ( county_out.county IS NULL AND cust_main.county IS NULL)
79             )
80         AND (    county_out.state   = cust_main.state
81               OR ( county_out.state  IS NULL AND cust_main.state  =  ''  )
82               OR ( county_out.state   =  ''  AND cust_main.state IS NULL )
83               OR ( county_out.state  IS NULL AND cust_main.state IS NULL )
84             )
85         AND county_out.country = cust_main.country
86         AND county_out.tax > 0
87     )
88   ";
89
90 } elsif ( $cgi->param('country' ) ) {
91
92   my $county  = dbh->quote( $cgi->param('county')  );
93   my $state   = dbh->quote( $cgi->param('state')   );
94   my $country = dbh->quote( $cgi->param('country') );
95   push @where, "( county  = $county OR $county = '' )",
96                "( state   = $state  OR $state = ''  )",
97                "  country = $country";
98   push @where, 'taxclass = '. dbh->quote( $cgi->param('taxclass') )
99     if $cgi->param('taxclass');
100
101 }
102
103 my $where = scalar(@where) ? 'WHERE '.join(' AND ', @where) : '';
104
105 my $count_query = "SELECT COUNT(*), SUM(amount)".
106                   "  FROM cust_tax_exempt $join_cust $where";
107
108 my $query = {
109   'table'     => 'cust_tax_exempt',
110   'addl_from' => $join_cust,
111   'hashref'   => {},
112   'select'    => join(', ',
113                    'cust_tax_exempt.*',
114                    'cust_main.custnum',
115                    FS::UI::Web::cust_sql_fields(),
116                  ),
117   'extra_sql' => $where,
118 };
119
120 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
121
122 my $conf = new FS::Conf;
123 my $money_char = $conf->config('money_char') || '$';
124
125 </%init>