on tax exemption report, show more info on the specific line item and invoice
[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 %><%= include( 'elements/search.html',
90                  'title'       => 'Tax exemptions',
91                  'name'        => 'tax exemptions',
92                  'query'       => $query,
93                  'count_query' => $count_query,
94                  'count_addl'  => [ $money_char. '%.2f total', ],
95                  'header'      => [
96                    '#',
97                    'Month',
98                    'Amount',
99                    'Line item',
100                    'Invoice',
101                    'Date',
102                    FS::UI::Web::cust_header(),
103                  ],
104                  'fields'      => [
105                    'exemptpkgnum',
106                    sub { $_[0]->month. '/'. $_[0]->year; },
107                    sub { $money_char. $_[0]->amount; },
108
109                    sub {
110                      $_[0]->billpkgnum. ': '.
111                      ( $_[0]->pkgnum > 0
112                          ? $_[0]->get('pkg')
113                          : $_[0]->get('itemdesc')
114                      ).
115                      ' ('.
116                      ( $_[0]->setup > 0
117                          ? $money_char. $_[0]->setup. ' setup'
118                          : ''
119                      ).
120                      ( $_[0]->setup > 0 && $_[0]->recur > 0
121                        ? ' / '
122                        : ''
123                      ).
124                      ( $_[0]->recur > 0
125                          ? $money_char. $_[0]->recur. ' recur'
126                          : ''
127                      ).
128                      ')';
129                    },
130
131                    'invnum',
132                    sub { time2str('%b %d %Y', shift->_date ) },
133
134                    \&FS::UI::Web::cust_fields,
135                  ],
136                  'links'       => [
137                    '',
138                    '',
139                    '',
140
141                    '',
142                    $ilink,
143                    $ilink,
144
145                    ( map { $clink } FS::UI::Web::cust_header() ),
146                  ],
147                  'align' => 'rrrlrc', # 'rlrrrc',
148            )
149 %>
150