Will things ever be the same again?
[freeside.git] / httemplate / search / cust_tax_exempt_pkg.cgi
1 %
2 %
3 %my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
4 %
5 %my $join_cust = "
6 %    JOIN cust_bill USING ( invnum )
7 %    LEFT JOIN cust_main USING ( custnum )
8 %";
9 %
10 %my $join_pkg = "
11 %    LEFT JOIN cust_pkg USING ( pkgnum )
12 %    LEFT JOIN part_pkg USING ( pkgpart )
13 %";
14 %
15 %my $join = "
16 %    JOIN cust_bill_pkg USING ( billpkgnum )
17 %    $join_cust
18 %    $join_pkg
19 %";
20 %
21 %my $where = "
22 %  WHERE _date >= $beginning AND _date <= $ending
23 %";
24 %#    AND payby != 'COMP'
25 %
26 %if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
27 %  $where .= " AND agentnum = $1 ";
28 %}
29 %
30 %if ( $cgi->param('out') ) {
31 %
32 %  $where .= "
33 %    AND 0 = (
34 %      SELECT COUNT(*) FROM cust_main_county AS county_out
35 %      WHERE (    county_out.county  = cust_main.county
36 %              OR ( county_out.county IS NULL AND cust_main.county  =  '' )
37 %              OR ( county_out.county  =  ''  AND cust_main.county IS NULL)
38 %              OR ( county_out.county IS NULL AND cust_main.county IS NULL)
39 %            )
40 %        AND (    county_out.state   = cust_main.state
41 %              OR ( county_out.state  IS NULL AND cust_main.state  =  ''  )
42 %              OR ( county_out.state   =  ''  AND cust_main.state IS NULL )
43 %              OR ( county_out.state  IS NULL AND cust_main.state IS NULL )
44 %            )
45 %        AND county_out.country = cust_main.country
46 %        AND county_out.tax > 0
47 %    )
48 %  ";
49 %
50 %} elsif ( $cgi->param('country' ) ) {
51 %
52 %  my $county  = dbh->quote( $cgi->param('county')  );
53 %  my $state   = dbh->quote( $cgi->param('state')   );
54 %  my $country = dbh->quote( $cgi->param('country') );
55 %  $where .= "
56 %    AND ( county  = $county OR $county = '' )
57 %    AND ( state   = $state  OR $state = '' )
58 %    AND   country = $country
59 %  ";
60 %  $where .= ' AND taxclass = '. dbh->quote( $cgi->param('taxclass') )
61 %    if $cgi->param('taxclass');
62 %
63 %}
64 %
65 %my $count_query = "SELECT COUNT(*), SUM(amount)".
66 %                  "  FROM cust_tax_exempt_pkg $join $where";
67 %
68 %my $query = {
69 %  'table'     => 'cust_tax_exempt_pkg',
70 %  'addl_from' => $join,
71 %  'hashref'   => {},
72 %  'select'    => join(', ',
73 %                   'cust_tax_exempt_pkg.*',
74 %                   'cust_bill_pkg.*',
75 %                   'cust_bill.*',
76 %                   'part_pkg.pkg',
77 %                   'cust_main.custnum',
78 %                   FS::UI::Web::cust_sql_fields(),
79 %                 ),
80 %  'extra_sql' => $where,
81 %};
82 %
83 %my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
84 %my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
85 %
86 %my $conf = new FS::Conf;
87 %my $money_char = $conf->config('money_char') || '$';
88 %
89 %
90 <% include( 'elements/search.html',
91                  'title'       => 'Tax exemptions',
92                  'name'        => 'tax exemptions',
93                  'query'       => $query,
94                  'count_query' => $count_query,
95                  'count_addl'  => [ $money_char. '%.2f total', ],
96                  'header'      => [
97                    '#',
98                    'Month',
99                    'Amount',
100                    'Line item',
101                    'Invoice',
102                    'Date',
103                    FS::UI::Web::cust_header(),
104                  ],
105                  'fields'      => [
106                    'exemptpkgnum',
107                    sub { $_[0]->month. '/'. $_[0]->year; },
108                    sub { $money_char. $_[0]->amount; },
109
110                    sub {
111                      $_[0]->billpkgnum. ': '.
112                      ( $_[0]->pkgnum > 0
113                          ? $_[0]->get('pkg')
114                          : $_[0]->get('itemdesc')
115                      ).
116                      ' ('.
117                      ( $_[0]->setup > 0
118                          ? $money_char. $_[0]->setup. ' setup'
119                          : ''
120                      ).
121                      ( $_[0]->setup > 0 && $_[0]->recur > 0
122                        ? ' / '
123                        : ''
124                      ).
125                      ( $_[0]->recur > 0
126                          ? $money_char. $_[0]->recur. ' recur'
127                          : ''
128                      ).
129                      ')';
130                    },
131
132                    'invnum',
133                    sub { time2str('%b %d %Y', shift->_date ) },
134
135                    \&FS::UI::Web::cust_fields,
136                  ],
137                  'links'       => [
138                    '',
139                    '',
140                    '',
141
142                    '',
143                    $ilink,
144                    $ilink,
145
146                    ( map { $clink } FS::UI::Web::cust_header() ),
147                  ],
148                  'align' => 'rrrlrc', # 'rlrrrc',
149            )
150 %>
151