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') ) },
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,
69 @on_null, #order_number
72 '', #line item description
95 ( map { $_ ne 'Cust. Status' ? $clink : '' }
96 FS::UI::Web::cust_header()
104 FS::UI::Web::cust_aligns(),
120 FS::UI::Web::cust_colors(),
137 FS::UI::Web::cust_styles(),
143 #(and now we're cust_bill_pay_pkg.html, false laziness w/cust_credit_bill_pkg)
144 #LOTS of false laziness below w/cust_bill_pkg.cgi
145 # and a little w/cust_credit.html
148 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
154 if ($cgi->param('show_order_number')) {
155 @on_header = ('Order Number');
156 @on_field = (sub { shift->cust_bill_pay->cust_pay->order_number });
161 my $conf = new FS::Conf;
163 my %payby = FS::payby->payby2shortname;
165 #here is the agent virtualization
167 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
169 my @where = ( $agentnums_sql );
171 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
172 push @where, "cust_pay.usernum = $1";
175 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
176 push @where, "cust_bill._date >= $beginning",
177 "cust_bill._date <= $ending";
179 my($cr_begin, $cr_end) = FS::UI::Web::parse_beginning_ending($cgi, 'payment');
180 push @where, "cust_pay._date >= $cr_begin",
181 "cust_pay._date <= $cr_end";
183 #payment amount? seems more what is expected than the applied amount
184 my @lt_gt = FS::UI::Web::parse_lt_gt($cgi, 'paid' );
185 s/paid/cust_pay.paid/g foreach (@lt_gt);
188 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
189 push @where, "cust_main.agentnum = $1";
192 if ( $cgi->param('billpkgnum') =~ /^(\d+)$/ ) {
193 push @where, "billpkgnum = $1";
197 # not specified: all classes
200 my $use_override = $cgi->param('use_override');
201 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
204 $comparison = "IS NULL";
206 $comparison = "= $1";
209 if ( $use_override ) {
211 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
212 override.classnum $comparison AND pkgpart_override IS NOT NULL
215 push @where, "part_pkg.classnum $comparison";
219 if ( $cgi->param('taxclass')
220 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
221 #(should we save a taxclass or a link to taxnum
222 # in cust_bill_pkg or something like
223 # cust_bill_pkg_tax_location?)
227 #override taxclass when use_override is specified? probably
228 #if ( $use_override ) {
231 # ' ( '. join(' OR ',
233 # ' ( part_pkg.taxclass = '. dbh->quote($_).
234 # ' AND pkgpart_override IS NULL '.
236 # ' override.taxclass = '. dbh->quote($_).
237 # ' AND pkgpart_override IS NOT NULL '.
240 # $cgi->param('taxclass')
248 map ' part_pkg.taxclass = '.dbh->quote($_),
249 $cgi->param('taxclass')
257 my @loc_param = qw( district city county state country );
259 if ( $cgi->param('out') ) {
261 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
262 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
263 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
266 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
267 if $cgi->param('istax');
271 SELECT COUNT(*) FROM cust_main_county
272 WHERE cust_main_county.tax > 0
277 #not linked to by anything, but useful for debugging "out of taxable region"
278 if ( grep $cgi->param($_), @loc_param ) {
280 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
282 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
283 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
284 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
287 push @where, $loc_sql;
291 } elsif ( $cgi->param('country') ) {
293 my @counties = $cgi->param('county');
295 if ( scalar(@counties) > 1 ) {
297 #hacky, could be more efficient. care if it is ever used for more than the
298 # tax-report_groups filtering kludge
301 ' ( '. join(' OR ', map {
303 my %ph = ( 'county' => dbh->quote($_),
304 map { $_ => dbh->quote( $cgi->param($_) ) }
305 qw( district city state country )
308 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
309 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
310 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
319 push @where, $locs_sql;
323 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
325 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
326 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
327 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
330 push @where, $loc_sql;
335 if ( $cgi->param('istax') ) {
336 $title = 'Tax credits';
337 $name = 'tax credits';
338 if ( $cgi->param('taxname') ) {
339 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
340 #} elsif ( $cgi->param('taxnameNULL') {
342 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
344 } elsif ( $cgi->param('nottax') ) {
345 $title = 'Payment applications to line items';
346 $name = 'applications';
347 #what can we usefully do with "taxname" ???? look up a class???
349 $title = 'Payment applications to line items';
350 $name = 'applications';
351 #warn "neither nottax nor istax parameters specified";
354 if ( $cgi->param('taxclassNULL')
355 && ! $cgi->param('istax') #no taxclass in this case
359 my %hash = ( 'country' => scalar($cgi->param('country')) );
360 foreach (qw( state county )) {
361 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
363 my $cust_main_county = qsearchs('cust_main_county', \%hash);
364 die "unknown base region for empty taxclass" unless $cust_main_county;
366 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
367 push @where, $same_sql if $same_sql;
371 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
373 # this should really be shoved out to FS::cust_pkg->location_sql or something
374 # along with the code in report_newtax.cgi
377 'county' => 'tax_rate_location.county',
378 'state' => 'tax_rate_location.state',
379 'city' => 'tax_rate_location.city',
380 'locationtaxid' => 'cust_bill_pkg_tax_rate_location.locationtaxid',
383 my %ph = map { ( $pn{$_} => dbh->quote( $cgi->param($_) || '' ) ) }
384 qw( city county state locationtaxid );
387 join( ' AND ', map { "( $_ = $ph{$_} OR $ph{$_} = '' AND $_ IS NULL)" }
393 if ( $cgi->param('itemdesc') ) {
394 if ( $cgi->param('itemdesc') eq 'Tax' ) {
395 push @where, "(itemdesc='Tax' OR itemdesc is null)";
397 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
401 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
402 my ( $group_op, $group_value ) = ( $1, $2 );
403 if ( $group_op eq '=' ) {
404 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
405 push @where, 'itemdesc = '. dbh->quote($group_value);
406 } elsif ( $group_op eq '!=' ) {
407 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
409 die "guru meditation #00de: group_op $group_op\n";
414 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
415 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
417 if ( $cgi->param('cust_tax') ) {
418 #false laziness -ish w/report_tax.cgi
420 if ( $cgi->param('taxname') ) {
421 my $q_taxname = dbh->quote($cgi->param('taxname'));
424 OR EXISTS ( SELECT 1 FROM cust_main_exemption
425 WHERE cust_main_exemption.custnum = cust_main.custnum
426 AND cust_main_exemption.taxname = $q_taxname )
430 $cust_exempt = " tax = 'Y' ";
433 push @where, $cust_exempt;
436 my $count_query = "SELECT COUNT(DISTINCT billpaypkgnum),
437 SUM(cust_bill_pay_pkg.amount)";
440 ' JOIN cust_bill ON ( cust_bill_pkg.invnum = cust_bill.invnum )'.
441 FS::UI::Web::join_cust_main('cust_bill', 'cust_pkg');
446 my $join_cust_bill_pkg = 'LEFT JOIN cust_bill_pkg USING ( billpkgnum )';
448 if ( $cgi->param('nottax') ) {
450 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
451 LEFT JOIN part_pkg USING ( pkgpart )
452 LEFT JOIN pkg_class USING ( classnum )
453 LEFT JOIN part_pkg AS override
454 ON pkgpart_override = override.pkgpart ';
455 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
456 if $conf->exists('tax-pkg_address');
458 } elsif ( $cgi->param('istax') ) {
460 #false laziness w/report_tax.cgi $taxfromwhere
461 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
462 $cgi->param('iscredit') eq 'rate') {
465 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ';
466 if ( $cgi->param('iscredit') eq 'rate' ) {
467 $join_pkg .= ', billpkgtaxratelocationnum )';
468 } elsif ( $conf->exists('tax-pkg_address') ) {
469 $join_pkg .= ', billpkgtaxlocationnum )';
470 push @where, "billpkgtaxratelocationnum IS NULL";
473 push @where, "billpkgtaxratelocationnum IS NULL";
476 $join_pkg .= ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
478 } elsif ( $conf->exists('tax-pkg_address') ) {
480 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
481 LEFT JOIN cust_location USING ( locationnum ) ';
483 #quelle kludge, somewhat false laziness w/report_tax.cgi
484 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
489 #warn "neither nottax nor istax parameters specified";
491 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
492 LEFT JOIN part_pkg USING ( pkgpart )
493 LEFT JOIN pkg_class USING ( classnum )';
497 my $where = ' WHERE '. join(' AND ', @where);
499 my $join_pay = ' LEFT JOIN cust_bill_pay USING ( billpaynum )
500 LEFT JOIN cust_pay USING ( paynum ) ';
502 $count_query .= " FROM cust_bill_pay_pkg
509 my @select = ( 'cust_bill_pay_pkg.*',
513 'cust_pay._date AS cust_pay_date',
516 push @select, 'part_pkg.pkg, pkg_class.classname' unless $cgi->param('istax');
517 push @select, 'cust_main.custnum',
518 FS::UI::Web::cust_sql_fields();
520 my @post_desc_header = ();
522 my @post_desc_null = ();
523 my $post_desc_align = '';
524 if ( $conf->exists('enable_taxclasses') && ! $cgi->param('istax') ) {
525 push @post_desc_header, 'Tax class';
526 push @post_desc, 'taxclass';
527 push @post_desc_null, '';
528 $post_desc_align .= 'l';
529 push @select, 'part_pkg.taxclass'; # or should this use override?
532 #warn "$join_cust_bill_pkg
538 'table' => 'cust_bill_pay_pkg',
539 'addl_from' => "$join_cust_bill_pkg
544 'select' => join(', ', @select ),
545 'extra_sql' => $where,
546 'order_by' => 'ORDER BY billpaypkgnum', #cust_bill. or cust_pay._date?
549 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
550 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
552 my $conf = new FS::Conf;
553 my $money_char = $conf->config('money_char') || '$';
555 my $tax_pkg_address = $conf->exists('tax-pkg_address');
556 my $tax_ship_address = $conf->exists('tax-ship_address');
558 my $location_sub = sub {
559 #my $cust_bill_pay_pkg = shift;
561 my $tax_Xlocation = $self->cust_bill_pkg_tax_Xlocation;
562 if ( defined($tax_Xlocation) && $tax_Xlocation ) {
564 if ( ref($tax_Xlocation) eq 'FS::cust_bill_pkg_tax_location' ) {
566 if ( $tax_Xlocation->taxtype eq 'FS::cust_main_county' ) {
567 my $cust_main_county = $tax_Xlocation->cust_main_county;
568 if ( $cust_main_county ) {
569 $cust_main_county->label;
571 ''; #cust_main_county record is gone... history? yuck.
574 '(CCH tax_rate)'; #XXX FS::tax_rate.. vendor taxes not yet handled here
577 } elsif ( ref($tax_Xlocation) eq 'FS::cust_bill_pkg_tax_rate_location' ) {
578 '(CCH)'; #XXX vendor taxes not yet handled here
580 'unknown tax_Xlocation '. ref($tax_Xlocation);
585 my $cust_bill_pkg = $self->cust_bill_pkg;
586 if ( $cust_bill_pkg->pkgnum > 0 ) {
587 my $cust_pkg = $cust_bill_pkg->cust_pkg;
588 if ( $tax_pkg_address && (my $cust_location = $cust_pkg->cust_location) ){
589 $cust_location->county_state_country;
591 my $cust_main = $cust_pkg->cust_main;
592 if ( $tax_ship_address && $cust_main->has_ship_address ) {
593 $cust_main->county_state_country('ship_');
595 $cust_main->county_state_country;
600 #tax? we shouldn't have wound up here then...
601 ''; #return customer ship or bill address? (depending on tax-ship_address)