1 <& elements/search.html,
2 'title' => 'Payment application detail', #to line item
3 'name_singular' => 'payment application',
5 'count_query' => $count_query,
6 'count_addl' => [ $money_char. '%.2f total', ],
32 FS::UI::Web::cust_header(),
36 sub { sprintf($money_char.'%.2f', shift->paid ) },
37 sub { $payby{shift->cust_bill_pay->cust_pay->payby} },
38 sub { my $cust_pay = shift->cust_bill_pay->cust_pay;
39 $cust_pay->payby =~ /^(CARD|CHEK)$/
40 ? $cust_pay->paymask : $cust_pay->payinfo;
42 sub { my $cust_pay = shift->cust_bill_pay->cust_pay;
43 $cust_pay->payby =~ /^CARD$/
44 ? cardtype($cust_pay->paymask) : '';
46 sub { time2str('%b %d %Y', shift->get('cust_pay_date') ) },
47 sub { shift->cust_bill_pay->cust_pay->order_number },
48 sub { shift->cust_bill_pay->cust_pay->otaker },
50 sub { sprintf($money_char.'%.2f', shift->amount ) },
52 sub { $_[0]->pkgnum > 0
53 ? $_[0]->get('pkg') # possibly use override.pkg
54 : $_[0]->get('itemdesc') # but i think this correct
56 'classname', #package class
60 sub { time2str('%b %d %Y', shift->_date ) },
61 \&FS::UI::Web::cust_fields,
72 '', #line item description
95 ( map { $_ ne 'Cust. Status' ? $clink : '' }
96 FS::UI::Web::cust_header()
99 'align' => 'rcrlrrlrlll',
100 #original value before cardtype & package were added
101 #why are there 13 cols?
105 FS::UI::Web::cust_aligns(),
121 FS::UI::Web::cust_colors(),
138 FS::UI::Web::cust_styles(),
144 #(and now we're cust_bill_pay_pkg.html, false laziness w/cust_credit_bill_pkg)
145 #LOTS of false laziness below w/cust_bill_pkg.cgi
146 # and a little w/cust_credit.html
149 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
151 my $conf = new FS::Conf;
153 my %payby = FS::payby->payby2shortname;
155 #here is the agent virtualization
157 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
159 my @where = ( $agentnums_sql );
161 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
162 push @where, "cust_pay.usernum = $1";
165 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
166 push @where, "cust_bill._date >= $beginning",
167 "cust_bill._date <= $ending";
169 my($cr_begin, $cr_end) = FS::UI::Web::parse_beginning_ending($cgi, 'payment');
170 push @where, "cust_pay._date >= $cr_begin",
171 "cust_pay._date <= $cr_end";
173 #payment amount? seems more what is expected than the applied amount
174 my @lt_gt = FS::UI::Web::parse_lt_gt($cgi, 'paid' );
175 s/paid/cust_pay.paid/g foreach (@lt_gt);
178 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
179 push @where, "cust_main.agentnum = $1";
182 if ( $cgi->param('billpkgnum') =~ /^(\d+)$/ ) {
183 push @where, "billpkgnum = $1";
187 # not specified: all classes
190 my $use_override = $cgi->param('use_override');
191 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
194 $comparison = "IS NULL";
196 $comparison = "= $1";
199 if ( $use_override ) {
201 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
202 override.classnum $comparison AND pkgpart_override IS NOT NULL
205 push @where, "part_pkg.classnum $comparison";
209 if ( $cgi->param('taxclass')
210 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
211 #(should we save a taxclass or a link to taxnum
212 # in cust_bill_pkg or something like
213 # cust_bill_pkg_tax_location?)
217 #override taxclass when use_override is specified? probably
218 #if ( $use_override ) {
221 # ' ( '. join(' OR ',
223 # ' ( part_pkg.taxclass = '. dbh->quote($_).
224 # ' AND pkgpart_override IS NULL '.
226 # ' override.taxclass = '. dbh->quote($_).
227 # ' AND pkgpart_override IS NOT NULL '.
230 # $cgi->param('taxclass')
238 map ' part_pkg.taxclass = '.dbh->quote($_),
239 $cgi->param('taxclass')
247 my @loc_param = qw( district city county state country );
249 if ( $cgi->param('out') ) {
251 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
252 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
253 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
256 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
257 if $cgi->param('istax');
261 SELECT COUNT(*) FROM cust_main_county
262 WHERE cust_main_county.tax > 0
267 #not linked to by anything, but useful for debugging "out of taxable region"
268 if ( grep $cgi->param($_), @loc_param ) {
270 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
272 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
273 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
274 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
277 push @where, $loc_sql;
281 } elsif ( $cgi->param('country') ) {
283 my @counties = $cgi->param('county');
285 if ( scalar(@counties) > 1 ) {
287 #hacky, could be more efficient. care if it is ever used for more than the
288 # tax-report_groups filtering kludge
291 ' ( '. join(' OR ', map {
293 my %ph = ( 'county' => dbh->quote($_),
294 map { $_ => dbh->quote( $cgi->param($_) ) }
295 qw( district city state country )
298 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
299 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
300 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
309 push @where, $locs_sql;
313 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
315 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
316 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
317 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
320 push @where, $loc_sql;
325 if ( $cgi->param('istax') ) {
326 $title = 'Tax credits';
327 $name = 'tax credits';
328 if ( $cgi->param('taxname') ) {
329 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
330 #} elsif ( $cgi->param('taxnameNULL') {
332 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
334 } elsif ( $cgi->param('nottax') ) {
335 $title = 'Payment applications to line items';
336 $name = 'applications';
337 #what can we usefully do with "taxname" ???? look up a class???
339 $title = 'Payment applications to line items';
340 $name = 'applications';
341 #warn "neither nottax nor istax parameters specified";
344 if ( $cgi->param('taxclassNULL')
345 && ! $cgi->param('istax') #no taxclass in this case
349 my %hash = ( 'country' => scalar($cgi->param('country')) );
350 foreach (qw( state county )) {
351 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
353 my $cust_main_county = qsearchs('cust_main_county', \%hash);
354 die "unknown base region for empty taxclass" unless $cust_main_county;
356 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
357 push @where, $same_sql if $same_sql;
361 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
363 # this should really be shoved out to FS::cust_pkg->location_sql or something
364 # along with the code in report_newtax.cgi
367 'county' => 'tax_rate_location.county',
368 'state' => 'tax_rate_location.state',
369 'city' => 'tax_rate_location.city',
370 'locationtaxid' => 'cust_bill_pkg_tax_rate_location.locationtaxid',
373 my %ph = map { ( $pn{$_} => dbh->quote( $cgi->param($_) || '' ) ) }
374 qw( city county state locationtaxid );
377 join( ' AND ', map { "( $_ = $ph{$_} OR $ph{$_} = '' AND $_ IS NULL)" }
383 if ( $cgi->param('itemdesc') ) {
384 if ( $cgi->param('itemdesc') eq 'Tax' ) {
385 push @where, "(itemdesc='Tax' OR itemdesc is null)";
387 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
391 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
392 my ( $group_op, $group_value ) = ( $1, $2 );
393 if ( $group_op eq '=' ) {
394 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
395 push @where, 'itemdesc = '. dbh->quote($group_value);
396 } elsif ( $group_op eq '!=' ) {
397 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
399 die "guru meditation #00de: group_op $group_op\n";
404 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
405 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
407 if ( $cgi->param('cust_tax') ) {
408 #false laziness -ish w/report_tax.cgi
410 if ( $cgi->param('taxname') ) {
411 my $q_taxname = dbh->quote($cgi->param('taxname'));
414 OR EXISTS ( SELECT 1 FROM cust_main_exemption
415 WHERE cust_main_exemption.custnum = cust_main.custnum
416 AND cust_main_exemption.taxname = $q_taxname )
420 $cust_exempt = " tax = 'Y' ";
423 push @where, $cust_exempt;
426 my $count_query = "SELECT COUNT(DISTINCT billpaypkgnum),
427 SUM(cust_bill_pay_pkg.amount)";
430 ' JOIN cust_bill ON ( cust_bill_pkg.invnum = cust_bill.invnum )'.
431 FS::UI::Web::join_cust_main('cust_bill', 'cust_pkg');
436 my $join_cust_bill_pkg = 'LEFT JOIN cust_bill_pkg USING ( billpkgnum )';
438 if ( $cgi->param('nottax') ) {
440 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
441 LEFT JOIN part_pkg USING ( pkgpart )
442 LEFT JOIN pkg_class USING ( classnum )
443 LEFT JOIN part_pkg AS override
444 ON pkgpart_override = override.pkgpart ';
445 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
446 if $conf->exists('tax-pkg_address');
448 } elsif ( $cgi->param('istax') ) {
450 #false laziness w/report_tax.cgi $taxfromwhere
451 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
452 $cgi->param('iscredit') eq 'rate') {
455 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ';
456 if ( $cgi->param('iscredit') eq 'rate' ) {
457 $join_pkg .= ', billpkgtaxratelocationnum )';
458 } elsif ( $conf->exists('tax-pkg_address') ) {
459 $join_pkg .= ', billpkgtaxlocationnum )';
460 push @where, "billpkgtaxratelocationnum IS NULL";
463 push @where, "billpkgtaxratelocationnum IS NULL";
466 $join_pkg .= ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
468 } elsif ( $conf->exists('tax-pkg_address') ) {
470 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
471 LEFT JOIN cust_location USING ( locationnum ) ';
473 #quelle kludge, somewhat false laziness w/report_tax.cgi
474 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
479 #warn "neither nottax nor istax parameters specified";
481 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
482 LEFT JOIN part_pkg USING ( pkgpart )
483 LEFT JOIN pkg_class USING ( classnum )';
487 my $where = ' WHERE '. join(' AND ', @where);
489 my $join_pay = ' LEFT JOIN cust_bill_pay USING ( billpaynum )
490 LEFT JOIN cust_pay USING ( paynum ) ';
492 $count_query .= " FROM cust_bill_pay_pkg
499 my @select = ( 'cust_bill_pay_pkg.*',
503 'cust_pay._date AS cust_pay_date',
506 push @select, 'part_pkg.pkg, pkg_class.classname' unless $cgi->param('istax');
507 push @select, 'cust_main.custnum',
508 FS::UI::Web::cust_sql_fields();
510 my @post_desc_header = ();
512 my @post_desc_null = ();
513 my $post_desc_align = '';
514 if ( $conf->exists('enable_taxclasses') && ! $cgi->param('istax') ) {
515 push @post_desc_header, 'Tax class';
516 push @post_desc, 'taxclass';
517 push @post_desc_null, '';
518 $post_desc_align .= 'l';
519 push @select, 'part_pkg.taxclass'; # or should this use override?
522 #warn "$join_cust_bill_pkg
528 'table' => 'cust_bill_pay_pkg',
529 'addl_from' => "$join_cust_bill_pkg
534 'select' => join(', ', @select ),
535 'extra_sql' => $where,
536 'order_by' => 'ORDER BY billpaypkgnum', #cust_bill. or cust_pay._date?
539 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
540 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
542 my $conf = new FS::Conf;
543 my $money_char = $conf->config('money_char') || '$';
545 my $tax_pkg_address = $conf->exists('tax-pkg_address');
546 my $tax_ship_address = $conf->exists('tax-ship_address');
548 my $location_sub = sub {
549 #my $cust_bill_pay_pkg = shift;
551 my $tax_Xlocation = $self->cust_bill_pkg_tax_Xlocation;
552 if ( defined($tax_Xlocation) && $tax_Xlocation ) {
554 if ( ref($tax_Xlocation) eq 'FS::cust_bill_pkg_tax_location' ) {
556 if ( $tax_Xlocation->taxtype eq 'FS::cust_main_county' ) {
557 my $cust_main_county = $tax_Xlocation->cust_main_county;
558 if ( $cust_main_county ) {
559 $cust_main_county->label;
561 ''; #cust_main_county record is gone... history? yuck.
564 '(CCH tax_rate)'; #XXX FS::tax_rate.. vendor taxes not yet handled here
567 } elsif ( ref($tax_Xlocation) eq 'FS::cust_bill_pkg_tax_rate_location' ) {
568 '(CCH)'; #XXX vendor taxes not yet handled here
570 'unknown tax_Xlocation '. ref($tax_Xlocation);
575 my $cust_bill_pkg = $self->cust_bill_pkg;
576 if ( $cust_bill_pkg->pkgnum > 0 ) {
577 my $cust_pkg = $cust_bill_pkg->cust_pkg;
578 if ( $tax_pkg_address && (my $cust_location = $cust_pkg->cust_location) ){
579 $cust_location->county_state_country;
581 my $cust_main = $cust_pkg->cust_main;
582 if ( $tax_ship_address && $cust_main->has_ship_address ) {
583 $cust_main->county_state_country('ship_');
585 $cust_main->county_state_country;
590 #tax? we shouldn't have wound up here then...
591 ''; #return customer ship or bill address? (depending on tax-ship_address)