Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / search / cust_tax_exempt_pkg.cgi
1 <% include( 'elements/search.html',
2                  'title'       => 'Tax exemptions',
3                  'name'        => '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                    'Line item',
12                    'Invoice',
13                    'Date',
14                    FS::UI::Web::cust_header(),
15                  ],
16                  'fields'      => [
17                    'exemptpkgnum',
18                    sub { $_[0]->month. '/'. $_[0]->year; },
19                    sub { $money_char. $_[0]->amount; },
20
21                    sub {
22                      $_[0]->billpkgnum. ': '.
23                      ( $_[0]->pkgnum > 0
24                          ? $_[0]->get('pkg')
25                          : $_[0]->get('itemdesc')
26                      ).
27                      ' ('.
28                      ( $_[0]->setup > 0
29                          ? $money_char. $_[0]->setup. ' setup'
30                          : ''
31                      ).
32                      ( $_[0]->setup > 0 && $_[0]->recur > 0
33                        ? ' / '
34                        : ''
35                      ).
36                      ( $_[0]->recur > 0
37                          ? $money_char. $_[0]->recur. ' recur'
38                          : ''
39                      ).
40                      ')';
41                    },
42
43                    'invnum',
44                    sub { time2str('%b %d %Y', shift->_date ) },
45
46                    \&FS::UI::Web::cust_fields,
47                  ],
48                  'links'       => [
49                    '',
50                    '',
51                    '',
52
53                    '',
54                    $ilink,
55                    $ilink,
56
57                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
58                          FS::UI::Web::cust_header()
59                    ),
60                  ],
61                  'align' => 'rrrlrc'.FS::UI::Web::cust_aligns(), # 'rlrrrc',
62                  'color' => [ 
63                               '',
64                               '',
65                               '',
66                               '',
67                               '',
68                               '',
69                               FS::UI::Web::cust_colors(),
70                             ],
71                  'style' => [ 
72                               '',
73                               '',
74                               '',
75                               '',
76                               '',
77                               '',
78                               FS::UI::Web::cust_styles(),
79                             ],
80            )
81 %>
82 <%once>
83
84 my $join_cust = "
85     JOIN cust_bill USING ( invnum )
86     LEFT JOIN cust_main USING ( custnum )
87 ";
88
89 my $join_pkg = "
90     LEFT JOIN cust_pkg USING ( pkgnum )
91     LEFT JOIN part_pkg USING ( pkgpart )
92 ";
93
94 my $join = "
95     JOIN cust_bill_pkg USING ( billpkgnum )
96     $join_cust
97     $join_pkg
98 ";
99
100 </%once>
101 <%init>
102
103 die "access denied"
104   unless $FS::CurrentUser::CurrentUser->access_right('View customer tax exemptions');
105
106 my @where = ("exempt_monthly = 'Y'");
107
108 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
109 if ( $beginning || $ending ) {
110   push @where, "_date >= $beginning",
111                "_date <= $ending";
112                #"payby != 'COMP';
113 }
114
115 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
116   push @where, "cust_main.agentnum = $1";
117 }
118
119 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
120   push @where,  "cust_main.custnum = $1";
121 }
122
123 if ( $cgi->param('out') ) {
124   # wtf? how would you ever get exemptions on a non-taxable package location?
125
126   push @where, "
127     0 = (
128       SELECT COUNT(*) FROM cust_main_county AS county_out
129       WHERE (    county_out.county  = cust_main.county
130               OR ( county_out.county IS NULL AND cust_main.county  =  '' )
131               OR ( county_out.county  =  ''  AND cust_main.county IS NULL)
132               OR ( county_out.county IS NULL AND cust_main.county IS NULL)
133             )
134         AND (    county_out.state   = cust_main.state
135               OR ( county_out.state  IS NULL AND cust_main.state  =  ''  )
136               OR ( county_out.state   =  ''  AND cust_main.state IS NULL )
137               OR ( county_out.state  IS NULL AND cust_main.state IS NULL )
138             )
139         AND county_out.country = cust_main.country
140         AND county_out.tax > 0
141     )
142   ";
143
144 } elsif ( $cgi->param('country' ) ) {
145
146   my $county  = dbh->quote( $cgi->param('county')  );
147   my $state   = dbh->quote( $cgi->param('state')   );
148   my $country = dbh->quote( $cgi->param('country') );
149   push @where, "( county  = $county OR $county = '' )",
150                "( state   = $state  OR $state = ''  )",
151                "  country = $country";
152   push @where, 'taxclass = '. dbh->quote( $cgi->param('taxclass') )
153     if $cgi->param('taxclass');
154
155 } elsif ( $cgi->param('taxnum') ) {
156
157   my $taxnum_in = join(',', grep /^\d+$/, $cgi->param('taxnum') );
158   push @where, "taxnum IN ($taxnum_in)" if $taxnum_in;
159
160 }
161
162 my $where = scalar(@where) ? 'WHERE '.join(' AND ', @where) : '';
163
164 my $count_query = "SELECT COUNT(*), SUM(amount)".
165                   "  FROM cust_tax_exempt_pkg $join $where";
166
167 my $query = {
168   'table'     => 'cust_tax_exempt_pkg',
169   'addl_from' => $join,
170   'hashref'   => {},
171   'select'    => join(', ',
172                    'cust_tax_exempt_pkg.*',
173                    'cust_bill_pkg.*',
174                    'cust_bill.*',
175                    'part_pkg.pkg',
176                    'cust_main.custnum',
177                    FS::UI::Web::cust_sql_fields(),
178                  ),
179   'extra_sql' => $where,
180 };
181
182 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
183 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
184
185 my $conf = new FS::Conf;
186 my $money_char = $conf->config('money_char') || '$';
187
188 </%init>