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', ],
29 FS::UI::Web::cust_header(),
33 sub { sprintf($money_char.'%.2f', shift->paid ) },
34 sub { $payby{shift->cust_bill_pay->cust_pay->payby} },
35 sub { my $cust_pay = shift->cust_bill_pay->cust_pay;
36 $cust_pay->payby =~ /^(CARD|CHEK)$/
37 ? $cust_pay->paymask : $cust_pay->payinfo;
40 sub { time2str('%b %d %Y', shift->get('cust_pay_date') ) },
41 sub { shift->cust_bill_pay->cust_pay->otaker },
43 sub { sprintf($money_char.'%.2f', shift->amount ) },
45 sub { $_[0]->pkgnum > 0
46 ? $_[0]->get('pkg') # possibly use override.pkg
47 : $_[0]->get('itemdesc') # but i think this correct
52 sub { time2str('%b %d %Y', shift->_date ) },
53 \&FS::UI::Web::cust_fields,
61 '', #line item description
80 ( map { $_ ne 'Cust. Status' ? $clink : '' }
81 FS::UI::Web::cust_header()
84 'align' => 'rcrrlrlllrrcl'.
87 FS::UI::Web::cust_aligns(),
100 FS::UI::Web::cust_colors(),
114 FS::UI::Web::cust_styles(),
120 #(and now we're cust_bill_pay_pkg.html, false laziness w/cust_credit_bill_pkg)
121 #LOTS of false laziness below w/cust_bill_pkg.cgi
122 # and a little w/cust_credit.html
125 unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
127 my $conf = new FS::Conf;
129 my %payby = FS::payby->payby2shortname;
131 #here is the agent virtualization
133 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
135 my @where = ( $agentnums_sql );
137 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
138 push @where, "cust_pay.usernum = $1";
141 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
142 push @where, "cust_bill._date >= $beginning",
143 "cust_bill._date <= $ending";
145 my($cr_begin, $cr_end) = FS::UI::Web::parse_beginning_ending($cgi, 'payment');
146 push @where, "cust_pay._date >= $cr_begin",
147 "cust_pay._date <= $cr_end";
149 #payment amount? seems more what is expected than the applied amount
150 my @lt_gt = FS::UI::Web::parse_lt_gt($cgi, 'paid' );
151 s/paid/cust_pay.paid/g foreach (@lt_gt);
154 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
155 push @where, "cust_main.agentnum = $1";
158 if ( $cgi->param('billpkgnum') =~ /^(\d+)$/ ) {
159 push @where, "billpkgnum = $1";
163 # not specified: all classes
166 my $use_override = $cgi->param('use_override');
167 if ( $cgi->param('classnum') =~ /^(\d+)$/ ) {
170 $comparison = "IS NULL";
172 $comparison = "= $1";
175 if ( $use_override ) {
177 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
178 override.classnum $comparison AND pkgpart_override IS NOT NULL
181 push @where, "part_pkg.classnum $comparison";
185 if ( $cgi->param('taxclass')
186 && ! $cgi->param('istax') #no part_pkg.taxclass in this case
187 #(should we save a taxclass or a link to taxnum
188 # in cust_bill_pkg or something like
189 # cust_bill_pkg_tax_location?)
193 #override taxclass when use_override is specified? probably
194 #if ( $use_override ) {
197 # ' ( '. join(' OR ',
199 # ' ( part_pkg.taxclass = '. dbh->quote($_).
200 # ' AND pkgpart_override IS NULL '.
202 # ' override.taxclass = '. dbh->quote($_).
203 # ' AND pkgpart_override IS NOT NULL '.
206 # $cgi->param('taxclass')
214 map ' part_pkg.taxclass = '.dbh->quote($_),
215 $cgi->param('taxclass')
223 my @loc_param = qw( district city county state country );
225 if ( $cgi->param('out') ) {
227 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql( 'ornull' => 1 );
228 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
229 $loc_sql =~ s/\?/'cust_main_county.'.shift(@param)/e;
232 $loc_sql =~ s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g
233 if $cgi->param('istax');
237 SELECT COUNT(*) FROM cust_main_county
238 WHERE cust_main_county.tax > 0
243 #not linked to by anything, but useful for debugging "out of taxable region"
244 if ( grep $cgi->param($_), @loc_param ) {
246 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
248 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
249 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
250 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
253 push @where, $loc_sql;
257 } elsif ( $cgi->param('country') ) {
259 my @counties = $cgi->param('county');
261 if ( scalar(@counties) > 1 ) {
263 #hacky, could be more efficient. care if it is ever used for more than the
264 # tax-report_groups filtering kludge
267 ' ( '. join(' OR ', map {
269 my %ph = ( 'county' => dbh->quote($_),
270 map { $_ => dbh->quote( $cgi->param($_) ) }
271 qw( district city state country )
274 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
275 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
276 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
285 push @where, $locs_sql;
289 my %ph = map { $_ => dbh->quote( scalar($cgi->param($_)) ) } @loc_param;
291 my ( $loc_sql, @param ) = FS::cust_pkg->location_sql;
292 while ( $loc_sql =~ /\?/ ) { #easier to do our own substitution
293 $loc_sql =~ s/\?/$ph{shift(@param)}/e;
296 push @where, $loc_sql;
301 if ( $cgi->param('istax') ) {
302 $title = 'Tax credits';
303 $name = 'tax credits';
304 if ( $cgi->param('taxname') ) {
305 push @where, 'itemdesc = '. dbh->quote( $cgi->param('taxname') );
306 #} elsif ( $cgi->param('taxnameNULL') {
308 push @where, "( itemdesc IS NULL OR itemdesc = '' OR itemdesc = 'Tax' )";
310 } elsif ( $cgi->param('nottax') ) {
311 $title = 'Payment applications to line items';
312 $name = 'applications';
313 #what can we usefully do with "taxname" ???? look up a class???
315 $title = 'Payment applications to line items';
316 $name = 'applications';
317 #warn "neither nottax nor istax parameters specified";
320 if ( $cgi->param('taxclassNULL')
321 && ! $cgi->param('istax') #no taxclass in this case
325 my %hash = ( 'country' => scalar($cgi->param('country')) );
326 foreach (qw( state county )) {
327 $hash{$_} = scalar($cgi->param($_)) if $cgi->param($_);
329 my $cust_main_county = qsearchs('cust_main_county', \%hash);
330 die "unknown base region for empty taxclass" unless $cust_main_county;
332 my $same_sql = $cust_main_county->sql_taxclass_sameregion;
333 push @where, $same_sql if $same_sql;
337 } elsif ( scalar( grep( /locationtaxid/, $cgi->param ) ) ) {
339 # this should really be shoved out to FS::cust_pkg->location_sql or something
340 # along with the code in report_newtax.cgi
343 'county' => 'tax_rate_location.county',
344 'state' => 'tax_rate_location.state',
345 'city' => 'tax_rate_location.city',
346 'locationtaxid' => 'cust_bill_pkg_tax_rate_location.locationtaxid',
349 my %ph = map { ( $pn{$_} => dbh->quote( $cgi->param($_) || '' ) ) }
350 qw( city county state locationtaxid );
353 join( ' AND ', map { "( $_ = $ph{$_} OR $ph{$_} = '' AND $_ IS NULL)" }
359 if ( $cgi->param('itemdesc') ) {
360 if ( $cgi->param('itemdesc') eq 'Tax' ) {
361 push @where, "(itemdesc='Tax' OR itemdesc is null)";
363 push @where, 'itemdesc='. dbh->quote($cgi->param('itemdesc'));
367 if ( $cgi->param('report_group') =~ /^(=|!=) (.*)$/ && $cgi->param('istax') ) {
368 my ( $group_op, $group_value ) = ( $1, $2 );
369 if ( $group_op eq '=' ) {
370 #push @where, 'itemdesc LIKE '. dbh->quote($group_value.'%');
371 push @where, 'itemdesc = '. dbh->quote($group_value);
372 } elsif ( $group_op eq '!=' ) {
373 push @where, '( itemdesc != '. dbh->quote($group_value) .' OR itemdesc IS NULL )';
375 die "guru meditation #00de: group_op $group_op\n";
380 push @where, 'cust_bill_pkg.pkgnum != 0' if $cgi->param('nottax');
381 push @where, 'cust_bill_pkg.pkgnum = 0' if $cgi->param('istax');
383 if ( $cgi->param('cust_tax') ) {
384 #false laziness -ish w/report_tax.cgi
386 if ( $cgi->param('taxname') ) {
387 my $q_taxname = dbh->quote($cgi->param('taxname'));
390 OR EXISTS ( SELECT 1 FROM cust_main_exemption
391 WHERE cust_main_exemption.custnum = cust_main.custnum
392 AND cust_main_exemption.taxname = $q_taxname )
396 $cust_exempt = " tax = 'Y' ";
399 push @where, $cust_exempt;
402 my $count_query = "SELECT COUNT(DISTINCT billpaypkgnum),
403 SUM(cust_bill_pay_pkg.amount)";
406 ' JOIN cust_bill ON ( cust_bill_pkg.invnum = cust_bill.invnum )'.
407 FS::UI::Web::join_cust_main('cust_bill', 'cust_pkg');
412 my $join_cust_bill_pkg = 'LEFT JOIN cust_bill_pkg USING ( billpkgnum )';
414 if ( $cgi->param('nottax') ) {
416 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
417 LEFT JOIN part_pkg USING ( pkgpart )
418 LEFT JOIN part_pkg AS override
419 ON pkgpart_override = override.pkgpart ';
420 $join_pkg .= ' LEFT JOIN cust_location USING ( locationnum ) '
421 if $conf->exists('tax-pkg_address');
423 } elsif ( $cgi->param('istax') ) {
425 #false laziness w/report_tax.cgi $taxfromwhere
426 if ( scalar( grep( /locationtaxid/, $cgi->param ) ) ||
427 $cgi->param('iscredit') eq 'rate') {
430 ' LEFT JOIN cust_bill_pkg_tax_rate_location USING ( billpkgnum ';
431 if ( $cgi->param('iscredit') eq 'rate' ) {
432 $join_pkg .= ', billpkgtaxratelocationnum )';
433 } elsif ( $conf->exists('tax-pkg_address') ) {
434 $join_pkg .= ', billpkgtaxlocationnum )';
435 push @where, "billpkgtaxratelocationnum IS NULL";
438 push @where, "billpkgtaxratelocationnum IS NULL";
441 $join_pkg .= ' LEFT JOIN tax_rate_location USING ( taxratelocationnum ) ';
443 } elsif ( $conf->exists('tax-pkg_address') ) {
445 $join_pkg .= ' LEFT JOIN cust_bill_pkg_tax_location USING ( billpkgnum )
446 LEFT JOIN cust_location USING ( locationnum ) ';
448 #quelle kludge, somewhat false laziness w/report_tax.cgi
449 s/cust_pkg\.locationnum/cust_bill_pkg_tax_location.locationnum/g for @where;
454 #warn "neither nottax nor istax parameters specified";
456 $join_pkg = ' LEFT JOIN cust_pkg USING ( pkgnum )
457 LEFT JOIN part_pkg USING ( pkgpart ) ';
461 my $where = ' WHERE '. join(' AND ', @where);
463 my $join_pay = ' LEFT JOIN cust_bill_pay USING ( billpaynum )
464 LEFT JOIN cust_pay USING ( paynum ) ';
466 $count_query .= " FROM cust_bill_pay_pkg
473 my @select = ( 'cust_bill_pay_pkg.*',
477 'cust_pay._date AS cust_pay_date',
480 push @select, 'part_pkg.pkg' unless $cgi->param('istax');
481 push @select, 'cust_main.custnum',
482 FS::UI::Web::cust_sql_fields();
484 my @post_desc_header = ();
486 my @post_desc_null = ();
487 my $post_desc_align = '';
488 if ( $conf->exists('enable_taxclasses') && ! $cgi->param('istax') ) {
489 push @post_desc_header, 'Tax class';
490 push @post_desc, 'taxclass';
491 push @post_desc_null, '';
492 $post_desc_align .= 'l';
493 push @select, 'part_pkg.taxclass'; # or should this use override?
496 warn "$join_cust_bill_pkg
502 'table' => 'cust_bill_pay_pkg',
503 'addl_from' => "$join_cust_bill_pkg
508 'select' => join(', ', @select ),
509 'extra_sql' => $where,
510 'order_by' => 'ORDER BY billpaypkgnum', #cust_bill. or cust_pay._date?
513 my $ilink = [ "${p}view/cust_bill.cgi?", 'invnum' ];
514 my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
516 my $conf = new FS::Conf;
517 my $money_char = $conf->config('money_char') || '$';
519 my $tax_pkg_address = $conf->exists('tax-pkg_address');
520 my $tax_ship_address = $conf->exists('tax-ship_address');
522 my $location_sub = sub {
523 #my $cust_bill_pay_pkg = shift;
525 my $tax_Xlocation = $self->cust_bill_pkg_tax_Xlocation;
526 if ( defined($tax_Xlocation) && $tax_Xlocation ) {
528 if ( ref($tax_Xlocation) eq 'FS::cust_bill_pkg_tax_location' ) {
530 if ( $tax_Xlocation->taxtype eq 'FS::cust_main_county' ) {
531 my $cust_main_county = $tax_Xlocation->cust_main_county;
532 if ( $cust_main_county ) {
533 $cust_main_county->label;
535 ''; #cust_main_county record is gone... history? yuck.
538 '(CCH tax_rate)'; #XXX FS::tax_rate.. vendor taxes not yet handled here
541 } elsif ( ref($tax_Xlocation) eq 'FS::cust_bill_pkg_tax_rate_location' ) {
542 '(CCH)'; #XXX vendor taxes not yet handled here
544 'unknown tax_Xlocation '. ref($tax_Xlocation);
549 my $cust_bill_pkg = $self->cust_bill_pkg;
550 if ( $cust_bill_pkg->pkgnum > 0 ) {
551 my $cust_pkg = $cust_bill_pkg->cust_pkg;
552 if ( $tax_pkg_address && (my $cust_location = $cust_pkg->cust_location) ){
553 $cust_location->county_state_country;
555 my $cust_main = $cust_pkg->cust_main;
556 if ( $tax_ship_address && $cust_main->has_ship_address ) {
557 $cust_main->county_state_country('ship_');
559 $cust_main->county_state_country;
564 #tax? we shouldn't have wound up here then...
565 ''; #return customer ship or bill address? (depending on tax-ship_address)