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,
27 FS::UI::Web::cust_header(),
31 sub { sprintf($money_char.'%.2f', shift->amount ) },
33 sub { sprintf($money_char.'%.2f', shift->get('exempt_credited') ) },
35 sub { time2str('%b %d %Y', shift->get('cust_credit_date') ) },
36 sub { shift->cust_credit_bill->cust_credit->otaker },
37 sub { shift->cust_credit_bill->cust_credit->reason_only },
38 sub { shift->cust_credit_bill->cust_credit->addlinfo },
40 sub { $_[0]->pkgnum > 0
41 ? $_[0]->get('pkg') # possibly use override.pkg
42 : $_[0]->get('itemdesc') # but i think this correct
47 sub { time2str('%b %d %Y', shift->_date ) },
48 \&FS::UI::Web::cust_fields,
57 '', #line item description
76 ( map { $_ ne 'Cust. Status' ? $clink : '' }
77 FS::UI::Web::cust_header()
80 'align' => 'rrrlllll'.
83 FS::UI::Web::cust_aligns(),
96 FS::UI::Web::cust_colors(),
110 FS::UI::Web::cust_styles(),
116 #LOTS of false laziness below w/cust_bill_pkg.cgi
117 # and a little w/cust_credit.html
120 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
122 my $conf = new FS::Conf;
124 #here is the agent virtualization
126 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
128 my @where = ( $agentnums_sql );
130 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
131 push @where, "cust_credit.usernum = $1";
134 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
135 push @where, "cust_bill._date >= $beginning",
136 "cust_bill._date <= $ending";
138 my($cr_begin, $cr_end) = FS::UI::Web::parse_beginning_ending($cgi, 'credit');
139 push @where, "cust_credit._date >= $cr_begin",
140 "cust_credit._date <= $cr_end";
142 #credit amount? seems more what is expected than the applied amount
143 my @lt_gt = FS::UI::Web::parse_lt_gt($cgi, 'amount' );
144 s/amount/cust_credit.amount/g foreach (@lt_gt);
147 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
148 push @where, "cust_main.agentnum = $1";
151 if ( $cgi->param('billpkgnum') =~ /^(\d+)$/ ) {
152 push @where, "billpkgnum = $1";
156 # not specified: all classes
159 my $use_override = $cgi->param('use_override');
160 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
163 $comparison = "IS NULL";
165 $comparison = "= $1";
168 if ( $use_override ) {
170 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
171 override.classnum $comparison AND pkgpart_override IS NOT NULL
174 push @where, "part_pkg.classnum $comparison";
178 if ( $cgi->param('taxclass')
179 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
180 #(should we save a taxclass or a link to taxnum
181 # in cust_bill_pkg or something like
182 # cust_bill_pkg_tax_location?)
186 #override taxclass when use_override is specified? probably
187 #if ( $use_override ) {
190 # ' ( '. join(' OR ',
192 # ' ( part_pkg.taxclass = '. dbh->quote($_).
193 # ' AND pkgpart_override IS NULL '.
195 # ' override.taxclass = '. dbh->quote($_).
196 # ' AND pkgpart_override IS NOT NULL '.
199 # $cgi->param('taxclass')
207 map ' part_pkg.taxclass = '.dbh->quote($_),
208 $cgi->param('taxclass')
216 my @loc_param = qw( district city county state country );
218 if ( $cgi->param('out') ) {
220 my $has_taxname = '';
221 if ( $cgi->param('taxname') ) {
222 $has_taxname = " AND COALESCE(cust_main_county.taxname, 'Tax') = "
223 . dbh->quote( $cgi->param('taxname') );
226 # use the exact criteria from the tax report
229 SELECT 1 FROM cust_tax_exempt_pkg
230 JOIN cust_main_county USING (taxnum)
231 WHERE cust_tax_exempt_pkg.billpkgnum = cust_bill_pkg.billpkgnum
232 AND cust_tax_exempt_pkg.creditbillpkgnum IS NULL
236 SELECT 1 FROM cust_bill_pkg_tax_location
237 JOIN cust_main_county USING (taxnum)
238 WHERE cust_bill_pkg_tax_location.taxable_billpkgnum = cust_bill_pkg.billpkgnum
242 SELECT 1 FROM cust_bill_pkg_tax_location
243 JOIN cust_main_county USING (taxnum)
244 WHERE cust_bill_pkg_tax_location.billpkgnum = cust_bill_pkg.billpkgnum
248 } elsif ( $cgi->param('country') ) {
250 my @counties = $cgi->param('county');
252 if ( scalar(@counties) > 1 ) {
254 #hacky, could be more efficient. care if it is ever used for more than the
255 # tax-report_groups filtering kludge
256 # (does that even still exist? if so, correct this (or location_sql itself)
257 # to work with modern cust_location links)
260 ' ( '. join(' OR ', map {
262 my %ph = ( 'county' => dbh->quote($_),
263 map { $_ => dbh->quote( $cgi->param($_) ) }
264 qw( district city state country )
267 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
268 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
269 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
278 push @where, $locs_sql;
283 foreach (@loc_param) {
284 if ( length($cgi->param($_)) ) {
285 my $quoted = dbh->quote($cgi->param($_));
286 push @loc_where, "(COALESCE(cust_location.$_, '') = $quoted)";
289 my $loc_sql = join(' AND ', @loc_where);
291 #my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
293 #my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
294 #while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
295 # $loc_sql =~ s/\?/$ph{shift(@param)}/e;
298 push @where, $loc_sql;
302 if ( $cgi->param('istax') ) {
303 $title = 'Tax credits';
304 $name = 'tax credits';
305 if ( $cgi->param('taxname') ) {
306 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
307 #} elsif ( $cgi->param('taxnameNULL') {
309 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
311 } elsif ( $cgi->param('nottax') ) {
312 $title = 'Credit applications to line items';
313 $name = 'applications';
314 #what can we usefully do with "taxname" ???? look up a class???
316 $title = 'Credit applications to line items';
317 $name = 'applications';
318 #warn "neither nottax nor istax parameters specified";
321 if ( $cgi->param('taxclassNULL')
322 && ! $cgi->param('istax') #no taxclass in this case
326 my %hash = ( 'country' => scalar($cgi->param('country')) );
327 foreach (qw( state county )) {
328 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
330 my $cust_main_county = qsearchs('cust_main_county', \%hash);
331 die "unknown base region for empty taxclass" unless $cust_main_county;
333 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
334 push @where, $same_sql if $same_sql;
338 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
340 # this should really be shoved out to FS::cust_pkg->location_sql or something
341 # along with the code in report_newtax.cgi
344 'county' => 'tax_rate_location.county',
345 'state' => 'tax_rate_location.state',
346 'city' => 'tax_rate_location.city',
347 'locationtaxid' => 'cust_bill_pkg_tax_rate_location.locationtaxid',
350 my %ph = map { ( $pn{$_} => dbh->quote( $cgi->param($_) || '' ) ) }
351 qw( city county state locationtaxid );
354 join( ' AND ', map { "( $_ = $ph{$_} OR $ph{$_} = '' AND $_ IS NULL)" }
360 if ( $cgi->param('itemdesc') ) {
361 if ( $cgi->param('itemdesc') eq 'Tax' ) {
362 push @where, "(itemdesc='Tax' OR itemdesc is null)";
364 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
368 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
369 my ( $group_op, $group_value ) = ( $1, $2 );
370 if ( $group_op eq '=' ) {
371 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
372 push @where, 'itemdesc = '. dbh->quote($group_value);
373 } elsif ( $group_op eq '!=' ) {
374 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
376 die "guru meditation #00de: group_op $group_op\n";
381 push @where, '(cust_bill_pkg.pkgnum != 0 OR cust_bill_pkg.feepart is not null)' if $cgi->param('nottax');
382 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
384 if ( $cgi->param('cust_tax') ) {
385 #false laziness -ish w/report_tax.cgi
387 if ( $cgi->param('taxname') ) {
388 my $q_taxname = dbh->quote($cgi->param('taxname'));
391 OR EXISTS ( SELECT 1 FROM cust_main_exemption
392 WHERE cust_main_exemption.custnum = cust_main.custnum
393 AND cust_main_exemption.taxname = $q_taxname )
397 $cust_exempt = " tax = 'Y' ";
400 push @where, $cust_exempt;
403 my $count_query = "SELECT COUNT(DISTINCT creditbillpkgnum),
404 SUM(cust_credit_bill_pkg.amount)";
405 if ( $cgi->param('nottax') ) {
406 $count_query .= ", SUM(exempt_credited)";
410 ' JOIN cust_bill ON ( cust_bill_pkg.invnum = cust_bill.invnum )'.
411 FS::UI::Web::join_cust_main('cust_bill', 'cust_pkg');
416 my $join_cust_bill_pkg = 'LEFT JOIN cust_bill_pkg USING ( billpkgnum )';
418 if ( $cgi->param('nottax') ) {
420 # There can be multiple cust_tax_exempt_pkg records with the same
421 # creditbillpkgnum iff the line item is exempt from multiple taxes.
422 # They will all have the same amount, except in the case where there are
423 # different exemption types and so the exemption amounts are different.
424 # In that case, show the amount of the largest exemption.
426 $join_cust_bill_pkg .= '
428 SELECT creditbillpkgnum,
429 MAX(0 - cust_tax_exempt_pkg.amount) AS exempt_credited
430 FROM cust_tax_exempt_pkg
431 WHERE creditbillpkgnum IS NOT NULL
432 GROUP BY creditbillpkgnum
433 ) AS exempt_credit USING (creditbillpkgnum)
435 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
436 LEFT JOIN part_pkg USING ( pkgpart )
437 LEFT JOIN part_pkg AS override
438 ON pkgpart_override = override.pkgpart ';
439 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
440 if $conf->exists('tax-pkg_address');
442 } elsif ( $cgi->param('istax') ) {
444 #false laziness w/report_tax.cgi $taxfromwhere
445 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
446 $cgi->param('iscredit') eq 'rate') {
449 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ';
450 if ( $cgi->param('iscredit') eq 'rate' ) {
451 $join_pkg .= ', billpkgtaxratelocationnum )';
452 } elsif ( $conf->exists('tax-pkg_address') ) {
453 $join_pkg .= ', billpkgtaxlocationnum )';
454 push @where, "billpkgtaxratelocationnum IS NULL";
457 push @where, "billpkgtaxratelocationnum IS NULL";
460 $join_pkg .= ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
462 } elsif ( $conf->exists('tax-pkg_address') ) {
464 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
465 LEFT JOIN cust_location USING ( locationnum ) ';
467 #quelle kludge, somewhat false laziness w/report_tax.cgi
468 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
473 #warn "neither nottax nor istax parameters specified";
475 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
476 LEFT JOIN part_pkg USING ( pkgpart ) ';
480 my $where = ' WHERE '. join(' AND ', @where);
482 my $join_credit = ' LEFT JOIN cust_credit_bill USING ( creditbillnum )
483 LEFT JOIN cust_credit USING ( crednum ) ';
485 $count_query .= " FROM cust_credit_bill_pkg
492 my @select = ( 'cust_credit_bill_pkg.*',
494 'cust_credit.otaker',
495 'cust_credit._date AS cust_credit_date',
498 push @select, 'part_pkg.pkg' unless $cgi->param('istax');
499 push @select, 'cust_main.custnum',
500 FS::UI::Web::cust_sql_fields();
502 if ( $cgi->param('istax') ) {
503 push @select, 'NULL AS exempt_credited'; # just display zero
504 } elsif ( $cgi->param('nottax') ) {
505 push @select, 'exempt_credited';
508 my @post_desc_header = ();
510 my @post_desc_null = ();
511 my $post_desc_align = '';
512 if ( $conf->exists('enable_taxclasses') && ! $cgi->param('istax') ) {
513 push @post_desc_header, 'Tax class';
514 push @post_desc, 'taxclass';
515 push @post_desc_null, '';
516 $post_desc_align .= 'l';
517 push @select, 'part_pkg.taxclass'; # or should this use override?
521 'table' => 'cust_credit_bill_pkg',
522 'addl_from' => "$join_cust_bill_pkg
527 'select' => join(', ', @select ),
528 'extra_sql' => $where,
529 'order_by' => 'ORDER BY creditbillpkgnum', #cust_bill. or cust_credit._date?
532 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
533 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
535 my $conf = new FS::Conf;
536 my $money_char = $conf->config('money_char') || '$';
538 my $tax_pkg_address = $conf->exists('tax-pkg_address');
539 my $tax_ship_address = $conf->exists('tax-ship_address');
541 my $location_sub = sub {
542 #my $cust_credit_bill_pkg = shift;
544 my $tax_Xlocation = $self->cust_bill_pkg_tax_Xlocation;
545 if ( defined($tax_Xlocation) && $tax_Xlocation ) {
547 if ( ref($tax_Xlocation) eq 'FS::cust_bill_pkg_tax_location' ) {
549 if ( $tax_Xlocation->taxtype eq 'FS::cust_main_county' ) {
550 my $cust_main_county = $tax_Xlocation->cust_main_county;
551 if ( $cust_main_county ) {
552 $cust_main_county->label;
554 ''; #cust_main_county record is gone... history? yuck.
557 '(CCH tax_rate)'; #XXX FS::tax_rate.. vendor taxes not yet handled here
560 } elsif ( ref($tax_Xlocation) eq 'FS::cust_bill_pkg_tax_rate_location' ) {
561 '(CCH)'; #XXX vendor taxes not yet handled here
563 'unknown tax_Xlocation '. ref($tax_Xlocation);
568 my $cust_bill_pkg = $self->cust_bill_pkg;
569 if ( $cust_bill_pkg->pkgnum > 0 ) {
570 my $cust_pkg = $cust_bill_pkg->cust_pkg;
571 if ( $tax_pkg_address && (my $cust_location = $cust_pkg->cust_location) ){
572 $cust_location->county_state_country;
574 my $cust_main = $cust_pkg->cust_main;
575 if ( $tax_ship_address && $cust_main->has_ship_address ) {
576 $cust_main->county_state_country('ship_');
578 $cust_main->county_state_country;
583 #tax? we shouldn't have wound up here then...
584 ''; #return customer ship or bill address? (depending on tax-ship_address)
591 my @count_addl = ( $money_char. '%.2f total', );
592 if ( $cgi->param('nottax') ) {
593 push @count_addl, ( $money_char. '%.2f tax exempt' );
596 if ( $cgi->param('debug') ) {
597 warn "\nQUERY:\n" . Dumper($query) . "\nCOUNT_QUERY:\n$count_query\n\n";