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