1 <% include( 'elements/search.html',
2 'title' => 'Tax credits', #well, actually application of
3 'name' => 'tax credits', # credit to line item
5 'count_query' => $count_query,
6 'count_addl' => [ $money_char. '%.2f total', ],
23 FS::UI::Web::cust_header(),
27 sub { sprintf($money_char.'%.2f', shift->amount ) },
29 sub { time2str('%b %d %Y', shift->get('cust_credit_date') ) },
31 sub { shift->cust_credit_bill->cust_credit->reason },
33 sub { $_[0]->pkgnum > 0
34 ? $_[0]->get('pkg') # possibly use override.pkg
35 : $_[0]->get('itemdesc') # but i think this correct
38 sub { time2str('%b %d %Y', shift->_date ) },
39 \&FS::UI::Web::cust_fields,
41 #'sort_fields' => [ #XXX add
51 ( map { $_ ne 'Cust. Status' ? $clink : '' }
52 FS::UI::Web::cust_header()
55 'align' => 'rrlllrr'.FS::UI::Web::cust_aligns(),
64 FS::UI::Web::cust_colors(),
74 FS::UI::Web::cust_styles(),
80 #LOTS of false laziness below w/cust_bill_pkg.cgi
83 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
85 my $conf = new FS::Conf;
87 #here is the agent virtualization
89 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
91 my @where = ( $agentnums_sql );
93 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
94 push @where, "cust_bill._date >= $beginning",
95 "cust_bill._date <= $ending";
97 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
98 push @where, "cust_main.agentnum = $1";
102 # not specified: all classes
105 my $use_override = $cgi->param('use_override');
106 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
109 $comparison = "IS NULL";
111 $comparison = "= $1";
114 if ( $use_override ) {
116 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
117 override.classnum $comparison AND pkgpart_override IS NOT NULL
120 push @where, "part_pkg.classnum $comparison";
124 if ( $cgi->param('taxclass')
125 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
126 #(should we save a taxclass or a link to taxnum
127 # in cust_bill_pkg or something like
128 # cust_bill_pkg_tax_location?)
132 #override taxclass when use_override is specified? probably
133 #if ( $use_override ) {
136 # ' ( '. join(' OR ',
138 # ' ( part_pkg.taxclass = '. dbh->quote($_).
139 # ' AND pkgpart_override IS NULL '.
141 # ' override.taxclass = '. dbh->quote($_).
142 # ' AND pkgpart_override IS NOT NULL '.
145 # $cgi->param('taxclass')
153 map ' part_pkg.taxclass = '.dbh->quote($_),
154 $cgi->param('taxclass')
162 my @loc_param = qw( city county state country );
164 if ( $cgi->param('out') ) {
166 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
167 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
168 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
171 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
172 if $cgi->param('istax');
176 SELECT COUNT(*) FROM cust_main_county
177 WHERE cust_main_county.tax > 0
182 #not linked to by anything, but useful for debugging "out of taxable region"
183 if ( grep $cgi->param($_), @loc_param ) {
185 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
187 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
188 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
189 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
192 push @where, $loc_sql;
196 } elsif ( $cgi->param('country') ) {
198 my @counties = $cgi->param('county');
200 if ( scalar(@counties) > 1 ) {
202 #hacky, could be more efficient. care if it is ever used for more than the
203 # tax-report_groups filtering kludge
206 ' ( '. join(' OR ', map {
208 my %ph = ( 'county' => dbh->quote($_),
209 map { $_ => dbh->quote( $cgi->param($_) ) }
210 qw( city state country )
213 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
214 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
215 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
224 push @where, $locs_sql;
228 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
230 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
231 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
232 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
235 push @where, $loc_sql;
240 if ( $cgi->param('istax') ) {
241 $title = 'Tax credits';
242 $name = 'tax credits';
243 if ( $cgi->param('taxname') ) {
244 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
245 #} elsif ( $cgi->param('taxnameNULL') {
247 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
249 } elsif ( $cgi->param('nottax') ) {
250 $title = 'Credit applications to line items';
251 $name = 'applications';
252 #what can we usefully do with "taxname" ???? look up a class???
254 $title = 'Credit applications to line items';
255 $name = 'applications';
256 #warn "neither nottax nor istax parameters specified";
259 if ( $cgi->param('taxclassNULL') ) {
261 my %hash = ( 'country' => scalar($cgi->param('country')) );
262 foreach (qw( state county )) {
263 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
265 my $cust_main_county = qsearchs('cust_main_county', \%hash);
266 die "unknown base region for empty taxclass" unless $cust_main_county;
268 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
269 push @where, $same_sql if $same_sql;
273 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
275 # this should really be shoved out to FS::cust_pkg->location_sql or something
276 # along with the code in report_newtax.cgi
279 'county' => 'tax_rate_location.county',
280 'state' => 'tax_rate_location.state',
281 'city' => 'tax_rate_location.city',
282 'locationtaxid' => 'cust_bill_pkg_tax_rate_location.locationtaxid',
285 my %ph = map { ( $pn{$_} => dbh->quote( $cgi->param($_) || '' ) ) }
286 qw( city county state locationtaxid );
289 join( ' AND ', map { "( $_ = $ph{$_} OR $ph{$_} = '' AND $_ IS NULL)" }
295 if ( $cgi->param('itemdesc') ) {
296 if ( $cgi->param('itemdesc') eq 'Tax' ) {
297 push @where, "(itemdesc='Tax' OR itemdesc is null)";
299 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
303 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
304 my ( $group_op, $group_value ) = ( $1, $2 );
305 if ( $group_op eq '=' ) {
306 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
307 push @where, 'itemdesc = '. dbh->quote($group_value);
308 } elsif ( $group_op eq '!=' ) {
309 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
311 die "guru meditation #00de: group_op $group_op\n";
316 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
317 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
319 if ( $cgi->param('cust_tax') ) {
320 #false laziness -ish w/report_tax.cgi
322 if ( $cgi->param('taxname') ) {
323 my $q_taxname = dbh->quote($cgi->param('taxname'));
326 OR EXISTS ( SELECT 1 FROM cust_main_exemption
327 WHERE cust_main_exemption.custnum = cust_main.custnum
328 AND cust_main_exemption.taxname = $q_taxname )
332 $cust_exempt = " tax = 'Y' ";
335 push @where, $cust_exempt;
338 my $count_query = "SELECT COUNT(DISTINCT billpkgnum),
339 SUM(cust_credit_bill_pkg.amount)";
342 ' JOIN cust_bill ON ( cust_bill_pkg.invnum = cust_bill.invnum )
343 LEFT JOIN cust_main ON ( cust_bill.custnum = cust_main.custnum ) ';
348 my $join_cust_bill_pkg = 'LEFT JOIN cust_bill_pkg USING ( billpkgnum )';
350 if ( $cgi->param('nottax') ) {
352 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
353 LEFT JOIN part_pkg USING ( pkgpart )
354 LEFT JOIN part_pkg AS override
355 ON pkgpart_override = override.pkgpart ';
356 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
357 if $conf->exists('tax-pkg_address');
359 } elsif ( $cgi->param('istax') ) {
361 #false laziness w/report_tax.cgi $taxfromwhere
362 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
363 $cgi->param('iscredit') eq 'rate') {
366 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ';
367 if ( $cgi->param('iscredit') eq 'rate' ) {
368 $join_pkg .= ', billpkgtaxratelocationnum )';
369 } elsif ( $conf->exists('tax-pkg_address') ) {
370 $join_pkg .= ', billpkgtaxlocationnum )';
371 push @where, "billpkgtaxratelocationnum IS NULL";
374 push @where, "billpkgtaxratelocationnum IS NULL";
377 $join_pkg .= ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
379 } elsif ( $conf->exists('tax-pkg_address') ) {
381 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
382 LEFT JOIN cust_location USING ( locationnum ) ';
384 #quelle kludge, somewhat false laziness w/report_tax.cgi
385 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
391 warn "neiether nottax nor istax parameters specified";
393 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
394 LEFT JOIN part_pkg USING ( pkgpart ) ';
398 my $where = ' WHERE '. join(' AND ', @where);
400 my $join_credit = ' LEFT JOIN cust_credit_bill USING ( creditbillnum )
401 LEFT JOIN cust_credit USING ( crednum ) ';
403 $count_query .= " FROM cust_credit_bill_pkg
410 my @select = ( 'cust_credit_bill_pkg.*',
412 'cust_credit.otaker',
413 'cust_credit._date AS cust_credit_date',
416 push @select, 'part_pkg.pkg' unless $cgi->param('istax');
417 push @select, 'cust_main.custnum',
418 FS::UI::Web::cust_sql_fields();
421 'table' => 'cust_credit_bill_pkg',
422 'addl_from' => "$join_pkg
427 'select' => join(', ', @select ),
428 'extra_sql' => $where,
429 'order_by' => 'ORDER BY creditbillpkgnum', #cust_bill. or cust_credit._date?
432 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
433 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
435 my $conf = new FS::Conf;
436 my $money_char = $conf->config('money_char') || '$';