whew, FINALLY can fix monthly exemption columns to work correctly. also make them...
[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._date',
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                    'Date',
98                    'Amount',
99
100                    #'Description',
101                    #'Setup charge',
102                    #'Recurring charge',
103                    #'Invoice',
104                    #'Date',
105
106                    FS::UI::Web::cust_header(),
107                  ],
108                  'fields'      => [
109                    'exemptpkgnum',
110                    sub { $_[0]->month. '/'. $_[0]->year; },
111                    'amount',
112
113                    #sub { $_[0]->pkgnum > 0
114                    #        ? $_[0]->get('pkg')
115                    #        : $_[0]->get('itemdesc')
116                    #    },
117                    ##strikethrough or "N/A ($amount)" or something these when
118                    ## they're not applicable to pkg_tax search
119                    #sub { sprintf($money_char.'%.2f', shift->setup ) },
120                    #sub { sprintf($money_char.'%.2f', shift->recur ) },
121                    #'invnum',
122                    #sub { time2str('%b %d %Y', shift->_date ) },
123
124                    \&FS::UI::Web::cust_fields,
125                  ],
126                  'links'       => [
127                    '',
128                    '',
129                    '',
130
131                    #'',
132                    #'',
133                    #'',
134                    #'',
135                    #$ilink,
136                    #$ilink,
137
138                    ( map { $clink } FS::UI::Web::cust_header() ),
139                  ],
140                  'align' => 'rrr', # 'rlrrrc',
141            )
142 %>
143