005d77c33d833a623ad69c7cfa6180b680280417
[freeside.git] / httemplate / search / cust_tax_exempt.cgi
1 <& 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                    'Inserted',
11                    'Amount',
12                    FS::UI::Web::cust_header(),
13                  ],
14                  'fields'      => [
15                    'exemptnum',
16                    sub { $_[0]->month. '/'. $_[0]->year; },
17                    sub { my $h = $_[0]->h_search('insert');
18                          $h ? time2str('%L/%d/%Y', $h->history_date ) : ''
19                        },
20                    sub { $money_char. $_[0]->amount; },
21
22                    \&FS::UI::Web::cust_fields,
23                  ],
24                  'links'       => [
25                    '',
26                    '',
27                    '',
28                    '',
29
30                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
31                          FS::UI::Web::cust_header()
32                    ),
33                  ],
34                  'align' => 'rrrr'.FS::UI::Web::cust_aligns(),
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 = FS::UI::Web::join_cust_main('cust_tax_exempt');
54
55 die "access denied"
56   unless $FS::CurrentUser::CurrentUser->access_right('View customer tax exemptions');
57
58 my @where = ();
59
60 #my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
61 #if ( $beginning || $ending ) {
62 #  push @where, "_date >= $beginning",
63 #               "_date <= $ending";
64 #               #"payby != 'COMP';
65 #}
66
67 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
68   push @where, "agentnum = $1";
69 }
70
71 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
72   push @where,  "cust_main.custnum = $1";
73 }
74
75 #prospect active inactive suspended cancelled
76 if ( grep { $cgi->param('status') eq $_ } FS::cust_main->statuses() ) {
77   my $method = $cgi->param('status'). '_sql';
78   #push @where, $class->$method();
79   push @where, FS::cust_main->$method();
80 }
81
82 if ( $cgi->param('out') ) {
83
84   push @where, "
85     0 = (
86       SELECT COUNT(*) FROM cust_main_county AS county_out
87       WHERE (    county_out.county  = cust_main.county
88               OR ( county_out.county IS NULL AND cust_main.county  =  '' )
89               OR ( county_out.county  =  ''  AND cust_main.county IS NULL)
90               OR ( county_out.county IS NULL AND cust_main.county IS NULL)
91             )
92         AND (    county_out.state   = cust_main.state
93               OR ( county_out.state  IS NULL AND cust_main.state  =  ''  )
94               OR ( county_out.state   =  ''  AND cust_main.state IS NULL )
95               OR ( county_out.state  IS NULL AND cust_main.state IS NULL )
96             )
97         AND county_out.country = cust_main.country
98         AND county_out.tax > 0
99     )
100   ";
101
102 } elsif ( $cgi->param('country' ) ) {
103
104   my $county  = dbh->quote( $cgi->param('county')  );
105   my $state   = dbh->quote( $cgi->param('state')   );
106   my $country = dbh->quote( $cgi->param('country') );
107   push @where, "( county  = $county OR $county = '' )",
108                "( state   = $state  OR $state = ''  )",
109                "  country = $country";
110   push @where, 'taxclass = '. dbh->quote( $cgi->param('taxclass') )
111     if $cgi->param('taxclass');
112
113 }
114
115 my $where = scalar(@where) ? 'WHERE '.join(' AND ', @where) : '';
116
117 my $count_query = "SELECT COUNT(*), SUM(amount)".
118                   "  FROM cust_tax_exempt $join_cust $where";
119
120 my $query = {
121   'table'     => 'cust_tax_exempt',
122   'addl_from' => $join_cust,
123   'hashref'   => {},
124   'select'    => join(', ',
125                    'cust_tax_exempt.*',
126                    'cust_main.custnum',
127                    FS::UI::Web::cust_sql_fields(),
128                  ),
129   'extra_sql' => $where,
130 };
131
132 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
133
134 my $conf = new FS::Conf;
135 my $money_char = $conf->config('money_char') || '$';
136
137 </%init>