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' => [ $money_char. '%.2f total', ],
25 FS::UI::Web::cust_header(),
29 sub { sprintf($money_char.'%.2f', shift->amount ) },
31 sub { time2str('%b %d %Y', shift->get('cust_credit_date') ) },
32 sub { shift->cust_credit_bill->cust_credit->otaker },
33 sub { shift->cust_credit_bill->cust_credit->reason },
35 sub { $_[0]->pkgnum > 0
36 ? $_[0]->get('pkg') # possibly use override.pkg
37 : $_[0]->get('itemdesc') # but i think this correct
42 sub { time2str('%b %d %Y', shift->_date ) },
43 \&FS::UI::Web::cust_fields,
50 '', #line item description
67 ( map { $_ ne 'Cust. Status' ? $clink : '' }
68 FS::UI::Web::cust_header()
74 FS::UI::Web::cust_aligns(),
85 FS::UI::Web::cust_colors(),
97 FS::UI::Web::cust_styles(),
103 #LOTS of false laziness below w/cust_bill_pkg.cgi
104 # and a little w/cust_credit.html
107 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
109 my $conf = new FS::Conf;
111 #here is the agent virtualization
113 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
115 my @where = ( $agentnums_sql );
117 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
118 push @where, "cust_credit.usernum = $1";
121 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
122 push @where, "cust_bill._date >= $beginning",
123 "cust_bill._date <= $ending";
125 my($cr_begin, $cr_end) = FS::UI::Web::parse_beginning_ending($cgi, 'credit');
126 push @where, "cust_credit._date >= $cr_begin",
127 "cust_credit._date <= $cr_end";
129 #credit amount? seems more what is expected than the applied amount
130 my @lt_gt = FS::UI::Web::parse_lt_gt($cgi, 'amount' );
131 s/amount/cust_credit.amount/g foreach (@lt_gt);
134 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
135 push @where, "cust_main.agentnum = $1";
138 if ( $cgi->param('billpkgnum') =~ /^(\d+)$/ ) {
139 push @where, "billpkgnum = $1";
143 # not specified: all classes
146 my $use_override = $cgi->param('use_override');
147 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
150 $comparison = "IS NULL";
152 $comparison = "= $1";
155 if ( $use_override ) {
157 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
158 override.classnum $comparison AND pkgpart_override IS NOT NULL
161 push @where, "part_pkg.classnum $comparison";
165 if ( $cgi->param('taxclass')
166 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
167 #(should we save a taxclass or a link to taxnum
168 # in cust_bill_pkg or something like
169 # cust_bill_pkg_tax_location?)
173 #override taxclass when use_override is specified? probably
174 #if ( $use_override ) {
177 # ' ( '. join(' OR ',
179 # ' ( part_pkg.taxclass = '. dbh->quote($_).
180 # ' AND pkgpart_override IS NULL '.
182 # ' override.taxclass = '. dbh->quote($_).
183 # ' AND pkgpart_override IS NOT NULL '.
186 # $cgi->param('taxclass')
194 map ' part_pkg.taxclass = '.dbh->quote($_),
195 $cgi->param('taxclass')
203 my @loc_param = qw( district city county state country );
205 if ( $cgi->param('out') ) {
206 # probably don't need this part
208 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
209 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
210 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
213 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
214 if $cgi->param('istax');
218 SELECT COUNT(*) FROM cust_main_county
219 WHERE cust_main_county.tax > 0
224 #not linked to by anything, but useful for debugging "out of taxable region"
225 if ( grep $cgi->param($_), @loc_param ) {
227 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
229 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
230 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
231 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
234 push @where, $loc_sql;
238 } elsif ( $cgi->param('country') ) {
240 my @counties = $cgi->param('county');
242 if ( scalar(@counties) > 1 ) {
244 #hacky, could be more efficient. care if it is ever used for more than the
245 # tax-report_groups filtering kludge
246 # (does that even still exist? if so, correct this (or location_sql itself)
247 # to work with modern cust_location links)
250 ' ( '. join(' OR ', map {
252 my %ph = ( 'county' => dbh->quote($_),
253 map { $_ => dbh->quote( $cgi->param($_) ) }
254 qw( district city state country )
257 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
258 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
259 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
268 push @where, $locs_sql;
273 foreach (@loc_param) {
274 if ( length($cgi->param($_)) ) {
275 my $quoted = dbh->quote($cgi->param($_));
276 push @loc_where, "(COALESCE(cust_location.$_, '') = $quoted)";
279 my $loc_sql = join(' AND ', @loc_where);
281 #my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
283 #my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
284 #while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
285 # $loc_sql =~ s/\?/$ph{shift(@param)}/e;
288 push @where, $loc_sql;
293 if ( $cgi->param('istax') ) {
294 $title = 'Tax credits';
295 $name = 'tax credits';
296 if ( $cgi->param('taxname') ) {
297 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
298 #} elsif ( $cgi->param('taxnameNULL') {
300 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
302 } elsif ( $cgi->param('nottax') ) {
303 $title = 'Credit applications to line items';
304 $name = 'applications';
305 #what can we usefully do with "taxname" ???? look up a class???
307 $title = 'Credit applications to line items';
308 $name = 'applications';
309 #warn "neither nottax nor istax parameters specified";
312 if ( $cgi->param('taxclassNULL')
313 && ! $cgi->param('istax') #no taxclass in this case
317 my %hash = ( 'country' => scalar($cgi->param('country')) );
318 foreach (qw( state county )) {
319 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
321 my $cust_main_county = qsearchs('cust_main_county', \%hash);
322 die "unknown base region for empty taxclass" unless $cust_main_county;
324 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
325 push @where, $same_sql if $same_sql;
329 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
331 # this should really be shoved out to FS::cust_pkg->location_sql or something
332 # along with the code in report_newtax.cgi
335 'county' => 'tax_rate_location.county',
336 'state' => 'tax_rate_location.state',
337 'city' => 'tax_rate_location.city',
338 'locationtaxid' => 'cust_bill_pkg_tax_rate_location.locationtaxid',
341 my %ph = map { ( $pn{$_} => dbh->quote( $cgi->param($_) || '' ) ) }
342 qw( city county state locationtaxid );
345 join( ' AND ', map { "( $_ = $ph{$_} OR $ph{$_} = '' AND $_ IS NULL)" }
351 if ( $cgi->param('itemdesc') ) {
352 if ( $cgi->param('itemdesc') eq 'Tax' ) {
353 push @where, "(itemdesc='Tax' OR itemdesc is null)";
355 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
359 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
360 my ( $group_op, $group_value ) = ( $1, $2 );
361 if ( $group_op eq '=' ) {
362 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
363 push @where, 'itemdesc = '. dbh->quote($group_value);
364 } elsif ( $group_op eq '!=' ) {
365 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
367 die "guru meditation #00de: group_op $group_op\n";
372 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
373 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
375 if ( $cgi->param('cust_tax') ) {
376 #false laziness -ish w/report_tax.cgi
378 if ( $cgi->param('taxname') ) {
379 my $q_taxname = dbh->quote($cgi->param('taxname'));
382 OR EXISTS ( SELECT 1 FROM cust_main_exemption
383 WHERE cust_main_exemption.custnum = cust_main.custnum
384 AND cust_main_exemption.taxname = $q_taxname )
388 $cust_exempt = " tax = 'Y' ";
391 push @where, $cust_exempt;
394 my $count_query = "SELECT COUNT(DISTINCT creditbillpkgnum),
395 SUM(cust_credit_bill_pkg.amount)";
398 ' JOIN cust_bill ON ( cust_bill_pkg.invnum = cust_bill.invnum )'.
399 FS::UI::Web::join_cust_main('cust_bill', 'cust_pkg');
404 my $join_cust_bill_pkg = 'LEFT JOIN cust_bill_pkg USING ( billpkgnum )';
406 if ( $cgi->param('nottax') ) {
408 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
409 LEFT JOIN part_pkg USING ( pkgpart )
410 LEFT JOIN part_pkg AS override
411 ON pkgpart_override = override.pkgpart ';
412 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
413 if $conf->exists('tax-pkg_address');
415 } elsif ( $cgi->param('istax') ) {
417 #false laziness w/report_tax.cgi $taxfromwhere
418 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
419 $cgi->param('iscredit') eq 'rate') {
422 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ';
423 if ( $cgi->param('iscredit') eq 'rate' ) {
424 $join_pkg .= ', billpkgtaxratelocationnum )';
425 } elsif ( $conf->exists('tax-pkg_address') ) {
426 $join_pkg .= ', billpkgtaxlocationnum )';
427 push @where, "billpkgtaxratelocationnum IS NULL";
430 push @where, "billpkgtaxratelocationnum IS NULL";
433 $join_pkg .= ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
435 } elsif ( $conf->exists('tax-pkg_address') ) {
437 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
438 LEFT JOIN cust_location USING ( locationnum ) ';
440 #quelle kludge, somewhat false laziness w/report_tax.cgi
441 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
446 #warn "neither nottax nor istax parameters specified";
448 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
449 LEFT JOIN part_pkg USING ( pkgpart ) ';
453 my $where = ' WHERE '. join(' AND ', @where);
455 my $join_credit = ' LEFT JOIN cust_credit_bill USING ( creditbillnum )
456 LEFT JOIN cust_credit USING ( crednum ) ';
458 $count_query .= " FROM cust_credit_bill_pkg
465 my @select = ( 'cust_credit_bill_pkg.*',
467 'cust_credit.otaker',
468 'cust_credit._date AS cust_credit_date',
471 push @select, 'part_pkg.pkg' unless $cgi->param('istax');
472 push @select, 'cust_main.custnum',
473 FS::UI::Web::cust_sql_fields();
475 my @post_desc_header = ();
477 my @post_desc_null = ();
478 my $post_desc_align = '';
479 if ( $conf->exists('enable_taxclasses') && ! $cgi->param('istax') ) {
480 push @post_desc_header, 'Tax class';
481 push @post_desc, 'taxclass';
482 push @post_desc_null, '';
483 $post_desc_align .= 'l';
484 push @select, 'part_pkg.taxclass'; # or should this use override?
488 'table' => 'cust_credit_bill_pkg',
489 'addl_from' => "$join_cust_bill_pkg
494 'select' => join(', ', @select ),
495 'extra_sql' => $where,
496 'order_by' => 'ORDER BY creditbillpkgnum', #cust_bill. or cust_credit._date?
499 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
500 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
502 my $conf = new FS::Conf;
503 my $money_char = $conf->config('money_char') || '$';
505 my $tax_pkg_address = $conf->exists('tax-pkg_address');
506 my $tax_ship_address = $conf->exists('tax-ship_address');
508 my $location_sub = sub {
509 #my $cust_credit_bill_pkg = shift;
511 my $tax_Xlocation = $self->cust_bill_pkg_tax_Xlocation;
512 if ( defined($tax_Xlocation) && $tax_Xlocation ) {
514 if ( ref($tax_Xlocation) eq 'FS::cust_bill_pkg_tax_location' ) {
516 if ( $tax_Xlocation->taxtype eq 'FS::cust_main_county' ) {
517 my $cust_main_county = $tax_Xlocation->cust_main_county;
518 if ( $cust_main_county ) {
519 $cust_main_county->label;
521 ''; #cust_main_county record is gone... history? yuck.
524 '(CCH tax_rate)'; #XXX FS::tax_rate.. vendor taxes not yet handled here
527 } elsif ( ref($tax_Xlocation) eq 'FS::cust_bill_pkg_tax_rate_location' ) {
528 '(CCH)'; #XXX vendor taxes not yet handled here
530 'unknown tax_Xlocation '. ref($tax_Xlocation);
535 my $cust_bill_pkg = $self->cust_bill_pkg;
536 if ( $cust_bill_pkg->pkgnum > 0 ) {
537 my $cust_pkg = $cust_bill_pkg->cust_pkg;
538 if ( $tax_pkg_address && (my $cust_location = $cust_pkg->cust_location) ){
539 $cust_location->county_state_country;
541 my $cust_main = $cust_pkg->cust_main;
542 if ( $tax_ship_address && $cust_main->has_ship_address ) {
543 $cust_main->county_state_country('ship_');
545 $cust_main->county_state_country;
550 #tax? we shouldn't have wound up here then...
551 ''; #return customer ship or bill address? (depending on tax-ship_address)