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') ) {
207 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
208 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
209 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
212 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
213 if $cgi->param('istax');
217 SELECT COUNT(*) FROM cust_main_county
218 WHERE cust_main_county.tax > 0
223 #not linked to by anything, but useful for debugging "out of taxable region"
224 if ( grep $cgi->param($_), @loc_param ) {
226 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
228 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
229 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
230 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
233 push @where, $loc_sql;
237 } elsif ( $cgi->param('country') ) {
239 my @counties = $cgi->param('county');
241 if ( scalar(@counties) > 1 ) {
243 #hacky, could be more efficient. care if it is ever used for more than the
244 # tax-report_groups filtering kludge
247 ' ( '. join(' OR ', map {
249 my %ph = ( 'county' => dbh->quote($_),
250 map { $_ => dbh->quote( $cgi->param($_) ) }
251 qw( district city state country )
254 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
255 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
256 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
265 push @where, $locs_sql;
269 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
271 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
272 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
273 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
276 push @where, $loc_sql;
281 if ( $cgi->param('istax') ) {
282 $title = 'Tax credits';
283 $name = 'tax credits';
284 if ( $cgi->param('taxname') ) {
285 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
286 #} elsif ( $cgi->param('taxnameNULL') {
288 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
290 } elsif ( $cgi->param('nottax') ) {
291 $title = 'Credit applications to line items';
292 $name = 'applications';
293 #what can we usefully do with "taxname" ???? look up a class???
295 $title = 'Credit applications to line items';
296 $name = 'applications';
297 #warn "neither nottax nor istax parameters specified";
300 if ( $cgi->param('taxclassNULL')
301 && ! $cgi->param('istax') #no taxclass in this case
305 my %hash = ( 'country' => scalar($cgi->param('country')) );
306 foreach (qw( state county )) {
307 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
309 my $cust_main_county = qsearchs('cust_main_county', \%hash);
310 die "unknown base region for empty taxclass" unless $cust_main_county;
312 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
313 push @where, $same_sql if $same_sql;
317 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
319 # this should really be shoved out to FS::cust_pkg->location_sql or something
320 # along with the code in report_newtax.cgi
323 'county' => 'tax_rate_location.county',
324 'state' => 'tax_rate_location.state',
325 'city' => 'tax_rate_location.city',
326 'locationtaxid' => 'cust_bill_pkg_tax_rate_location.locationtaxid',
329 my %ph = map { ( $pn{$_} => dbh->quote( $cgi->param($_) || '' ) ) }
330 qw( city county state locationtaxid );
333 join( ' AND ', map { "( $_ = $ph{$_} OR $ph{$_} = '' AND $_ IS NULL)" }
339 if ( $cgi->param('itemdesc') ) {
340 if ( $cgi->param('itemdesc') eq 'Tax' ) {
341 push @where, "(itemdesc='Tax' OR itemdesc is null)";
343 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
347 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
348 my ( $group_op, $group_value ) = ( $1, $2 );
349 if ( $group_op eq '=' ) {
350 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
351 push @where, 'itemdesc = '. dbh->quote($group_value);
352 } elsif ( $group_op eq '!=' ) {
353 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
355 die "guru meditation #00de: group_op $group_op\n";
360 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
361 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
363 if ( $cgi->param('cust_tax') ) {
364 #false laziness -ish w/report_tax.cgi
366 if ( $cgi->param('taxname') ) {
367 my $q_taxname = dbh->quote($cgi->param('taxname'));
370 OR EXISTS ( SELECT 1 FROM cust_main_exemption
371 WHERE cust_main_exemption.custnum = cust_main.custnum
372 AND cust_main_exemption.taxname = $q_taxname )
376 $cust_exempt = " tax = 'Y' ";
379 push @where, $cust_exempt;
382 my $count_query = "SELECT COUNT(DISTINCT creditbillpkgnum),
383 SUM(cust_credit_bill_pkg.amount)";
386 ' JOIN cust_bill ON ( cust_bill_pkg.invnum = cust_bill.invnum )'.
387 FS::UI::Web::join_cust_main('cust_bill', 'cust_pkg');
392 my $join_cust_bill_pkg = 'LEFT JOIN cust_bill_pkg USING ( billpkgnum )';
394 if ( $cgi->param('nottax') ) {
396 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
397 LEFT JOIN part_pkg USING ( pkgpart )
398 LEFT JOIN part_pkg AS override
399 ON pkgpart_override = override.pkgpart ';
400 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
401 if $conf->exists('tax-pkg_address');
403 } elsif ( $cgi->param('istax') ) {
405 #false laziness w/report_tax.cgi $taxfromwhere
406 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
407 $cgi->param('iscredit') eq 'rate') {
410 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ';
411 if ( $cgi->param('iscredit') eq 'rate' ) {
412 $join_pkg .= ', billpkgtaxratelocationnum )';
413 } elsif ( $conf->exists('tax-pkg_address') ) {
414 $join_pkg .= ', billpkgtaxlocationnum )';
415 push @where, "billpkgtaxratelocationnum IS NULL";
418 push @where, "billpkgtaxratelocationnum IS NULL";
421 $join_pkg .= ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
423 } elsif ( $conf->exists('tax-pkg_address') ) {
425 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
426 LEFT JOIN cust_location USING ( locationnum ) ';
428 #quelle kludge, somewhat false laziness w/report_tax.cgi
429 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
434 #warn "neither nottax nor istax parameters specified";
436 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
437 LEFT JOIN part_pkg USING ( pkgpart ) ';
441 my $where = ' WHERE '. join(' AND ', @where);
443 my $join_credit = ' LEFT JOIN cust_credit_bill USING ( creditbillnum )
444 LEFT JOIN cust_credit USING ( crednum ) ';
446 $count_query .= " FROM cust_credit_bill_pkg
453 my @select = ( 'cust_credit_bill_pkg.*',
455 'cust_credit.otaker',
456 'cust_credit._date AS cust_credit_date',
459 push @select, 'part_pkg.pkg' unless $cgi->param('istax');
460 push @select, 'cust_main.custnum',
461 FS::UI::Web::cust_sql_fields();
463 my @post_desc_header = ();
465 my @post_desc_null = ();
466 my $post_desc_align = '';
467 if ( $conf->exists('enable_taxclasses') && ! $cgi->param('istax') ) {
468 push @post_desc_header, 'Tax class';
469 push @post_desc, 'taxclass';
470 push @post_desc_null, '';
471 $post_desc_align .= 'l';
472 push @select, 'part_pkg.taxclass'; # or should this use override?
476 'table' => 'cust_credit_bill_pkg',
477 'addl_from' => "$join_cust_bill_pkg
482 'select' => join(', ', @select ),
483 'extra_sql' => $where,
484 'order_by' => 'ORDER BY creditbillpkgnum', #cust_bill. or cust_credit._date?
487 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
488 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
490 my $conf = new FS::Conf;
491 my $money_char = $conf->config('money_char') || '$';
493 my $tax_pkg_address = $conf->exists('tax-pkg_address');
494 my $tax_ship_address = $conf->exists('tax-ship_address');
496 my $location_sub = sub {
497 #my $cust_credit_bill_pkg = shift;
499 my $tax_Xlocation = $self->cust_bill_pkg_tax_Xlocation;
500 if ( defined($tax_Xlocation) && $tax_Xlocation ) {
502 if ( ref($tax_Xlocation) eq 'FS::cust_bill_pkg_tax_location' ) {
504 if ( $tax_Xlocation->taxtype eq 'FS::cust_main_county' ) {
505 my $cust_main_county = $tax_Xlocation->cust_main_county;
506 if ( $cust_main_county ) {
507 $cust_main_county->label;
509 ''; #cust_main_county record is gone... history? yuck.
512 '(CCH tax_rate)'; #XXX FS::tax_rate.. vendor taxes not yet handled here
515 } elsif ( ref($tax_Xlocation) eq 'FS::cust_bill_pkg_tax_rate_location' ) {
516 '(CCH)'; #XXX vendor taxes not yet handled here
518 'unknown tax_Xlocation '. ref($tax_Xlocation);
523 my $cust_bill_pkg = $self->cust_bill_pkg;
524 if ( $cust_bill_pkg->pkgnum > 0 ) {
525 my $cust_pkg = $cust_bill_pkg->cust_pkg;
526 if ( $tax_pkg_address && (my $cust_location = $cust_pkg->cust_location) ){
527 $cust_location->county_state_country;
529 my $cust_main = $cust_pkg->cust_main;
530 if ( $tax_ship_address && $cust_main->has_ship_address ) {
531 $cust_main->county_state_country('ship_');
533 $cust_main->county_state_country;
538 #tax? we shouldn't have wound up here then...
539 ''; #return customer ship or bill address? (depending on tax-ship_address)