1 <& elements/search.html,
2 'title' => 'Credit application detail', #to line item
3 'name_singular' => 'credit application',
5 'count_query' => $count_query,
6 'count_addl' => \@count_addl,
26 FS::UI::Web::cust_header(),
30 sub { sprintf($money_char.'%.2f', shift->amount ) },
32 sub { sprintf($money_char.'%.2f', shift->get('exempt_credited') ) },
34 sub { time2str('%b %d %Y', shift->get('cust_credit_date') ) },
35 sub { shift->cust_credit_bill->cust_credit->otaker },
36 sub { shift->cust_credit_bill->cust_credit->reason },
38 sub { $_[0]->pkgnum > 0
39 ? $_[0]->get('pkg') # possibly use override.pkg
40 : $_[0]->get('itemdesc') # but i think this correct
45 sub { time2str('%b %d %Y', shift->_date ) },
46 \&FS::UI::Web::cust_fields,
54 '', #line item description
72 ( map { $_ ne 'Cust. Status' ? $clink : '' }
73 FS::UI::Web::cust_header()
79 FS::UI::Web::cust_aligns(),
91 FS::UI::Web::cust_colors(),
104 FS::UI::Web::cust_styles(),
110 #LOTS of false laziness below w/cust_bill_pkg.cgi
111 # and a little w/cust_credit.html
114 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
116 my $conf = new FS::Conf;
118 #here is the agent virtualization
120 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
122 my @where = ( $agentnums_sql );
124 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
125 push @where, "cust_credit.usernum = $1";
128 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
129 push @where, "cust_bill._date >= $beginning",
130 "cust_bill._date <= $ending";
132 my($cr_begin, $cr_end) = FS::UI::Web::parse_beginning_ending($cgi, 'credit');
133 push @where, "cust_credit._date >= $cr_begin",
134 "cust_credit._date <= $cr_end";
136 #credit amount? seems more what is expected than the applied amount
137 my @lt_gt = FS::UI::Web::parse_lt_gt($cgi, 'amount' );
138 s/amount/cust_credit.amount/g foreach (@lt_gt);
141 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
142 push @where, "cust_main.agentnum = $1";
145 if ( $cgi->param('billpkgnum') =~ /^(\d+)$/ ) {
146 push @where, "billpkgnum = $1";
150 # not specified: all classes
153 my $use_override = $cgi->param('use_override');
154 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
157 $comparison = "IS NULL";
159 $comparison = "= $1";
162 if ( $use_override ) {
164 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
165 override.classnum $comparison AND pkgpart_override IS NOT NULL
168 push @where, "part_pkg.classnum $comparison";
172 if ( $cgi->param('taxclass')
173 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
174 #(should we save a taxclass or a link to taxnum
175 # in cust_bill_pkg or something like
176 # cust_bill_pkg_tax_location?)
180 #override taxclass when use_override is specified? probably
181 #if ( $use_override ) {
184 # ' ( '. join(' OR ',
186 # ' ( part_pkg.taxclass = '. dbh->quote($_).
187 # ' AND pkgpart_override IS NULL '.
189 # ' override.taxclass = '. dbh->quote($_).
190 # ' AND pkgpart_override IS NOT NULL '.
193 # $cgi->param('taxclass')
201 map ' part_pkg.taxclass = '.dbh->quote($_),
202 $cgi->param('taxclass')
210 my @loc_param = qw( district city county state country );
212 if ( $cgi->param('out') ) {
213 # probably don't need this part
215 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
216 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
217 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
220 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
221 if $cgi->param('istax');
225 SELECT COUNT(*) FROM cust_main_county
226 WHERE cust_main_county.tax > 0
231 #not linked to by anything, but useful for debugging "out of taxable region"
232 if ( grep $cgi->param($_), @loc_param ) {
234 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
236 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
237 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
238 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
241 push @where, $loc_sql;
245 } elsif ( $cgi->param('country') ) {
247 my @counties = $cgi->param('county');
249 if ( scalar(@counties) > 1 ) {
251 #hacky, could be more efficient. care if it is ever used for more than the
252 # tax-report_groups filtering kludge
253 # (does that even still exist? if so, correct this (or location_sql itself)
254 # to work with modern cust_location links)
257 ' ( '. join(' OR ', map {
259 my %ph = ( 'county' => dbh->quote($_),
260 map { $_ => dbh->quote( $cgi->param($_) ) }
261 qw( district city state country )
264 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
265 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
266 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
275 push @where, $locs_sql;
280 foreach (@loc_param) {
281 if ( length($cgi->param($_)) ) {
282 my $quoted = dbh->quote($cgi->param($_));
283 push @loc_where, "(COALESCE(cust_location.$_, '') = $quoted)";
286 my $loc_sql = join(' AND ', @loc_where);
288 #my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
290 #my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
291 #while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
292 # $loc_sql =~ s/\?/$ph{shift(@param)}/e;
295 push @where, $loc_sql;
299 if ( $cgi->param('istax') ) {
300 $title = 'Tax credits';
301 $name = 'tax credits';
302 if ( $cgi->param('taxname') ) {
303 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
304 #} elsif ( $cgi->param('taxnameNULL') {
306 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
308 } elsif ( $cgi->param('nottax') ) {
309 $title = 'Credit applications to line items';
310 $name = 'applications';
311 #what can we usefully do with "taxname" ???? look up a class???
313 $title = 'Credit applications to line items';
314 $name = 'applications';
315 #warn "neither nottax nor istax parameters specified";
318 if ( $cgi->param('taxclassNULL')
319 && ! $cgi->param('istax') #no taxclass in this case
323 my %hash = ( 'country' => scalar($cgi->param('country')) );
324 foreach (qw( state county )) {
325 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
327 my $cust_main_county = qsearchs('cust_main_county', \%hash);
328 die "unknown base region for empty taxclass" unless $cust_main_county;
330 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
331 push @where, $same_sql if $same_sql;
335 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
337 # this should really be shoved out to FS::cust_pkg->location_sql or something
338 # along with the code in report_newtax.cgi
341 'county' => 'tax_rate_location.county',
342 'state' => 'tax_rate_location.state',
343 'city' => 'tax_rate_location.city',
344 'locationtaxid' => 'cust_bill_pkg_tax_rate_location.locationtaxid',
347 my %ph = map { ( $pn{$_} => dbh->quote( $cgi->param($_) || '' ) ) }
348 qw( city county state locationtaxid );
351 join( ' AND ', map { "( $_ = $ph{$_} OR $ph{$_} = '' AND $_ IS NULL)" }
357 if ( $cgi->param('itemdesc') ) {
358 if ( $cgi->param('itemdesc') eq 'Tax' ) {
359 push @where, "(itemdesc='Tax' OR itemdesc is null)";
361 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
365 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
366 my ( $group_op, $group_value ) = ( $1, $2 );
367 if ( $group_op eq '=' ) {
368 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
369 push @where, 'itemdesc = '. dbh->quote($group_value);
370 } elsif ( $group_op eq '!=' ) {
371 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
373 die "guru meditation #00de: group_op $group_op\n";
378 push @where, '(cust_bill_pkg.pkgnum != 0 OR cust_bill_pkg.feepart is not null)' if $cgi->param('nottax');
379 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
381 if ( $cgi->param('cust_tax') ) {
382 #false laziness -ish w/report_tax.cgi
384 if ( $cgi->param('taxname') ) {
385 my $q_taxname = dbh->quote($cgi->param('taxname'));
388 OR EXISTS ( SELECT 1 FROM cust_main_exemption
389 WHERE cust_main_exemption.custnum = cust_main.custnum
390 AND cust_main_exemption.taxname = $q_taxname )
394 $cust_exempt = " tax = 'Y' ";
397 push @where, $cust_exempt;
400 my $count_query = "SELECT COUNT(DISTINCT creditbillpkgnum),
401 SUM(cust_credit_bill_pkg.amount)";
402 if ( $cgi->param('nottax') ) {
403 $count_query .= ", SUM(exempt_credited)";
407 ' JOIN cust_bill ON ( cust_bill_pkg.invnum = cust_bill.invnum )'.
408 FS::UI::Web::join_cust_main('cust_bill', 'cust_pkg');
413 my $join_cust_bill_pkg = 'LEFT JOIN cust_bill_pkg USING ( billpkgnum )';
415 if ( $cgi->param('nottax') ) {
417 # There can be multiple cust_tax_exempt_pkg records with the same
418 # creditbillpkgnum iff the line item is exempt from multiple taxes.
419 # They will all have the same amount, except in the case where there are
420 # different exemption types and so the exemption amounts are different.
421 # In that case, show the amount of the largest exemption.
423 $join_cust_bill_pkg .= '
425 SELECT creditbillpkgnum,
426 MAX(0 - cust_tax_exempt_pkg.amount) AS exempt_credited
427 FROM cust_tax_exempt_pkg
428 WHERE creditbillpkgnum IS NOT NULL
429 GROUP BY creditbillpkgnum
430 ) AS exempt_credit USING (creditbillpkgnum)
432 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
433 LEFT JOIN part_pkg USING ( pkgpart )
434 LEFT JOIN part_pkg AS override
435 ON pkgpart_override = override.pkgpart ';
436 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
437 if $conf->exists('tax-pkg_address');
439 } elsif ( $cgi->param('istax') ) {
441 #false laziness w/report_tax.cgi $taxfromwhere
442 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
443 $cgi->param('iscredit') eq 'rate') {
446 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ';
447 if ( $cgi->param('iscredit') eq 'rate' ) {
448 $join_pkg .= ', billpkgtaxratelocationnum )';
449 } elsif ( $conf->exists('tax-pkg_address') ) {
450 $join_pkg .= ', billpkgtaxlocationnum )';
451 push @where, "billpkgtaxratelocationnum IS NULL";
454 push @where, "billpkgtaxratelocationnum IS NULL";
457 $join_pkg .= ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
459 } elsif ( $conf->exists('tax-pkg_address') ) {
461 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
462 LEFT JOIN cust_location USING ( locationnum ) ';
464 #quelle kludge, somewhat false laziness w/report_tax.cgi
465 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
470 #warn "neither nottax nor istax parameters specified";
472 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
473 LEFT JOIN part_pkg USING ( pkgpart ) ';
477 my $where = ' WHERE '. join(' AND ', @where);
479 my $join_credit = ' LEFT JOIN cust_credit_bill USING ( creditbillnum )
480 LEFT JOIN cust_credit USING ( crednum ) ';
482 $count_query .= " FROM cust_credit_bill_pkg
489 my @select = ( 'cust_credit_bill_pkg.*',
491 'cust_credit.otaker',
492 'cust_credit._date AS cust_credit_date',
495 push @select, 'part_pkg.pkg' unless $cgi->param('istax');
496 push @select, 'cust_main.custnum',
497 FS::UI::Web::cust_sql_fields();
499 if ( $cgi->param('istax') ) {
500 push @select, 'NULL AS exempt_credited'; # just display zero
501 } elsif ( $cgi->param('nottax') ) {
502 push @select, 'exempt_credited';
505 my @post_desc_header = ();
507 my @post_desc_null = ();
508 my $post_desc_align = '';
509 if ( $conf->exists('enable_taxclasses') && ! $cgi->param('istax') ) {
510 push @post_desc_header, 'Tax class';
511 push @post_desc, 'taxclass';
512 push @post_desc_null, '';
513 $post_desc_align .= 'l';
514 push @select, 'part_pkg.taxclass'; # or should this use override?
518 'table' => 'cust_credit_bill_pkg',
519 'addl_from' => "$join_cust_bill_pkg
524 'select' => join(', ', @select ),
525 'extra_sql' => $where,
526 'order_by' => 'ORDER BY creditbillpkgnum', #cust_bill. or cust_credit._date?
529 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
530 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
532 my $conf = new FS::Conf;
533 my $money_char = $conf->config('money_char') || '$';
535 my $tax_pkg_address = $conf->exists('tax-pkg_address');
536 my $tax_ship_address = $conf->exists('tax-ship_address');
538 my $location_sub = sub {
539 #my $cust_credit_bill_pkg = shift;
541 my $tax_Xlocation = $self->cust_bill_pkg_tax_Xlocation;
542 if ( defined($tax_Xlocation) && $tax_Xlocation ) {
544 if ( ref($tax_Xlocation) eq 'FS::cust_bill_pkg_tax_location' ) {
546 if ( $tax_Xlocation->taxtype eq 'FS::cust_main_county' ) {
547 my $cust_main_county = $tax_Xlocation->cust_main_county;
548 if ( $cust_main_county ) {
549 $cust_main_county->label;
551 ''; #cust_main_county record is gone... history? yuck.
554 '(CCH tax_rate)'; #XXX FS::tax_rate.. vendor taxes not yet handled here
557 } elsif ( ref($tax_Xlocation) eq 'FS::cust_bill_pkg_tax_rate_location' ) {
558 '(CCH)'; #XXX vendor taxes not yet handled here
560 'unknown tax_Xlocation '. ref($tax_Xlocation);
565 my $cust_bill_pkg = $self->cust_bill_pkg;
566 if ( $cust_bill_pkg->pkgnum > 0 ) {
567 my $cust_pkg = $cust_bill_pkg->cust_pkg;
568 if ( $tax_pkg_address && (my $cust_location = $cust_pkg->cust_location) ){
569 $cust_location->county_state_country;
571 my $cust_main = $cust_pkg->cust_main;
572 if ( $tax_ship_address && $cust_main->has_ship_address ) {
573 $cust_main->county_state_country('ship_');
575 $cust_main->county_state_country;
580 #tax? we shouldn't have wound up here then...
581 ''; #return customer ship or bill address? (depending on tax-ship_address)
588 my @count_addl = ( $money_char. '%.2f total', );
589 if ( $cgi->param('nottax') ) {
590 push @count_addl, ( $money_char. '%.2f tax exempt' );
593 if ( $cgi->param('debug') ) {
594 warn "\nQUERY:\n" . Dumper($query) . "\nCOUNT_QUERY:\n$count_query\n\n";